> > Topic

Basic framework of a page function

Dec 14, 2006 / pixtur
Apr 3, 2010 / guest
 

Attached files

No files uploaded
 
Basic framework of a page function
From No3wiki

Maybe it's a good idea to go through the source code of an example. Let's take commentView() defined in pages/comments.inc

function commentView(){

Since we are dealing with pages, we have to define the global page handler $PH:

global $PH;

Form and page-attributes are collected in a internal array. You access this parameters with the get()-function. Also all Db-base objects derived from DbProjectItem provide the functions getVisibleById() and getEditableById() which validate the current user rights on the fly. The pageHandler function abortWarning() will return to the previous page and print a message a the top of the page.

Together the following code could be read as get the object of requested comment from database only if visible. Otherwise return to previous page.

### get task ####
if(!$comment=Comment::getVisibleById(get('comment'))) {
    $PH->abortWarning("invalid comment-id",ERROR_FATAL);
}
       
if(!$project= Project::getVisibleById($comment->project)) {
    $PH->abortWarning("invalid project-id",ERROR_FATAL);
}

In commentView we wil see links to other page functions like commentEdit. After editing a comment we would like to go back to this page. Therefore we need to define a from_handle with the parameters necessary to render the current page:

### create from handle ###
$from_handle=$PH->defineFromHandle(array('comment'=>$comment->id));

If the $comments has been made on a task, get this task. Since Task::getVisibleById() returns NULL on failure, this code could be also written as: $task= Task::getVisibleById($comment->task);

$task = $comment->task ? Task::getVisibleById($comment->task):NULL;

Next we set up things necessary for rendering the page.

### set up page and write header ####
{
    $page= new Page();
                       
    $page->cur_tab='projects';

A lot of navigation is done by breadcrumbs. For projects and tasks getting the breadcrumbs can depend of project state or parent-tasks, etc. That's why we have different functions for this (defined in render/render_misc.inc).

    if($task) {
        $page->crumbs=build_task_crumbs($task);
    }
    else {
        $page->crumbs=build_project_crumbs($project);
    }

Notice that the current breadcrumb is given as an target_id / pageHandle. Providing an undefined invalid pagehandle would trigger a warning:

    $page->crumbs[]=new NaviCrumb(array('target_id' =>'commentView',
    ...
    ));


The page-type has to be translated:

    $page->type= __('Comment');

    $page->title=$comment->name;
    $page->title_minor="";
    if($comment->state== -1) {
        $page->title_minor=sprintf(__('(deleted %s)','page title add on with date of deletion'),
                                   date("D, d.m.Y",strtotime($comment->deleted)));
    }

Page functions are shown in the upper right corner of the page. They are defined just like functions for lists or context-menu functions:

    ### page functions ###
    $page->add_function(new PageFunction(array(
        'target'=>'commentEdit',
        'params'=>array('comment'=>$comment->id),
        'icon'=>'edit',
        'tooltip'=>__('Edit this comment'),
        'name'=>'edit'
    )));

Since we have all necessary information for the head, let's print it:

    ### render title ###
    echo(new PageHeader);
}

This opens the content area:

echo (new PageContentOpen);

Normal html-content can be rendered to PageBlocks. This blocks can be toggled (collapsed/expanded). To store the current state, each block requires an id.

#--- summary ----------------------------------------------------------------
{
    $block=new PageBlock(array(
        'title'=>__('Description'),
        'id'=>'description'
    ));
    $block->render_blockStart();
    $str= text2html($comment->description, $project);

    echo "";

    echo "$str";

    echo "";

    $block->render_blockEnd();
}

There is a very elaborated set for rendering lists. Those include sorting for columns, costumizing etc.

#--- list comments -------------------------------------------------------------
{
    #$comments= $task->getComments();
    #$list=new ListBlock_comments();
    #$list->no_items_html=$PH->getLink('commentNew',,array('parent_task'=>$task->id));
    #$list->render_list($comments);
}

For some functions on this page we have to pass some parameters as a hidden field. There two ways for calling a new page:
  1. following a link provided by $PH->getLink() or $PH->getUrl()
  2. submitting the current form. All pages inside streber are forms.
    echo '<input type="hidden" name="prj" value="'.$comment->project.'">';

Finally we have to close the html-blocks:

    echo (new PageContentClose);
    echo (new PageHtmlEnd);
}

No Comments