PageRenderTime 51ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/controllers/admin/AdminCartRulesController.php

https://bitbucket.org/marcenuc/prestashop
PHP | 585 lines | 488 code | 47 blank | 50 comment | 71 complexity | 093bdf2a63b2e95a9a0943063d8de1ad MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0
  1. <?php
  2. /*
  3. * 2007-2012 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2012 PrestaShop SA
  23. * @version Release: $Revision: 7060 $
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. * International Registered Trademark & Property of PrestaShop SA
  26. */
  27. class AdminCartRulesControllerCore extends AdminController
  28. {
  29. public function __construct()
  30. {
  31. $this->table = 'cart_rule';
  32. $this->className = 'CartRule';
  33. $this->lang = true;
  34. $this->addRowAction('delete');
  35. $this->addRowAction('edit');
  36. $this->bulk_actions = array('delete' => array('text' => $this->l('Delete selected'), 'confirm' => $this->l('Delete selected items?')));
  37. $this->fields_list = array(
  38. 'id_cart_rule' => array('title' => $this->l('ID'), 'align' => 'center', 'width' => 25),
  39. 'name' => array('title' => $this->l('Name')),
  40. 'priority' => array('title' => $this->l('Priority')),
  41. 'code' => array('title' => $this->l('Code')),
  42. 'quantity' => array('title' => $this->l('Quantity')),
  43. 'date_to' => array('title' => $this->l('Until')),
  44. 'active' => array('title' => $this->l('Status'), 'align' => 'center', 'active' => 'status', 'type' => 'bool', 'orderby' => false),
  45. );
  46. parent::__construct();
  47. }
  48. public function postProcess()
  49. {
  50. if (Tools::isSubmit('submitAddcart_rule') || Tools::isSubmit('submitAddcart_ruleAndStay'))
  51. {
  52. // If the reduction is associated to a specific product, then it must be part of the product restrictions
  53. if ((int)Tools::getValue('reduction_product') && Tools::getValue('apply_discount_to') == 'specific' && Tools::getValue('apply_discount') != 'off')
  54. {
  55. $reduction_product = (int)Tools::getValue('reduction_product');
  56. // First, check if it is not already part of the restrictions
  57. $already_restricted = false;
  58. if (is_array($rule_group_array = Tools::getValue('product_rule_group')) && count($rule_group_array) && Tools::getValue('product_restriction'))
  59. foreach ($rule_group_array as $rule_group_id)
  60. if (is_array($rule_array = Tools::getValue('product_rule_'.$rule_group_id)) && count($rule_array))
  61. foreach ($rule_array as $rule_id)
  62. if (Tools::getValue('product_rule_'.$rule_group_id.'_'.$rule_id.'_type') == 'products'
  63. && in_array($reduction_product, Tools::getValue('product_rule_select_'.$rule_group_id.'_'.$rule_id)))
  64. {
  65. $already_restricted = true;
  66. break 2;
  67. }
  68. if ($already_restricted == false)
  69. {
  70. // Check the product restriction
  71. $_POST['product_restriction'] = 1;
  72. // Add a new rule group
  73. $rule_group_id = 1;
  74. if (is_array($rule_group_array))
  75. {
  76. // Empty for (with a ; at the end), that just find the first rule_group_id available in rule_group_array
  77. for ($rule_group_id = 1; in_array($rule_group_id, $rule_group_array); ++$rule_group_id)
  78. 42;
  79. $_POST['product_rule_group'][] = $rule_group_id;
  80. }
  81. else
  82. $_POST['product_rule_group'] = array($rule_group_id);
  83. // Set a quantity of 1 for this new rule group
  84. $_POST['product_rule_group_'.$rule_group_id.'_quantity'] = 1;
  85. // Add one rule to the new rule group
  86. $_POST['product_rule_'.$rule_group_id] = array(1);
  87. // Set a type 'product' for this 1 rule
  88. $_POST['product_rule_'.$rule_group_id.'_1_type'] = 'products';
  89. // Add the product in the selected products
  90. $_POST['product_rule_select_'.$rule_group_id.'_1'] = array($reduction_product);
  91. }
  92. }
  93. // These are checkboxes (which aren't sent through POST when they are not check), so they are forced to 0
  94. foreach (array('country', 'carrier', 'group', 'cart_rule', 'product', 'shop') as $type)
  95. if (!Tools::getValue($type.'_restriction'))
  96. $_POST[$type.'_restriction'] = 0;
  97. // Remove the gift if the radio button is set to "no"
  98. if (!(int)Tools::getValue('free_gift'))
  99. $_POST['gift_product'] = 0;
  100. // Retrieve the product attribute id of the gift (if available)
  101. if ($id_product = (int)Tools::getValue('gift_product'))
  102. $_POST['gift_product_attribute'] = (int)Tools::getValue('ipa_'.$id_product);
  103. // Idiot-proof control
  104. if (strtotime(Tools::getValue('date_from')) > strtotime(Tools::getValue('date_to')))
  105. $this->errors[] = Tools::displayError('The voucher cannot end before it begins');
  106. if ((int)Tools::getValue('minimum_amount') < 0)
  107. $this->errors[] = Tools::displayError('Minimum amount cannot be lower than 0');
  108. if ((float)Tools::getValue('reduction_percent') < 0 || (float)Tools::getValue('reduction_percent') > 100)
  109. $this->errors[] = Tools::displayError('Reduction percent must be between 0% and 100%');
  110. if ((int)Tools::getValue('reduction_amount') < 0)
  111. $this->errors[] = Tools::displayError('Reduction amount cannot be lower than 0');
  112. }
  113. return parent::postProcess();
  114. }
  115. protected function afterUpdate($current_object)
  116. {
  117. // All the associations are deleted for an update, then recreated when we call the "afterAdd" method
  118. $id_cart_rule = Tools::getValue('id_cart_rule');
  119. foreach (array('country', 'carrier', 'group', 'product_rule_group', 'shop') as $type)
  120. Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'cart_rule_'.$type.'` WHERE `id_cart_rule` = '.(int)$id_cart_rule);
  121. Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'cart_rule_product_rule` WHERE `id_product_rule_group` NOT IN (SELECT `id_product_rule_group` FROM `'._DB_PREFIX_.'cart_rule_product_rule_group`)');
  122. Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'cart_rule_product_rule_value` WHERE `id_product_rule` NOT IN (SELECT `id_product_rule` FROM `'._DB_PREFIX_.'cart_rule_product_rule`)');
  123. Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'cart_rule_combination` WHERE `id_cart_rule_1` = '.(int)$id_cart_rule.' OR `id_cart_rule_2` = '.(int)$id_cart_rule);
  124. $this->afterAdd($current_object);
  125. }
  126. public function processAdd()
  127. {
  128. if ($cart_rule = parent::processAdd())
  129. $this->context->smarty->assign('new_cart_rule', $cart_rule);
  130. if (Tools::getValue('submitFormAjax'))
  131. $this->redirect_after = false;
  132. return $cart_rule;
  133. }
  134. /* @TODO Move this function into CartRule */
  135. protected function afterAdd($currentObject)
  136. {
  137. // Add restrictions for generic entities like country, carrier and group
  138. foreach (array('country', 'carrier', 'group', 'shop') as $type)
  139. if (Tools::getValue($type.'_restriction') && is_array($array = Tools::getValue($type.'_select')) && count($array))
  140. {
  141. $values = array();
  142. foreach ($array as $id)
  143. $values[] = '('.(int)$currentObject->id.','.(int)$id.')';
  144. Db::getInstance()->execute('INSERT INTO `'._DB_PREFIX_.'cart_rule_'.$type.'` (`id_cart_rule`, `id_'.$type.'`) VALUES '.implode(',', $values));
  145. }
  146. // Add cart rule restrictions
  147. if (Tools::getValue('cart_rule_restriction') && is_array($array = Tools::getValue('cart_rule_select')) && count($array))
  148. {
  149. $values = array();
  150. foreach ($array as $id)
  151. $values[] = '('.(int)$currentObject->id.','.(int)$id.')';
  152. Db::getInstance()->execute('INSERT INTO `'._DB_PREFIX_.'cart_rule_combination` (`id_cart_rule_1`, `id_cart_rule_2`) VALUES '.implode(',', $values));
  153. }
  154. // Add product rule restrictions
  155. if (Tools::getValue('product_restriction') && is_array($ruleGroupArray = Tools::getValue('product_rule_group')) && count($ruleGroupArray))
  156. {
  157. foreach ($ruleGroupArray as $ruleGroupId)
  158. {
  159. Db::getInstance()->execute('INSERT INTO `'._DB_PREFIX_.'cart_rule_product_rule_group` (`id_cart_rule`, `quantity`)
  160. VALUES ('.(int)$currentObject->id.', '.(int)Tools::getValue('product_rule_group_'.$ruleGroupId.'_quantity').')');
  161. $id_product_rule_group = Db::getInstance()->Insert_ID();
  162. if (is_array($ruleArray = Tools::getValue('product_rule_'.$ruleGroupId)) && count($ruleArray))
  163. foreach ($ruleArray as $ruleId)
  164. {
  165. Db::getInstance()->execute('INSERT INTO `'._DB_PREFIX_.'cart_rule_product_rule` (`id_product_rule_group`, `type`)
  166. VALUES ('.(int)$id_product_rule_group.', "'.pSQL(Tools::getValue('product_rule_'.$ruleGroupId.'_'.$ruleId.'_type')).'")');
  167. $id_product_rule = Db::getInstance()->Insert_ID();
  168. $values = array();
  169. foreach (Tools::getValue('product_rule_select_'.$ruleGroupId.'_'.$ruleId) as $id)
  170. $values[] = '('.(int)$id_product_rule.','.(int)$id.')';
  171. $values = array_unique($values);
  172. if (count($values))
  173. Db::getInstance()->execute('INSERT INTO `'._DB_PREFIX_.'cart_rule_product_rule_value` (`id_product_rule`, `id_item`) VALUES '.implode(',', $values));
  174. }
  175. }
  176. }
  177. // If the new rule has no cart rule restriction, then it must be added to the white list of the other cart rules that have restrictions
  178. if (!Tools::getValue('cart_rule_restriction'))
  179. {
  180. Db::getInstance()->execute('
  181. INSERT INTO `'._DB_PREFIX_.'cart_rule_combination` (`id_cart_rule_1`, `id_cart_rule_2`) (
  182. SELECT id_cart_rule, '.(int)$currentObject->id.' FROM `'._DB_PREFIX_.'cart_rule` WHERE cart_rule_restriction = 1
  183. )');
  184. }
  185. // And if the new cart rule has restrictions, previously unrestricted cart rules may now be restricted (a mug of coffee is strongly advised to understand this sentence)
  186. else
  187. {
  188. $ruleCombinations = Db::getInstance()->executeS('
  189. SELECT cr.id_cart_rule
  190. FROM '._DB_PREFIX_.'cart_rule cr
  191. WHERE cr.id_cart_rule != '.(int)$currentObject->id.'
  192. AND cr.cart_rule_restriction = 0
  193. AND cr.id_cart_rule NOT IN (
  194. SELECT IF(id_cart_rule_1 = '.(int)$currentObject->id.', id_cart_rule_2, id_cart_rule_1)
  195. FROM '._DB_PREFIX_.'cart_rule_combination
  196. WHERE '.(int)$currentObject->id.' = id_cart_rule_1
  197. OR '.(int)$currentObject->id.' = id_cart_rule_2
  198. )');
  199. foreach ($ruleCombinations as $incompatibleRule)
  200. {
  201. Db::getInstance()->execute('UPDATE `'._DB_PREFIX_.'cart_rule` SET cart_rule_restriction = 1 WHERE id_cart_rule = '.(int)$incompatibleRule['id_cart_rule'].' LIMIT 1');
  202. Db::getInstance()->execute('
  203. INSERT IGNORE INTO `'._DB_PREFIX_.'cart_rule_combination` (`id_cart_rule_1`, `id_cart_rule_2`) (
  204. SELECT id_cart_rule, '.(int)$incompatibleRule['id_cart_rule'].' FROM `'._DB_PREFIX_.'cart_rule`
  205. WHERE active = 1
  206. AND id_cart_rule != '.(int)$currentObject->id.'
  207. AND id_cart_rule != '.(int)$incompatibleRule['id_cart_rule'].'
  208. )');
  209. }
  210. }
  211. }
  212. /* Retrieve the cart rule product rule groups in the POST data if available, and in the database if there is none */
  213. public function getProductRuleGroupsDisplay($cart_rule)
  214. {
  215. $productRuleGroupsArray = array();
  216. if (Tools::getValue('product_restriction') && is_array($array = Tools::getValue('product_rule_group')) && count($array))
  217. {
  218. $i = 1;
  219. foreach ($array as $ruleGroupId)
  220. {
  221. $productRulesArray = array();
  222. if (is_array($array = Tools::getValue('product_rule_'.$ruleGroupId)) && count($array))
  223. {
  224. foreach ($array as $ruleId)
  225. {
  226. $productRulesArray[] = $this->getProductRuleDisplay(
  227. $ruleGroupId,
  228. $ruleId,
  229. Tools::getValue('product_rule_'.$ruleGroupId.'_'.$ruleId.'_type'),
  230. Tools::getValue('product_rule_select_'.$ruleGroupId.'_'.$ruleId)
  231. );
  232. }
  233. }
  234. $productRuleGroupsArray[] = $this->getProductRuleGroupDisplay(
  235. $i++,
  236. (int)Tools::getValue('product_rule_group_'.$ruleGroupId.'_quantity'),
  237. $productRulesArray
  238. );
  239. }
  240. }
  241. else
  242. {
  243. $i = 1;
  244. foreach ($cart_rule->getProductRuleGroups() as $productRuleGroup)
  245. {
  246. $j = 1;
  247. $productRulesDisplay = array();
  248. foreach ($productRuleGroup['product_rules'] as $id_product_rule => $productRule)
  249. $productRulesDisplay[] = $this->getProductRuleDisplay($i, $j++, $productRule['type'], $productRule['values']);
  250. $productRuleGroupsArray[] = $this->getProductRuleGroupDisplay($i++, $productRuleGroup['quantity'], $productRulesDisplay);
  251. }
  252. }
  253. return $productRuleGroupsArray;
  254. }
  255. /* Return the form for a single cart rule group either with or without product_rules set up */
  256. public function getProductRuleGroupDisplay($product_rule_group_id, $product_rule_group_quantity = 1, $product_rules = null)
  257. {
  258. Context::getContext()->smarty->assign('product_rule_group_id', $product_rule_group_id);
  259. Context::getContext()->smarty->assign('product_rule_group_quantity', $product_rule_group_quantity);
  260. Context::getContext()->smarty->assign('product_rules', $product_rules);
  261. return Context::getContext()->smarty->fetch('controllers/cart_rules/product_rule_group.tpl');
  262. }
  263. public function getProductRuleDisplay($product_rule_group_id, $product_rule_id, $product_rule_type, $selected = array())
  264. {
  265. Context::getContext()->smarty->assign(
  266. array(
  267. 'product_rule_group_id' => (int)$product_rule_group_id,
  268. 'product_rule_id' => (int)$product_rule_id,
  269. 'product_rule_type' => $product_rule_type,
  270. )
  271. );
  272. switch ($product_rule_type)
  273. {
  274. case 'attributes':
  275. $attributes = array('selected' => array(), 'unselected' => array());
  276. $results = Db::getInstance()->executeS('
  277. SELECT CONCAT(agl.name, " - ", al.name) as name, a.id_attribute as id
  278. FROM '._DB_PREFIX_.'attribute_group_lang agl
  279. LEFT JOIN '._DB_PREFIX_.'attribute a ON a.id_attribute_group = agl.id_attribute_group
  280. LEFT JOIN '._DB_PREFIX_.'attribute_lang al ON (a.id_attribute = al.id_attribute AND al.id_lang = '.(int)Context::getContext()->language->id.')
  281. WHERE agl.id_lang = '.(int)Context::getContext()->language->id.'
  282. ORDER BY agl.name, al.name');
  283. foreach ($results as $row)
  284. $attributes[in_array($row['id'], $selected) ? 'selected' : 'unselected'][] = $row;
  285. Context::getContext()->smarty->assign('product_rule_itemlist', $attributes);
  286. $choose_content = $this->createTemplate('controllers/cart_rules/product_rule_itemlist.tpl')->fetch();
  287. Context::getContext()->smarty->assign('product_rule_choose_content', $choose_content);
  288. break;
  289. case 'products':
  290. $products = array('selected' => array(), 'unselected' => array());
  291. $results = Db::getInstance()->executeS('
  292. SELECT DISTINCT name, p.id_product as id
  293. FROM '._DB_PREFIX_.'product p
  294. LEFT JOIN `'._DB_PREFIX_.'product_lang` pl
  295. ON (p.`id_product` = pl.`id_product`
  296. AND pl.`id_lang` = '.(int)Context::getContext()->language->id.Shop::addSqlRestrictionOnLang('pl').')
  297. '.Shop::addSqlAssociation('product', 'p').'
  298. WHERE id_lang = '.(int)Context::getContext()->language->id.'
  299. ORDER BY name');
  300. foreach ($results as $row)
  301. $products[in_array($row['id'], $selected) ? 'selected' : 'unselected'][] = $row;
  302. Context::getContext()->smarty->assign('product_rule_itemlist', $products);
  303. $choose_content = $this->createTemplate('controllers/cart_rules/product_rule_itemlist.tpl')->fetch();
  304. Context::getContext()->smarty->assign('product_rule_choose_content', $choose_content);
  305. break;
  306. case 'manufacturers':
  307. $products = array('selected' => array(), 'unselected' => array());
  308. $results = Db::getInstance()->executeS('
  309. SELECT name, id_manufacturer as id
  310. FROM '._DB_PREFIX_.'manufacturer
  311. ORDER BY name');
  312. foreach ($results as $row)
  313. $products[in_array($row['id'], $selected) ? 'selected' : 'unselected'][] = $row;
  314. Context::getContext()->smarty->assign('product_rule_itemlist', $products);
  315. $choose_content = $this->createTemplate('controllers/cart_rules/product_rule_itemlist.tpl')->fetch();
  316. Context::getContext()->smarty->assign('product_rule_choose_content', $choose_content);
  317. break;
  318. case 'suppliers':
  319. $products = array('selected' => array(), 'unselected' => array());
  320. $results = Db::getInstance()->executeS('
  321. SELECT name, id_supplier as id
  322. FROM '._DB_PREFIX_.'supplier
  323. ORDER BY name');
  324. foreach ($results as $row)
  325. $products[in_array($row['id'], $selected) ? 'selected' : 'unselected'][] = $row;
  326. Context::getContext()->smarty->assign('product_rule_itemlist', $products);
  327. $choose_content = $this->createTemplate('controllers/cart_rules/product_rule_itemlist.tpl')->fetch();
  328. Context::getContext()->smarty->assign('product_rule_choose_content', $choose_content);
  329. break;
  330. case 'categories':
  331. $categories = array('selected' => array(), 'unselected' => array());
  332. $results = Db::getInstance()->executeS('
  333. SELECT DISTINCT name, c.id_category as id
  334. FROM '._DB_PREFIX_.'category c
  335. LEFT JOIN `'._DB_PREFIX_.'category_lang` cl
  336. ON (c.`id_category` = cl.`id_category`
  337. AND cl.`id_lang` = '.(int)Context::getContext()->language->id.Shop::addSqlRestrictionOnLang('cl').')
  338. '.Shop::addSqlAssociation('category', 'c').'
  339. WHERE id_lang = '.(int)Context::getContext()->language->id.'
  340. ORDER BY name');
  341. foreach ($results as $row)
  342. $categories[in_array($row['id'], $selected) ? 'selected' : 'unselected'][] = $row;
  343. Context::getContext()->smarty->assign('product_rule_itemlist', $categories);
  344. $choose_content = $this->createTemplate('controllers/cart_rules/product_rule_itemlist.tpl')->fetch();
  345. Context::getContext()->smarty->assign('product_rule_choose_content', $choose_content);
  346. break;
  347. default :
  348. Context::getContext()->smarty->assign('product_rule_itemlist', array('selected' => array(), 'unselected' => array()));
  349. Context::getContext()->smarty->assign('product_rule_choose_content', '');
  350. }
  351. return $this->createTemplate('controllers/cart_rules/product_rule.tpl')->fetch();
  352. }
  353. public function ajaxProcess()
  354. {
  355. if (Tools::isSubmit('newProductRule'))
  356. die ($this->getProductRuleDisplay(Tools::getValue('product_rule_group_id'), Tools::getValue('product_rule_id'), Tools::getValue('product_rule_type')));
  357. if (Tools::isSubmit('newProductRuleGroup') && $product_rule_group_id = Tools::getValue('product_rule_group_id'))
  358. die ($this->getProductRuleGroupDisplay($product_rule_group_id, Tools::getValue('product_rule_group_'.$product_rule_group_id.'_quantity', 1)));
  359. if (Tools::isSubmit('customerFilter'))
  360. {
  361. $search_query = trim(Tools::getValue('q'));
  362. $customers = Db::getInstance()->executeS('
  363. SELECT `id_customer`, `email`, CONCAT(`firstname`, \' \', `lastname`) as cname
  364. FROM `'._DB_PREFIX_.'customer`
  365. WHERE `deleted` = 0 AND is_guest = 0 AND active = 1
  366. AND (
  367. `id_customer` = '.(int)$search_query.'
  368. OR `email` LIKE "%'.pSQL($search_query).'%"
  369. OR `firstname` LIKE "%'.pSQL($search_query).'%"
  370. OR `lastname` LIKE "%'.pSQL($search_query).'%"
  371. )
  372. ORDER BY `firstname`, `lastname` ASC
  373. LIMIT 50');
  374. die(Tools::jsonEncode($customers));
  375. }
  376. // Both product filter (free product and product discount) search for products
  377. if (Tools::isSubmit('giftProductFilter') || Tools::isSubmit('reductionProductFilter'))
  378. {
  379. $products = Product::searchByName(Context::getContext()->language->id, trim(Tools::getValue('q')));
  380. die(Tools::jsonEncode($products));
  381. }
  382. }
  383. protected function searchProducts($search)
  384. {
  385. if ($products = Product::searchByName((int)$this->context->language->id, $search))
  386. {
  387. foreach ($products as &$product)
  388. {
  389. $combinations = array();
  390. $productObj = new Product((int)$product['id_product'], false, (int)$this->context->language->id);
  391. $attributes = $productObj->getAttributesGroups((int)$this->context->language->id);
  392. $product['formatted_price'] = Tools::displayPrice(Tools::convertPrice($product['price_tax_incl'], $this->context->currency), $this->context->currency);
  393. foreach ($attributes as $attribute)
  394. {
  395. if (!isset($combinations[$attribute['id_product_attribute']]['attributes']))
  396. $combinations[$attribute['id_product_attribute']]['attributes'] = '';
  397. $combinations[$attribute['id_product_attribute']]['attributes'] .= $attribute['attribute_name'].' - ';
  398. $combinations[$attribute['id_product_attribute']]['id_product_attribute'] = $attribute['id_product_attribute'];
  399. $combinations[$attribute['id_product_attribute']]['default_on'] = $attribute['default_on'];
  400. if (!isset($combinations[$attribute['id_product_attribute']]['price']))
  401. {
  402. $price_tax_incl = Product::getPriceStatic((int)$product['id_product'], true, $attribute['id_product_attribute']);
  403. $combinations[$attribute['id_product_attribute']]['formatted_price'] = Tools::displayPrice(Tools::convertPrice($price_tax_incl, $this->context->currency), $this->context->currency);
  404. }
  405. }
  406. foreach ($combinations as &$combination)
  407. $combination['attributes'] = rtrim($combination['attributes'], ' - ');
  408. $product['combinations'] = $combinations;
  409. }
  410. return array(
  411. 'products' => $products,
  412. 'found' => true
  413. );
  414. }
  415. else
  416. return array('found' => false, 'notfound' => Tools::displayError('No product found'));
  417. }
  418. public function ajaxProcessSearchProducts()
  419. {
  420. $array = $this->searchProducts(Tools::getValue('product_search'));
  421. $this->content = trim(Tools::jsonEncode($array));
  422. }
  423. public function renderForm()
  424. {
  425. $back = Tools::safeOutput(Tools::getValue('back', ''));
  426. if (empty($back))
  427. $back = self::$currentIndex.'&token='.$this->token;
  428. $this->toolbar_btn['save-and-stay'] = array(
  429. 'href' => '#',
  430. 'desc' => $this->l('Save and Stay')
  431. );
  432. // Todo: change for "Media" version
  433. $this->addJs(_PS_JS_DIR_.'jquery/plugins/jquery.typewatch.js');
  434. $this->addJs(_PS_JS_DIR_.'jquery/plugins/fancybox/jquery.fancybox.js');
  435. $this->addJs(_PS_JS_DIR_.'jquery/plugins/autocomplete/jquery.autocomplete.js');
  436. $this->addCss(_PS_JS_DIR_.'jquery/plugins/fancybox/jquery.fancybox.css');
  437. $this->addCss(_PS_JS_DIR_.'jquery/plugins/autocomplete/jquery.autocomplete.css');
  438. $current_object = $this->loadObject(true);
  439. // All the filter are prefilled with the correct information
  440. $customer_filter = '';
  441. if (Validate::isUnsignedId($current_object->id_customer) &&
  442. ($customer = new Customer($current_object->id_customer)) &&
  443. Validate::isLoadedObject($customer))
  444. $customer_filter = $customer->firstname.' '.$customer->lastname.' ('.$customer->email.')';
  445. $gift_product_filter = '';
  446. if (Validate::isUnsignedId($current_object->gift_product) &&
  447. ($product = new Product($current_object->gift_product, false, $this->context->language->id)) &&
  448. Validate::isLoadedObject($product))
  449. $gift_product_filter = (!empty($product->reference) ? $product->reference : $product->name);
  450. $reduction_product_filter = '';
  451. if (Validate::isUnsignedId($current_object->reduction_product) &&
  452. ($product = new Product($current_object->reduction_product, false, $this->context->language->id)) &&
  453. Validate::isLoadedObject($product))
  454. $reduction_product_filter = (!empty($product->reference) ? $product->reference : $product->name);
  455. $product_rule_groups = $this->getProductRuleGroupsDisplay($current_object);
  456. $attribute_groups = AttributeGroup::getAttributesGroups($this->context->language->id);
  457. $currencies = Currency::getCurrencies();
  458. $languages = Language::getLanguages();
  459. $countries = $current_object->getAssociatedRestrictions('country', true, true);
  460. $groups = $current_object->getAssociatedRestrictions('group', false, true);
  461. $shops = $current_object->getAssociatedRestrictions('shop', false, false);
  462. $cart_rules = $current_object->getAssociatedRestrictions('cart_rule', false, true);
  463. $carriers = $current_object->getAssociatedRestrictions('carrier', true, false);
  464. foreach ($carriers as &$carriers2)
  465. foreach ($carriers2 as &$carrier)
  466. foreach ($carrier as $field => &$value)
  467. if ($field == 'name' && $value == '0')
  468. $value = Configuration::get('PS_SHOP_NAME');
  469. $gift_product_select = '';
  470. $gift_product_attribute_select = '';
  471. if ((int)$current_object->gift_product)
  472. {
  473. $search_products = $this->searchProducts($gift_product_filter);
  474. foreach ($search_products['products'] as $product)
  475. {
  476. $gift_product_select .= '
  477. <option value="'.$product['id_product'].'" '.($product['id_product'] == $current_object->gift_product ? 'selected="selected"' : '').'>
  478. '.$product['name'].(count($product['combinations']) == 0 ? ' - '.$product['formatted_price'] : '').'
  479. </option>';
  480. if (count($product['combinations']))
  481. {
  482. $gift_product_attribute_select .= '<select class="id_product_attribute" id="ipa_'.$product['id_product'].'" name="ipa_'.$product['id_product'].'">';
  483. foreach ($product['combinations'] as $combination)
  484. {
  485. $gift_product_attribute_select .= '
  486. <option '.($combination['id_product_attribute'] == $current_object->gift_product_attribute ? 'selected="selected"' : '').' value="'.$combination['id_product_attribute'].'">
  487. '.$combination['attributes'].' - '.$combination['formatted_price'].'
  488. </option>';
  489. }
  490. $gift_product_attribute_select .= '</select>';
  491. }
  492. }
  493. }
  494. $product = new Product($current_object->gift_product);
  495. $this->context->smarty->assign(
  496. array(
  497. 'show_toolbar' => true,
  498. 'toolbar_btn' => $this->toolbar_btn,
  499. 'toolbar_scroll' => $this->toolbar_scroll,
  500. 'title' => array($this->l('Payment'), $this->l('Cart Rules')),
  501. 'defaultDateFrom' => date('Y-m-d H:00:00'),
  502. 'defaultDateTo' => date('Y-m-d H:00:00', strtotime('+1 month')),
  503. 'customerFilter' => $customer_filter,
  504. 'giftProductFilter' => $gift_product_filter,
  505. 'gift_product_select' => $gift_product_select,
  506. 'gift_product_attribute_select' => $gift_product_attribute_select,
  507. 'reductionProductFilter' => $reduction_product_filter,
  508. 'defaultCurrency' => Configuration::get('PS_CURRENCY_DEFAULT'),
  509. 'id_lang_default' => Configuration::get('PS_LANG_DEFAULT'),
  510. 'languages' => $languages,
  511. 'currencies' => $currencies,
  512. 'countries' => $countries,
  513. 'carriers' => $carriers,
  514. 'groups' => $groups,
  515. 'shops' => $shops,
  516. 'cart_rules' => $cart_rules,
  517. 'product_rule_groups' => $product_rule_groups,
  518. 'product_rule_groups_counter' => count($product_rule_groups),
  519. 'attribute_groups' => $attribute_groups,
  520. 'currentIndex' => self::$currentIndex,
  521. 'currentToken' => $this->token,
  522. 'currentObject' => $current_object,
  523. 'currentTab' => $this,
  524. 'hasAttribute' => $product->hasAttributes(),
  525. )
  526. );
  527. $this->content .= $this->createTemplate('form.tpl')->fetch();
  528. $this->addJqueryUI('ui.datepicker');
  529. return parent::renderForm();
  530. }
  531. public function displayAjaxSearchCartRuleVouchers()
  532. {
  533. $found = false;
  534. if ($vouchers = CartRule::getCartsRuleByCode(Tools::getValue('q'), (int)$this->context->language->id))
  535. $found = true;
  536. echo Tools::jsonEncode(array('found' => $found, 'vouchers' => $vouchers));
  537. }
  538. }