New in tutorials 1.2.0 is an API for creating and editing articles, which functions similar to the way one could write code in IP.Board that would let you post topics and replies from outside of IP.Board. To take advantage of this API, here is some sample code you can use in your applications.
To create a new tutorial:
/* Grab the post class */
$classToLoad = IPSLib::loadLibrary( IPSLib::getAppDir( 'tutorials' ) . '/sources/post.php', 'post', 'tutorials' );
$postClass = new $classToLoad( ipsRegistry::instance() );
/* Set our variables */
$postClass->setCategoryID( $categoryID );
$postClass->setTutorialTitle( $tutorialTitle );
$postClass->setTutorialDescription( $tutorialDescription );
$postClass->setIsPreview( FALSE );
$postClass->setSettings( array( 'htmlstatus' => 0, 'is_pinned' => 0 ) );
$postClass->setAuthor( $authorID );
$postClass->setPublished( TRUE );
$postClass->setPostContent( $unParsedTutorialContent );
/* Try and post it */
try
{
if ( $postClass->addTutorial( 'new' ) === FALSE )
{
if ( count( $postClass->_postErrors ) )
{
print 'Posting errors: ' . implode( '<br />', $postClass->_postErrors );
}
exit;
}
}
catch( Exception $error )
{
print "Posting failed: " . $error->getMessage();
exit;
}
/* Fetch the resulting tutorial data, if you need to do anything with it */
$tutorial = $postClass->getTutorialData();/* Grab the post class */
$classToLoad = IPSLib::loadLibrary( IPSLib::getAppDir( 'tutorials' ) . '/sources/post.php', 'post', 'tutorials' );
$postClass = new $classToLoad( ipsRegistry::instance() );
/* Grab the info from the existing tutorial */
$tutorial = ipsRegistry::DB()->buildAndFetch( array( 'select' => '*', 'from' => 'tutorials_articles', 'where' => 'a_id=' . $tutorialID ) );
/* Set our variables */
$postClass->setTutorialID( $tutorialID );
$postClass->setCategoryID( $tutorial['a_cat'] );
$postClass->setTutorialTitle( $tutorial['a_name'] );
$postClass->setTutorialDescription( $tutorial['a_desc'] );
$postClass->setIsPreview( FALSE );
$postClass->setSettings( array( 'htmlstatus' => $tutorial['a_htmlstate'], 'is_pinned' => $tutorial['a_pinned'] ) );
$postClass->setAuthor( $tutorial['a_mid'] );
$postClass->setPublished( TRUE );
$postClass->setPostContent( $tutorial['a_content'] );
/* Try and post it */
try
{
if ( $postClass->editTutorial() === FALSE )
{
if ( $postClass->_postErrors )
{
print 'Posting errors: ' . $postClass->_postErrors;
}
exit;
}
}
catch( Exception $error )
{
print "Posting failed: " . $error->getMessage();
exit;
}
/* Fetch the resulting tutorial data, if you need to do anything with it */
$tutorial = $postClass->getTutorialData();