Example #1
0
 public function update($null_values = false)
 {
     if (Tools::isSubmit('submitAddcategoryAndBackToParent')) {
         $checked_artists_id = array();
         foreach ($_POST as $key => $val) {
             $hasString = strpos($key, 'related_artists');
             if ($hasString !== false) {
                 $checked_artists_id[] = $val;
             }
         }
         $serialize_artists = serialize($checked_artists_id);
         $this->related_artists[1] = $serialize_artists;
     }
     if ($this->id_parent == $this->id) {
         throw new PrestaShopException('a category cannot be its own parent');
     }
     if ($this->is_root_category && $this->id_parent != (int) Configuration::get('PS_ROOT_CATEGORY')) {
         $this->is_root_category = 0;
     }
     // Update group selection
     $this->updateGroup($this->groupBox);
     $this->level_depth = $this->calcLevelDepth();
     // If the parent category was changed, we don't want to have 2 categories with the same position
     $changed = $this->getDuplicatePosition();
     if ($changed) {
         if (Tools::isSubmit('checkBoxShopAsso_category')) {
             foreach (Tools::getValue('checkBoxShopAsso_category') as $id_asso_object => $row) {
                 foreach ($row as $id_shop => $value) {
                     $this->addPosition(Category::getLastPosition((int) $this->id_parent, (int) $id_shop), (int) $id_shop);
                 }
             }
         } else {
             foreach (Shop::getShops(true) as $shop) {
                 $this->addPosition(max(1, Category::getLastPosition((int) $this->id_parent, $shop['id_shop'])), $shop['id_shop']);
             }
         }
     }
     $ret = parent::update($null_values);
     if ($changed && (!isset($this->doNotRegenerateNTree) || !$this->doNotRegenerateNTree)) {
         $this->cleanPositions((int) $this->id_parent);
         Category::regenerateEntireNtree();
         $this->recalculateLevelDepth($this->id);
     }
     Hook::exec('actionCategoryUpdate', array('category' => $this));
     return $ret;
 }
Example #2
0
 /**
  * Add some categories to a shop
  * @param array $categories
  * @return bool
  */
 public static function addToShop(array $categories, $id_shop)
 {
     if (!is_array($categories)) {
         return false;
     }
     $sql = 'INSERT INTO `' . _DB_PREFIX_ . 'category_shop` (`id_category`, `id_shop`) VALUES';
     $tab_categories = array();
     foreach ($categories as $id_category) {
         $tab_categories[] = new Category($id_category);
         $sql .= '("' . (int) $id_category . '", "' . (int) $id_shop . '"),';
     }
     // removing last comma to avoid SQL error
     $sql = substr($sql, 0, strlen($sql) - 1);
     $return = Db::getInstance()->execute($sql);
     // we have to update position for every new entries
     foreach ($tab_categories as $category) {
         /** @var Category $category */
         $category->addPosition(Category::getLastPosition($category->id_parent, $id_shop), $id_shop);
     }
     return $return;
 }