PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/admin444/tabs/AdminAttributeGenerator.php

http://marocmall.googlecode.com/
PHP | 270 lines | 238 code | 22 blank | 10 comment | 11 complexity | 6025d4016759a54f448192ad970e6489 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * Attribute generator tab for admin panel, AdminAttributeGenerator.php
  4. * @category admin
  5. *
  6. * @author PrestaShop <support@prestashop.com>
  7. * @copyright PrestaShop
  8. * @license http://www.opensource.org/licenses/osl-3.0.php Open-source licence 3.0
  9. * @version 1.3
  10. *
  11. */
  12. @ini_set('max_execution_time', 3600);
  13. include_once(PS_ADMIN_DIR.'/../classes/AdminTab.php');
  14. class AdminAttributeGenerator extends AdminTab
  15. {
  16. private $combinations = array();
  17. private $product;
  18. private function addAttribute($arr, $price = 0, $weight = 0)
  19. {
  20. foreach ($arr AS $attr)
  21. {
  22. $price += floatval($_POST['price_impact'][intval($attr)]);
  23. $weight += floatval($_POST['weight_impact'][intval($attr)]);
  24. }
  25. if ($this->product->id)
  26. {
  27. return (array(
  28. 'id_product' => intval($this->product->id),
  29. 'price' => floatval($price),
  30. 'weight' => floatval($weight),
  31. 'ecotax' => 0,
  32. 'quantity' => intval($_POST['quantity']),
  33. 'reference' => pSQL($_POST['reference']),
  34. 'default_on' => 0));
  35. }
  36. return array();
  37. }
  38. static private function createCombinations($list)
  39. {
  40. if (sizeof($list) <= 1)
  41. return sizeof($list) ? array_map(create_function('$v', 'return (array($v));'), $list[0]) : $list;
  42. $res = array();
  43. $first = array_pop($list);
  44. foreach ($first AS $attribute)
  45. {
  46. $tab = self::createCombinations($list);
  47. foreach ($tab AS $toAdd)
  48. $res[] = is_array($toAdd) ? array_merge($toAdd, array($attribute)) : array($toAdd, $attribute);
  49. }
  50. return $res;
  51. }
  52. public function postProcess()
  53. {
  54. global $currentIndex;
  55. $this->product = new Product(intval(Tools::getValue('id_product')));
  56. if (isset($_POST['generate']))
  57. {
  58. if (!is_array(Tools::getValue('options')))
  59. $this->_errors[] = Tools::displayError('You need to choose at least 1 attribute.');
  60. else
  61. {
  62. $tab = array_values($_POST['options']);
  63. if (sizeof($tab) AND Validate::isLoadedObject($this->product))
  64. {
  65. self::setAttributesImpacts($this->product->id, $tab);
  66. $this->combinations = array_values(self::createCombinations($tab));
  67. $values = array_values(array_map(array($this, 'addAttribute'), $this->combinations));
  68. $this->product->deleteProductAttributes();
  69. $res = $this->product->addProductAttributeMultiple($values);
  70. $this->product->addAttributeCombinationMultiple($res, $this->combinations);
  71. $this->product->updateQuantityProductWithAttributeQuantity();
  72. }
  73. else
  74. $this->_errors[] = Tools::displayError('Unable to initialize parameters, combinations is missing or object cannot be load.');
  75. }
  76. }
  77. elseif (isset($_POST['back']))
  78. Tools::redirectAdmin($currentIndex.'&id_product='.intval(Tools::getValue('id_product')).'&id_category='.intval(Tools::getValue('id_category')).'&addproduct'.'&tabs=2&token='.Tools::getValue('token'));
  79. parent::postProcess();
  80. }
  81. static private function displayAndReturnAttributeJs()
  82. {
  83. global $cookie;
  84. $attributes = Attribute::getAttributes(intval($cookie->id_lang), true);
  85. $attributeJs = array();
  86. foreach ($attributes AS $k => $attribute)
  87. $attributeJs[$attribute['id_attribute_group']][$attribute['id_attribute']] = $attribute['name'];
  88. echo '
  89. <script type="text/javascript">
  90. var attrs = new Array();
  91. attrs[0] = new Array(0, \'---\');';
  92. foreach ($attributeJs AS $idgrp => $group)
  93. {
  94. echo '
  95. attrs['.$idgrp.'] = new Array(0, \'---\' ';
  96. foreach ($group AS $idattr => $attrname)
  97. echo ', '.$idattr.', \''.addslashes(($attrname)).'\'';
  98. echo ');';
  99. }
  100. echo '
  101. </script>';
  102. return $attributeJs;
  103. }
  104. private function displayGroupSelect($attributeJs, $attributesGroups)
  105. {
  106. echo ' <div>
  107. <select multiple name="attributes[]" id="attribute_group" style="width: 200px; height: 350px;">';
  108. foreach ($attributesGroups AS $k => $attributeGroup)
  109. {
  110. $idGroup = $attributeGroup['id_attribute_group'];
  111. if (isset($attributeJs[$idGroup]))
  112. {
  113. echo ' <optgroup name="'.$idGroup.'" id="'.$idGroup.'" label="'.htmlspecialchars(stripslashes($attributeGroup['name'])).'">';
  114. foreach ($attributeJs[$idGroup] AS $k => $v)
  115. echo ' <option name="'.$k.'" id="attr_'.$k.'" value="'.htmlspecialchars($v, ENT_QUOTES).'" title="'.htmlspecialchars($v, ENT_QUOTES).'"">'.$v.'</option>';
  116. echo ' </optgroup>';
  117. }
  118. }
  119. echo ' </select>
  120. </div>';
  121. }
  122. static private function setAttributesImpacts($id_product, $tab)
  123. {
  124. $attributes = array();
  125. foreach ($tab AS $group)
  126. foreach ($group AS $attribute)
  127. $attributes[] = '('.intval($id_product).', '.intval($attribute).', '.floatval($_POST['price_impact'][intval($attribute)]).', '.floatval($_POST['weight_impact'][intval($attribute)]).')';
  128. return Db::getInstance()->Execute(
  129. 'INSERT INTO `'._DB_PREFIX_.'attribute_impact` (`id_product`, `id_attribute`, `price`, `weight`)
  130. VALUES '.implode(',', $attributes).'
  131. ON DUPLICATE KEY UPDATE `price`=VALUES(price), `weight`=VALUES(weight)'
  132. );
  133. }
  134. static private function getAttributesImpacts($id_product)
  135. {
  136. $tab = array();
  137. $result = Db::getInstance()->ExecuteS(
  138. 'SELECT ai.`id_attribute`, ai.`price`, ai.`weight`
  139. FROM `'._DB_PREFIX_.'attribute_impact` ai
  140. WHERE ai.`id_product` = '.intval($id_product));
  141. if (!$result)
  142. return array();
  143. foreach ($result AS $impact)
  144. {
  145. $tab[$impact['id_attribute']]['price'] = floatval($impact['price']);
  146. $tab[$impact['id_attribute']]['weight'] = floatval($impact['weight']);
  147. }
  148. return $tab;
  149. }
  150. private function displayGroupeTable($attributeJs, $attributesGroups)
  151. {
  152. global $cookie;
  153. $currency = new Currency(Configuration::get('PS_CURRENCY_DEFAULT'));
  154. $combinationsGroups = $this->product->getAttributesGroups(intval($cookie->id_lang));
  155. $attributes = array();
  156. $impacts = self::getAttributesImpacts($this->product->id);
  157. foreach ($combinationsGroups AS &$combination)
  158. {
  159. $target = &$attributes[$combination['id_attribute_group']][$combination['id_attribute']];
  160. $target = $combination;
  161. if (isset($impacts[$combination['id_attribute']]))
  162. {
  163. $target['price'] = $impacts[$combination['id_attribute']]['price'];
  164. $target['weight'] = $impacts[$combination['id_attribute']]['weight'];
  165. }
  166. }
  167. foreach ($attributesGroups AS $k => $attributeGroup)
  168. {
  169. $idGroup = $attributeGroup['id_attribute_group'];
  170. if (isset($attributeJs[$idGroup]))
  171. {
  172. echo '
  173. <br class="clear"/>
  174. <table class="table" cellpadding="0" cellspacing="0" align="left" style="margin-bottom: 10px; display: none;">
  175. <thead>
  176. <tr>
  177. <th id="tab_h1" style="width: 250px">'.htmlspecialchars(stripslashes($attributeGroup['name'])).'</th>
  178. <th id="tab_h2" style="width: 150px">'.$this->l('Price impact').' ('.$currency->sign.')'.' <sup>*</sup></th>
  179. <th style="width: 150px">'.$this->l('Weight impact').' ('.Configuration::get('PS_WEIGHT_UNIT').')'.'</th>
  180. </tr>
  181. </thead>
  182. <tbody id="table_'.$idGroup.'" name="result_table">
  183. </tbody>
  184. </table>';
  185. if (isset($attributes[$idGroup]))
  186. foreach ($attributes[$idGroup] AS $k => $attribute)
  187. echo '<script type="text/javascript">getE(\'table_\' + '.$idGroup.').appendChild(create_attribute_row('.$k.', '.$idGroup.', \''.addslashes($attribute['attribute_name']).'\', '.$attribute['price'].', '.$attribute['weight'].'));toggle(getE(\'table_\' + '.$idGroup.').parentNode, true);</script>';
  188. }
  189. }
  190. echo '<p><sup>*</sup> '.$this->l('tax included').'</p>';
  191. }
  192. public function displayForm($isMainTab = true)
  193. {
  194. global $currentIndex, $cookie;
  195. parent::displayForm();
  196. $jsAttributes = self::displayAndReturnAttributeJs();
  197. $attributesGroups = AttributeGroup::getAttributesGroups(intval($cookie->id_lang));
  198. $this->product = new Product(intval(Tools::getValue('id_product')));
  199. if (isset($_POST['generate']) AND !sizeof($this->_errors))
  200. echo '
  201. <div class="module_confirmation conf confirm">
  202. <img src="../img/admin/ok.gif" alt="" title="" style="margin-right:5px; float:left;" />
  203. '.sizeof($this->combinations).' '.$this->l('product(s) successfully created.').'
  204. </div>';
  205. echo '
  206. <script type="text/javascript" src="../js/attributesBack.js"></script>
  207. <form enctype="multipart/form-data" method="post" id="generator" action=""'.$currentIndex.'&id_category='.intval(Tools::getValue('id_category')).'token='.Tools::getValue('token').'">
  208. <fieldset style="margin-bottom: 35px;"><legend><img src="../img/admin/asterisk.gif" />'.$this->l('Attributes generator').'</legend>'.
  209. $this->l('Add or modify attributes for this product:').'
  210. <br /><br />
  211. ';
  212. echo '
  213. <div style="padding-top:10px; float: left; width: 570px;">
  214. <div style="float:left;">
  215. <label>'.$this->l('Quantity:').'</label>
  216. <div class="margin-form">
  217. <input type="text" size="20" name="quantity" value="1"/>
  218. </div>
  219. <label>'.$this->l('Reference:').'</label>
  220. <div class="margin-form">
  221. <input type="text" size="20" name="reference" value="'.$this->product->reference.'"/>
  222. </div>
  223. </div>
  224. <div style="float:left; text-align:center; margin-left:20px;">
  225. <input type="submit" class="button" style="margin-bottom:5px;" name="generate" value="'.$this->l('Generate').'" /><br />
  226. <input type="submit" class="button" name="back" value="'.$this->l('Back to product').'" />
  227. </div>
  228. <br style="clear:both;" />
  229. <div style="margin-top: 15px;">';
  230. self::displayGroupeTable($jsAttributes, $attributesGroups);
  231. echo '
  232. </div>
  233. </div>
  234. <div style="float: left; margin-left: 60px;">
  235. ';
  236. self::displayGroupSelect($jsAttributes, $attributesGroups);
  237. echo '
  238. <div>
  239. <input class="button" type="button" style="margin-left: 20px;" value="'.$this->l('Add').'" class="button" onclick="add_attr_multiple();" />
  240. <input class="button" type="button" style="margin-left: 20px;" value="'.$this->l('Delete').'" class="button" onclick="del_attr_multiple();" />
  241. </div>
  242. </div>
  243. <br />
  244. </fieldset>
  245. </form>';
  246. }
  247. }
  248. ?>