In editing upon submiting a create, edit or a delete request a callback function is triggered after the data has been parsed.
These callback functions perform the final saving operation, so if you want to make additional data manipulations before changes are saved then these functions should be used.
Wikipedia provides an API for fetching information from it.
We will use a request that will return information about the country that is being edited to find out it's currency code.
Before we start we need to create an additional field for the currency in database. To do that go to Administrator panel and click Database section.
Select world_boundaries entry from data table and click Edit. Now add a new field named currency of type character varying with length 3
and leave the "Update layer fields and editing rules" checked (to find out more about editing database table see Edit).
Since we don't want any user input to that field we must remove it from the Editing rules. To do that go to Administrator panel and click Layers section.
Select World boundaries layer, right click on it and press Edit. Click on the Editing rules section. Now search for currency element in the xml and remove it (see Editing rules for more info).
Now let's add some code that will populate this field automatically. We will use the Giscuit_Db_Table_Layer_Worldboundaries class which extends Giscuit_Db_Table_Layer_Abstract class and overwrite the addItemForEditCallback and updateItemForEditCallback methods.
<?php
class Giscuit_Db_Table_Layer_Worldboundaries extends Giscuit_Db_Table_Layer_Abstract
{
/**
* Save a row in database
*
* @param Zend_Db_Table_Row $row New row with data
* @return integer $pkey Row id
*/
public function addItemForEditCallback($row, $form = NULL)
{
//Set currency field
$row->currency = $this->getCountryCurrency($row->name);
//Execute parent method code
$pkey = $row->save();
return $pkey;
}
/**
* Update row in database
*
* @param Zend_Db_Table_Row $row Edited row
* @return bool Update/don't update row in database
*/
public function updateItemForEditCallback($row, $form = NULL)
{
//Set currency field
$row->currency = $this->getCountryCurrency($row->name);
//Execute parent method code
$row->save();
return true;
}
/**
* Get currency for a country
*
* @param string $name Country name
* @return string $currency Country currency code
*/
public function getCountryCurrency($name)
{
//Wiki url for fetching info about a country
$url = 'http://en.wikipedia.org/w/api.php?action=query&prop=revisions&titles=' . $name .
'&rvprop=content&format=txt';
//Set user agent so that wikipedia will allow the request
$opts = array(
'http'=>array(
'method' => "GET",
'header' => "Accept-language: en\r\n",
'user_agent' => "Giscuit (+http://giscuit.com/)"
)
);
//Explode the result by lines
$countryInfo =
explode("\n", $countryInfo);
//Find the currency code line
$currency = '';
foreach ($countryInfo as $line) {
if (substr($line, 0, 14) ==
'|currency_code' ||
substr($line, 0, 15) ==
'| currency_code') {
$currency = $line;
break;
}}
//If line exists get the currency code value and return it
if (! empty($currency)) {
$currency =
explode("=", $currency);
$currency =
trim($currency[1]);
return $currency;
}
//Otherwise return empty string
return '';
}
}
Put this code into "library/Giscuit/Db/Table/Layer/Worldboundaries.php" file and check the results by adding/editing a country and then using the Information tool on it.
A new field should be visible wich displays the country's currency.
Similar method exists when delete action is performed (deleteItemForEditCallback), and a method to parse geometry WKT (parseGeometryBeforeEdit).