public function createService(ServiceLocatorInterface $sm)
 {
     $config = $sm->get('config');
     if (!isset($config['ZfcDatagrid'])) {
         throw new InvalidArgumentException('Config key "ZfcDatagrid" is missing');
     }
     /* @var $application \Zend\Mvc\Application */
     $application = $sm->get('application');
     $grid = new Datagrid();
     $grid->setOptions($config['ZfcDatagrid']);
     $grid->setMvcEvent($application->getMvcEvent());
     if ($sm->has('translator') === true) {
         $grid->setTranslator($sm->get('translator'));
     }
     $grid->init();
     return $grid;
 }
 /**
  * VERY UGLY DEPENDECY...
  *
  * @todo Refactor :-)
  *
  * @see \ZfcDatagrid\Renderer\RendererInterface::prepareViewModel()
  */
 public function prepareViewModel(Datagrid $grid)
 {
     $viewModel = $this->getViewModel();
     $viewModel->setVariable('gridId', $grid->getId());
     $viewModel->setVariable('title', $this->getTitle());
     $viewModel->setVariable('parameters', $grid->getParameters());
     $viewModel->setVariable('overwriteUrl', $grid->getUrl());
     $viewModel->setVariable('templateToolbar', $this->getToolbarTemplate());
     foreach ($this->getToolbarTemplateVariables() as $key => $value) {
         $viewModel->setVariable($key, $value);
     }
     $viewModel->setVariable('rendererName', $this->getName());
     $options = $this->getOptions();
     $generalParameterNames = $options['generalParameterNames'];
     $viewModel->setVariable('generalParameterNames', $generalParameterNames);
     $viewModel->setVariable('columns', $this->getColumns());
     $columnsHidden = [];
     foreach ($this->getColumns() as $column) {
         if ($column->isHidden()) {
             $columnsHidden[] = $column->getUniqueId();
         }
     }
     $viewModel->setVariable('columnsHidden', $columnsHidden);
     $viewModel->setVariable('rowStyles', $grid->getRowStyles());
     $viewModel->setVariable('paginator', $this->getPaginator());
     $viewModel->setVariable('data', $this->getData());
     $viewModel->setVariable('filters', $this->getFilters());
     $viewModel->setVariable('rowClickAction', $grid->getRowClickAction());
     $viewModel->setVariable('massActions', $grid->getMassActions());
     $viewModel->setVariable('isUserFilterEnabled', $grid->isUserFilterEnabled());
     /*
      * renderer specific parameter names
      */
     $optionsRenderer = $this->getOptionsRenderer();
     $viewModel->setVariable('optionsRenderer', $optionsRenderer);
     if ($this->isExport() === false) {
         $parameterNames = $optionsRenderer['parameterNames'];
         $viewModel->setVariable('parameterNames', $parameterNames);
         $activeParameters = [];
         $activeParameters[$parameterNames['currentPage']] = $this->getCurrentPageNumber();
         $sortColumns = [];
         $sortDirections = [];
         foreach ($this->getSortConditions() as $sortCondition) {
             $sortColumns[] = $sortCondition['column']->getUniqueId();
             $sortDirections[] = $sortCondition['sortDirection'];
         }
         $activeParameters[$parameterNames['sortColumns']] = implode(',', $sortColumns);
         $activeParameters[$parameterNames['sortDirections']] = implode(',', $sortDirections);
         $viewModel->setVariable('activeParameters', $activeParameters);
     }
     $viewModel->setVariable('exportRenderers', $grid->getExportRenderers());
 }
 /**
  * Call initGrid on rendering
  */
 public function render()
 {
     $this->initGrid();
     return parent::render();
 }
Exemple #4
0
 public function testSetViewModelException()
 {
     $grid = new Datagrid();
     $grid->getViewModel();
     $customView = $this->getMock('Zend\\View\\Model\\ViewModel');
     $this->setExpectedException('Exception', 'A viewModel is already set. Did you already called $grid->render() or $grid->getViewModel() before?');
     $grid->setViewModel($customView);
 }