Page 1 of 1

Any API in Joomla 1.0.x to add some new content ?

Posted: Tue Oct 09, 2007 3:05 pm
by operron
I've started to write a small component that upload a kml file to the server and now I would like this component to automatically create a new content based on that file.

Is there any API like:

create_content(section_id, category_id, published, author_id, content_text);

which could do the job?

Or do I need to create such a function myself and write all the necessary code to update the database ?
Any hint on what needs to be done?

Thanks for your help!

Re: Any API in Joomla 1.0.x to add some new content ?

Posted: Tue Oct 09, 2007 8:54 pm
by operron
I had a look at com_content and came with the following function to achieve what I want:

Code: Select all

function addContent($sectionid, $categoryid, $published, $authorid, $title, $content_text)
{
        global $database;
        $nullDate = $database->getNullDate();
        $row = new mosContent( $database );
        // load the row from the db table: Initialization
        $row->load( 0 );
        $row->id                = 0;
        $row->catid             = $categoryid;
        $row->sectionid         = $sectionid;
        $row->version           = 0;
        $row->state             = 0;
        $row->ordering          = 0;
        $row->images            = array();
        $row->publish_up        = date( 'Y-m-d', time() );
        $row->publish_down      = 'Never';
        $row->modified_by          = 0;

        // Start to really fill in the content...
        $row->title         = $title;
        $row->introtext     = $content_text;
        $row->fulltext      = "";
        $row->created       = date( 'Y-m-d H:i:s' );
        $row->created_by    = $authorid;
        $row->state = 1;

        // Various checks and cleanup
        if (strlen(trim( $row->publish_up )) <= 10) {
                $row->publish_up .= ' 00:00:00';
        }
        $row->publish_up = mosFormatDate( $row->publish_up, _CURRENT_SERVER_TIME_FORMAT, -$mosConfig_offset );

        if (trim( $row->publish_down ) == 'Never' || trim( $row->publish_down ) == '') {
                $row->publish_down = $nullDate;
        } else {
                if (strlen(trim( $row->publish_down )) <= 10) {
                        $row->publish_down .= ' 00:00:00';
                }
                $row->publish_down = mosFormatDate( $row->publish_down, _CURRENT_SERVER_TIME_FORMAT, -$mosConfig_offset );
        }


        // code cleaner for xhtml transitional compliance
        $row->introtext = str_replace( '<br>', '<br />', $row->introtext );
        $row->fulltext  = str_replace( '<br>', '<br />', $row->fulltext );

        // remove <br /> take being automatically added to empty fulltext
        $length = strlen( $row->fulltext ) < 9;
        $search = strstr( $row->fulltext, '<br />');
        if ( $length && $search ) {
               $row->fulltext = NULL;
        }

        $row->title = ampReplace( $row->title );
        $row->check();
        $row->version++;
        $row->store();
        $row->checkin();
        $row->updateOrder( "catid = " . (int) $row->catid );

        $msg = "Content created and saved !";
        $link = strval( mosGetParam( $_POST, 'referer', '' ) );
        mosRedirect( $link, $msg );
}