* @copyright 2007-2011 PrestaShop SA * @version Release: $Revision: 7556 $ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) * International Registered Trademark & Property of PrestaShop SA */ @ini_set('max_execution_time', 3600); include_once(PS_ADMIN_DIR.'/../classes/AdminTab.php'); class AdminAttributeGenerator extends AdminTab { private $combinations = array(); private $product; private function addAttribute($arr, $price = 0, $weight = 0) { foreach ($arr AS $attr) { $price += (float)($_POST['price_impact_'.(int)($attr)]); $weight += (float)($_POST['weight_impact'][(int)($attr)]); } if ($this->product->id) { return (array( 'id_product' => (int)($this->product->id), 'price' => (float)($price), 'weight' => (float)($weight), 'ecotax' => 0, 'quantity' => (int)($_POST['quantity']), 'reference' => pSQL($_POST['reference']), 'default_on' => 0)); } return array(); } static private function createCombinations($list) { if (sizeof($list) <= 1) return sizeof($list) ? array_map(create_function('$v', 'return (array($v));'), $list[0]) : $list; $res = array(); $first = array_pop($list); foreach ($first AS $attribute) { $tab = self::createCombinations($list); foreach ($tab AS $toAdd) $res[] = is_array($toAdd) ? array_merge($toAdd, array($attribute)) : array($toAdd, $attribute); } return $res; } public function postProcess() { global $currentIndex; $this->product = new Product((int)(Tools::getValue('id_product'))); if (isset($_POST['generate'])) { if (!is_array(Tools::getValue('options'))) $this->_errors[] = Tools::displayError('Please choose at least 1 attribute.'); else { $tab = array_values($_POST['options']); if (sizeof($tab) AND Validate::isLoadedObject($this->product)) { self::setAttributesImpacts($this->product->id, $tab); $this->combinations = array_values(self::createCombinations($tab)); $values = array_values(array_map(array($this, 'addAttribute'), $this->combinations)); $this->product->deleteProductAttributes(); $res = $this->product->addProductAttributeMultiple($values); $this->product->addAttributeCombinationMultiple($res, $this->combinations); $this->product->updateQuantityProductWithAttributeQuantity(); } else $this->_errors[] = Tools::displayError('Unable to initialize parameters, combination is missing or object cannot be loaded.'); } } elseif (isset($_POST['back'])) Tools::redirectAdmin($currentIndex.'&id_product='.(int)(Tools::getValue('id_product')).'&id_category='.(int)(Tools::getValue('id_category')).'&addproduct'.'&tabs=3&token='.Tools::getValue('token')); parent::postProcess(); } static private function displayAndReturnAttributeJs() { global $cookie; $attributes = Attribute::getAttributes((int)($cookie->id_lang), true); $attributeJs = array(); foreach ($attributes AS $k => $attribute) $attributeJs[$attribute['id_attribute_group']][$attribute['id_attribute']] = $attribute['name']; echo ' '; return $attributeJs; } private function displayGroupSelect($attributeJs, $attributesGroups) { echo '