View source
<?php
namespace Drupal\forena\File;
use Drupal\forena\Report;
class ReportFile extends FileBase {
private $report_cache = array();
public function __construct() {
$report_path = variable_get('forena_report_repos', '');
if (!$report_path) {
$report_path = variable_get('file_' . file_default_scheme() . '_path', conf_path() . '/files/reports');
}
$default_directory = rtrim($report_path, '/');
$directories = module_invoke_all('forena_report_directory');
foreach ($directories as $dir) {
$this->includes[] = rtrim($dir, '/');
}
parent::__construct($default_directory, $directories, array(
'frx',
'skinfo',
'css',
'js',
));
}
public function allReports() {
global $language;
$reports = array();
$this
->validateAllCache();
$data = $this
->getCache('frx');
$def_language = language_default('language');
if ($data) {
foreach ($data as $base_name => $obj) {
if ($obj->cache) {
if ($obj->cache['language'] != 'en') {
$rpt_name = substr($base_name, strlen($obj->cache['language']) + 1);
}
else {
$rpt_name = $base_name;
}
if ($obj->cache['language'] == $language->language) {
$reports[$rpt_name] = $obj;
}
elseif ($obj->cache['language'] == $def_language && (!isset($reports[$rpt_name]) || $reports[$rpt_name]->cache['language'] == 'en')) {
$reports[$rpt_name] = $obj;
}
elseif ($obj->cache['language'] == 'en' && !isset($reports[$rpt_name])) {
$reports[$rpt_name] = $obj;
}
}
}
}
uasort($reports, '\\Drupal\\forena\\File\\ReportFile::reportCompare');
return $reports;
}
public static function reportCompare($a, $b) {
$c = strnatcasecmp($a->cache['category'], $b->cache['category']);
if (!$c) {
$c = strnatcasecmp($a->cache['title'], $b->cache['title']);
}
return $c;
}
public static function reportTitleCompare($a, $b) {
$c = strnatcasecmp($a['title'], $b['title']);
return $c;
}
public function getReportCacheInfo($name) {
global $language;
$this
->validateAllCache();
$data = $this
->getCache('frx');
if ($language->language != 'en') {
$lang = $language->language;
$name = "{$lang}/{$name}";
}
return @$data[$name];
}
public function menuReports() {
global $language;
$this
->validateAllCache();
$data = $this
->getCache('frx');
$reports = array();
foreach ($data as $base_name => $obj) {
if ($obj->cache && isset($obj->cache['menu']['path']) && ($obj->cache['language'] == $language->language || $obj->cache['language'] == 'en' && !isset($obj->cache['menu']['path']))) {
if ($obj->cache['language'] != 'en') {
$obj->name = substr($base_name, 3);
}
else {
$obj->name = $base_name;
}
$reports[$obj->cache['menu']['path']] = $obj;
}
}
return $reports;
}
public function reportsByCategory($categories = array()) {
global $language;
$this
->validateAllCache();
$data = $this
->allReports();
$reports = array();
if ($data) {
foreach ($data as $base_name => $obj) {
if ($obj->cache && @$obj->cache['category'] && !$obj->cache['hidden']) {
$cache = $obj->cache;
if (!$categories || array_search($cache['category'], $categories) !== FALSE) {
$access = TRUE;
if (@$cache['access']) {
$access = FALSE;
foreach ($cache['access'] as $provider => $callbacks) {
if (user_access('access ' . $provider . ' data')) {
foreach ($callbacks as $callback => $args) {
if ($callback) {
foreach ($args as $arg) {
if (function_exists($callback) && $arg) {
$a = $callback($arg);
if ($a) {
$access = TRUE;
}
}
else {
$access = TRUE;
}
}
}
else {
$access = TRUE;
}
}
}
}
}
if ($access) {
$reports[$cache['category']][] = array(
'title' => $cache['title'],
'category' => $cache['category'],
'report_name' => $base_name,
);
}
}
}
}
}
$sort = defined('SORT_NATURAL') ? SORT_NATURAL : SORT_REGULAR;
if ($reports) {
foreach ($reports as $category => $list) {
uasort($reports[$category], '\\Drupal\\forena\\File\\ReportFile::reportTitleCompare');
}
}
ksort($reports, $sort);
return $reports;
}
public function skins() {
$this
->validateAllCache();
$this
->getCache('skinfo');
$skins = array();
if (isset($this->cache['skinfo'])) {
foreach ($this->cache['skinfo'] as $name => $obj) {
$skins[$name] = isset($obj->cache['name']) ? $obj->cache['name'] : $name;
}
}
return $skins;
}
public function buildCache($ext, $base_name, &$object) {
switch ($ext) {
case 'frx':
$lang = 'en';
if (module_exists('locale')) {
@(list($tlang, $tname) = explode('/', $base_name, 2));
if (array_key_exists($tlang, language_list())) {
$lang = $tlang;
}
}
try {
$r_xml = file_get_contents($object->file);
$r = @new Report($r_xml);
} catch (Exception $e) {
}
if (!$r->rpt_xml) {
$s = t('Unable to load Report %s', array(
'%s' => $object->file,
));
if (user_access('design any report')) {
drupal_set_message($s, 'error', FALSE);
}
}
$cache = isset($r->rpt_xml) ? forena_load_cache($r->rpt_xml) : NULL;
if ($r->rpt_xml) {
$cache['title'] = $r->title;
$cache['language'] = $lang;
$cache['category'] = $r->category;
$cache['hidden'] = @$r->options['hidden'];
}
$object->cache = $cache;
if ($r) {
$r
->__destruct();
}
unset($r);
break;
case 'skinfo':
$object->cache = drupal_parse_info_format(file_get_contents($object->file));
break;
}
}
}