context = Context::getContext(); if (is_object($this->context->smarty)) { $this->smarty = $this->context->smarty; } if(version_compare(_PS_VERSION_, '1.7', '>=')) $this->is17 = true; } public static function getInstance() { if (!(isset(self::$instance)) || !self::$instance) { self::$instance = new Ets_pr_defines(); } return self::$instance; } public function l($string) { return Translate::getModuleTranslation('ets_promotion', $string, pathinfo(__FILE__, PATHINFO_FILENAME)); } public function installDb(){ if(!is_dir(_PS_ETS_PR_IMG_DIR_)) { @mkdir(_PS_ETS_PR_IMG_DIR_,0755,true); @copy(dirname(__FILE__).'/index.php', _PS_ETS_PR_IMG_DIR_. 'index.php'); } $res = Db::getInstance()->execute('CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'ets_pr_rule` ( `id_ets_pr_rule` INT(11) NOT NULL AUTO_INCREMENT , `active` INT(1) , `priority` INT(11), `quantity` INT(11), `quantity_per_user` INT(11), `apply_other_rule_presteahop` INT(1) , `apply_other_rule` INT(1) , `from_date` DATETIME , `to_date` DATETIME , `id_shop` INT(11) NOT NULL , `new_customer` INT(11), `date_add` DATETIME , `date_upd` DATETIME , PRIMARY KEY (`id_ets_pr_rule`)) ENGINE = '._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci'); $res &= Db::getInstance()->execute('CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'ets_pr_condition_rule` ( `id_ets_pr_condition_rule` INT(11) NOT NULL AUTO_INCREMENT , `id_ets_pr_rule` INT(11) NOT NULL , `parent_codition` VARCHAR(222) , `condition` VARCHAR(32), `id_customers` VARCHAR(222), `only_apply_on_default_group` INT(1), `id_groups` VARCHAR(222), `id_genders` VARCHAR(222), `age_from` INT(11), `age_to` INT(11), `on_customer_birthday` INT(11), `verified_customer` INT(11), `customer_signed_up_from` datetime, `customer_signed_up_to` datetime, `days_since_signed_up_cal` VARCHAR(22), `days_since_singed_up_day` INT(11), `newsletter_registration` VARCHAR(50), `order_criteria` VARCHAR(50), `first_order_of_customer` INT(1), `number_of_order_cal` VARCHAR(50), `number_of_order` INT(11), `number_of_product_cal` VARCHAR(50), `number_of_product` INT(11), `amount_of_money_spent_cal` VARCHAR(50), `amount_of_money_spent` FLOAT(10,2), `amount_of_money_spent_tax_incl` INT(1), `amount_of_money_spent_shipping_incl` INT(1), `amount_of_money_spent_discount_incl` INT(1), `order_time_in` VARCHAR(50), `order_time_in_last_day` INT(11), `order_time_in_from` datetime, `order_time_in_to` datetime, `order_status_in` VARCHAR(300), `cart_amount_cal` VARCHAR(50), `cart_amount` FLOAT(10,2), `cart_amount_tax_incl` INT(1), `cart_amount_shipping_incl` INT(1), `cart_amount_discount_incl` INT(1), `total_product_quantity_cal` VARCHAR(50), `total_product_quantity` INT(11), `quantity_of_same_product_cal` VARCHAR(50), `quantity_of_same_product` INT(11), `number_of_different_product_cal` VARCHAR(50), `number_of_different_product` INT(11), `number_of_product_in_same_category_cal` VARCHAR(50), `number_of_product_in_same_category` INT(11), `apply_for_discounted_products` INT(1), `products_with_different_attribute` INT(1), `applicable_product_categories` VARCHAR(50), `include_specific_products` VARCHAR(300), `exclude_products` VARCHAR(300), `apply_all_attribute` INT(1), `select_attributes` VARCHAR(300), `apply_all_features` INT(1), `select_features` VARCHAR(300), `apply_all_supplier` INT(1), `select_suppliers` VARCHAR(300), `apply_all_manufacturer` INT(1), `select_manufacturers` VARCHAR(300), `apply_for_product_price_cal` VARCHAR(50), `apply_for_product_price` FLOAT(10,2), `apply_for_availabled_quantity_stock_cal` VARCHAR(50), `apply_for_availabled_quantity_stock` INT(11), `delivery_total_cart_weight_cal` VARCHAR(50), `delivery_total_cart_weight` FLOAT(10,2), `delivery_apply_to_carriers` VARCHAR(300), `delivery_id_zone` INT(11), `delivery_id_country` INT(11), `delivery_id_state` INT(11), `delivery_speed_grade_cal` VARCHAR(50), `delivery_speed_grade` FLOAT(10,2), `payments` VARCHAR(500), `specific_occasion` VARCHAR(50), `specific_occasion_hour_of_day_from` text, `specific_occasion_hour_of_day_to` text, `specific_occasion_day_of_week` text, `specific_occasion_day_of_week_from` text, `specific_occasion_day_of_week_to` text, `specific_occasion_month_of_year` text, `specific_occasion_month_of_year_from` text, `specific_occasion_month_of_year_to` text, `specific_occasion_date_from` text, `specific_occasion_date_to` text, `include_sub_categories` INT(1), `applicable_categories` text, INDEX(id_ets_pr_rule), PRIMARY KEY (`id_ets_pr_condition_rule`)) ENGINE = '._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci'); $res &= Db::getInstance()->execute('CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'ets_pr_rule_lang` ( `id_ets_pr_rule` INT(11) NOT NULL , `id_lang` INT(11) NOT NULL, `name` VARCHAR(1000), `description` TEXT, PRIMARY KEY (`id_ets_pr_rule`,`id_lang`)) ENGINE = '._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci') ; $res &= Db::getInstance()->execute('CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'ets_pr_action_rule` ( `id_ets_pr_action_rule` INT(11) NOT NULL AUTO_INCREMENT , `id_ets_pr_rule` INT(11) NOT NULL , `type_action` VARCHAR(222), `apply_free_shipping` INT(11), `discount_on_shipping_cost` VARCHAR(32), `amount_discount_on_shipping_cost` FLOAT(10,2), `percent_discount_on_shipping_cost` FLOAT(10,2), `maximum_discount_on_shipping_cost` FLOAT(10,2), `carrier_apply_discount_on_shipping_cost` VARCHAR(200), `discount_on_total_cart` VARCHAR(32), `percent_total_cart` FLOAT(10,2), `amount_total_cart` FLOAT(10,2), `maximum_discount_value` FLOAT(10,2), `exclude_tax_before_cal_discount` INT(1), `exclude_shipping_fee_before_cal_disount` INT(1), `discount_on_product` VARCHAR(50), `discount_type` VARCHAR(50), `amount_discount_product` FLOAT(10,2), `percent_discount_proudct` FLOAT(10,2), `maximum_discount_product` FLOAT(10,2), `apply_for_discounted_product` INT(1), `applicable_product_categories` VARCHAR(50), `include_specific_products` VARCHAR(300), `exclude_products` VARCHAR(300), `apply_all_attribute` INT(1), `select_attributes` VARCHAR(300), `apply_all_features` INT(1), `select_features` VARCHAR(300), `apply_all_supplier` INT(1), `select_suppliers` VARCHAR(300), `apply_all_manufacturer` INT(1), `select_manufacturers` VARCHAR(300), `apply_for_product_price_cal` VARCHAR(50), `apply_for_product_price` FLOAT(10,2), `apply_for_availabled_quantity_stock_cal` VARCHAR(50), `apply_for_availabled_quantity_stock` INT(11), `purchase_quantity_more_of_same_product` INT(11), `discount_on_all_quantities` VARCHAR(50), `percent_discount_on_all_quantities` FLOAT(10,2), `amount_discount_on_all_quantities` FLOAT(10,2), `maximum_discount_on_all_quantities` FLOAT(10,2), `discount_all_quantities_action_on` VARCHAR(100), `product_with_deffierent_attribute` INT(1), `gift_or_with_discount_product` VARCHAR(100), `gift_product` VARCHAR(300), `quantity_gift_product` INT(11), `apply_action_on` VARCHAR(20), `gift_add_selected_product_to_cart` VARCHAR(100), `new_product_with_discount_price` VARCHAR(50), `percent_new_product_with_discount_price` FLOAT(10,2), `amount_new_product_with_discount_price` FLOAT(10,2), `maximum_new_product_with_discount_price` FLOAT(10,2), `purchase_more_product_quantity` INT(11), `discount_purchase_more_product_quantity` VARCHAR(100), `percent_discount_purchase_more_product_quantity` FLOAT(10,2), `amount_discount_purchase_more_product_quantity` FLOAT(10,2), `maximum_discount_purchase_more_product_quantity` FLOAT(10,2), `start_counting_purchase_more_product_quantity` VARCHAR(100), `purchase_more_product_amount` FLOAT(10,2), `discount_purchase_more_product_amount` VARCHAR(100), `percent_discount_purchase_more_product_amount` FLOAT(10,2), `amount_discount_purchase_more_product_amount` FLOAT(10,2), `maximum_discount_purchase_more_product_amount` FLOAT(10,2), `start_counting_purchase_more_product_amount` VARCHAR(100), `purchase_discount_on_next_same_product` INT(11), `discount_on_next_same_product` VARCHAR(100), `percent_discount_on_next_same_product` FLOAT(10,2), `amount_discount_on_next_same_product` FLOAT(10,2), `amount_discount_on_next_same_product_tax_incl` INT(1), `maximum_discount_on_next_same_product` FLOAT(10,2), `apply_on_next_same_product` INT(11), `action_on_next_same_product` VARCHAR(100), `repeat_the_action_on_next_same_product` VARCHAR(100), `maximum_time_on_next_same_product` INT(11), `include_sub_categories` INT(1), `reduction_currency` INT(11), `reduction_tax` INT(1), `reduction_currency_discount_product` INT(11), `reduction_tax_discount_product` INT(1), `reduction_currency_discount_all_quantities` INT(11), `reduction_tax_discount_all_quantities` INT(1), `reduction_currency_discount_on_shipping_cost` INT(11), `reduction_tax_discount_on_shipping_cost` INT(1), `reduction_currency_discount_on_new_product` INT(11), `reduction_tax_discount_on_new_product` INT(1), `reduction_currency_discount_on_more_product_quantity` INT(11), `reduction_tax_discount_on_more_product_quantity` INT(1), `reduction_currency_discount_on_more_product_amount` INT(11), `reduction_tax_discount_on_more_product_amount` INT(1), `reduction_currency_discount_on_next_same_product` INT(11), `applicable_categories` text, `code` VARCHAR(222) , `highlight` INT(1), `apply_discount_code` VARCHAR(10), INDEX(id_ets_pr_rule), PRIMARY KEY (`id_ets_pr_action_rule`)) ENGINE = '._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci'); $res &= Db::getInstance()->execute('CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'ets_pr_promote_rule` ( `id_ets_pr_promote_rule` INT(11) NOT NULL AUTO_INCREMENT , `id_ets_pr_rule` INT(11) NOT NULL , `way_to_promote` VARCHAR(55), `position_to_display` VARCHAR(1000), `text_color` VARCHAR(55), `background_color` VARCHAR(55), `button_close_color` VARCHAR(55), `title_popup_color` VARCHAR(55), `background_title_popup_color` VARCHAR(55), `background_content_popup_color` VARCHAR(55), `border_radius` VARCHAR(55), `close_button_popup_color` VARCHAR(55), `applicable_product_categories` VARCHAR(500), `applicable_categories` VARCHAR(500), `include_sub_categories` VARCHAR(500), `include_specific_products` VARCHAR(500), `exclude_products` VARCHAR(500), `delay_popup` INT(11), `notification_text_color` VARCHAR(55), `notification_background_color` VARCHAR(55), `notification_border_color` VARCHAR(55), INDEX(id_ets_pr_rule), PRIMARY KEY (`id_ets_pr_promote_rule`)) ENGINE = '._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci'); $res &= Db::getInstance()->execute('CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'ets_pr_promote_rule_lang` ( `id_ets_pr_promote_rule` INT(11) NOT NULL, `id_lang` INT(11) NOT NULL , `notification` text, `banner` VARCHAR(200), `banner_link` VARCHAR(300), `content_bar` VARCHAR(500), `title_popup` VARCHAR(500), `content_popup` VARCHAR(500), PRIMARY KEY (`id_ets_pr_promote_rule`,`id_lang`)) ENGINE = '._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci'); $res &= Db::getInstance()->execute('CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'ets_pr_cart_rule` ( `id_ets_pr_rule` INT(11) NOT NULL, `id_cart` INT(11) NOT NULL, `date_add` datetime, PRIMARY KEY (`id_ets_pr_rule`,`id_cart`)) ENGINE = '._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci'); if(!$this->checkCreatedColumn('order_cart_rule','id_ets_pr_rule')) $res &= Db::getInstance()->execute('ALTER TABLE `'._DB_PREFIX_.'order_cart_rule` ADD `id_ets_pr_rule` INT(11)'); self::alterDb(); return $res; } public static function alterDb() { $sqls = array(); $sqls[] = 'ALTER TABLE `'._DB_PREFIX_.'ets_pr_condition_rule` CHANGE `amount_of_money_spent` `amount_of_money_spent` DECIMAL(20,6) NULL DEFAULT NULL, CHANGE `cart_amount` `cart_amount` DECIMAL(20,6) NULL DEFAULT NULL, CHANGE `apply_for_product_price` `apply_for_product_price` DECIMAL(20,6) NULL DEFAULT NULL, CHANGE `delivery_total_cart_weight` `delivery_total_cart_weight` DECIMAL(20,6) NULL DEFAULT NULL, CHANGE `delivery_speed_grade` `delivery_speed_grade` DECIMAL(20,6) NULL DEFAULT NULL'; $sqls[] = 'ALTER TABLE `'._DB_PREFIX_.'ets_pr_condition_rule` CHANGE `include_specific_products` `include_specific_products` TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, CHANGE `exclude_products` `exclude_products` TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, CHANGE `select_attributes` `select_attributes` TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, CHANGE `select_features` `select_features` TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, CHANGE `select_suppliers` `select_suppliers` TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, CHANGE `select_manufacturers` `select_manufacturers` TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, CHANGE `delivery_apply_to_carriers` `delivery_apply_to_carriers` TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL'; $sqls[] = 'ALTER TABLE `'._DB_PREFIX_.'ets_pr_action_rule` CHANGE `amount_discount_on_shipping_cost` `amount_discount_on_shipping_cost` DECIMAL(20,6) NULL DEFAULT NULL, CHANGE `percent_discount_on_shipping_cost` `percent_discount_on_shipping_cost` DECIMAL(20,6) NULL DEFAULT NULL, CHANGE `maximum_discount_on_shipping_cost` `maximum_discount_on_shipping_cost` DECIMAL(20,6) NULL DEFAULT NULL, CHANGE `percent_total_cart` `percent_total_cart` DECIMAL(20,6) NULL DEFAULT NULL, CHANGE `amount_total_cart` `amount_total_cart` DECIMAL(20,6) NULL DEFAULT NULL, CHANGE `maximum_discount_value` `maximum_discount_value` DECIMAL(20,6) NULL DEFAULT NULL, CHANGE `amount_discount_product` `amount_discount_product` DECIMAL(20,6) NULL DEFAULT NULL, CHANGE `percent_discount_proudct` `percent_discount_proudct` DECIMAL(20,6) NULL DEFAULT NULL, CHANGE `maximum_discount_product` `maximum_discount_product` DECIMAL(20,6) NULL DEFAULT NULL, CHANGE `apply_for_product_price` `apply_for_product_price` DECIMAL(20,6) NULL DEFAULT NULL, CHANGE `percent_discount_on_all_quantities` `percent_discount_on_all_quantities` DECIMAL(20,6) NULL DEFAULT NULL, CHANGE `amount_discount_on_all_quantities` `amount_discount_on_all_quantities` DECIMAL(20,6) NULL DEFAULT NULL, CHANGE `maximum_discount_on_all_quantities` `maximum_discount_on_all_quantities` DECIMAL(20,6) NULL DEFAULT NULL, CHANGE `percent_new_product_with_discount_price` `percent_new_product_with_discount_price` DECIMAL(20,6) NULL DEFAULT NULL, CHANGE `amount_new_product_with_discount_price` `amount_new_product_with_discount_price` DECIMAL(20,6) NULL DEFAULT NULL, CHANGE `maximum_new_product_with_discount_price` `maximum_new_product_with_discount_price` DECIMAL(20,6) NULL DEFAULT NULL, CHANGE `percent_discount_purchase_more_product_quantity` `percent_discount_purchase_more_product_quantity` DECIMAL(20,6) NULL DEFAULT NULL, CHANGE `amount_discount_purchase_more_product_quantity` `amount_discount_purchase_more_product_quantity` DECIMAL(20,6) NULL DEFAULT NULL, CHANGE `maximum_discount_purchase_more_product_quantity` `maximum_discount_purchase_more_product_quantity` DECIMAL(20,6) NULL DEFAULT NULL, CHANGE `purchase_more_product_amount` `purchase_more_product_amount` DECIMAL(20,6) NULL DEFAULT NULL, CHANGE `percent_discount_purchase_more_product_amount` `percent_discount_purchase_more_product_amount` DECIMAL(20,6) NULL DEFAULT NULL, CHANGE `amount_discount_purchase_more_product_amount` `amount_discount_purchase_more_product_amount` DECIMAL(20,6) NULL DEFAULT NULL, CHANGE `maximum_discount_purchase_more_product_amount` `maximum_discount_purchase_more_product_amount` DECIMAL(20,6) NULL DEFAULT NULL, CHANGE `percent_discount_on_next_same_product` `percent_discount_on_next_same_product` DECIMAL(20,6) NULL DEFAULT NULL, CHANGE `amount_discount_on_next_same_product` `amount_discount_on_next_same_product` DECIMAL(20,6) NULL DEFAULT NULL, CHANGE `maximum_discount_on_next_same_product` `maximum_discount_on_next_same_product` DECIMAL(20,6) NULL DEFAULT NULL'; $sqls[] = 'ALTER TABLE `ps_ets_pr_action_rule` CHANGE `include_specific_products` `include_specific_products` TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, CHANGE `exclude_products` `exclude_products` TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, CHANGE `select_attributes` `select_attributes` TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, CHANGE `select_features` `select_features` TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, CHANGE `select_suppliers` `select_suppliers` TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, CHANGE `select_manufacturers` `select_manufacturers` TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, CHANGE `gift_product` `gift_product` TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL'; foreach($sqls as $sql) Db::getInstance()->execute($sql); return true; } public function checkCreatedColumn($table, $column) { $fieldsCustomers = Db::getInstance()->ExecuteS('DESCRIBE ' . _DB_PREFIX_ . pSQL($table)); $check_add = false; foreach ($fieldsCustomers as $field) { if ($field['Field'] == $column) { $check_add = true; break; } } return $check_add; } public function unInstallDb() { $tables = array( 'ets_pr_rule', 'ets_pr_rule_lang', 'ets_pr_condition_rule', 'ets_pr_action_rule', 'ets_pr_promote_rule', 'ets_pr_promote_rule_lang', 'ets_pr_cart_rule' ); if($tables) { foreach($tables as $table) Db::getInstance()->execute('DROP TABLE IF EXISTS `' . _DB_PREFIX_ . pSQL($table).'`'); } if($this->checkCreatedColumn('order_cart_rule','id_ets_pr_rule')) Db::getInstance()->execute('ALTER TABLE `'._DB_PREFIX_.'order_cart_rule` DROP `id_ets_pr_rule`'); return true; } public function searchProduct() { if (($query = Tools::getValue('q', false)) && Validate::isCleanHtml($query)) { $imageType = Ets_pr_defines::getFormattedName('cart'); if ($pos = strpos($query, ' (ref:')) { $query = Tools::substr($query, 0, $pos); } $excludeIds = Tools::getValue('excludeIds', false); $excludedProductIds = array(); if ($excludeIds && $excludeIds != 'NaN' && Validate::isCleanHtml($excludeIds)) { $excludeIds = implode(',', array_map('intval', explode(',', $excludeIds))); if($excludeIds && ($ids = explode(',',$excludeIds)) ) { foreach($ids as $id) { if($id) { $id = explode('-',$id); if(isset($id[0]) && isset($id[1]) && !$id[1]) { $excludedProductIds[] = (int)$id[0]; } } } } } else { $excludeIds = false; } $excludeVirtuals = (bool)Tools::getValue('excludeVirtuals', false); $exclude_packs = (bool)Tools::getValue('exclude_packs', false); if (version_compare(_PS_VERSION_, '1.6.1.0', '<')) { $imgLeftJoin = ' LEFT JOIN `' . _DB_PREFIX_ . 'image` i ON (i.`id_product` = p.`id_product`) '.Shop::addSqlAssociation('image', 'i', false, 'image_shop.cover = 1'); } else { $imgLeftJoin = ' LEFT JOIN `' . _DB_PREFIX_ . 'image_shop` image_shop ON (image_shop.`id_product` = p.`id_product` AND image_shop.id_shop=' . (int)$this->context->shop->id . ' AND image_shop.cover = 1) '; } $sql = 'SELECT p.`id_product`, pl.`link_rewrite`, p.`reference`, pl.`name`, image_shop.`id_image` id_image, il.`legend`, p.`cache_default_attribute` FROM `' . _DB_PREFIX_ . 'product` p ' . Shop::addSqlAssociation('product', 'p') . ' LEFT JOIN `' . _DB_PREFIX_ . 'product_lang` pl ON (pl.id_product = p.id_product AND pl.id_lang = ' . (int)$this->context->language->id . Shop::addSqlRestrictionOnLang('pl') . ') '. pSQL($imgLeftJoin) .' LEFT JOIN `' . _DB_PREFIX_ . 'image_lang` il ON (image_shop.`id_image` = il.`id_image` AND il.`id_lang` = ' . (int)$this->context->language->id . ') LEFT JOIN `'._DB_PREFIX_.'product_shop` ps ON (p.`id_product` = ps.`id_product`) WHERE '.($excludedProductIds ? 'p.`id_product` NOT IN('.implode(',',array_map('intval',$excludedProductIds)).') AND ' : '').' (pl.name LIKE \'%' . pSQL($query) . '%\' OR p.reference LIKE \'%' . pSQL($query) . '%\' OR p.id_product = '.(int)$query.') AND ps.`active` = 1 AND ps.`id_shop` = '.(int)$this->context->shop->id . ($excludeVirtuals ? ' AND NOT EXISTS (SELECT 1 FROM `' . _DB_PREFIX_ . 'product_download` pd WHERE (pd.id_product = p.id_product))' : '') . ($exclude_packs ? ' AND (p.cache_is_pack IS NULL OR p.cache_is_pack = 0)' : '') . ' GROUP BY p.id_product'; if (($items = Db::getInstance()->executeS($sql))) { $results = array(); foreach ($items as $item) { if (Combination::isFeatureActive() && (int)$item['cache_default_attribute']) { $sql = 'SELECT pa.`id_product_attribute`, pa.`reference`, ag.`id_attribute_group`, pai.`id_image`, agl.`name` AS group_name, al.`name` AS attribute_name, NULL as `attribute`, a.`id_attribute` FROM `' . _DB_PREFIX_ . 'product_attribute` pa ' . Shop::addSqlAssociation('product_attribute', 'pa') . ' LEFT JOIN `' . _DB_PREFIX_ . 'product_attribute_combination` pac ON pac.`id_product_attribute` = pa.`id_product_attribute` LEFT JOIN `' . _DB_PREFIX_ . 'attribute` a ON a.`id_attribute` = pac.`id_attribute` LEFT JOIN `' . _DB_PREFIX_ . 'attribute_group` ag ON ag.`id_attribute_group` = a.`id_attribute_group` LEFT JOIN `' . _DB_PREFIX_ . 'attribute_lang` al ON (a.`id_attribute` = al.`id_attribute` AND al.`id_lang` = ' . (int)$this->context->language->id . ') LEFT JOIN `' . _DB_PREFIX_ . 'attribute_group_lang` agl ON (ag.`id_attribute_group` = agl.`id_attribute_group` AND agl.`id_lang` = ' . (int)$this->context->language->id . ') LEFT JOIN `' . _DB_PREFIX_ . 'product_attribute_image` pai ON pai.`id_product_attribute` = pa.`id_product_attribute` WHERE pa.`id_product` = ' . (int)$item['id_product'] . ($excludeIds ? ' AND NOT FIND_IN_SET(CONCAT(pa.`id_product`,"-", IF(pa.`id_product_attribute` IS NULL,0,pa.`id_product_attribute`)), "' . pSQL($excludeIds) . '")' : '') . ' GROUP BY pa.`id_product_attribute`, ag.`id_attribute_group` ORDER BY pa.`id_product_attribute`'; if (($combinations = Db::getInstance()->executeS($sql))) { foreach ($combinations as $combination) { $results[$combination['id_product_attribute']]['id_product'] = $item['id_product']; $results[$combination['id_product_attribute']]['id_product_attribute'] = $combination['id_product_attribute']; $results[$combination['id_product_attribute']]['name'] = $item['name']; // get name attribute with combination !empty($results[$combination['id_product_attribute']]['attribute']) ? $results[$combination['id_product_attribute']]['attribute'] .= ' ' . $combination['group_name'] . '-' . $combination['attribute_name'] : $results[$combination['id_product_attribute']]['attribute'] = $item['attribute'] . ' ' . $combination['group_name'] . '-' . $combination['attribute_name']; // get reference combination if (!empty($combination['reference'])) { $results[$combination['id_product_attribute']]['ref'] = $combination['reference']; } else { $results[$combination['id_product_attribute']]['ref'] = !empty($item['reference']) ? $item['reference'] : ''; } // get image combination if (empty($results[$combination['id_product_attribute']]['image'])) { $results[$combination['id_product_attribute']]['image'] = str_replace('http://', Tools::getShopProtocol(), $this->context->link->getImageLink($item['link_rewrite'], (!empty($combination['id_image'])? (int)$combination['id_image'] : (int)$item['id_image']),$imageType)); } } } } else { if(!$item['id_image']) { $image = Product::getCover($item['id_product']); if($image) $item['id_image'] = $image['id_image']; } $results[] = array( 'id_product' => (int)($item['id_product']), 'id_product_attribute' => 0, 'name' => $item['name'], 'attribute' => '', 'ref' => (!empty($item['reference']) ? $item['reference'] : ''), 'image' =>$item['id_image'] ? str_replace('http://', Tools::getShopProtocol(), $this->context->link->getImageLink($item['link_rewrite'], $item['id_image'], $imageType)):str_replace('http://', Tools::getShopProtocol(), $this->context->link->getImageLink($item['link_rewrite'], $this->context->language->iso_code.'-default', $imageType)), ); } } if ($results) { foreach ($results as &$item) echo trim($item['id_product'] . '|' . (int)($item['id_product_attribute']) . '|' . Tools::ucfirst($item['name']). '|' . $item['attribute'] . '|' . $item['ref'] . '|' . $item['image']).'|'.Context::getContext()->link->getProductLink($item['id_product'],null,null,null,null,null,$item['id_product_attribute']). "\n"; } } die; } die; } public function searchCustomer() { if (($query = Tools::getValue('q', false)) && Validate::isCleanHtml($query)) { $excludeIds = Tools::getValue('excludeIds', false); $excludedCustomerds = array(); if ($excludeIds && $excludeIds != 'NaN' && Validate::isCleanHtml($excludeIds)) { $excludeIds = implode(',', array_map('intval', explode(',', $excludeIds))); if($excludeIds && ($ids = explode(',',$excludeIds)) ) { foreach($ids as $id) { $excludedCustomerds[] = $id; } } } else { $excludeIds = false; } $sql = 'SELECT * FROM `'._DB_PREFIX_.'customer` WHERE id_shop="'.(int)Context::getContext()->shop->id.'" AND (CONCAT(firstname," ",lastname) LIKE "%'.pSQL($query).'%" OR email like "%'.pSQL($query).'%")'.($excludedCustomerds ? ' AND id_customer NOT IN ('.implode(',',array_map('intval',$excludedCustomerds)).')':''); $customers = Db::getInstance()->executeS($sql); if ($customers) { foreach ($customers as &$item) echo $item['id_customer'] . '|' . $item['firstname'].' '.$item['lastname'] . '|' . $item['email']. "\n"; } } die; } public static function getFormattedName($name) { $themeName = Context::getContext()->shop->theme_name; $nameWithoutThemeName = str_replace(['_' . $themeName, $themeName . '_'], '', $name); //check if the theme name is already in $name if yes only return $name if ($themeName !== null && strstr($name, $themeName) && ImageType::getByNameNType($name)) { return $name; } if (ImageType::getByNameNType($nameWithoutThemeName . '_' . $themeName)) { return $nameWithoutThemeName . '_' . $themeName; } if (ImageType::getByNameNType($themeName . '_' . $nameWithoutThemeName)) { return $themeName . '_' . $nameWithoutThemeName; } return $nameWithoutThemeName . '_default'; } public static function getProductsByIds($products) { if (!$products) return false; if (!is_array($products)) { $IDs = explode(',', $products); $products = array(); foreach ($IDs as $ID) { if ($ID &&($tmpIDs = explode('-', $ID)) && $tmpIDs[0] ) { $products[] = array( 'id_product' => $tmpIDs[0], 'id_product_attribute' => isset($tmpIDs[1])? $tmpIDs[1] : 0, ); } } } if($products) { $context = Context::getContext(); $id_group = isset($context->customer->id) && $context->customer->id? Customer::getDefaultGroupId((int)$context->customer->id) : (int)Group::getCurrent()->id; $group = new Group($id_group); $useTax = $group->price_display_method? false : true; $imageType = self::getFormattedName('cart'); foreach($products as &$product) { $p = new Product($product['id_product'], true, $context->language->id, $context->shop->id); $product['link_rewrite'] = $p->link_rewrite; $product['price_float'] = $p->getPrice($useTax,$product['id_product_attribute'] ? $product['id_product_attribute'] : null); $product['price'] = Tools::displayPrice($product['price_float']); $product['name'] = $p->name; $product['description_short'] = $p->description_short; $image = ($product['id_product_attribute'] && ($image = self::getCombinationImageById($product['id_product_attribute'],$context->language->id))) ? $image : Product::getCover($product['id_product']); $product['link'] = $context->link->getProductLink($product,null,null,null,null,null,$product['id_product_attribute'] ? $product['id_product_attribute'] : 0); $product['id_image'] = isset($image['id_image']) && $image['id_image'] ? $image['id_image'] : $context->language->iso_code.'-default'; $product['image'] = $context->link->getImageLink($p->link_rewrite, isset($image['id_image']) ? $image['id_image'] : $context->language->iso_code.'-default', $imageType); if($product['id_product_attribute']) { $attributes = $p->getAttributeCombinationsById((int)$product['id_product_attribute'],$context->language->id); if($attributes) { $product['attributes']=''; foreach($attributes as $attribute) { $product['attributes'] .= $attribute['group_name'].': '.$attribute['attribute_name'].', '; } $product['attributes'] = trim($product['attributes'],', '); } } } unset($context); } return $products; } public static function getCombinationImageById($id_product_attribute, $id_lang) { if(version_compare(_PS_VERSION_,'1.6.1.0', '>=')) { return Product::getCombinationImageById($id_product_attribute, $id_lang); } else { if (!Combination::isFeatureActive() || !$id_product_attribute) { return false; } $result = Db::getInstance()->executeS(' SELECT pai.`id_image`, pai.`id_product_attribute`, il.`legend` FROM `'._DB_PREFIX_.'product_attribute_image` pai LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (il.`id_image` = pai.`id_image`) LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.`id_image` = pai.`id_image`) WHERE pai.`id_product_attribute` = '.(int)$id_product_attribute.' AND il.`id_lang` = '.(int)$id_lang.' ORDER by i.`position` LIMIT 1' ); if (!$result) { return false; } return $result[0]; } } public function getConfigInputs() { return array( array( 'type' => 'text', 'label' => $this->l('Title of product gift'), 'lang' => true, 'name' => 'ETS_PR_TITLE_OF_PRODUCT_GIFT', 'default' => $this->l('Please choose product gift'), 'default_lang' => 'Please choose product gift', 'validate' => 'isCleanHtml', 'tab' => 'general', ), array( 'name' => 'ETS_PR_STATUS_ORDER_VALIDATED', 'label' => $this->l('Total order amount calculated if order status is'), 'type'=> 'checkbox', 'values' => array( 'query' => OrderState::getOrderStates($this->context->language->id), 'id' => 'id_order_state', 'name' => 'name', ), 'default' => $this->getOrderStateDefault('approved'), 'tab' => 'general', ), array( 'type'=> 'switch', 'label' => $this->l('Auto clear discount codes if expired'), 'name' => 'ETS_PR_AUTO_CLEAR_RULE_EXPIRED', 'values' => array( array( 'id' => 'active_on', 'value' => 1, 'label' => $this->l('Yes') ), array( 'id' => 'active_off', 'value' => 0, 'label' => $this->l('No') ) ), 'default' => 0, 'validate' => 'isUnsignedInt', 'tab' => 'general', 'desc' => $this->l('Make sure cronjob is already configured on the server to clear expired discount codes automatically. [highlighta]Configure Cronjob.[end_highlighta]') ), array( 'type'=> 'switch', 'label' => $this->l('Auto clear discount codes if used'), 'name' => 'ETS_PR_AUTO_CLEAR_DISCOUNT_USED', 'values' => array( array( 'id' => 'active_on', 'value' => 1, 'label' => $this->l('Yes') ), array( 'id' => 'active_off', 'value' => 0, 'label' => $this->l('No') ) ), 'default' => 0, 'validate' => 'isUnsignedInt', 'tab' => 'general', 'desc' => $this->l('Make sure cronjob is already configured on the server to clear expired discount codes automatically. [highlighta]Configure Cronjob.[end_highlighta]') ), array( 'type' => 'html', 'html_content' => $this->renderFromCronJob(), 'tab' => 'cronjob', 'name'=>'', ), ); } public function renderFromCronJob() { $context = Context::getContext(); if(!Configuration::getGlobalValue('ETS_PR_CRONJOB_TOKEN')) Configuration::updateGlobalValue('ETS_PR_CRONJOB_TOKEN',Tools::passwdGen(12)); $cronjob_last= ''; $run_cronjob = false; if(Configuration::get('ETS_PR_AUTO_CLEAR_RULE_EXPIRED') || Configuration::get('ETS_PR_AUTO_CLEAR_DISCOUNT_USED')) $check_run = false; else $check_run = true; if($cronjob_time = Configuration::getGlobalValue('ETS_PR_TIME_LOG_CRONJOB')) { $last_time = strtotime($cronjob_time); $time = strtotime(date('Y-m-d H:i:s'))-$last_time; if($time <= 43200 && $time) $run_cronjob = true; else $run_cronjob = false; if($time > 86400) { $cronjob_last = Tools::displayDate($cronjob_time,null,true); } elseif($time) { $check_run = true; if($hours =floor($time/3600)) { $cronjob_last .= $hours.' '.$this->l('hours').' '; $time = $time%3600; } if($minutes = floor($time/60)) { $cronjob_last .= $minutes.' '.$this->l('minutes').' '; $time = $time%60; } if($time) $cronjob_last .= $time.' '.$this->l('seconds').' '; $cronjob_last .= $this->l('ago'); } } $ETS_PR_CRONJOB_TOKEN = Tools::getValue('ETS_PR_CRONJOB_TOKEN',Configuration::getGlobalValue('ETS_PR_CRONJOB_TOKEN')); $this->context->smarty->assign( array( 'dir_cronjob' => _PS_MODULE_DIR_.'ets_promotion/cronjob.php', 'php_path' => (defined('PHP_BINDIR') && PHP_BINDIR && is_string(PHP_BINDIR) ? PHP_BINDIR.'/' : '').'php ', 'link_conjob' => '', 'ETS_PR_CRONJOB_TOKEN' => $ETS_PR_CRONJOB_TOKEN, 'cronjob_log' => file_exists(_PS_ETS_PROMOTION_LOG_DIR_.'ets_promotion.log') ? Tools::file_get_contents(_PS_ETS_PROMOTION_LOG_DIR_.'ets_promotion.log'):'', 'ETS_PR_SAVE_CRONJOB_LOG' => Configuration::getGlobalValue('ETS_PR_SAVE_CRONJOB_LOG'), 'run_cronjob' => $run_cronjob, 'cronjob_last' => $cronjob_last, 'check_run' => $check_run, ) ); return $context->smarty->fetch(_PS_MODULE_DIR_.'ets_promotion/views/templates/hook/cron.tpl'); } public function getOrderStateDefault($type='pending') { $orderStates = OrderState::getOrderStates($this->context->language->id); $defaults= array(); if($type=='pending') $templates = array('cheque','bankwire','cashondelivery','outofstock','preparation'); elseif($type=='approved') $templates = array('payment','payment',''); elseif($type=='cancel') $templates = array('order_canceled','refund','payment_error'); if($orderStates) { foreach($orderStates as $orderState) { if(in_array($orderState['template'],$templates)) $defaults[] = $orderState['id_order_state']; } } if($defaults) return implode(',',$defaults); else return ''; } public static function getCategoriesById($id_categories) { if($id_categories) { $ids = explode(',',$id_categories); $sql = 'SELECT * FROM `'._DB_PREFIX_.'category` c LEFT JOIN `'._DB_PREFIX_.'category_lang` cl on (c.id_category = cl.id_category AND cl.id_lang="'.(int)Context::getContext()->language->id.'") WHERE c.id_category IN ('.implode(',',array_map('intval',$ids)).')'; return Db::getInstance()->executeS($sql); } } public static function displayText($content=null,$tag=null,$class=null,$id=null,$href=null,$blank=false,$src = null,$alt = null,$name = null,$value = null,$type = null,$data_id_product = null,$rel = null,$attr_datas=null, $title = null) { $text =''; if($tag) { $text .= '<'.$tag.($class ? ' class="'.$class.'"':'').($id ? ' id="'.$id.'"':''); if($href) $text .=' href="'.$href.'"'; if($blank && $tag ='a') $text .=' target="_blank"'; if($src) $text .=' src ="'.$src.'"'; if($name) $text .=' name="'.$name.'"'; if($value) $text .=' value ="'.$value.'"'; if($type) $text .= ' type="'.$type.'"'; if($data_id_product) $text .=' data-id_product="'.(int)$data_id_product.'"'; if($rel) { $text .=' rel="'.$rel.'"'; } if($alt) $text .=' alt="'.$alt.'"'; if($title) $text .=' title="'.$title.'"'; if($attr_datas) { foreach($attr_datas as $data) { $text .=' '.$data['name'].'='.'"'.$data['value'].'"'; } } if($tag=='img' || $tag=='br' || $tag=='input') $text .='/>'; else $text .='>'; if ($tag && $tag != 'img' && $tag != 'input' && $tag != 'br' && !is_null($content)) $text .= $content; if ($tag && $tag != 'img' && $tag != 'path' && $tag != 'input' && $tag != 'br') $text .= '<'.'/' . $tag . '>'; } else if ($content) { $text = $content; } return $text; } public static function checkEnableOtherShop($id_module) { $sql = 'SELECT * FROM `' . _DB_PREFIX_ . 'module_shop` WHERE `id_module` = ' . (int) $id_module . ' AND `id_shop` NOT IN(' . implode(', ', Shop::getContextListShopID()) . ')'; return Db::getInstance()->executeS($sql); } public static function activeTab($module_name) { return Db::getInstance()->execute('UPDATE `'._DB_PREFIX_.'tab` SET enabled=1 where module ="'.pSQL($module_name).'"'); } }