Giscuit
Prev Next

Modifying Information and Search results

Introduction

In this tutorial we will modify Information and Search results of a layer, thus showing how to enhance their behaviour. The layer used will be World boundaries, which is added by default upon installation. Our goal will be to create a link on the resulting country name (requested by Information or Search tool), which will open the country's wikipedia page (for example Moldova will have this link: http://en.wikipedia.org/wiki/Moldova).

Modifying Information and Search results

What we need to do is extend the Giscuit_Db_Table_Layer_Abstract class methods located in "library/Giscuit/Db/Table/Layer/Abstract.php" file. This class is the default table that is extended by all new layer tables, so for World boundaries we already have a class created in "library/Giscuit/Db/Table/Layer/Worldboundaries.php" file.

The class has no specific methods from the start. Each information request is passed through formatInfoResults method and search requests are passed through formatSearchResults method. Extended methods displayed in the code below will first call it's parent method and then perform additional modifications on the results.

  1. <?php
  2. class Giscuit_Db_Table_Layer_Worldboundaries extends Giscuit_Db_Table_Layer_Abstract
  3. {
  4.  
  5.     /**
  6.      * Format information results
  7.      * 
  8.      * @param stdClass $itemField - field descriptor
  9.      * @param string $name - field name
  10.      * @param mixed $value - field value
  11.      * @param Zend_Form $form - layer form
  12.      * @return void 
  13.      */
  14.     public function formatInfoResults($itemField$name$value$form)
  15.     {
  16.         //Call parent class method
  17.         parent::formatInfoResults($itemField$name$value$form);
  18.         
  19.         $translate Zend_Registry::get('Zend_Translate');
  20.         
  21.         //Check for "name" field which is to be modified
  22.         if ($name == 'name'{
  23.             //Create link to wikipedia and overwrite the element's html
  24.             $link 'http://en.wikipedia.org/wiki/' $value;
  25.             $itemField->html $translate->_($name": <a target=\"_blank\" href=\"{$link}\" title=\"{$value}\">{$value}</a>";
  26.         }}
  27.     
  28.     /**
  29.      * Format search results
  30.      * 
  31.      * @param Zend_Db_Table_Rowset $rows - resulting rows
  32.      * @param Zend_Form $form - layer form
  33.      * @return Zend_Db_Table_Rowset 
  34.      */
  35.     public function formatSearchResults($rows$form)
  36.     {
  37.         //Call parent class method and get results
  38.         $rows parent::formatSearchResults($rows$form);
  39.         
  40.         $translate Zend_Registry::get('Zend_Translate');
  41.         
  42.         //Iterate through results
  43.         foreach ($rows as $key => $value{
  44.             //Create link to wikipedia and assign it to each "name" field
  45.             $link 'http://en.wikipedia.org/wiki/' $rows[$key]['name'];
  46.             $rows[$key]['name'"<a target=\"_blank\" href=\"{$link}\" title=\"{$rows[$key]['name']}\">{$rows[$key]['name']}</a>";
  47.         }
  48.         
  49.         return $rows;
  50.     }}

Put the code above into "library/Giscuit/Db/Table/Layer/Worldboundaries.php" file and check the results by using Information and Search tool on World boundaries layer. Upon displaying results the country's name should be a link which will open a new web page with a link to the it's wikipedia page.

You can extend the default functionality for any layer by overwriting their existing methods.

Prev Up Next
Creating your first plugin Developer Extending Editing functionality

COPYRIGHT ® 2012, VEC