/** * Method to add category related items. * * @param Item $item The item * @param array $categories The category ids * * @return boolean true on success * * @since 2.0 */ public function saveCategoryItemRelations($item, $categories) { // check ACL if (!$item->canEdit()) { return false; } //init vars $db = $this->app->database; if (!is_array($categories)) { $categories = array($categories); } // trigger an event to let 3rd party extend the category list $this->app->event->dispatcher->notify($this->app->event->create($item, 'item:beforeSaveCategoryRelations', array('categories' => &$categories))); $categories = array_unique($categories); // delete category to item relations $query = "DELETE FROM " . ZOO_TABLE_CATEGORY_ITEM . " WHERE item_id=" . (int) $item->id; // execute database query $db->query($query); // Generate the sql query for the categories $query_string = '(%s,' . (int) $item->id . ')'; $category_strings = array(); foreach ($categories as $category) { if (is_numeric($category)) { $category_strings[] = sprintf($query_string, $category); } } // add category to item relations // insert relation to database if (!empty($category_strings)) { $query = "INSERT INTO " . ZOO_TABLE_CATEGORY_ITEM . " (category_id, item_id) VALUES " . implode(',', $category_strings); // execute database query $db->query($query); } $this->clearItemCategoryCache(); return true; }