/** * Copy Categories by Path (recurisve copy). * * @param string $apath The path to copy from. * @param intiger $newparent_id The categoryID of the new parent category. * @param string $field The field to use for the path reference (optional) (default='ipath'). * @param boolean $includeRoot Whether or not to also move the root folder (optional) (default=true). * * @return true or false. */ public static function copyCategoriesByPath($apath, $newparent_id, $field = 'ipath', $includeRoot = true) { if (!$apath || !$newparent_id) { return false; } $cats = self::getSubCategoriesByPath($apath, 'ipath', $field, true, true); $newParentCats = self::getSubCategories($newparent_id, true, true, true, true, true); $newParent = $newParentCats[0]; if (!$newParent || !$cats) { return false; } $currentPaths = array(); foreach ($newParentCats as $p) { $currentPaths[] = $p['path_relative']; } // need to make sure that after copying categories will have unique paths foreach ($cats as $k => $cat) { if ($includeRoot) { // root node is included - just check path uniqueness for root // subnodes will inherit it's name in paths $catBasePath = $newParent['path_relative'] . '/'; if ($k === 0 && in_array($catBasePath . $cats[0]['name'], $currentPaths)) { // path is not unique - add arbitrary " Copy" sufix to category name $cats[0]['name'] .= ' ' . __('Copy'); if (in_array($catBasePath . $cats[0]['name'], $currentPaths)) { // if there is already such name // find first free name by adding number at the end $i = 1; $name = $cats[0]['name']; while (in_array($catBasePath . $name, $currentPaths)) { $name = $cats[0]['name'] . ' ' . $i++; } $cats[0]['name'] = $name; } } } elseif ($k !== 0) { // root node is excluded - need to check each subnode if it's path will be unique // follow the same routin that for the root node $catPath = explode('/', $cat['path_relative']); array_shift($catPath); array_pop($catPath); $catBasePath = $newParent['path_relative'] . '/' . implode('/', $catPath); if (in_array($catBasePath . $cats[$k]['name'], $currentPaths)) { $cats[$k]['name'] .= ' ' . __('Copy'); if (in_array($catBasePath . $cats[$k]['name'], $currentPaths)) { $i = 1; $name = $cats[$k]['name']; while (in_array($catBasePath . $name, $currentPaths)) { $name = $cats[$k]['name'] . ' ' . $i++; } $cats[$k]['name'] = $name; } } } } $oldToNewID = array(); $oldToNewID[$cats[0]['parent_id']] = $newParent['id']; // since array_shift() resets numeric array indexes, we remove the leading element like this if (!$includeRoot) { foreach ($cats as $k => $v) { if (isset($v['ipath']) && $v['ipath'] == $apath) { unset($cats[$k]); } } } $ak = array_keys($cats); foreach ($ak as $v) { $cat = $cats[$v]; $oldID = $cat['id']; $cat['id'] = ''; $cat['parent_id'] = isset($oldToNewID[$cat['parent_id']]) ? $oldToNewID[$cat['parent_id']] : $newParent['id']; $cat['sort_value'] = null; $catObj = new Categories_DBObject_Category($cat); $catObj->insert(); $oldToNewID[$oldID] = $catObj->_objData['id']; } // rebuild iPath since now we have all new PathIDs self::rebuildPaths('ipath', 'id'); // rebuild also Pahts since names could be changed self::rebuildPaths(); return true; }
private function _createdefaultcategory($regpath = '/__SYSTEM__/Modules/Global') { // get the language $lang = ZLanguage::getLanguageCode(); // get the category path for which we're going to insert our place holder category $rootcat = CategoryUtil::getCategoryByPath('/__SYSTEM__/Modules'); $qCat = CategoryUtil::getCategoryByPath('/__SYSTEM__/Modules/Ephemerides'); if (!$qCat) { // create placeholder for all our migrated categories $cat = new Categories_DBObject_Category(); $cat->setDataField('parent_id', $rootcat['id']); $cat->setDataField('name', 'Ephemerides'); $cat->setDataField('display_name', array($lang => $this->__('Ephemerides'))); $cat->setDataField('display_desc', array($lang => $this->__('Ephemerides'))); if (!$cat->validate('admin')) { return false; } $cat->insert(); $cat->update(); } // get the category path for which we're going to insert our upgraded categories $rootcat = CategoryUtil::getCategoryByPath($regpath); if ($rootcat) { // create an entry in the categories registry $registry = new Categories_DBObject_Registry(); $registry->setDataField('modname', 'Ephemerides'); $registry->setDataField('table', 'ephem'); $registry->setDataField('property', 'Main'); $registry->setDataField('category_id', $rootcat['id']); $registry->insert(); } else { return false; } return true; }
/** * 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); }
/** * resequence categories */ public function resequence() { if (!SecurityUtil::checkPermission('Categories::', '::', ACCESS_EDIT)) { return LogUtil::registerPermissionError(); } $dr = (int)FormUtil::getPassedValue('dr', 0, 'GET'); $url = System::serverGetVar('HTTP_REFERER'); if (!$dr) { return LogUtil::registerError($this->__('Error! The document root is invalid.'), null, $url); } $cats = CategoryUtil::getSubCategories($dr, false, false, false, false); $cats = CategoryUtil::resequence($cats, 10); $ak = array_keys($cats); foreach ($ak as $k) { $obj = new Categories_DBObject_Category($cats[$k]); $obj->update(); } $this->redirect(System::serverGetVar('HTTP_REFERER')); }
public function save() { $this->checkAjaxToken(); $mode = $this->request->getPost()->get('mode', 'new'); $accessLevel = $mode == 'edit' ? ACCESS_EDIT : ACCESS_ADD; $this->throwForbiddenUnless(SecurityUtil::checkPermission('Categories::', '::', $accessLevel)); $result = array(); $cat = new Categories_DBObject_Category(); $cat->getDataFromInput(); if (!$cat->validate()) { $args = array('cid' => $cat->getDataField('id'), 'parent' => $cat->getDataField('parent_id'), 'mode' => $mode); return $this->edit($args); } $attributes = array(); $values = $this->request->getPost()->get('attribute_value'); foreach ($this->request->getPost()->get('attribute_name') as $index => $name) { if (!empty($name)) { $attributes[$name] = $values[$index]; } } $cat->setDataField('__ATTRIBUTES__', $attributes); if ($mode == 'edit') { // retrieve old category from DB $category = $this->request->getPost()->get('category'); $oldCat = new Categories_DBObject_Category(DBObject::GET_FROM_DB, $category['id']); // update new category data $cat->update(); // since a name change will change the object path, we must rebuild it here if ($oldCat->getDataField('name') != $cat->getDataField('name')) { CategoryUtil::rebuildPaths('path', 'name', $cat->getDataField('id')); } } else { $cat->insert(); // update new category data $cat->update(); } $categories = CategoryUtil::getSubCategories($cat->getDataField('id'), true, true, true, true, true); $options = array('nullParent' => $cat->getDataField('parent_id'), 'withWraper' => false); $node = CategoryUtil::getCategoryTreeJS((array) $categories, true, true, $options); $leafStatus = array('leaf' => array(), 'noleaf' => array()); foreach ($categories as $c) { if ($c['is_leaf']) { $leafStatus['leaf'][] = $c['id']; } else { $leafStatus['noleaf'][] = $c['id']; } } $result = array('action' => $mode == 'edit' ? 'edit' : 'add', 'cid' => $cat->getDataField('id'), 'parent' => $cat->getDataField('parent_id'), 'node' => $node, 'leafstatus' => $leafStatus, 'result' => true); return new Zikula_Response_Ajax($result); }
/** * migrate old local categories to the categories module */ private function _migratecategories() { // get the language file $lang = ZLanguage::getLanguageCode(); $catPath = '/__SYSTEM__/Modules/Quotes'; // create root category and entry in the categories registry $this->_createdefaultcategory($catPath); // get the category path for which we're going to insert our upgraded categories $rootcat = CategoryUtil::getCategoryByPath($catPath); // create placeholder for all our migrated quotes $cat = new Categories_DBObject_Category(); $cat->setDataField('parent_id', $rootcat['id']); $cat->setDataField('name', 'Imported'); $cat->setDataField('display_name', array($lang => $this->__('Imported quotes'))); $cat->setDataField('display_desc', array($lang => $this->__('Quotes imported from .7x version'))); if (!$cat->validate('admin')) { return false; } $cat->insert(); $cat->update(); $catid = $cat->getDataField('id'); unset($cat); // migrate page category assignments $prefix = System::getVar('prefix'); $sql = "SELECT pn_qid FROM {$prefix}_quotes"; $result = DBUtil::executeSQL($sql); $quotes = array(); for (; !$result->EOF; $result->MoveNext()) { $quotes[] = array('qid' => $result->fields[0], '__CATEGORIES__' => array('Main' => $catid), '__META__' => array('module' => 'Quotes')); } foreach ($quotes as $quote) { if (!DBUtil::updateObject($quote, 'quotes', '', 'qid')) { return LogUtil::registerError($this->__('Error! Update attempt failed.')); } } return true; }
/** * edit category */ public function edit() { $cid = FormUtil::getPassedValue('cid', 0); $root_id = FormUtil::getPassedValue('dr', 1); $mode = FormUtil::getPassedValue('mode', 'new'); $allCats = ''; $editCat = ''; $languages = ZLanguage::getInstalledLanguages(); // indicates that we're editing if ($mode == 'edit') { if (!SecurityUtil::checkPermission('Categories::category', "::", ACCESS_ADMIN)) { return LogUtil::registerPermissionError(); } if (!$cid) { return LogUtil::registerError($this->__('Error! Cannot determine valid \'cid\' for edit mode in \'Categories_admin_edit\'.')); } $category = new Categories_DBObject_Category(); $editCat = $category->select($cid); if ($editCat == false) { return LogUtil::registerError($this->__('Sorry! No such item found.'), 404); } } else { // new category creation if (!SecurityUtil::checkPermission('Categories::category', '::', ACCESS_ADD)) { return LogUtil::registerPermissionError(); } // since we inherit the domain settings from the parent, we get // the inherited (and merged) object from session if (isset($_SESSION['newCategory']) && $_SESSION['newCategory']) { $editCat = $_SESSION['newCategory']; unset($_SESSION['newCategory']); $category = new Categories_DBObject_Category(); // need this for validation info } // if we're back from validation get the object from input elseif (FormUtil::getValidationErrors()) { $category = new Categories_DBObject_Category(DBObject::GET_FROM_VALIDATION_FAILED); // need this for validation info $editCat = $category->get(); } // someone just pressen 'new' -> populate defaults else { $category = new Categories_DBObject_Category(); // need this for validation info $editCat['sort_value'] = '0'; } } $reloadOnCatChange = ($mode != 'edit'); $allCats = CategoryUtil::getSubCategories($root_id, true, true, true, false, true); // now remove the categories which are below $editCat ... // you should not be able to set these as a parent category as it creates a circular hierarchy (see bug #4992) if (isset($editCat['ipath'])) { $cSlashEdit = StringUtil::countInstances($editCat['ipath'], '/'); foreach ($allCats as $k => $v) { $cSlashCat = StringUtil::countInstances($v['ipath'], '/'); if ($cSlashCat >= $cSlashEdit && strpos($v['ipath'], $editCat['ipath']) !== false) { unset($allCats[$k]); } } } $selector = CategoryUtil::getSelector_Categories($allCats, 'id', (isset($editCat['parent_id']) ? $editCat['parent_id'] : 0), 'category[parent_id]', isset($defaultValue) ? $defaultValue : null, null, $reloadOnCatChange); $attributes = isset($editCat['__ATTRIBUTES__']) ? $editCat['__ATTRIBUTES__'] : array(); $this->view->assign('mode', $mode) ->assign('category', $editCat) ->assign('attributes', $attributes) ->assign('languages', $languages) ->assign('categorySelector', $selector) ->assign('validation', $category->_objValidation); if ($mode == 'edit') { $this->view->assign('haveSubcategories', CategoryUtil::haveDirectSubcategories($cid)) ->assign('haveLeafSubcategories', CategoryUtil::haveDirectSubcategories($cid, false, true)); } return $this->view->fetch('categories_admin_edit.tpl'); }
/** * move category */ public function move() { $this->checkCsrfToken(); if (!SecurityUtil::checkPermission('Categories::', '::', ACCESS_EDIT)) { return LogUtil::registerPermissionError(); } if (FormUtil::getPassedValue('category_cancel', null, 'POST')) { return System::redirect(ModUtil::url('Categories', 'admin', 'view')); } $cid = FormUtil::getPassedValue('cid', null, 'POST'); $cat = new Categories_DBObject_Category(); $cat->get($cid); $cat->move($_POST['category']['parent_id']); $msg = __f('Done! Moved the %s category.', $cat->_objData['name']); LogUtil::registerStatus($msg); $this->redirect(ModUtil::url('Categories', 'admin', 'view')); }
/** * create placeholder for categories */ private function _feeds_createdefaultcategory($regpath = '/__SYSTEM__/Modules/Global') { // load necessary classes Loader::loadClass('CategoryUtil'); Loader::loadClassFromModule('Categories', 'Category'); Loader::loadClassFromModule('Categories', 'CategoryRegistry'); // get the language code $lang = ZLanguage::getLanguageCode(); $dom = ZLanguage::getModuleDomain('Feeds'); // get the category path for which we're going to insert our place holder category $rootcat = CategoryUtil::getCategoryByPath('/__SYSTEM__/Modules'); $fCat = CategoryUtil::getCategoryByPath('/__SYSTEM__/Modules/Feeds'); if (!$fCat) { // create placeholder for all the module categories $cat = new Categories_DBObject_Category(); $cat->setDataField('parent_id', $rootcat['id']); $cat->setDataField('name', 'Feeds'); $cat->setDataField('display_name', array($lang => __('Feeds', $dom))); $cat->setDataField('display_desc', array($lang => __('Feed Reader.', $dom))); if (!$cat->validate('admin')) { return false; } $cat->insert(); $cat->update(); } // get the category path for which the feeds will be classified $rootcat = CategoryUtil::getCategoryByPath($regpath); if ($rootcat) { // create an entry in the categories registry $registry = new Categories_DBObject_Registry(); $registry->setDataField('modname', 'Feeds'); $registry->setDataField('table', 'feeds'); $registry->setDataField('property', 'Main'); $registry->setDataField('category_id', $rootcat['id']); $registry->insert(); } else { return false; } return true; }
function _addressbook_createdefaultcategory() { $dom = ZLanguage::getModuleDomain('AddressBook'); // get the language file $lang = ZLanguage::getLanguageCode(); // get the category path for which we're going to insert our place holder category $rootcat = CategoryUtil::getCategoryByPath('/__SYSTEM__/Modules'); $adrCat = CategoryUtil::getCategoryByPath('/__SYSTEM__/Modules/AddressBook'); if (!$adrCat) { $cat = new Categories_DBObject_Category(); $cat->setDataField('parent_id', $rootcat['id']); $cat->setDataField('name', 'AddressBook'); $cat->setDataField('display_name', array($lang => $this->__('AddressBook'))); $cat->setDataField('display_desc', array($lang => $this->__('Adress administration.'))); if (!$cat->validate('admin')) { return false; } $cat->insert(); $cat->update(); } // create the first 2 categories $adrCat = CategoryUtil::getCategoryByPath('/__SYSTEM__/Modules/AddressBook'); $adrCat1 = CategoryUtil::getCategoryByPath('/__SYSTEM__/Modules/AddressBook/Business'); if (!$adrCat1) { $cat = new Categories_DBObject_Category(); $cat->setDataField('parent_id', $adrCat['id']); $cat->setDataField('name', 'Business'); $cat->setDataField('is_leaf', 1); $cat->setDataField('display_name', array($lang => $this->__('Business'))); $cat->setDataField('display_desc', array($lang => $this->__('Business'))); if (!$cat->validate('admin')) { return false; } $cat->insert(); $cat->update(); } $adrCat2 = CategoryUtil::getCategoryByPath('/__SYSTEM__/Modules/AddressBook/Personal'); if (!$adrCat2) { $cat = new Categories_DBObject_Category(); $cat->setDataField('parent_id', $adrCat['id']); $cat->setDataField('name', 'Personal'); $cat->setDataField('is_leaf', 1); $cat->setDataField('display_name', array($lang => $this->__('Personal'))); $cat->setDataField('display_desc', array($lang => $this->__('Personal'))); if (!$cat->validate('admin')) { return false; } $cat->insert(); $cat->update(); } if ($adrCat) { // place category registry entry for products (key == Products) $registry = new Categories_DBObject_Registry(); $registry->setDataField('modname', 'AddressBook'); $registry->setDataField('table', 'addressbook_address'); $registry->setDataField('property', 'AddressBook'); $registry->setDataField('category_id', $adrCat['id']); $registry->insert(); } // now the old prefix field // get the category path for which we're going to insert our place holder form of address $rootcat = CategoryUtil::getCategoryByPath('/__SYSTEM__/General'); $foaCat = CategoryUtil::getCategoryByPath('/__SYSTEM__/General/Form of address'); if (!$foaCat) { $cat = new Categories_DBObject_Category(); $cat->setDataField('parent_id', $rootcat['id']); $cat->setDataField('name', 'Form of address'); $cat->setDataField('display_name', array($lang => $this->__('Form of address'))); $cat->setDataField('display_desc', array($lang => $this->__('Form of address'))); if (!$cat->validate('admin')) { return false; } $cat->insert(); $cat->update(); } // create the first 2 categories $foaCat = CategoryUtil::getCategoryByPath('/__SYSTEM__/General/Form of address'); $foaCat1 = CategoryUtil::getCategoryByPath('/__SYSTEM__/General/Form of address/Mr'); if (!$foaCat1) { $cat = new Categories_DBObject_Category(); $cat->setDataField('parent_id', $foaCat['id']); $cat->setDataField('name', 'Mr'); $cat->setDataField('is_leaf', 1); $cat->setDataField('display_name', array($lang => $this->__('Mr.'))); $cat->setDataField('display_desc', array($lang => $this->__('Mr.'))); if (!$cat->validate('admin')) { return false; } $cat->insert(); $cat->update(); } $foaCat2 = CategoryUtil::getCategoryByPath('/__SYSTEM__/General/Form of address/Mrs'); if (!$foaCat2) { $cat = new Categories_DBObject_Category(); $cat->setDataField('parent_id', $foaCat['id']); $cat->setDataField('name', 'Mrs'); $cat->setDataField('is_leaf', 1); $cat->setDataField('display_name', array($lang => $this->__('Mrs.'))); $cat->setDataField('display_desc', array($lang => $this->__('Mrs.'))); if (!$cat->validate('admin')) { return false; } $cat->insert(); $cat->update(); } return true; }
/** * create the Topics category tree */ private function _createtopicscategory($regpath = '/__SYSTEM__/Modules/Topics') { // get the language file $lang = ZLanguage::getLanguageCode(); // get the category path for which we're going to insert our place holder category $rootcat = CategoryUtil::getCategoryByPath('/__SYSTEM__/Modules'); // create placeholder for all the migrated topics $tCat = CategoryUtil::getCategoryByPath('/__SYSTEM__/Modules/Topics'); if (!$tCat) { // create placeholder for all our migrated categories $cat = new Categories_DBObject_Category(); $cat->setDataField('parent_id', $rootcat['id']); $cat->setDataField('name', 'Topics'); // pnModLangLoad doesn't handle type 1 modules //pnModLangLoad('Topics', 'version'); $cat->setDataField('display_name', array($lang => $this->__('Topics'))); $cat->setDataField('display_desc', array($lang => $this->__('Display and manage topics'))); if (!$cat->validate('admin')) { return false; } $cat->insert(); $cat->update(); } // get the category path for which we're going to insert our upgraded News categories $rootcat = CategoryUtil::getCategoryByPath($regpath); if ($rootcat) { // create an entry in the categories registry to the Topic property $registry = new Categories_DBObject_Registry(); $registry->setDataField('modname', 'News'); $registry->setDataField('table', 'stories'); $registry->setDataField('property', 'Topic'); $registry->setDataField('category_id', $rootcat['id']); $registry->insert(); } else { return false; } return true; }