/** * 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; }