/** * Create a single category and return its ID. If the category alias already * exists, return the ID of that specific category alias. * * @param type $level * @param type $levelpath * @param type $parent_id * @return type */ private function createCategory($level = 1, $levelpath = '1', $parent_id = 1) { $logger = $this->getState('logger'); $title = 'Overload '; $alias = 'overload-'; $title .= $levelpath; $alias .= str_replace('.', '-', $levelpath); $data = array( 'parent_id' => $parent_id, 'level' => $level, 'extension' => 'com_content', 'title' => $title, 'alias' => $alias, 'description' => '<p>Sample content generated by Overload</p>', 'access' => 1, 'params' => array('target' => '', 'image' => ''), 'metadata' => array('page_title' => '', 'author' => '', 'robots' => '', 'tags' => ''), 'hits' => 0, 'language' => '*', 'associations' => array(), 'published' => 1 ); $basePath = JPATH_ADMINISTRATOR . '/components/com_categories'; require_once $basePath . '/models/category.php'; $config = array('table_path' => $basePath . '/tables'); $model = new CategoriesModelCategory($config); $result = $model->save($data); if($result === false) { $db = $this->getDbo(); $query = $db->getQuery(true); $query ->select('id') ->from( $db->qn('#__categories') ) ->where($db->qn('alias').' = '.$db->q($alias)); $db->setQuery($query); $id = $db->loadResult(); JLog::add("Existing category $levelpath, ID $id", JLog::DEBUG); // Enable an existing category $cat = $model->getItem($id); if(!$cat->published) { $cat->published = 1; } $cat = (array)$cat; $model->save($cat); return $id; } else { $id = $model->getState($model->getName().'.id'); JLog::add("New category $levelpath, ID $id", JLog::DEBUG); return $id; } }