Esempio n. 1
0
 /**
  * Load the data
  */
 public function loadData($render = 'getRenderer')
 {
     if ($this->isDataLoaded === true) {
         return true;
     }
     if ($this->isInit() !== true) {
         throw new \Exception('The init() method has to be called, before you can call loadData()!');
     }
     if ($this->hasDataSource() === false) {
         throw new \Exception('No datasource defined! Please call "setDataSource()" first"');
     }
     /**
      * Apply cache
      */
     $renderer = $this->{$render}();
     /**
      * Step 1.1) Only select needed columns (performance)
      */
     $this->getDataSource()->setColumns($this->getColumns());
     /**
      * Step 1.2) Sorting
      */
     foreach ($renderer->getSortConditions() as $condition) {
         $this->getDataSource()->addSortCondition($condition['column'], $condition['sortDirection']);
     }
     /**
      * Step 1.3) Filtering
      */
     foreach ($renderer->getFilters($this->getKeepCacheFilter()) as $filter) {
         $this->getDataSource()->addFilter($filter);
     }
     $this->getDataSource()->execute();
     $paginatorAdapter = $this->getDataSource()->getPaginatorAdapter();
     \Zend\Paginator\Paginator::setDefaultScrollingStyle('Sliding');
     $this->paginator = new Paginator($paginatorAdapter);
     $this->paginator->setCurrentPageNumber($renderer->getCurrentPageNumber());
     $this->paginator->setItemCountPerPage($renderer->getItemsPerPage($this->getDefaultItemsPerPage()));
     /* @var $currentItems \ArrayIterator */
     $data = $this->paginator->getCurrentItems();
     if (!is_array($data)) {
         if ($data instanceof \Zend\Db\ResultSet\ResultSet) {
             $data = $data->toArray();
         } elseif ($data instanceof ArrayIterator) {
             $data = $data->getArrayCopy();
         } else {
             $add = '';
             if (is_object($data)) {
                 $add = get_class($data);
             } else {
                 $add = '[no object]';
             }
             throw new \Exception('The paginator returned an unknow result: ' . $add . ' (allowed: \\ArrayIterator or a plain php array)');
         }
     }
     /*
      * Save cache
      */
     if ($renderer->isExport() === false) {
         $cacheData = array('sortConditions' => $renderer->getSortConditions(), 'filters' => $renderer->getFilters(), 'currentPage' => $this->getPaginator()->getCurrentPageNumber());
         $success = $this->getCache()->setItem($this->getCacheId(), $cacheData);
         if ($success !== true) {
             $options = $this->getCache()->getOptions();
             if (isset($options['cache_dir'])) {
                 throw new \Exception('Could not save the datagrid cache. Does the directory "' . $options->getCacheDir() . '" exists and is writeable?');
             } else {
                 throw new \Exception('Could not save the datagrid cache.Check configuration file');
             }
         }
     }
     /*
      * Step 3) Format the data - Translate - Replace - Date / time / datetime - Numbers - ...
      */
     $prepareData = new PrepareData($data, $this->getColumns());
     $prepareData->setRendererName($this->getRendererName());
     $prepareData->setTranslator($this->getTranslator());
     $prepareData->prepare();
     $this->preparedData = $prepareData->getData();
     $this->isDataLoaded = true;
 }
 public function testPrepareDataFormatter()
 {
     $data = $this->data;
     $col1 = clone $this->col1;
     $col1->setFormatter(new \Zf2datatable\Column\Formatter\Link());
     $prepare = new PrepareData($data, array($this->colId, $col1, $this->col2));
     $prepare->setRendererName('jqGrid');
     $data[0]['idConcated'] = '1';
     $data[1]['idConcated'] = '2';
     $data[2]['idConcated'] = '3';
     $data[0]['col1'] = '<a href="test">test</a>';
     $data[1]['col1'] = '<a href="test">test</a>';
     $data[2]['col1'] = '<a href="test">test</a>';
     $data[1]['col2'] = '';
     $this->assertEquals($data, $prepare->getData());
 }