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); }
/** * 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); }
/** * rebuild path structure */ public function rebuild_paths() { if (!SecurityUtil::checkPermission('Categories::', '::', ACCESS_ADMIN)) { return LogUtil::registerPermissionError(); } CategoryUtil::rebuildPaths('path', 'name'); CategoryUtil::rebuildPaths('ipath', 'id'); LogUtil::registerStatus(__('Done! Rebuilt the category paths.')); $this->redirect(ModUtil::url('Categories', 'admin', 'view')); }