diff --git a/Module.php b/Module.php new file mode 100644 index 0000000..c02c99b --- /dev/null +++ b/Module.php @@ -0,0 +1,5 @@ + __DIR__ . '/src/ZfcDatagridExamples/Controller/CategoryController.php', + 'ZfcDatagridExamples\Controller\MinimalController' => __DIR__ . '/src/ZfcDatagridExamples/Controller/MinimalController.php', + 'ZfcDatagridExamples\Controller\PersonController' => __DIR__ . '/src/ZfcDatagridExamples/Controller/PersonController.php', + 'ZfcDatagridExamples\Controller\PersonDoctrine2Controller' => __DIR__ . '/src/ZfcDatagridExamples/Controller/PersonDoctrine2Controller.php', + 'ZfcDatagridExamples\Controller\PersonZendController' => __DIR__ . '/src/ZfcDatagridExamples/Controller/PersonZendController.php', + 'ZfcDatagridExamples\Data\Doctrine2' => __DIR__ . '/src/ZfcDatagridExamples/Data/Doctrine2.php', + 'ZfcDatagridExamples\Data\PhpArray' => __DIR__ . '/src/ZfcDatagridExamples/Data/PhpArray.php', + 'ZfcDatagridExamples\Data\ZendSelect' => __DIR__ . '/src/ZfcDatagridExamples/Data/ZendSelect.php', + 'ZfcDatagridExamples\Module' => __DIR__ . '/src/ZfcDatagridExamples/Module.php', + 'ZfcDatagridTest\ModuleTest' => __DIR__ . '/tests/ZfcDatagridExamplesTest/ModuleTest.php', +); diff --git a/config/module.config.php b/config/module.config.php new file mode 100644 index 0000000..cd5885b --- /dev/null +++ b/config/module.config.php @@ -0,0 +1,165 @@ + array( + + 'invokables' => array( + // Datasources + 'zfcDatagrid.examples.data.phpArray' => 'ZfcDatagridExamples\Data\PhpArray', + 'zfcDatagrid.examples.data.doctrine2' => 'ZfcDatagridExamples\Data\Doctrine2', + 'zfcDatagrid.examples.data.zendSelect' => 'ZfcDatagridExamples\Data\ZendSelect' + ) + ), + + 'controllers' => array( + 'invokables' => array( + 'ZfcDatagridExamples\Controller\Person' => 'ZfcDatagridExamples\Controller\PersonController', + 'ZfcDatagridExamples\Controller\PersonDoctrine2' => 'ZfcDatagridExamples\Controller\PersonDoctrine2Controller', + 'ZfcDatagridExamples\Controller\PersonZend' => 'ZfcDatagridExamples\Controller\PersonZendController', + 'ZfcDatagridExamples\Controller\Minimal' => 'ZfcDatagridExamples\Controller\MinimalController', + 'ZfcDatagridExamples\Controller\Category' => 'ZfcDatagridExamples\Controller\CategoryController' + ) + ), + + 'router' => array( + 'routes' => array( + 'ZfcDatagrid' => array( + 'type' => 'Literal', + 'options' => array( + 'route' => '/zfcDatagrid', + 'defaults' => array( + '__NAMESPACE__' => 'ZfcDatagridExamples\Controller', + 'controller' => 'person', + 'action' => 'bootstrap' + ) + ), + + 'may_terminate' => true, + 'child_routes' => array( + 'default' => array( + 'type' => 'Segment', + 'options' => array( + 'route' => '/[:controller[/:action]]', + 'constraints' => array( + 'controller' => '[a-zA-Z][a-zA-Z0-9_-]*', + 'action' => '[a-zA-Z][a-zA-Z0-9_-]*' + ), + 'defaults' => array() + ), + + 'may_terminate' => true, + 'child_routes' => array( + 'wildcard' => array( + 'type' => 'Wildcard', + + 'may_terminate' => true, + 'child_routes' => array( + 'wildcard' => array( + 'type' => 'Wildcard' + ) + ) + ) + ) + ) + ) + ) + ) + ), + + 'console' => array( + 'router' => array( + 'routes' => array( + 'datagrid-person' => array( + 'options' => array( + 'route' => 'datagrid person [--page=] [--items=] [--filterBys=] [--filterValues=] [--sortBys=] [--sortDirs=]', + 'defaults' => array( + 'controller' => 'ZfcDatagridExamples\Controller\Person', + 'action' => 'console' + ) + ) + ), + + 'datagrid-category' => array( + 'options' => array( + 'route' => 'datagrid category [--page=] [--items=] [--filterBys=] [--filterValues=] [--sortBys=] [--sortDirs=]', + 'defaults' => array( + 'controller' => 'ZfcDatagridExamples\Controller\Category', + 'action' => 'console' + ) + ) + ) + ) + ) + ), + + /** + * The ZF2 DbAdapter + Doctrine2 connection is must for examples! + */ + 'zfcDatagrid_dbAdapter' => array( + 'driver' => 'Pdo_Sqlite', + 'database' => __DIR__ . '/../src/ZfcDatagrid/Examples/Data/examples.sqlite' + ), + + 'doctrine' => array( + 'connection' => array( + 'orm_zfcDatagrid' => array( + 'driverClass' => 'Doctrine\DBAL\Driver\PDOSqlite\Driver', + 'params' => array( + 'charset' => 'utf8', + 'path' => __DIR__ . '/../src/ZfcDatagrid/Examples/Data/examples.sqlite' + ) + ) + ), + + 'configuration' => array( + 'orm_zfcDatagrid' => array( + 'metadata_cache' => 'array', + 'query_cache' => 'array', + 'result_cache' => 'array', + 'driver' => 'orm_zfcDatagrid', + 'generate_proxies' => true, + 'proxy_dir' => 'data/ZfcDatagrid/Proxy', + 'proxy_namespace' => 'ZfcDatagrid\Proxy', + 'filters' => array() + ) + ), + + 'driver' => array( + 'ZfcDatagrid_Driver' => array( + 'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver', + 'cache' => 'array', + 'paths' => array( + __DIR__ . '/../src/ZfcDatagrid/Examples/Entity' + ) + ), + + 'orm_zfcDatagrid' => array( + 'class' => 'Doctrine\ORM\Mapping\Driver\DriverChain', + 'drivers' => array( + 'ZfcDatagridExamples\Entity' => 'ZfcDatagrid_Driver' + ) + ) + ), + + // now you define the entity manager configuration + 'entitymanager' => array( + // This is the alternative config + 'orm_zfcDatagrid' => array( + 'connection' => 'orm_zfcDatagrid', + 'configuration' => 'orm_zfcDatagrid' + ) + ), + + 'eventmanager' => array( + 'orm_crawler' => array() + ), + + 'sql_logger_collector' => array( + 'orm_crawler' => array() + ), + + 'entity_resolver' => array( + 'orm_crawler' => array() + ) + ) +); diff --git a/src/ZfcDatagridExamples/Controller/CategoryController.php b/src/ZfcDatagridExamples/Controller/CategoryController.php new file mode 100644 index 0000000..bf38d16 --- /dev/null +++ b/src/ZfcDatagridExamples/Controller/CategoryController.php @@ -0,0 +1,178 @@ +getServiceLocator()->get('ZfcDatagrid\Datagrid'); + $grid->setTitle('Categories'); + $grid->setDefaultItemsPerPage(5); + $grid->setDataSource($this->getServiceLocator() + ->get('zfcDatagrid.examples.data.phpArray') + ->getCategorys()); + + $col = new Column\Select('id'); + $col->setIdentity(); + $grid->addColumn($col); + + $colParentId = new Column\Select('parentId'); + $colParentId->setHidden(true); + $grid->addColumn($colParentId); + + $colHasChildren = new Column\Select('hasChildren'); + $colHasChildren->setHidden(true); + $grid->addColumn($colHasChildren); + + $colTags = new Column\Select('tags'); + $colTags->setLabel('Tags'); + $colTags->setHidden(true); + $colTags->setWidth(30); + $colTags->setType(new Type\PhpArray()); + $grid->addColumn($colTags); + + $toggle = new Column\Action\Button(); + $toggle->setLabel('+'); + $toggle->addShowOnValue($colHasChildren, 'y'); + $toggle->setAttribute('onclick', 'console.log($(this).parent().parent().attr(\'id\'));$.get(\'/zfcDatagrid/category/tree\', function (data) { + console.log(this); + $.each(data.data, function (index, value) { + $(\'#'.$grid->getId().'\').jqGrid(\'addRowData\', value.idConcated, value, \'after\', 5); + }); + });'); + + $col = new Column\Action(); + $col->setLabel(' '); + $col->setUniqueId('expandAction'); + $col->addAction($toggle); + $grid->addColumn($col); + + $col = new Column\Select('name'); + $col->setLabel('Name'); + $col->setWidth(50); + $col->setSortDefault(1); + $col->setRendererParameter('formatter', ' + function (cellvalue, options, rowObject) { + var valuePrint = cellvalue; + + $.each(rowObject.'.$colTags->getUniqueId().', function (index, value) { + valuePrint += \' \' + value + \'\'; + }); + + return valuePrint; + } + ', 'jqGrid'); + $grid->addColumn($col); + + $toggle = new Column\Action\Button(); + $toggle->setLabel('other action...'); + $toggle->addShowOnValue($colHasChildren, 'y'); + $toggle->setAttribute('onclick', 'alert(\'clicked\');'); + + $col = new Column\Action(); +// $col->setLabel('A'); + $col->addAction($toggle); + $grid->addColumn($col); + + return $grid; + } + + /** + * Simple bootstrap table + * + * @return \ZfcDatagrid\Controller\ViewModel + */ + public function bootstrapAction() + { + $grid = $this->getGrid(); +// $grid->setRendererName('bootstrapTable'); + $grid->render(); + + return $grid->getResponse(); + } + + public function jqgridAction() + { +// $form = new CategoryFilterForm(); + + $viewModel = new ViewModel(); + + $grid = $this->getGrid(); + $grid->setRendererName('jqGrid'); + $grid->setUserFilterDisabled(true); + + $grid->render(); + + return $grid->getResponse(); + } + + public function consoleAction() + { + $viewModel = new ViewModel(); + + $grid = $this->getGrid(); + $grid->render(); + + return $grid->getResponse(); + } + + /** + * Get the tree data from a record + */ + public function treeAction() + { + $data = array(); + $data[] = array( + 'id' => 10, + 'idParent' => '', + 'hasChildren' => 'n', + 'name' => 'blubb', + 'tags' => array('Tag1', 'tag2...'), + 'expandAction' => '', + 'action' => '', + 'idConcated' => 'blubb' + ); + + $viewModel = new JsonModel(); + $viewModel->setVariable('data', $data); + + return $viewModel; + + return ' + [ + {"id":1,"parentId":"","hasChildren":"y","name":"Root","tags":["Tag1","Tag2"," Martin"," ZfcDatagrid"],"expandAction":"","action":"\u003Ca class=\u0022btn\u0022 href=\u0022#\u0022 onclick=\u0022alert(\u0027clicked\u0027);\u0022\u003Eblubb\u003C\/a\u003E","idConcated":"1"} + ,{"id":5,"parentId":"","hasChildren":"y","name":"Root2","tags":[""],"expandAction":"", action":"\u003Ca class=\u0022btn\u0022 href=\u0022#\u0022 onclick=\u0022alert(\u0027clicked\u0027);\u0022\u003Eblubb\u003C\/a\u003E","idConcated":"5"} + ,{"id":6,"parentId":5,"hasChildren":"n","name":"Second level of Root2","tags":[""],"expandAction":"","action":"","idConcated":"6"} + ,{"id":2,"parentId":1,"hasChildren":"y","name":"Second level: entry 1","tags":["Tag1","Root"," Root2"],"expandAction":"","action":"\u003Ca class=\u0022btn\u0022 href=\u0022#\u0022 onclick=\u0022alert(\u0027clicked\u0027);\u0022\u003Eblubb\u003C\/a\u003E","idConcated":"2"} + ,{"id":3,"parentId":2,"hasChildren":"n","name":"Third level: entry 1","tags":["cool"," stuff"],"expandAction":"","action":"","idConcated":"3"} + ]; + + '; + /* + * var myData= [ + {"id":1,"parentId":"","hasChildren":"y","name":"     asdf test","action": "","idConcated":"10"} + ,{"id":5,"parentId":"","hasChildren":"y","name":"    asdf2","action":"","idConcated":"50"} + ,{"id":6,"parentId":5,"hasChildren":"n","name":"    asdf3 level of Root2","action":"","idConcated":"60"} ,{"id":2,"parentId":1,"hasChildren":"y","name":"    asdf4 level: entry 1","action":"","idConcated":"20"} ,{"id":3,"parentId":2,"hasChildren":"n","name":"    asdf5 level: entry 1","action":"","idConcated":"30"} + ]; + + $.each(myData, function (index, value) { + $('#defaultGrid').jqGrid('addRowData', value.idConcated, value, 'after', 5); + }); + + //$('#defaultGrid').jqGrid('addRowData', 'idConcated', myData, 'first', 5); + */ + } +} diff --git a/src/ZfcDatagridExamples/Controller/MinimalController.php b/src/ZfcDatagridExamples/Controller/MinimalController.php new file mode 100644 index 0000000..043ff74 --- /dev/null +++ b/src/ZfcDatagridExamples/Controller/MinimalController.php @@ -0,0 +1,45 @@ + 'Wayne? John!', + ); + $row2 = array( + 'displayName' => 'Name2', + ); + + $data = array( + $row, + $row2 + ); + + /* @var $grid \ZfcDatagrid\Datagrid */ + $grid = $this->getServiceLocator()->get('ZfcDatagrid\Datagrid'); + $grid->setTitle('Minimal grid'); + + //you can change here the renderer (allowed: bootstrapTable / jqgrid...default is bootstrapTable + $grid->setRendererName('jqGrid'); + $grid->setDataSource($data); + + $col = new Column\Select('displayName'); + $col->setLabel('Name'); + $grid->addColumn($col); + + $grid->render(); + + return $grid->getResponse(); + } +} diff --git a/src/ZfcDatagridExamples/Controller/PersonController.php b/src/ZfcDatagridExamples/Controller/PersonController.php new file mode 100644 index 0000000..06b34c7 --- /dev/null +++ b/src/ZfcDatagridExamples/Controller/PersonController.php @@ -0,0 +1,239 @@ +getServiceLocator()->get('ZfcDatagrid\Datagrid'); + $grid->setTitle('Persons'); + $grid->setDefaultItemsPerPage(5); + $grid->setDataSource($this->getServiceLocator() + ->get('zfcDatagrid.examples.data.phpArray') + ->getPersons()); + + $col = new Column\Select('id'); + $col->setIdentity(); + $grid->addColumn($col); + + $action = new Action\Mass(); + $action->setTitle('test'); + $action->setLink('/test'); + $grid->addMassAction($action); + + { + /** + * Gravatar example + * - take the email from the datasource + * - object makes the rest :-) + * + * @note Could be whatever you want -> Grab data from everywhere you want with dynamic parameters :-) + */ + $colEmail = new Column\Select('email'); + $colEmail->setLabel('E-Mail'); + $colEmail->setHidden(true); + $grid->addColumn($colEmail); + + $dataPopulation = new Column\DataPopulation\Object(); + $dataPopulation->setObject(new Column\DataPopulation\Object\Gravatar()); + $dataPopulation->addObjectParameterColumn('email', $colEmail); + + $col = new Column\ExternalData('avatar'); + $col->setLabel('Avatar'); + $col->setDataPopulation($dataPopulation); + $col->setType(new Type\Image()); + $grid->addColumn($col); + } + + $col = new Column\Select('displayName'); + $col->setLabel('Displayname'); + $col->setWidth(25); + $col->setSortDefault(1, 'ASC'); + $col->setFormatter(new Email()); + $col->addStyle(new Style\Bold()); + $grid->addColumn($col); + + $col = new Column\Select('familyName'); + $col->setLabel('Familyname'); + $col->setWidth(15); + $grid->addColumn($col); + + $col = new Column\Select('givenName'); + $col->setLabel('Givenname'); + $col->setWidth(15); + $col->setSortDefault(2, 'DESC'); + $grid->addColumn($col); + + $style = new Style\BackgroundColor(array( + 200, + 200, + 200 + )); + $style->addByValue($col, 'Martin'); + $grid->addRowStyle($style); + + $col = new Column\Select('gender'); + $col->setLabel('Gender'); + $col->setWidth(10); + $col->setReplaceValues(array( + 'm' => 'male', + 'f' => 'female' + )); + $style = new Style\BackgroundColor(array( + 200, + 100, + 100 + )); + $style->addByValue($col, 'male'); + $col->addStyle($style); + $col->setTranslationEnabled(true); + $grid->addColumn($col); + + { + $col = new Column\Select('age'); + $col->setLabel('Age'); + $col->setWidth(5); + $col->setType(new Type\Number()); + $col->setFilterDefaultValue('>=20'); + + $style = new Style\Color(Style\Color::$RED); + $style->addByValue($col, 20); + $col->addStyle($style); + + $grid->addColumn($col); + } + + { + $colType = new Type\Number(); + $colType->addAttribute(\NumberFormatter::FRACTION_DIGITS, 2); + $colType->setSuffix(' kg'); + + $col = new Column\Select('weight'); + $col->setLabel('Weight'); + $col->setWidth(10); + $col->setType($colType); + $col->setFilterDefaultOperation(\ZfcDatagrid\Filter::GREATER_EQUAL); + $grid->addColumn($col); + } + + $col = new Column\Select('birthday'); + $col->setLabel('Birthday'); + $col->setWidth(10); + $col->setType(new Type\DateTime()); + $col->setUserSortDisabled(true); + $grid->addColumn($col); + + { + $colType = new Type\DateTime('Y-m-d H:i:s', \IntlDateFormatter::MEDIUM, \IntlDateFormatter::MEDIUM); + $colType->setSourceTimezone('Europe/Vienna'); + $colType->setOutputTimezone('UTC'); + + $col = new Column\Select('changeDate'); + $col->setLabel('Last change'); + $col->setWidth(15); + $col->setType($colType); + $grid->addColumn($col); + } + + $action = new Column\Action\Button(); + $action->setLabel('test'); + $action->setAttribute('href', '/someAction/id/' . $action->getRowIdPlaceholder()); + + $col = new Column\Action(); + $col->setLabel('Actions'); + $col->setWidth(10); + $col->addAction($action); + $grid->addColumn($col); + + $grid->setRowClickAction($action); + + return $grid; + } + + /** + * bootstrap table + * + * @return \ZfcDatagrid\Controller\ViewModel + */ + public function bootstrapAction() + { + $grid = $this->getGrid(); + + $grid->render(); + + return $grid->getResponse(); + } + + /** + * bootstrap table + * + * @return \ZfcDatagrid\Controller\ViewModel + */ + public function jqgridAction() + { + $grid = $this->getGrid(); + $grid->setRendererName('jqGrid'); + + $grid->render(); + + return $grid->getResponse(); + } + + /** + * Usage + * php index.php show example grid --page 1 + * php index.php show example grid --page 2 + * + * @return \Zend\Http\Response\Stream + */ + public function consoleAction() + { + /* @var $grid \ZfcDatagrid\Datagrid */ + $grid = $this->getServiceLocator()->get('ZfcDatagrid\Datagrid'); + $grid->setTitle('Persons'); + $grid->setDefaultItemsPerPage(5); + $grid->setDataSource($this->getServiceLocator() + ->get('zfcDatagrid.examples.data.phpArray') + ->getPersons()); + + $col = new Column\Select('id'); + $col->setIdentity(); + $grid->addColumn($col); + + $col = new Column\Select('displayName'); + $col->setLabel('Displayname'); + $col->setWidth(25); + $col->setSortDefault(1, 'ASC'); + $col->addStyle(new Style\Bold()); + $grid->addColumn($col); + + $col = new Column\Select('familyName'); + $col->setLabel('Familyname'); + $col->setWidth(15); + $grid->addColumn($col); + + $col = new Column\Select('givenName'); + $col->setLabel('Givenname'); + $col->setWidth(15); + $grid->addColumn($col); + + $col = new Column\Select('age'); + $col->setLabel('Age'); + $col->setWidth(10); + $col->setType(new Type\Number()); + $grid->addColumn($col); + + $grid->render(); + + return $grid->getResponse(); + } +} diff --git a/src/ZfcDatagridExamples/Controller/PersonDoctrine2Controller.php b/src/ZfcDatagridExamples/Controller/PersonDoctrine2Controller.php new file mode 100644 index 0000000..0e3a72c --- /dev/null +++ b/src/ZfcDatagridExamples/Controller/PersonDoctrine2Controller.php @@ -0,0 +1,182 @@ +getServiceLocator()->get('ZfcDatagrid\Datagrid'); + $grid->setTitle('Persons'); + $grid->setDefaultItemsPerPage(5); + $grid->setDataSource($this->getServiceLocator() + ->get('zfcDatagrid.examples.data.doctrine2') + ->getPersons()); + + $col = new Column\Select('id', 'p'); + $col->setIdentity(); + $grid->addColumn($col); + + { + /** + * Gravatar example + * - take the email from the datasource + * - object makes the rest :-) + * + * @note Could be whatever you want -> Grab data from everywhere you want with dynamic parameters :-) + */ + $colEmail = new Column\Select('email', 'p'); + $colEmail->setLabel('E-Mail'); + $colEmail->setHidden(); + + $dataPopulation = new Column\DataPopulation\Object(); + $dataPopulation->setObject(new Column\DataPopulation\Object\Gravatar()); + $dataPopulation->addObjectParameterColumn('email', $colEmail); + + $col = new Column\ExternalData('avatar'); + $col->setLabel('Avatar'); + $col->setType(new Type\Image()); + $col->setDataPopulation($dataPopulation); + $grid->addColumn($col); + } + + $col = new Column\Select('displayName', 'p'); + $col->setLabel('Displayname'); + $col->setWidth(25); + $col->setSortDefault(1, 'ASC'); + $col->addStyle(new Style\Bold()); + $grid->addColumn($col); + + $col = new Column\Select('familyName', 'p'); + $col->setLabel('Familyname'); + $col->setWidth(15); + $grid->addColumn($col); + + $col = new Column\Select('givenName', 'p'); + $col->setLabel('Givenname'); + $col->setWidth(15); + $col->setSortDefault(2, 'DESC'); + $grid->addColumn($col); + + $grid->addColumn($colEmail); + + $col = new Column\Select('gender', 'p'); + $col->setLabel('Gender'); + $col->setWidth(10); + $col->setReplaceValues(array( + 'm' => 'male', + 'f' => 'female' + )); + $col->setTranslationEnabled(true); + $grid->addColumn($col); + + { + $col = new Column\Select('age', 'p'); + $col->setLabel('Age'); + $col->setWidth(5); + $col->setType(new Type\Number()); + $col->setFilterDefaultValue('>=20'); + + $style = new Style\Color(Style\Color::$RED); + $style->addByValue($col, 20); + $col->addStyle($style); + + $grid->addColumn($col); + } + + { + $colType = new Type\Number(); + $colType->addAttribute(\NumberFormatter::FRACTION_DIGITS, 2); + $colType->setSuffix(' kg'); + + $col = new Column\Select('weight', 'p'); + $col->setLabel('Weight'); + $col->setWidth(10); + $col->setType($colType); + $grid->addColumn($col); + } + + $col = new Column\Select('birthday', 'p'); + $col->setLabel('Birthday'); + $col->setWidth(10); + $col->setType(new Type\DateTime()); + $col->setUserSortDisabled(true); + $grid->addColumn($col); + + { + $colType = new Type\DateTime('Y-m-d H:i:s', \IntlDateFormatter::MEDIUM, \IntlDateFormatter::MEDIUM); + $colType->setSourceTimezone('Europe/Vienna'); + $colType->setOutputTimezone('UTC'); + + $col = new Column\Select('changeDate', 'p'); + $col->setLabel('Last change'); + $col->setWidth(15); + $col->setType($colType); + $grid->addColumn($col); + } + + $grid->render(); + + return $grid->getResponse(); + } + + /** + * Usage + * php index.php show example grid --page 1 + * php index.php show example grid --page 2 + * + * @return \Zend\Http\Response\Stream + */ + public function consoleAction() + { + /* @var $grid \ZfcDatagrid\Datagrid */ + $grid = $this->getServiceLocator()->get('zfcDatagrid'); + $grid->setTitle('Persons'); + $grid->setDefaultItemsPerPage(5); + $grid->setDataSource($this->getServiceLocator() + ->get('zfcDatagrid.examples.data.doctrine2') + ->getPersons()); + + $col = new Column\Select('id'); + $col->setIdentity(); + $grid->addColumn($col); + + $col = new Column\Select('displayName'); + $col->setLabel('Displayname'); + $col->setWidth(25); + $col->setSortDefault(1, 'ASC'); + $col->addStyle(new Style\Bold()); + $grid->addColumn($col); + + $col = new Column\Select('familyName'); + $col->setLabel('Familyname'); + $col->setWidth(15); + $grid->addColumn($col); + + $col = new Column\Select('givenName'); + $col->setLabel('Givenname'); + $col->setWidth(15); + $grid->addColumn($col); + + $col = new Column\Select('age'); + $col->setLabel('Age'); + $col->setWidth(10); + $col->setType(new Type\Number()); + $grid->addColumn($col); + + $grid->render(); + + return $grid->getResponse(); + } +} diff --git a/src/ZfcDatagridExamples/Controller/PersonZendController.php b/src/ZfcDatagridExamples/Controller/PersonZendController.php new file mode 100644 index 0000000..a43a9a2 --- /dev/null +++ b/src/ZfcDatagridExamples/Controller/PersonZendController.php @@ -0,0 +1,189 @@ +getServiceLocator()->get('zfcDatagrid_dbAdapter'); + + /* @var $grid \ZfcDatagrid\Datagrid */ + $grid = $this->getServiceLocator()->get('ZfcDatagrid\Datagrid'); + $grid->setTitle('Persons'); + $grid->setDefaultItemsPerPage(5); + $grid->setDataSource($this->getServiceLocator() + ->get('zfcDatagrid.examples.data.zendSelect') + ->getPersons(), $dbAdapter); + + $col = new Column\Select('id', 'p'); + $col->setIdentity(); + $grid->addColumn($col); + + { + /** + * Gravatar example + * - take the email from the datasource + * - object makes the rest :-) + * + * @note Could be whatever you want -> Grab data from everywhere you want with dynamic parameters :-) + */ + $colEmail = new Column\Select('email', 'p'); + $colEmail->setLabel('E-Mail'); + $colEmail->setHidden(); + + $dataPopulation = new Column\DataPopulation\Object(); + $dataPopulation->setObject(new Column\DataPopulation\Object\Gravatar()); + $dataPopulation->addObjectParameterColumn('email', $colEmail); + + $col = new Column\ExternalData('avatar'); + $col->setLabel('Avatar'); + $col->setDataPopulation($dataPopulation); + $col->setType(new Type\Image()); + $grid->addColumn($col); + } + + $col = new Column\Select('displayName', 'p'); + $col->setLabel('Displayname'); + $col->setWidth(25); + $col->setSortDefault(1, 'ASC'); + $col->addStyle(new Style\Bold()); + $grid->addColumn($col); + + $col = new Column\Select('familyName', 'p'); + $col->setLabel('Familyname'); + $col->setWidth(15); + $grid->addColumn($col); + + $col = new Column\Select('givenName', 'p'); + $col->setLabel('Givenname'); + $col->setWidth(15); + $col->setSortDefault(2, 'DESC'); + $grid->addColumn($col); + + $grid->addColumn($colEmail); + + $col = new Column\Select('gender', 'p'); + $col->setLabel('Gender'); + $col->setWidth(10); + $col->setReplaceValues(array( + 'm' => 'male', + 'f' => 'female' + )); + $col->setTranslationEnabled(true); + $grid->addColumn($col); + + { + $col = new Column\Select('age', 'p'); + $col->setLabel('Age'); + $col->setWidth(5); + $col->setType(new Type\Number()); + + $style = new Style\Color(Style\Color::$RED); + $style->addByValue($col, 20); + $col->addStyle($style); + + $grid->addColumn($col); + } + + { + $colType = new Type\Number(); + $colType->addAttribute(\NumberFormatter::FRACTION_DIGITS, 2); + $colType->setSuffix(' kg'); + + $col = new Column\Select('weight', 'p'); + $col->setLabel('Weight'); + $col->setWidth(10); + $col->setType($colType); + $grid->addColumn($col); + } + + $col = new Column\Select('birthday', 'p'); + $col->setLabel('Birthday'); + $col->setWidth(10); + $col->setType(new Type\DateTime()); + $col->setUserSortDisabled(true); + $grid->addColumn($col); + + { + $colType = new Type\DateTime('Y-m-d H:i:s', \IntlDateFormatter::MEDIUM, \IntlDateFormatter::MEDIUM); + $colType->setSourceTimezone('Europe/Vienna'); + $colType->setOutputTimezone('UTC'); + + $col = new Column\Select('changeDate', 'p'); + $col->setLabel('Last change'); + $col->setWidth(15); + $col->setType($colType); + $grid->addColumn($col); + } + + $col = new Column\Select('name', 'g'); + $col->setLabel('Group'); + $col->setWidth(10); + $grid->addColumn($col); + + $grid->render(); + + return $grid->getResponse(); + } + + /** + * Usage + * php index.php show example grid --page 1 + * php index.php show example grid --page 2 + * + * @return \Zend\Http\Response\Stream + */ + public function consoleAction() + { + /* @var $grid \ZfcDatagrid\Datagrid */ + $grid = $this->getServiceLocator()->get('ZfcDatagrid\Datagrid'); + $grid->setTitle('Persons'); + $grid->setDefaultItemsPerPage(5); + $grid->setDataSource($this->getServiceLocator() + ->get('zfcDatagrid.examples.data.zendSelect') + ->getPersons()); + + $col = new Column\Select('id'); + $col->setIdentity(); + $grid->addColumn($col); + + $col = new Column\Select('displayName'); + $col->setLabel('Displayname'); + $col->setWidth(25); + $col->setSortDefault(1, 'ASC'); + $col->addStyle(new Style\Bold()); + $grid->addColumn($col); + + $col = new Column\Select('familyName'); + $col->setLabel('Familyname'); + $col->setWidth(15); + $grid->addColumn($col); + + $col = new Column\Select('givenName'); + $col->setLabel('Givenname'); + $col->setWidth(15); + $grid->addColumn($col); + + $col = new Column\Select('age'); + $col->setLabel('Age'); + $col->setWidth(10); + $col->setType(new Type\Number()); + $grid->addColumn($col); + + $grid->render(); + + return $grid->getResponse(); + } +} diff --git a/src/ZfcDatagridExamples/Data/Doctrine2.php b/src/ZfcDatagridExamples/Data/Doctrine2.php new file mode 100644 index 0000000..334849e --- /dev/null +++ b/src/ZfcDatagridExamples/Data/Doctrine2.php @@ -0,0 +1,121 @@ +serviceLocator = $serviceLocator; + } + + /** + * Get service locator + * + * @return ServiceLocatorInterface + */ + public function getServiceLocator() + { + return $this->serviceLocator; + } + + private function createTables() + { + /* @var $entityManager \Doctrine\ORM\EntityManager */ + $entityManager = $this->getServiceLocator()->get('doctrine.entitymanager.orm_zfcDatagrid'); + + /* @var $cli \Symfony\Component\Console\Application */ + $cli = $this->getServiceLocator()->get('doctrine.cli'); + $helperSet = $cli->getHelperSet(); + $helperSet->set(new EntityManagerHelper($entityManager), 'em'); + + $fp = tmpfile(); + + // $input = new StringInput('orm:schema-tool:create --dump-sql'); + $input = new StringInput('orm:schema-tool:create'); + + /* @var $command \Doctrine\ORM\Tools\Console\Command\SchemaTool\CreateCommand */ + $command = $cli->get('orm:schema-tool:create'); + $returnCode = $command->run($input, new StreamOutput($fp)); + + $phpArray = $this->getServiceLocator()->get('zfcDatagrid.examples.data.phpArray'); + $persons = $phpArray->getPersons(); + + $this->createData(new Person(), $persons); + // $entityManager->f + // fseek($fp, 0); + // $output = ''; + // while (! feof($fp)) { + // $output = fread($fp, 4096); + // } + // fclose($fp); + + // echo '
';
+        // print_r($output);
+        // print_r($returnCode);
+        // echo 'DONE!';
+        // exit();
+    }
+
+    private function createData($entity, $data)
+    {
+        /* @var $entityManager \Doctrine\ORM\EntityManager */
+        $entityManager = $this->getServiceLocator()->get('doctrine.entitymanager.orm_zfcDatagrid');
+
+        foreach ($data as $row) {
+
+            $newEntity = clone $entity;
+            foreach ($row as $key => $value) {
+                $method = 'set' . ucfirst($key);
+                $newEntity->{$method}($value);
+            }
+
+            $entityManager->persist($newEntity);
+        }
+
+        $entityManager->flush();
+    }
+
+    /**
+     *
+     * @return QueryBuilder
+     */
+    public function getPersons()
+    {
+        /* @var $entityManager \Doctrine\ORM\EntityManager */
+        $entityManager = $this->getServiceLocator()->get('doctrine.entitymanager.orm_zfcDatagrid');
+        $personRepo = $entityManager->getRepository('ZfcDatagridExamples\Entity\Person');
+
+        // if ($entityManager->getConnection()->isConnected() === true) {
+
+        // }
+
+        // Test if the SqLite is ready...
+        try {
+            $data = $personRepo->find(2);
+        } catch (\Exception $e) {
+            $this->createTables();
+            $data = $personRepo->find(2);
+        }
+
+        $qb = $entityManager->createQueryBuilder();
+        $qb->select('p');
+        $qb->from('ZfcDatagridExamples\Entity\Person', 'p');
+
+        return $qb;
+    }
+}
diff --git a/src/ZfcDatagridExamples/Data/PhpArray.php b/src/ZfcDatagridExamples/Data/PhpArray.php
new file mode 100644
index 0000000..7c8854a
--- /dev/null
+++ b/src/ZfcDatagridExamples/Data/PhpArray.php
@@ -0,0 +1,196 @@
+serviceLocator = $serviceLocator;
+    }
+
+    /**
+     * Get service locator
+     *
+     * @return ServiceLocatorInterface
+     */
+    public function getServiceLocator()
+    {
+        return $this->serviceLocator;
+    }
+
+    /**
+     *
+     * @return array
+     */
+    public function getPersons()
+    {
+        $row = array(
+            'id' => 1,
+            'displayName' => 'Wayne? John!',
+            'familyName' => 'Wayne',
+            'givenName' => 'John',
+            'email' => 'unknown@gmail.com',
+            'gender' => 'm',
+            'age' => 111,
+            'weight' => 50,
+            'birthday' => '1987-10-03 00:00:00',
+            'changeDate' => '2001-04-19 14:30:41'
+        );
+        $row2 = array(
+            'id' => 2,
+            'displayName' => 'Franz Ferdinand',
+            'familyName' => 'Ferdinand',
+            'givenName' => 'Franz',
+            'email' => 'unknown@gmail.com',
+            'gender' => 'm',
+            'age' => 20,
+            'weight' => 123.12222,
+            'birthday' => '1981-01-31 00:00:00',
+            'changeDate' => '1999-12-31 22:30:41'
+        );
+        $row3 = array(
+            'id' => 3,
+            'displayName' => 'Peter Kaiser',
+            'familyName' => 'Kaiser',
+            'givenName' => 'Peter',
+            'email' => 'unknown@test.com',
+            'gender' => 'm',
+            'age' => 23,
+            'weight' => 70.23,
+            'birthday' => '1991-10-03 00:00:00',
+            'changeDate' => '2013-04-19 09:30:41'
+        );
+        $row4 = array(
+            'id' => 4,
+            'displayName' => 'Martin Keckeis',
+            'familyName' => 'Keckeis',
+            'givenName' => 'Martin',
+            'email' => 'martin.keckeis1@gmail.com',
+            'gender' => 'm',
+            'age' => 25,
+            'weight' => 70,
+            'birthday' => '1987-10-03 00:00:00',
+            'changeDate' => '2001-04-19 14:30:41'
+        );
+        $row5 = array(
+            'id' => 5,
+            'displayName' => 'Anna Marie \' Franz',
+            'familyName' => 'Franz',
+            'givenName' => 'Anna Marie',
+            'email' => 'unknown@test.com',
+            'gender' => 'f',
+            'age' => 20,
+            'weight' => 123.12222,
+            'birthday' => '1981-01-31 00:00:00',
+            'changeDate' => '1999-12-31 22:30:41'
+        );
+        $row6 = array(
+            'id' => 6,
+            'displayName' => 'Sarah Blumenfeld',
+            'familyName' => 'Blumenfeld',
+            'givenName' => 'Sarah',
+            'email' => 'unknown@test.com',
+            'gender' => 'f',
+            'age' => 23,
+            'weight' => 70.23,
+            'birthday' => '1991-10-03 00:00:00',
+            'changeDate' => '2013-04-19 09:30:41'
+        );
+
+        $data = array(
+            $row,
+            $row2,
+            $row3,
+            $row4,
+            $row5,
+            $row6
+        );
+
+        return $data;
+    }
+
+    /**
+     *
+     * @return array
+     */
+    public function getCategorys()
+    {
+        $id = 0;
+
+        $data = array();
+
+        $data[] = array(
+            'id' => ++ $id,
+            'parentId' => null,
+            'hasChildren' => 'y',
+            'name' => 'Root',
+            'tags' => 'Tag1,Tag2, Martin, ZfcDatagrid'
+        );
+
+        $data[] = array(
+            'id' => ++ $id,
+            'parentId' => $id - 1,
+            'hasChildren' => 'y',
+            'name' => 'Second level: entry 1',
+            'level' => 1,
+            'tags' => 'Tag1,Root, Root2'
+        );
+
+        $data[] = array(
+            'id' => ++ $id,
+            'parentId' => $id - 1,
+            'hasChildren' => 'n',
+            'name' => 'Third level: entry 1',
+            'tags' => 'cool, stuff'
+        );
+
+        $data[] = array(
+            'id' => ++ $id,
+            'parentId' => $id - 2,
+            'hasChildren' => 'n',
+            'name' => 'Third level: entry 2'
+        );
+
+        $data[] = array(
+            'id' => ++ $id,
+            'parentId' => null,
+            'hasChildren' => 'y',
+            'name' => 'Root2'
+        );
+
+        $data[] = array(
+            'id' => ++ $id,
+            'parentId' => $id - 1,
+            'hasChildren' => 'n',
+            'name' => 'Second level of Root2',
+            'level' => 1
+        );
+
+        return $data;
+    }
+
+    public function getRandom()
+    {
+        $maxRow = 9999;
+
+        $persons = $this->getPersons();
+
+        $data = array();
+        for ($i = 1; $i <= $maxRow; $i ++) {
+            $data[] = $persons[rand(0, 5)];
+        }
+
+        return $data;
+    }
+}
diff --git a/src/ZfcDatagridExamples/Data/ZendSelect.php b/src/ZfcDatagridExamples/Data/ZendSelect.php
new file mode 100644
index 0000000..896d0cd
--- /dev/null
+++ b/src/ZfcDatagridExamples/Data/ZendSelect.php
@@ -0,0 +1,49 @@
+serviceLocator = $serviceLocator;
+    }
+
+    /**
+     * Get service locator
+     *
+     * @return ServiceLocatorInterface
+     */
+    public function getServiceLocator()
+    {
+        return $this->serviceLocator;
+    }
+
+    /**
+     *
+     * @return \Zend\Db\Sql\Select
+     */
+    public function getPersons()
+    {
+        $select = new Select();
+        $select->from(array(
+            'p' => 'person'
+        ));
+        $select->join(array(
+            'g' => 'group'
+        ), 'g.id = p.primaryGroupId', 'name', 'left');
+
+        return $select;
+    }
+}
diff --git a/src/ZfcDatagridExamples/Data/examples.sqlite b/src/ZfcDatagridExamples/Data/examples.sqlite
new file mode 100644
index 0000000..d4557b5
Binary files /dev/null and b/src/ZfcDatagridExamples/Data/examples.sqlite differ
diff --git a/src/ZfcDatagridExamples/Module.php b/src/ZfcDatagridExamples/Module.php
new file mode 100644
index 0000000..c61ad90
--- /dev/null
+++ b/src/ZfcDatagridExamples/Module.php
@@ -0,0 +1,76 @@
+ array(
+                __DIR__ . '/../../autoload_classmap.php'
+            ),
+            
+            'Zend\Loader\StandardAutoloader' => array(
+                'namespaces' => array(
+                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__
+                )
+            )
+        );
+    }
+
+    public function getConfig()
+    {
+        return include __DIR__ . '/../../config/module.config.php';
+    }
+
+    public function getServiceConfig()
+    {
+        if (class_exists('DoctrineORMModule\Service\DBALConnectionFactory')) {
+            // For the doctrine examples!
+            return array(
+                'factories' => array(
+                    'doctrine.connection.orm_zfcDatagrid' => new \DoctrineORMModule\Service\DBALConnectionFactory('orm_zfcDatagrid'),
+                    'doctrine.configuration.orm_zfcDatagrid' => new \DoctrineORMModule\Service\ConfigurationFactory('orm_zfcDatagrid'),
+                    'doctrine.entitymanager.orm_zfcDatagrid' => new \DoctrineORMModule\Service\EntityManagerFactory('orm_zfcDatagrid'),
+                    
+                    'doctrine.driver.orm_zfcDatagrid' => new \DoctrineModule\Service\DriverFactory('orm_zfcDatagrid'),
+                    'doctrine.eventmanager.orm_zfcDatagrid' => new \DoctrineModule\Service\EventManagerFactory('orm_zfcDatagrid'),
+                    'doctrine.entity_resolver.orm_zfcDatagrid' => new \DoctrineORMModule\Service\EntityResolverFactory('orm_zfcDatagrid'),
+                    'doctrine.sql_logger_collector.orm_zfcDatagrid' => new \DoctrineORMModule\Service\SQLLoggerCollectorFactory('orm_zfcDatagrid')
+                )
+            );
+        }
+        
+        return array();
+    }
+
+    public function getConsoleUsage(Console $console)
+    {
+        return array(
+            'Display the example console datagrid',
+            'datagrid person' => 'Show person datagrid',
+            'datagrid category' => 'Show category datagrid',
+            
+            'Options:',
+            array(
+                '--page=NUMBER',
+                'Number of the page to display [1...n]'
+            ),
+            array(
+                '--items=NUMBER',
+                'How much items to display per page [1...n]'
+            ),
+            array(
+                '--sortBys=COLUMN',
+                'Unique id of the column(s) to sort (split with: ,)'
+            ),
+            array(
+                '--sortDirs=DIRECTION',
+                'Sort direction of the columns [ASC|DESC] (split with: ,)'
+            )
+        );
+    }
+}
diff --git a/tests/ZfcDatagridExamplesTest/ModuleTest.php b/tests/ZfcDatagridExamplesTest/ModuleTest.php
new file mode 100644
index 0000000..15f9e6f
--- /dev/null
+++ b/tests/ZfcDatagridExamplesTest/ModuleTest.php
@@ -0,0 +1,54 @@
+assertTrue(is_array($module->getAutoloaderConfig()));
+        $this->assertCount(2, $module->getAutoloaderConfig());
+        $this->assertArrayHasKey('Zend\Loader\StandardAutoloader', $module->getAutoloaderConfig());
+        $this->assertArrayHasKey('Zend\Loader\ClassMapAutoloader', $module->getAutoloaderConfig());
+    }
+
+    public function testGetConfig()
+    {
+        $module = new Module();
+
+        $this->assertTrue(is_array($module->getConfig()));
+        $this->assertCount(9, $module->getConfig());
+        $this->assertArrayHasKey('ZfcDatagrid', $module->getConfig());
+    }
+
+    public function testSetGetConsoleUsage()
+    {
+        $module = new Module();
+
+        $console = $this->getMock('Zend\Console\Adapter\AbstractAdapter');
+
+        $this->assertTrue(is_array($module->getConsoleUsage($console)));
+        $this->assertCount(8, $module->getConsoleUsage($console));
+    }
+
+    public function testGetServiceConfig()
+    {
+        $module = new Module();
+
+        $this->assertTrue(is_array($module->getServiceConfig()));
+
+        $serviceConfig = $module->getServiceConfig();
+        $this->assertCount(1, $serviceConfig);
+
+        $this->assertArrayHasKey('factories', $serviceConfig);
+        $this->assertCount(7, $serviceConfig['factories']);
+    }
+}