Found at: http://publish.ez.no/article/articleprint/57/

PHP Widgets



A widget is a GUI element which is reusable. Development packages and libraries for C and C++ often have these ready for use. In HTML the form elements would substitute widgets. Now you might wonder why you would need to create further complexity by creating something in PHP for accessing these elements, keep reading and I'll provide you with my problem and my solution.

The point is that when programming on a large project like eZ publish there is often a lot of tasks which are repetetive. Creating a date selector isn't as simple as you want, you end up creating the same code several times, and everything should be based on templates so that the designer can add his touch to the end result.

A date selector is just a couple of form elements, not a big deal in creating them perhaps, but when you need several selectors, and you know you need them in the future, its an opportunity for creating code which solves the problem.

What if I could just write something like this:


eZDateSelector::createSelector( "Publish" );


instead of what I'd usually have to write for the same effect:


$ini =& INIFile::globalINI();
$Language = $ini->read_var( "eZWidgetsMain", "Language" );
$t = new eZTemplate( "ezwidgets/" .
    $ini->read_var( "eZWidgetsMain", "TemplateDir" ),
    "ezwidgets/intl/", $Language, "ezdatetime.php" );
$t->setAllStrings();

if( $version > 1 && is_numeric( $version ) )
{
    $template = $template . $version . ".tpl";
}
else
{
    $template = $template . ".tpl";
}

$t->set_file( array(
    "page_tpl" => "$template"
    ) );

$t->set_block( "page_tpl", "month_items_tpl", "month_items" );

$localeObject =& new eZLocale( $Language );

$Day = $prefix . "Day" . $postfix;
$Month = $prefix . "Month" . $postfix;
$Year = $prefix . "Year" . $postfix;

$t->set_var( "day", $GLOBALS["$Day"] );
$t->set_var( "month", $GLOBALS["$Month"] );
$t->set_var( "year", $GLOBALS["$Year"] );
$t->set_var( "prefix", $prefix );
$t->set_var( "postfix", $postfix );

for( $i = 1; $i <= 12; $i++ )
{
    if( $GLOBALS["$Month"] == $i )
    {
        $t->set_var( "selected", "selected" );
    }
    else
    {
       $t->set_var( "selected", "" );
    }
    $t->set_var( "month_name", $localeObject->monthName( $i, false ) );
    $t->set_var( "month_number", $i );
    $t->parse( "month_items", "month_items_tpl", true );
}

$t->parse( "selector", "page_tpl" );


As you can see from the code I've used som other objects, they are all part of eZ publish.

The thing with widgets is that you need them often, and you need them to solve the same problem. Thus you could add more code to the widget, for example code for checking the validity of a text field.

Well, attached you'll find the examples I've used; you'll need the classes in eZ publish to run them, but you'll graps the idea.

Caveats

It could be tempting to use classes and abstraction for most things in a site, and I must say that in theory it sounds very great. But we also should remember that it incurrs an overhead to the rendering of a page. Thus I only suggest you use this method for forms and not for live content which most of your visitors will view. At leat not without proper caching of the results.

Attached files:

Widgets
daily.tar.gz

| Back to normal page view |