/** * update category */ public function edit() { $this->checkCsrfToken(); if (!SecurityUtil::checkPermission('Categories::', '::', ACCESS_EDIT)) { return LogUtil::registerPermissionError(); } $dr = (int)FormUtil::getPassedValue('dr', 0, 'POST'); $ref = System::serverGetVar('HTTP_REFERER'); $returnfunc = strpos($ref, "useredit") !== false ? 'useredit' : 'edit'; $url = ModUtil::url('Categories', 'user', $returnfunc, array('dr' => $dr)); if (!$dr) { return LogUtil::registerError($this->__('Error! The document root is invalid.'), null, $url); } $obj = new Categories_DBObject_Category (); $data = $obj->getDataFromInput(); $oldData = $obj->get($data['id']); $obj->setData($data); if (!$oldData) { $msg = $this->__f('Error! Cannot retrieve category with ID %s.', $data['id']); return LogUtil::registerError($msg, null, $url); } if ($oldData['is_locked']) { //! %1$s is the id, %2$s is the name return LogUtil::registerError($this->__f('Notice: The administrator has locked the category \'%2$s\' (ID \'%$1s\'). You cannot edit or delete it.', array($data['id'], $oldData['name'])), null, $url); } if (!$obj->validate()) { $_POST['cid'] = (int)$_POST['category']['id']; $this->redirect(ModUtil::url('Categories', 'user', 'edit', $_POST) . '#top'); } $attributes = array(); $values = FormUtil::getPassedValue('attribute_value', 'POST'); foreach (FormUtil::getPassedValue('attribute_name', 'POST') as $index => $name) { if (!empty($name)) $attributes[$name] = $values[$index]; } $obj->setDataField('__ATTRIBUTES__', $attributes); // update new category data $obj->update(); // since a name change will change the object path, we must rebuild it here if ($oldData['name'] != $data['name']) { CategoryUtil::rebuildPaths('path', 'name', $data['id']); } $msg = $this->__f('Done! Saved the %s category.', $oldData['name']); LogUtil::registerStatus($msg); $this->redirect($url); }
/** * Return a category object by ID * * @param string $rootPath The path of the parent category. * @param string $name The name of the category. * @param string $value The value of the category (optional) (default=null). * @param string $displayname The displayname of the category (optional) (default=null, uses $name). * @param string $description The description of the category (optional) (default=null, uses $name). * @param string $attributes The attributes array to bind to the category (optional) (default=null). * * @return The resulting folder object */ public static function createCategory($rootPath, $name, $value=null, $displayname=null, $description=null, $attributes=null) { if (!isset($rootPath) || !$rootPath) { return LogUtil::registerError(__f("Error! Received invalid parameter '%s'", 'rootPath')); } if (!isset($name) || !$name) { return LogUtil::registerError(__f("Error! Received invalid parameter '%s'", 'name')); } if (!$displayname) { $displayname = $name; } if (!$description) { $description = $name; } $lang = ZLanguage::getLanguageCode(); $rootCat = self::getCategoryByPath($rootPath); if (!$rootCat) { return LogUtil::registerError(__f("Error! Non-existing root category '%s' received", $rootPath)); } $checkCat = self::getCategoryByPath("$rootPath/$name"); if (!$checkCat) { $cat = new Categories_DBObject_Category(); $data = array(); $data['parent_id'] = $rootCat['id']; $data['name'] = $name; $data['display_name'] = array($lang => $displayname); $data['display_desc'] = array($lang => $description); if ($value) { $data['value'] = $value; } if ($attributes && is_array($attributes)) { $data['__ATTRIBUTES__'] = $attributes; } $cat->setData($data); if (!$cat->validate('admin')) { return false; } $cat->insert(); $cat->update(); return $cat->getDataField('id'); } return false; }
/** * edit categories for the currently logged in user */ public function edituser() { if (!SecurityUtil::checkPermission('Categories::category', '::', ACCESS_EDIT)) { return LogUtil::registerPermissionError(); } if (!UserUtil::isLoggedIn()) { return LogUtil::registerError($this->__('Error! Editing mode for user-owned categories is only available to users who have logged-in.')); } $allowUserEdit = $this->getVar('allowusercatedit', 0); if (!$allowUserEdit) { return LogUtil::registerError($this->__('Error! User-owned category editing has not been enabled. This feature can be enabled by the site administrator.')); } $userRoot = $this->getVar('userrootcat', 0); if (!$userRoot) { return LogUtil::registerError($this->__('Error! Could not determine the user root node.')); } $userRootCat = CategoryUtil::getCategoryByPath($userRoot); if (!$userRoot) { return LogUtil::registerError($this->__f('Error! The user root node seems to point towards an invalid category: %s.', $userRoot)); } if ($userRootCat == 1) { return LogUtil::registerError($this->__("Error! The root directory cannot be modified in 'user' mode")); } $userCatName = $this->getusercategoryname(); if (!$userCatName) { return LogUtil::registerError($this->__('Error! Cannot determine user category root node name.')); } $thisUserRootCatPath = $userRoot . '/' . $userCatName; $thisUserRootCat = CategoryUtil::getCategoryByPath($thisUserRootCatPath); $dr = null; if (!$thisUserRootCat) { $autoCreate = $this->getVar('autocreateusercat', 0); if (!$autoCreate) { return LogUtil::registerError($this->__("Error! The user root category node for this user does not exist, and the automatic creation flag (autocreate) has not been set.")); } $installer = new Categories_Installer(ServiceUtil::getManager()); $cat = array('id' => '', 'parent_id' => $userRootCat['id'], 'name' => $userCatName, 'display_name' => unserialize($installer->makeDisplayName($userCatName)), 'display_desc' => unserialize($installer->makeDisplayDesc()), 'security_domain' => 'Categories::', 'path' => $thisUserRootCatPath, 'status' => 'A'); $obj = new Categories_DBObject_Category(); $obj->setData($cat); $obj->insert(); // since the original insert can't construct the ipath (since // the insert id is not known yet) we update the object here $obj->update(); $dr = $obj->getID(); $autoCreateDefaultUserCat = $this->getVar('autocreateuserdefaultcat', 0); if ($autoCreateDefaultUserCat) { $userdefaultcatname = $this->getVar('userdefaultcatname', $this->__('Default')); $cat = array('id' => '', 'parent_id' => $dr, 'name' => $userdefaultcatname, 'display_name' => unserialize($installer->makeDisplayName($userdefaultcatname)), 'display_desc' => unserialize($installer->makeDisplayDesc()), 'security_domain' => 'Categories::', 'path' => $thisUserRootCatPath . '/' . $userdefaultcatname, 'status' => 'A'); $obj->setData($cat); $obj->insert(); // since the original insert can't construct the ipath (since // the insert id is not known yet) we update the object here $obj->update(); } } else { $dr = $thisUserRootCat['id']; } $url = ModUtil::url('Categories', 'user', 'edit', array('dr' => $dr)); $this->redirect($url); }
/** * migrate old local categories to the categories module */ private function _news_migratecategories() { // load the admin language file // pull all data from the old tables $tables = DBUtil::getTables(); $columns = $tables['news_column']; $sql = "SELECT $columns[catid], $columns[title] FROM {$tables[stories_cat]}"; $result = DBUtil::executeSQL($sql); $categories = array(array(0, 'Articles')); for (; !$result->EOF; $result->MoveNext()) { $categories[] = $result->fields; } $sql = "SELECT $columns[topicid], $columns[topicname], $columns[topicimage], $columns[topictext] FROM {$tables[topics]}"; $result = DBUtil::executeSQL($sql); $topics = array(); for (; !$result->EOF; $result->MoveNext()) { $topics[] = $result->fields; } // get the language file $lang = ZLanguage::getLanguageCode(); // create the Main category and entry in the categories registry $this->_createdefaultcategory('/__SYSTEM__/Modules/News'); // create the Topics category and entry in the categories registry $this->_createtopicscategory('/__SYSTEM__/Modules/Topics'); // get the category path for which we're going to insert our upgraded News categories $rootcat = CategoryUtil::getCategoryByPath('/__SYSTEM__/Modules/News'); // migrate our main categories $categorymap = array(); foreach ($categories as $category) { $cat = new Categories_DBObject_Category(); $cat->setDataField('parent_id', $rootcat['id']); $cat->setDataField('name', $category[1]); $cat->setDataField('display_name', array($lang => $category[1])); $cat->setDataField('display_desc', array($lang => $category[1])); if (!$cat->validate('admin')) { return false; } $cat->insert(); $cat->update(); $categorymap[$category[0]] = $cat->getDataField('id'); } // get the category path for which we're going to insert our upgraded Topics categories $rootcat = CategoryUtil::getCategoryByPath('/__SYSTEM__/Modules/Topics'); // migrate our topic categories $topicsmap = array(); foreach ($topics as $topic) { $cat = new Categories_DBObject_Category(); $data = $cat->getData(); $data['parent_id'] = $rootcat['id']; $data['name'] = $topic[1]; $data['value'] = -1; $data['display_name'] = array($lang => $topic[3]); $data['display_desc'] = array($lang => $topic[3]); $data['__ATTRIBUTES__']['topic_image'] = $topic[2]; $cat->setData ($data); if (!$cat->validate('admin')) { return false; } $cat->insert(); $cat->update(); $topicsmap[$topic[0]] = $cat->getDataField('id'); } // After an upgrade we want the legacy topic template variables to point to the Topic property $this->setVar('topicproperty', 'Topic'); // migrate page category assignments $sql = "SELECT $columns[sid], $columns[catid], $columns[topic] FROM {$tables[stories]}"; $result = DBUtil::executeSQL($sql); $pages = array(); for (; !$result->EOF; $result->MoveNext()) { $pages[] = array('sid' => $result->fields[0], '__CATEGORIES__' => array( 'Main' => $categorymap[$result->fields[1]], 'Topic' => $topicsmap[$result->fields[2]]), '__META__' => array('module' => 'News')); } foreach ($pages as $page) { if (!DBUtil::updateObject($page, 'stories', '', 'sid')) { return LogUtil::registerError($this->__('Error! Could not update the article categories.')); } } // drop old table DBUtil::dropTable('stories_cat'); // we don't drop the topics table - this is the job of the topics module // finally drop the secid column DBUtil::dropColumn('stories', $columns['catid']); DBUtil::dropColumn('stories', $columns['topic']); return true; }