Exemplo n.º 1
0
 /**
  * Object creation
  */
 public function processAdd()
 {
     if (!Tools::getValue('categoryBox') || !in_array(Tools::getValue('id_category'), Tools::getValue('categoryBox'))) {
         $this->errors[] = $this->l('You need to select at least the root category.');
     }
     if (Tools::isSubmit('id_category_default')) {
         $_POST['id_category'] = (int) Tools::getValue('id_category_default');
     }
     /* Checking fields validity */
     $this->validateRules();
     if (!count($this->errors)) {
         $object = new $this->className();
         $this->copyFromPost($object, $this->table);
         $this->beforeAdd($object);
         if (!$object->add()) {
             $this->errors[] = Tools::displayError('An error occurred while creating an object.') . ' <b>' . $this->table . ' (' . Db::getInstance()->getMsgError() . ')</b>';
         } else {
             if (($_POST[$this->identifier] = $object->id) && $this->postImage($object->id) && !count($this->errors) && $this->_redirect) {
                 $parent_id = (int) Tools::getValue('id_parent', 1);
                 $this->afterAdd($object);
                 $this->updateAssoShop($object->id);
                 // Save and stay on same form
                 if (Tools::isSubmit('submitAdd' . $this->table . 'AndStay')) {
                     $this->redirect_after = self::$currentIndex . '&' . $this->identifier . '=' . $object->id . '&conf=3&update' . $this->table . '&token=' . $this->token;
                 }
                 // Save and back to parent
                 if (Tools::isSubmit('submitAdd' . $this->table . 'AndBackToParent')) {
                     $this->redirect_after = self::$currentIndex . '&' . $this->identifier . '=' . $parent_id . '&conf=3&token=' . $this->token;
                 }
                 // Default behavior (save and back)
                 if (empty($this->redirect_after)) {
                     $this->redirect_after = self::$currentIndex . ($parent_id ? '&' . $this->identifier . '=' . $object->id : '') . '&conf=3&token=' . $this->token;
                 }
             }
         }
     }
     $this->errors = array_unique($this->errors);
     if (count($this->errors) > 0) {
         $this->display = 'add';
         return;
     }
     // specific import for stock
     if (isset($import_data['stock_available']) && isset($import_data['product']) && Tools::isSubmit('useImportData')) {
         $id_src_shop = (int) Tools::getValue('importFromShop');
         if ($object->getGroup()->share_stock == false) {
             StockAvailable::copyStockAvailableFromShopToShop($id_src_shop, $object->id);
         }
     }
     $categories = Tools::getValue('categoryBox');
     array_unshift($categories, Configuration::get('PS_ROOT_CATEGORY'));
     Category::updateFromShop($categories, $object->id);
     Search::indexation(true);
     return $object;
 }