Ejemplo n.º 1
0
    /**
     * 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;
    }
Ejemplo n.º 2
0
 /**
  * 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;
 }
Ejemplo n.º 3
0
 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);
 }
Ejemplo n.º 4
0
    /**
     * migrate old local categories to the categories module
     */
    private function _migratecategories()
    {
        // load the admin language file
        // pull all data from the old table
        $prefix = System::getVar('prefix');
        $sql    = "SELECT pn_secname, pn_image, pn_secid FROM {$prefix}_sections";
        $result = DBUtil::executeSQL($sql);
        $categories = array();
        for (; !$result->EOF; $result->MoveNext()) {
            $categories[] = $result->fields;
        }

        // get the language file
        $lang = ZLanguage::getLanguageCode();

        // create root category and entry in the categories registry
        $this->_createdefaultcategory('/__SYSTEM__/Modules/Pages');

        // get the category path for which we're going to insert our upgraded categories
        $rootcat = CategoryUtil::getCategoryByPath('/__SYSTEM__/Modules/Pages');

        // migrate our categories
        $categorymap = array();
        foreach ($categories as $category) {
            $cat = new Categories_DBObject_Category ();
            $cat->setDataField('parent_id', $rootcat['id']);
            $cat->setDataField('name', $category[0]);
            $cat->setDataField('display_name', array($lang => $category[0]));
            $cat->setDataField('display_desc', array($lang => $category[0]));
            $cat->setDataField('data1', $category[1]);
            if (!$cat->validate('admin')) {
                return false;
            }
            $cat->insert();
            $cat->update();
            $categorymap[$category[2]] = $cat->getDataField('id');
        }

        // migrate page category assignments
        $sql = "SELECT pn_pageid, pn_secid FROM {$prefix}_pages";
        $result = DBUtil::executeSQL($sql);
        $pages = array();
        for (; !$result->EOF; $result->MoveNext()) {
            $pages[] = array('pageid' => $result->fields[0],
                    '__CATEGORIES__' => array('Main' => $categorymap[$result->fields[1]]),
                    '__META__' => array('module' => 'Pages'));
        }

        foreach ($pages as $page) {
            if (!DBUtil::updateObject($page, 'pages', '', 'pageid')) {
                return LogUtil::registerError($this->__('Error! Update attempt failed.'));
            }
        }

        // drop old table
        DBUtil::dropTable('sections');

        // finally drop the secid column
        DBUtil::dropColumn('pages', 'pn_secid');

        return true;
    }
Ejemplo n.º 5
0
 function _addressbook_migrateprefixes()
 {
     $dom = ZLanguage::getModuleDomain('AddressBook');
     $dbprefix = System::getVar('prefix');
     $dbprefix = $dbprefix ? $dbprefix . '_' : '';
     // pull old prefix values
     $sql = "SELECT pre_id, pre_name FROM {$dbprefix}addressbook_prefixes";
     $result = DBUtil::executeSQL($sql);
     $prefixes = array();
     for (; !$result->EOF; $result->MoveNext()) {
         $prefixes[] = $result->fields;
     }
     // 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__/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();
     }
     // get the category path for which we're going to insert our upgraded News categories
     $foaCat = CategoryUtil::getCategoryByPath('/__SYSTEM__/General/Form of address');
     // migrate our main categories
     foreach ($prefixes as $prefix) {
         $cat = new Categories_DBObject_Category();
         $cat->setDataField('parent_id', $foaCat['id']);
         $cat->setDataField('name', $prefix[1]);
         $cat->setDataField('is_leaf', 1);
         $cat->setDataField('display_name', array($lang => $prefix[1]));
         $cat->setDataField('display_desc', array($lang => $prefix[1]));
         if (!$cat->validate('admin')) {
             return false;
         }
         $cat->insert();
         $cat->update();
         $catid = $cat->getDataField('id');
         $sql = "UPDATE {$dbprefix}addressbook_address SET adr_prefix = {$catid} WHERE adr_prefix = {$prefix['0']}";
         if (!DBUtil::executeSQL($sql)) {
             return LogUtil::registerError($this->__('Error! Update attempt failed.'));
         }
     }
     // now drop the prefixes table
     $sql = "DROP TABLE " . $dbprefix . "addressbook_prefixes";
     DBUtil::executeSQL($sql);
     return true;
 }
Ejemplo n.º 6
0
    /**
     * 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;
    }