* @license PHP License * @package WB * @subpackage content */ /** * Load base class */ WBClass::load( 'WBContent' ); /** * Content component: Chart * * Display charts * * @version 1.1.0 * @package WB * @subpackage content */ class WBContent_Chart extends WBContent { /** * my parameter list * * - tmpl display template * - action either data, config or template * - chart name of datasource * - datasource datasource directory * - contentdisposition file name for downloads * - requiredgroup access group * - addbom add byte order marker 0xEF, 0xBB, 0xBF * - delimiter of CSV columns * - enclosure of CSV data * - ttl time to live for cached chart data and config * * @var array */ protected $config = array( 'tmpl' => 'default', 'action' => 'template', 'chart' => 'default', 'datasource' => 'view', 'requiredgroup' => WBContent::GROUP_ANON, 'contentdisposition' => '', 'addbom' => '0', 'delimiter' => ';', 'enclosure' => '"', 'ttl' => 3600 ); /** * run * * run component * * @return array parameter list */ public function run() { if (!$this->isUserInGroup($this->config['requiredgroup'])) { $this->loadTemplates('accessDenied'); return $this->config; } $this->config['action'] = strtolower($this->config['action']); if ($this->config['action'] == 'template') { $this->loadTemplates($this->config['tmpl']); $this->tmpl->addGlobalVars($this->config); return $this->config; } if ($this->config['action'] == 'data') { return $this->downloadData(); } if ($this->config['action'] == 'config') { return $this->downloadConfig(); } // this actually won't happen because download exits $this->config['action'] = 'template'; return $this->config; } /** * download data * * */ protected function downloadData() { $randyConf = array( 'addbom' => $this->config['addbom'], 'delimiter' => $this->config['delimiter'], 'enclosure' => $this->config['enclosure'] ); $ext = 'csv'; $chart = $this->config['chart']; if (strstr($this->config['chart'], '.')) { $name = explode('.', $this->config['chart']); $ext = strtolower(array_pop($name)); $chart = implode('.', $name); } $file = null; $path = 'var/cache/chart/data/' . $chart; switch ($ext) { case 'json': $path .= '.json'; $mime = 'application/json'; if ($this->isCacheValid($file, $path)) { $this->downloadFile($file->realpath(), $mime, $this->config['contentdisposition']); return; } $randy = WBClass::create('WBDatasource_Renderer_JSON'); break; default: case 'csv': $path .= '.csv'; $mime = 'text/csv'; if ($this->isCacheValid($file, $path)) { $this->downloadFile($file->realpath(), $mime, $this->config['contentdisposition']); return; } $randy = WBClass::create('WBDatasource_Renderer_CSV'); break; } $randy->setConfig($randyConf); $view = WBClass::create('WBDatasource_View'); $view->addVars($this->req->export()); $view->setRenderer($randy); $view->render($this->config['datasource'] . '/' . $chart); $rFile = $randy->getFile(); $file->touch($path); $file->rename($rFile->realpath()); $rFile->clear(); $this->downloadFile($file->realpath(), $mime, $this->config['contentdisposition']); } /** * download chart settings * */ protected function downloadConfig() { $ext = 'csv'; $chart = $this->config['chart']; if (strstr($this->config['chart'], '.')) { $name = explode('.', $this->config['chart']); $ext = strtolower(array_pop($name)); $chart = implode('.', $name); } $file = null; $path = 'var/cache/chart/config/' . $chart; switch ($ext) { case 'json': $mime = 'application/json'; $path .= '.json'; if ($this->isCacheValid($file, $path)) { $file = $file->realpath(); break; } $config = WBClass::create('WBConfig'); $config->load('chart/' . $chart); $json = json_encode($config->get('config')); $file->touch($path); $file = $file->realpath(); file_put_contents($file, $json); break; default: case 'xml': $mime = 'text/xml'; $randy = WBClass::create('WBDatasource_Renderer_CSV'); $base = WBParam::get('wb/dir/base'); $etc = WBParam::get('wb/dir/config', 'etc'); $file = sprintf('%s/%s/chart/%s.xml', $base, $etc, $this->config['chart']); if (!file_exists($file)) { $file = sprintf('%s/%s-default/chart/%s.xml', $base, $etc, $this->config['chart']); } break; } $this->downloadFile($file, $mime, $this->config['contentdisposition']); } /** * Check cache file * * Check if cache file exists and TTL is not exeeded. Also caching parameter will be checked. * * @param WBFile $file * @param string $path * @return bool true if cache is still OK */ private function isCacheValid(&$file, $path) { $file = WBClass::create('WBFile'); if (!$file->exists($path)) { return false; } if (1 > WBParam::get('wb/cache/use', 0)) { return false; } $mTime = filemtime($file->realpath()) + intval($this->config['ttl']); if (time() > $mTime) { return false; } return true; } }