eZ Community » Learn » eZ Publish » An Introduction to Developing eZ...

An Introduction to Developing eZ Publish Extensions

Friday 10 December 2010 10:24:31 am

  • Currently 5 out of 5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Now we extend our example by creating a new view. We want to save the array with the example data in the database, so we create a new database table (using, for example, phpMyAdmin) with the name "jacextension_data" in our ez39_plain database, with the columns id | user_id | created | value ( see listing 11).

CREATE TABLE jacextension_data ( 
    id INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY , 
    user_id INT( 11 ) NOT NULL , 
    created INT( 11 ) NOT NULL , 
    value VARCHAR( 50 ) NOT NULL 
) ENGINE = MYISAM ;

Listing 11. Run the SQL command on the ezp_plain database to create a new jacextension_data table.

Next we copy list.php to create.php, list.tpl to create.tpl and we extend module.php with a new view and user role function ("create"), which links to the PHP file create.php. Then we change the template call in create.php to design:modul1/create.tpl and adjust the rights for the view modul1/create. Clear the cache.

Now the URL http://localhost/ez/index.php/plain_site/modul1/create will work. It is the same view as modul1/list but without view parameters.

To store data in our MySQL database, we will build an HTML form that sends the data to our new view (with POST and GET variables). We create a new form in the template create.tpl with a text line name. In our example we want to send the data with GET. We also insert a new template variable {$status_message} to show the user a message (see listing 12).

{* create.tpl – template for Modulview .../modul1/create 
Html form to save the new data *} 
<form action={'modul1/create'|ezurl} method="get"> 
Name :<br /> 
<input name="name" type="text" size="50" maxlength="50"><br /> 
<input type="submit" name="DataCreation" value="Create new data"> 
<input type="reset" value="Cancel"> 
</form> 
<hr> 
Status: {$status_message}

Listing 12. Template jacextension/design/standard/templates/modul1/create.tpl with a form to save the new data.

GET / POST

At this point we change list.php to show the GET name variable, which the HTML form transmits to the script. After that we show it in the status field in the template. To show the variable GET / POST we use the eZ Publish framework with the eZHTTPTool class.

First we take a reference of the object of eZHTTPTool with $http = eZHTTPTool::instance(); With $http->hasVariable('name'); we can discover if the variable $_GET['name'] or $_POST['name'] exists and with $http->variable('name'); we can display it. If only GET or POST variables should be shown, we can use $http->hasGetVariable('name'); or $http->hasPostVariable('name');

Information about other functions (for example access to sessions) are described in the API documentation for eZ Publish. For the eZHTTPTool class, see http://pubsvn.ez.no/doxygen/4.4/html/classeZHTTPTool.html.

If we want to write our own log file we use eZLog::write(). This is often useful because the default log files contain a lot of information and are not easy to read (see Listing 13).

<?php
// modul1/create.php – Function file of View create 
$Module = $Params['Module'];
 
// take copy of global object 
$http = eZHTTPTool::instance (); 
$value = '';
 
// If the variable 'name' is sent by GET or POST, show variable 
if( $http->hasVariable('name') ) 
    $value = $http->variable ('name');
 
if( $value != '' ) 
    $statusMessage = 'Name: '. $value; 
else 
    $statusMessage = 'Please insert data';
 
// initialize Templateobject 
$tpl = eZTemplate::factory(); // From eZPublish 4.3. For previous versions, use templateInit() function instead 
$tpl->setVariable( 'status_message', $statusMessage );
 
// Write variable $statusMessage in the file eZ Debug Output / Log 
// here the 4 different types: Notice, Debug, Warning, Error 
eZDebug::writeNotice( $statusMessage, 'jacextension:modul1/list.php' ); 
eZDebug::writeDebug( $statusMessage, 'jacextension:modul1/list.php' ); 
eZDebug::writeWarning( $statusMessage, 'jacextension:modul1/list.php' ); 
eZDebug::writeError( $statusMessage, 'jacextension:modul1/list.php' );
 
// $statusMessage write own Log file to ezroot/var/log/jacextension_modul1.log  
eZLog::write ( $statusMessage, 'jacextension_modul1.log' );
 
$Result = array(); 
// search/replace template and save result for $module_result.content 
$Result['content'] = $tpl->fetch( 'design:modul1/create.tpl' );
 
// generate route Modul1/create 
$Result['path'] = array(  array( 'url'  => 'modul1/list',
                                 'text' => 'Modul1'), 
                          array( 'url'  => false,
                                 'text' => 'create' ) ); 
?>

Listing 13. jacextension/module/modul1/create.php with examples to show variables GET/POST and generate Debug messages and Log files

36 542 Users on board!

Tutorial menu

Printable

Printer Friendly version of the full article on one page with plain styles

Author(s)

Proudly Developed with from