2
votes

Does anyone know how to properly insert new content categories to the DB programatically? For each post in the categories table, there is also a post saved in the assets table with lft and rgt set. Is there any native Joomla class I can use for this instead of plain SQL?

4

4 Answers

0
votes

You can perhaps use the save() in category.php file.

File location: root\administrator\components\com_categories\models\category.php

It saves the form data supplied to it!

The JOS_assets table is to store the ACL for each asset that is created.

If you do not update this table while programatically creating the category, the default ACL will apply. And when later you open and save the category in the administrative panel, the ACL will be updated as it should have been by core Joomla!.

You can create an SQL query very easily though and update the asset table as well. Its easy to understand once you open the table's content in phpmyadmin.

2
votes

Please Please Only use the native classes, which categories will handle for you seamlessly. As soon as you add categories the whole thing will be handled automagically. Just look at any core component to see how.

It is not easy to update the assets table using sql, it is all very specifically managed and part of a complex series of foreign keyed tables.

Extend JTable or JTableContent to handle this.

2
votes

Here is some code I just whipped together that just uses the JTableCategory class, so it can be used simply on the front or admin side of Joomla

$table = JTable::getInstance('category');

$data = array();
// name the category
$data['title'] = $title;
// set the parent category for the new category
$data['parent_id'] = $parent_id;
// set what extension the category is for
$data['extension'] = $extension;
// Set the category to be published by default
$data['published'] = 1;

// setLocation uses the parent_id and updates the nesting columns correctly
$table->setLocation($data['parent_id'], 'last-child');
// push our data into the table object
$table->bind($data);
// some data checks including setting the alias based on the name
if ($table->check()) {
    // and store it!
    $table->store();
    // Success
} else {
    // Error
}

Naturally you would want to get the data pieces set correctly, but these are the core ones to set.

1
votes

Here is a function I've created just for this purpose, after some digging & experimenting.

It uses core classes, so it needs an access to them (for me it's basically a part of Joomla component).

Mind, it's for Joomla 3, for Joomla 2.5 and before, you need to change JModelLegacy to JModel.

function createCategory( $name, $parent_id, $note )
{
    JTable::addIncludePath( JPATH_ADMINISTRATOR . '/components/com_categories/tables' );

    $cat_model = JModelLegacy::getInstance( 'Category', 'CategoriesModel' );

    $data = array (
        'id' => 0,
        'parent_id' => $parent_id,
        'extension' => 'com_content',
        'title' => $name,
        'alias' => '',
        'note' => $note,
        'description' => '',
        'published' => '1',
        'access' => '1',
        'metadesc' => '',
        'metakey' => '',
        'created_user_id' => '0',
        'language' => '*',
        'rules' => array(
            'core.create' => array(),
            'core.delete' => array(),
            'core.edit' => array(),
            'core.edit.state' => array(),
            'core.edit.own' => array(),
        ),
        'params' => array(
            'category_layout' => '',
            'image' => '',
        ),
        'metadata' => array(
            'author' => '',
            'robots' => '',
        ),
    );

    if( !$cat_model->save( $data ) )
    {
        return NULL;
    }

    $categories = JCategories::getInstance( 'Content' );
    $subcategory = $categories->get( $cat_model->getState( "category.id" ) );
    return $subcategory;
}