Tuesday, May 08, 2007

Adding some content to the freshly created php page in KnowledgeTree DMS (KTDMS Series 2)

Carrying forward from the same example as described in series 1, you would obviously want to define and output some data out of the php page you created. In this series we will use the dispatcher and templating classes provided by Knowledgetree to output some simple data out of this page.

Pages in knowledge tree are created by extending the KTStandardDispatcher class. For every custom class you create, it should have at least one method "do_main" which is the first of your methods that gets called automatically when a web user tries to access this page. Additional methods can be defined in the format "do_" that can be called conditionally based on the http request variable "action" (HTTP POST/GET vars). For example, you can define a method "do_createnewrecord" and call the php page with a http request variable "action" of value "createnewrecord", and the page will then automatically look for and execute the "do_createnewrecord" method defined in the page class.

Assuming you are already well familiar with the plugins/pages/actions... concepts in ktdms, the following example creates a page plugin class called "MyTrainingRecords", by extending the KTStandardDispatcher class. KTDMS uses the smarty templating system, so I created a test template file called test.smarty that simply outputs a Hello World message. The do_main method calls some of the default variables defined in the KTStandardDispatcher class to instantiate the template and render its output.

//all required includes defined in series 1, plus
require_once KT_LIB_DIR . '/dispatcher.inc.php';

class MyTrainingRecords extends KTStandardDispatcher {
function MyTrainingRecords()
{
//lets add a breadcrumb detail for this page ;optional
$this->aBreadCrumbs = array(
array('action'=>'mytrainingrecords', 'name'=>_kt("My Traininig Records")),
);
return parent::KTStandardDispatcher();
}

function do_main()
{
$oTemplate =& $this->oValidator->validateTemplate('ktabr/test');
$this->oPage->setTitle('My Training Records Page');
return $oTemplate->render();
}
}

$oDispatcher = new MyTrainingRecords();
$oDispatcher->dispatch();

For a plugin page to be meaningful, its class has to be instantiated and dispatched by the dispatcher (by explicitly calling the dispatch method), which is what the last few lines of the above code do.

The below screenshot shows the output of the newly created php page, along with the breadcrumb detail as defined.

No comments:

About Me

My photo
Princeton, New Jersey, United States
I am an Information Technology Analyst with extensive background in the Clinical Industry that includes working for CROs, Pharmaceutical and Medical Diagnostic companies.