Montag, 18. Mai 2009

Symfony Sortable I18n-field in Admin-List

Works with Symfony 1.0

This Article shows how to make a I18n Field in a Symfony-Admin-List Sortable.
In this example i use the "name"-field as I18n-field.

First, open the file apps/[APPNAME]/modules/[MODULENAME]/action/actions.class.php and add the function:

protected function addSortCriteria($c)
{
if ($sort_column = $this->getUser()->getAttribute('sort', null, 'sf_admin/[MODULENAME]/sort'))
{
if ($sort_column=='name'){
if (strlen(sfContext::getInstance()->getUser()->getCulture())==2) {
$culture=sfContext::getInstance()->getUser()->getCulture();
}else{
$culture="de";
}
$culture=strtolower($culture);
$c->addJoin([MODULENAME]I18nPeer::ID,[MODULENAME]Peer::ID);
$c->add([MODULENAME]I18nPeer::CULTURE, $culture);
$sort_column=[MODULENAME]I18nPeer::NAME;
}else{
$sort_column = [MODULENAME]Peer::translateFieldName($sort_column, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_COLNAME);
}
if ($this->getUser()->getAttribute('type', null, 'sf_admin/[MODULENAME]/sort') == 'asc')
{
$c->addAscendingOrderByColumn($sort_column);
}
else
{
$c->addDescendingOrderByColumn($sort_column);
}
}
}


Create a __toString function in /lib/model/[MODULENAME].php:

public function __toString () {
if (strlen(sfContext::getInstance()->getUser()->getCulture())==2) {
$culture=sfContext::getInstance()->getUser()->getCulture();
}else{
$culture="de";
}
$culture=strtolower($culture);
$culture[0]=strtoupper($culture[0]);
return $this->__call("getNameI18n".$culture,"");
}
public function __call($m, $a)
{
$data = @split('I18n', $m, 2);
if( count($data) != 2 )
throw new Exception('Tried to call unknown method '.get_class($this).'::'.$m);
list( $method, $culture ) = $data;
$this->setCulture( $culture );
return call_user_func_array(array($this, $method), $a);
}


Next, create a File "_name.php" in apps/[APPNAME]/modules/[MODULENAME]/templates/_name.php:


echo $[MODELNAME]->__toString();
?>


In generator.yml add the _name-field to the display section in list:


...
list:
display: [_name, ... ]
....


Next, copy the _list_th_tabular.php file from the cache-directory to your apps/[APPNAME]/modules/[MODULENAME]/templates directory and change the <th />-tag of the name-field to something like this:

...
<th id="sf_admin_list_th_name">
getAttribute('sort', null, 'sf_admin/[MODULENAME]/sort') == 'name'): ?>
getAttribute('type', 'asc', 'sf_admin/[MODULENAME]/sort') == 'asc' ? 'desc' : 'asc')) ?>
(getAttribute('type', 'asc', 'sf_admin/[MODULENAME]/sort')) ?>)



</th>
...


That's it

Keine Kommentare:

Kommentar veröffentlichen