* @license PHP License * @package WB * @subpackage content */ /** * Load base class */ WBClass::load('WBContent_Data'); /** * Content component: Data Report * * Display charts * * @version 1.0.0 * @package WB * @subpackage content */ class WBContent_Data_Report extends WBContent_Data { /** * my parameter list * * - action either data, config or template * - datasource one or list of datasources * - requiredgroup access group * - addbom add byte order marker 0xEF, 0xBB, 0xBF * - delimiter of CSV columns * - enclosure of CSV data * * @var array */ protected $config = array( 'action' => 'filter', 'datasource' => 'view', 'requiredgroup' => WBContent::GROUP_ANON, 'addbom' => '0', 'delimiter' => ';', 'enclosure' => '"' ); /** * Current filte values * @var array */ private $filterValues = array(); /** * run * * run component * * @return array parameter list */ public function run() { if (!$this->isUserInGroup($this->config['requiredgroup'])) { $this->loadTemplates('accessDenied'); return $this->config; } // normalize datasource list if (!is_array($this->config['datasource'])) { $this->config['datasource'] = array($this->config['datasource']); } switch (strtolower($this->config['action'])) { case 'download': $this->download($this->config['datasource'][0]); break; default: case 'list': $this->listDatasources($this->config['datasource']); break; } return $this->config; } /** * Display list of datasources * * Load config of each datasource including filter and add to list * * @param array $datasources */ private function listDatasources($datasources) { $list = array(); foreach ($datasources as $ds) { $this->conf->load('datasource/' . $ds); $info = array( 'info_title' => $this->conf->get('view/info/title', $ds), 'info_blurb' => $this->conf->get('view/info/blurb', ''), 'datasource' => $ds, 'select' => $this->conf->get('view/select'), 'filter' => $this->getFilter($ds) ); $list[] = $info; } usort($list, array($this, 'compare')); $this->loadTemplates('list'); $this->tmpl->addRows('list_entry', $list); } /** * Custom compare function * * Compare info_title of array * @see usort() * @param array $a * @param array $b * @return integer -1, 0 or 1 */ private function compare($a, $b) { if ($a['info_title'] < $b['info_title']) { return -1; } if ($a['info_title'] > $b['info_title']) { return 1; } return 0; } /** * Get filter HTML for datasource * * @param string $ds path/name of datasource * @param bool $submitted * @return string $html code of form */ private function getFilter($ds, $submitted = false) { $this->conf->load('datasource/' . $ds); $filter = $this->conf->get('view/filter'); if (empty($filter)) { return ''; } $this->loadTemplates($ds . '/filter'); $formTmplName = $this->formTmplName; $this->formTmplName = 'filter_snippet'; $this->tmpl->addGlobalVar('datasource', $ds); $html = $this->processForm('filter', array(), 'filter_save', $submitted); $this->formTmplName = $formTmplName; return $html; } /** * Download CSV data * * Apply filter on datasource and download as CSV * @param string $ds * @see downloadFile() */ protected function download($ds) { $randyConf = array( 'addbom' => $this->config['addbom'], 'delimiter' => $this->config['delimiter'], 'enclosure' => $this->config['enclosure'] ); $randy = WBClass::create('WBDatasource_Renderer_CSV'); $randy->setConfig($randyConf); $view = WBClass::create('WBDatasource_View'); $view->setRenderer($randy); $filter = $this->getFilter($ds, true); if (!empty($filter)) { $view->addVars($this->filterValues); } $view->render($ds); $file = $randy->getFile(); $name = str_replace('/', '_', $ds) . '.csv'; $this->downloadFile($file->realpath(), 'text/csv', $name); } /** * Fetch list of form elements * * The standard behaviour loads form defintions from XML config * file located in form config dir {@link getFormConfigDir() } * Overwrite this to implement your own behaviour. * * @param string name of the xml- and template-filename * @return array $elements */ protected function getFormElementList($name) { if ('filter' != $name) { return parent::getFormElementList($name); } return $this->conf->get('view/filter/form/elements'); } /** * Select filter * * Use filter values to create search clause and build get-parameter string * as well as list of hidden input fields to pass filter options through pages * and forms. * * @param patForms $form * @param array $values */ public function onFilterValid($form, $values) { $this->filterValues = $values; } /** * load templates from file * * @param string $tmpl */ protected function loadTemplates($tmpl, $local = true) { if ('filter' == $tmpl) { return; } return parent::loadTemplates($tmpl, $local); } }