Codice PHP:
<?php
class SiteController extends Zend_Controller_Action
{
/**
* [EN]If a action don't exist, just redirect to the basic
*
* @param string $name
* @param array $var
*/
function __call($name, $var)
{
$this->_redirect ( 'default/site/basic', array ('exit' => 1 ) );
return false;
}
/**
* [EN] I think this is needed for something. can't remember
*
*/
function init()
{
$this->view->url = Zend_Registry::get ( 'config' )->site->url;
}
/**
* Same as __call
*
*/
function indexAction()
{
$this->_forward ( 'basic' );
}
/**
* Show the source code for this controller
*
*/
function codeAction()
{
$this->render ( 'code' );
}
/**
* [EN] Simplify the datagrid creation process
* [EN] Instead of having to write "long" lines of code we can simplify this.
* [EN] In fact if you have a Class that extends the Zend_Controller_Action
* [EN] It's not a bad idea put this piece o code there. May be very useful
*
*
* @return $grid
*/
function grid()
{
$export = $this->getRequest ()->getParam ( 'export' );
$db = Zend_Registry::get ( 'db' );
switch ($export)
{
case 'odt' :
$grid = "Bvb_Grid_Deploy_Odt";
break;
case 'ods' :
$grid = "Bvb_Grid_Deploy_Ods";
break;
case 'xml' :
$grid = "Bvb_Grid_Deploy_Xml";
break;
case 'csv' :
$grid = "Bvb_Grid_Deploy_Csv";
break;
case 'excel' :
$grid = "Bvb_Grid_Deploy_Excel";
break;
case 'word' :
$grid = "Bvb_Grid_Deploy_Word";
break;
case 'wordx' :
$grid = "Bvb_Grid_Deploy_Wordx";
break;
case 'pdf' :
$grid = "Bvb_Grid_Deploy_Pdf";
break;
case 'print' :
$grid = "Bvb_Grid_Deploy_Print";
break;
default :
$grid = "Bvb_Grid_Deploy_Table";
break;
}
$grid = new $grid ( $db, 'Grid Example', 'media/temp', array ('download' ) );
$grid->escapeOutput ( false );
$grid->addTemplateDir ( 'My/Template/Table', 'My_Template_Table', 'table' );
$grid->addElementDir ( 'My/Validate', 'My_Validate', 'validator' );
$grid->addElementDir ( 'My/Filter', 'My_Filter', 'filter' );
$grid->addFormatterDir ( 'My/Formatter', 'My_Formatter' );
$grid->imagesUrl = $this->getRequest ()->getBaseUrl () . '/public/images/';
$grid->cache = array ('use' => 0, 'instance' => Zend_Registry::get ( 'cache' ), 'tag' => 'grid' );
return $grid;
}
/**
* Performing CRUD operations.
*
* Check how easy it is to set a form.
*
*/
function crudAction()
{
$db = Zend_Registry::get ( 'db' );
$grid = $this->grid ( 'table' );
$grid->from ( 'crud' )
->order ( 'id DESC ' );
$paises = $db->fetchCol ( "SELECT DISTINCT(Name) FROM Country ORDER BY Name ASC " );
$language = $db->fetchCol ( "SELECT DISTINCT(Language) FROM CountryLanguage ORDER BY Language ASC" );
$grid->addColumn ( 'id', array ('title' => 'ID', 'hide' => 1 ) );
$grid->addColumn ( 'firstname', array ('title' => 'First Name' ) );
$grid->addColumn ( 'lastname', array ('title' => 'Last Name' ) );
$grid->addColumn ( 'email', array ('title' => 'Email' ) );
$grid->addColumn ( 'age', array ('title' => 'Age' ) );
$grid->addColumn ( 'language', array ('title' => 'Language' ) );
$grid->addColumn ( 'date_added', array ('title' => 'Updated', 'format' => array ('date', 'en_US' ), 'class' => 'width_150' ) );
$grid->addColumn ( 'country', array ('title' => 'Country' ) );
$form = new Bvb_Grid_Form ( );
$form->add ( 1 )
->edit ( 1 )
->button ( 1 )
->delete ( 1 )
->onAddForce ( array ('date_added' => date ( 'Y-m-d H:i:s' ) ) )
->onEditForce ( array ('date_added' => date ( 'Y-m-d H:i:s' ) ) );
#->onDeleteCascade(array('table'=>'teste','parentField'=>'age','childField'=>'op','operand'=>'='))
$fAdd = new Bvb_Grid_Form_Column ( 'firstname' );
$fAdd->title ( 'First name' )
->validators ( array ('StringLength' => array (3, 10 ) ) )
->filters ( array ('StripTags', 'StringTrim', 'StringToLower' ) )
->attributes(array('type'=>'password'))
->description ( 'Insert your first name. (password type...)' );
$lastName = new Bvb_Grid_Form_Column ( 'lastname' );
$lastName->title ( 'Last name' )
->description ( 'Your last name' )
->validators ( array ('StringLength' => array (3, 10 ) ) );
$country = new Bvb_Grid_Form_Column ( 'country' );
$country->title ( 'Country' )
->description ( 'Choose your Country' )
->values ( array_combine ( $paises, $paises ) );
$email = new Bvb_Grid_Form_Column ( 'email' );
$email->title ( 'Email Address' )
->validators ( array ('EmailAddress' ) )
->filters ( array ('StripTags', 'StringTrim', 'StringToLower' ) )
->description ( 'Insert you email address' );
$lang = new Bvb_Grid_Form_Column ( 'language' );
$lang->title ( 'Language' )
->description ( 'Your language' )
->values ( array_combine ( $language, $language ) );
$age = new Bvb_Grid_Form_Column ( 'age' );
$age->title ( 'Age' )
->description ( 'Choose your age' )
->values ( array_combine ( range ( 10, 100 ), range ( 10, 100 ) ) );
$form->addColumns ( $fAdd, $lastName, $email, $lang, $country, $age );
$grid->addForm ( $form );
//Add filters
$filters = new Bvb_Grid_Filters ( );
$filters->addFilter ( 'firstname' )
->addFilter ( 'lastname' )
->addFilter ( 'email' )
->addFilter ( 'age', array ('distinct' => array ('name' => 'age', 'field' => 'age' ) ) )
->addFilter ( 'country', array ('distinct' => array ('name' => 'country', 'field' => 'country' ) ) )
->addFilter ( 'language', array ('distinct' => array ('name' => 'language', 'field' => 'language' ) ) );
$grid->addFilters ( $filters );
$this->view->pages = $grid->deploy ();
$this->render ( 'index' );
}
/**
* The 'most' basic example.
*
* Please check the $pdf array to see how we can configure the templates header and footer.
* If you are exporting to PDF you can even choose between a letter format or A4 format, and set the page orientation
* landascape or '' (empty) for vertical
*
*/
function basicAction()
{
$grid = $this->grid ( 'table' );
$grid->from ( 'City' );
$pdf = array ('logo' => 'public/images/logo.png', 'baseUrl' => '/grid/', 'title' => 'DataGrid Zend Framework', 'subtitle' => 'Easy and powerfull - (Demo document)', 'footer' => 'Downloaded from: [url]http://www.petala-azul.com[/url] ', 'size' => 'a4', #letter || a4
'orientation' => 'landscape', # || ''
'page' => 'Page N.' );
$grid->setTemplate ( 'print', 'print', $pdf );
$grid->setTemplate ( 'pdf', 'pdf', $pdf );
$grid->setTemplate ( 'word', 'word', $pdf );
$grid->setTemplate ( 'wordx', 'wordx', $pdf );
$grid->setTemplate ( 'ods', 'ods', $pdf );
$grid->setPrimary(false);
$this->view->pages = $grid->deploy ();
$this->render ( 'index' );
}
/**
* If you don't like to work with array when adding columns, you can work by dereferencing objects
*
*/
function columnAction()
{
$grid = $this->grid ( 'table' );
$grid->from ( 'Country as c INNER JOIN City as ct ON c.Capital=ct.ID ' )
->table ( array ('c' => 'Country', 'ct' => 'City' ) )
->order ( 'c.Continent, c.Name' )
->setPagination ( 20 );
#->noFilters(1);
#->noOrder(1);
$cap = new Bvb_Grid_Column ( 'c.Name AS cap' );
$cap->title ( 'Country (Capital)' )
->decorator ( '{{c.Name}} [i]({{ct.Name}})[/i]' );
$name = new Bvb_Grid_Column ( 'ct.Name' );
$name->title ( 'Capital' )
->hide ( 1 );
$continent = new Bvb_Grid_Column ( 'c.Continent' );
$continent->title ( 'Continent' );
$population = new Bvb_Grid_Column ( 'c.Population' );
$population->title ( 'Population' )
->class ( 'width_80' );
$lifeExpectation = new Bvb_Grid_Column ( 'c.LifeExpectancy' );
$lifeExpectation->title ( 'Life E.' )
->class ( 'width_50' );
$governmentForm = new Bvb_Grid_Column ( 'c.GovernmentForm' );
$governmentForm->title ( 'Government Form' );
$headState = new Bvb_Grid_Column ( 'c.HeadOfState' );
$headState->title ( 'Head Of State' );
$grid->addColumns ( $cap, $name, $continent, $population, $lifeExpectation, $governmentForm, $headState );
$filters = new Bvb_Grid_Filters ( );
$filters->addFilter ( 'c.Name', array ('distinct' => array ('field' => 'c.Name AS cap', 'name' => 'c.Name AS cap' ) ) )
->addFilter ( 'ct.Name', array ('distinct' => array ('field' => 'ct.Name', 'name' => 'ct.Name' ) ) )
->addFilter ( 'c.Continent', array ('distinct' => array ('field' => 'c.Continent', 'name' => 'c.Continent' ) ) )
->addFilter ( 'c.LifeExpectancy', array ('distinct' => array ('field' => 'c.LifeExpectancy', 'name' => 'c.LifeExpectancy' ) ) )
->addFilter ( 'c.GovernmentForm', array ('distinct' => array ('field' => 'c.GovernmentForm', 'name' => 'c.GovernmentForm' ) ) )
->addFilter ( 'c.HeadOfState' )
->addFilter ( 'c.Population' );
$grid->addFilters ( $filters );
$this->view->pages = $grid->deploy ();
$this->render ( 'index' );
}
/**
* @param object $object
* @return array
*/
function object2array($object)
{
$return = NULL;
if (is_array ( $object ))
{
foreach ( $object as $key => $value )
$return [$key] = self::object2array ( $value );
} else
{
$var = get_object_vars ( $object );
if ($var)
{
foreach ( $var as $key => $value )
$return [$key] = self::object2array ( $value );
} else
{
return strval ( $object );
}
}
return $return;
}
}