View source
<?php
function forena_menu() {
$items = array();
$items['admin/settings/forena'] = array(
'page callback' => 'drupal_get_form',
'page arguments' => array(
'forena_settings',
),
'title' => 'Forena Reports',
'description' => t('Tell Forena where to store report files and how users should access them.'),
'access arguments' => array(
'administer forena reports',
),
'type' => MENU_NORMAL_ITEM,
'file' => 'forena.admin.inc',
);
$items['admin/settings/forena/general'] = array(
'title' => 'General',
'type' => MENU_DEFAULT_LOCAL_TASK,
'file' => 'forena.admin.inc',
'weight' => -10,
);
$items['admin/settings/forena/formats'] = array(
'page callback' => 'drupal_get_form',
'page arguments' => array(
'forena_doc_formats_settings',
),
'title' => t('Document Types'),
'access arguments' => array(
'administer forena reports',
),
'type' => MENU_LOCAL_TASK,
'file' => 'forena.admin.inc',
);
$items['reports/%'] = array(
'page callback' => 'forena_report',
'page arguments' => array(
1,
),
'title' => t('Reports'),
'access arguments' => array(
'access content',
),
'type' => MENU_CALLBACK,
'file' => 'forena.common.inc',
);
$items['reports/%/view'] = array(
'title' => t('View'),
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
'file' => 'forena.common.inc',
'access arguments' => array(
'design any report',
),
);
$items['reports/%/params'] = array(
'page callback' => 'forena_admin_params',
'page arguments' => array(
1,
),
'title' => t('Params'),
'access arguments' => array(
'design any report',
),
'type' => MENU_LOCAL_TASK,
);
$items['reports/%/layout'] = array(
'title' => 'Layout',
'page callback' => 'forena_layout_report',
'page arguments' => array(
1,
),
'access arguments' => array(
'design any report',
),
'description' => t('Edit the layout of your report'),
'type' => MENU_LOCAL_TASK,
'file' => 'forena.common.inc',
);
$items['reports/%/data'] = array(
'title' => 'Data',
'page callback' => 'forena_data_block',
'access arguments' => array(
'design any report',
),
'description' => t('Add a data block to your report'),
'type' => MENU_LOCAL_TASK,
);
$items['reports/%/fields'] = array(
'title' => 'Fields',
'page callback' => 'forena_fields_report',
'page arguments' => array(
1,
),
'access arguments' => array(
'design any report',
),
'description' => t('Edit the fields of your report'),
'type' => MENU_LOCAL_TASK,
);
$items['reports/%/delete'] = array(
'title' => 'Delete Report',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'forena_delete_form',
),
'access arguments' => array(
'delete report',
),
'type' => MENU_NORMAL_ITEM,
'file' => 'forena.admin.inc',
);
$items['reports/add'] = array(
'title' => t('Add Report'),
'page callback' => 'forena_add_report',
'page arguments' => array(
1,
),
'access arguments' => array(
'create any report',
),
'description' => t('Create a new report'),
'type' => MENU_NORMAL_ITEM,
);
$items['report_doc/%'] = array(
'page callback' => 'forena_report',
'page arguments' => array(
1,
),
'access arguments' => array(
'access content',
),
'type' => MENU_CALLBACK,
'file' => 'forena.common.inc',
);
$items['forena'] = array(
'page callback' => 'forena_user_reports',
'page arguments' => array(),
'title' => t('My Reports'),
'access arguments' => array(
'list reports',
),
'type' => MENU_NORMAL_ITEM,
);
$items['forena/xml/%'] = array(
'page callback' => 'forena_block_xml',
'page arguments' => array(
2,
),
'access callback' => TRUE,
'file' => 'forena.common.inc',
'type' => MENU_CALLBACK,
);
$items['forena/fields/format/autocomplete'] = array(
'page callback' => 'forena_fields_format_autocomplete',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
$items['forena/data_block/autocomplete'] = array(
'page callback' => 'forena_data_block_autocomplete',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
$items['forena/categories/autocomplete'] = array(
'page callback' => 'forena_categories_autocomplete',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
function forena_block($op = 'list', $delta = 0, $edit = array()) {
if ($op == 'list') {
$blocks[0] = array(
'info' => t('My reports'),
'status' => 1,
'region' => 'right',
'visibility' => 1,
'pages' => 'forena',
);
return $blocks;
}
if ($op == 'view') {
require_once 'forena.common.inc';
switch ($delta) {
case 0:
$block = array(
'subject' => 'My Reports',
'content' => forena_my_reports_block(),
);
break;
}
return $block;
}
}
function forena_categories_autocomplete($string = '') {
require_once 'forena.admin.inc';
$categories = forena_get_categories($string);
print drupal_json($categories);
}
function forena_data_block_autocomplete($string = '') {
require_once 'forena.admin.inc';
$data_blocks = forena_user_data_blocks($string);
if ($data_blocks) {
$temp = array_values($data_blocks);
$data_blocks = array_combine($temp, $temp);
}
else {
$data_blocks = array();
}
print drupal_json($data_blocks);
}
function forena_fields_format_autocomplete($string = '') {
require_once 'forena.common.inc';
$matches = array();
$formats = forena_supported_formats();
if ($string == "*") {
$matches = $formats;
print drupal_json($matches);
return;
}
if ($formats && $string) {
foreach ($formats as $name => $value) {
if (strpos(strtolower($name), strtolower($string)) !== FALSE || strpos(strtolower($value), strtolower($string)) !== FALSE) {
$matches[$name] = $value;
}
}
}
print drupal_json($matches);
}
function forena_layout_report() {
require_once 'forena.admin.inc';
return drupal_get_form('forena_layout_form');
}
function forena_admin_params() {
require_once 'forena.admin.inc';
return drupal_get_form('forena_admin_params_form');
}
function forena_add_report($new_report) {
require_once 'forena.admin.inc';
return drupal_get_form('forena_layout_form', $new_report);
}
function forena_data_block() {
require_once 'forena.admin.inc';
return drupal_get_form('forena_data_block_form');
}
function forena_parameters_report() {
require_once 'forena.admin.inc';
$desc = forena_report_desc();
$name = $desc['name'];
$filename = $desc['filename'];
$format = $desc['format'];
$report = forena_get_report($name);
if ($report) {
$r = forena_report_object();
return drupal_get_form('forena_parameters_form');
}
else {
drupal_not_found();
}
}
function forena_fields_report() {
require_once 'forena.admin.inc';
return drupal_get_form('forena_fields_form');
}
function forena_perm() {
$perms = array(
'administer forena reports',
'list reports',
'create any report',
'design any report',
'delete report',
'perform email merge',
);
return $perms;
}
function forena_block_xml($block_name = '') {
$parms = $_GET;
unset($parms['q']);
$block_name = str_replace('.', '/', $block_name);
$xml = forena_xml($block_name, $parms);
if (is_object($xml)) {
header('Content-Type: text/xml');
print $xml
->asXML();
}
}
function forena_xml($block_name, $parms = array()) {
require_once 'forena.common.inc';
$xml = forena_invoke_data_provider($block_name, $parms);
return $xml;
}
function forena_user_reports() {
require_once 'forena.common.inc';
$reports = forena_get_user_reports();
if (!$reports) {
$output = 'No Reports Found';
}
$report_repos = variable_get('forena_path', 'reports');
foreach ($reports as $category => $reports) {
$links .= '<li><a href="#' . urlencode($category) . '">' . $category . '</a></li> ';
$output .= '<h3><a name="' . urlencode($category) . '"/>' . $category . '</h3>';
$output .= '<ul>';
foreach ($reports as $r) {
$output .= '<li>' . l($r['title'], $report_repos . '/' . str_replace('/', '.', $r['report_name'])) . '</li>';
}
$output .= '</ul>';
}
return $output;
}
function forena_report($name_in, $parms = array(), $print = TRUE) {
require_once 'forena.common.inc';
$desc = forena_report_desc($name_in);
$name = $desc['name'];
$format = $desc['format'];
$filename = $desc['filename'];
if (!$parms) {
$parms = $_GET;
}
else {
$parms = (array) $parms;
}
unset($parms['q']);
if ($name) {
$r = forena_get_report($name, $parms);
if ($r) {
$r_params = $r->parameters;
if ($r_params) {
foreach ($r_params as $key => $parm) {
if (!$parms[$key] && $parm['value']) {
$parms[$key] = $parm['value'];
$reload_params = TRUE;
}
if (!$parms[$key] && strcmp($parm['require'], "1") == 0 && !$parm['value']) {
$missing_parms = TRUE;
}
}
}
if ($reload_params) {
$r = forena_get_report($name, $parms);
}
$r_parms = $r->parameters;
$form = $r->options['form'];
$rpt_xml = $r->rpt_xml;
if (!$form) {
$form = variable_get('forena_default_form', 'letter');
}
$q;
$title = $r->title;
if (!$missing_parms) {
$output .= $r
->render($format);
$css_files = forena_report_css($desc, $form, $format);
}
if ($format) {
$header = '<h1>' . $title . '</h1>';
$output = $header . $output;
$css_text = '';
$r_text = '';
if ($css_files) {
foreach ($css_files as $css_file) {
$css_text .= file_get_contents($css_file);
}
}
$options = array(
'css' => $css_text,
'docname' => str_replace(' ', '_', $title),
'xml' => $r_text,
'title' => $title[0],
);
$output = forena_generate_doc($format, $output, $options, $print);
return $output;
}
else {
if ($r_parms) {
$output = drupal_get_form('forena_parameters_form') . $output;
}
$title = $r->title;
drupal_set_title($title);
foreach ($parms as $key => $value) {
$q .= "&" . $key . '=' . $value;
}
$q = trim($q, '&');
$rpt_xml = $r->rpt_xml;
$nodes = $rpt_xml
->xpath('//frx:docgen/frx:doc');
$div .= '<div class="doclinks">';
$default_doctypes = variable_get('forena_doc_defaults', array());
$base = base_path();
if (!$missing_parms) {
if (!$nodes) {
foreach ($default_doctypes as $value) {
if (is_object(forena_get_doctypes($value))) {
$div .= '<a class="doclinks" href="' . $base . 'report_doc/' . $name_in . '.' . $value . '?' . $q . '">' . strtoupper($value) . '</a>';
}
}
}
else {
$doctypes = forena_supported_doctypes();
foreach ($nodes as $value) {
$arr = $value
->attributes();
$type = (string) $arr['type'];
if ($doctypes[$type]) {
if (is_object(forena_get_doctypes($type))) {
$div .= '<a class="doclinks" href="' . $base . 'report_doc/' . $name_in . '.' . $type . '?' . $q . '">' . strtoupper($type) . '</a>';
}
}
}
}
$div .= '</div>';
$output = $div . '<div class="forena-report">' . $output . '</div>';
}
$path = drupal_get_path('module', 'forena');
drupal_add_css($path . '/forena.css', 'module');
if ($css_files) {
foreach ($css_files as $css_file) {
drupal_add_css($css_file, 'module');
}
}
$output = check_markup($output, variable_get('forena_input_format', FILTER_FORMAT_DEFAULT), FALSE);
return $output;
}
}
else {
require_once 'forena.admin.inc';
forena_delete_report($name, FALSE);
drupal_not_found();
}
}
else {
drupal_not_found();
}
}
function forena_render_report($report, $format = '', $data = '', $options = array(), $print = TRUE) {
require_once 'forena.common.inc';
$o = forena_report_object($report, $data);
$output = $o
->render($format);
if ($format) {
$output = forena_generate_doc($format, $output, $options, $print);
return $output;
}
else {
return $output;
}
}
function forena_forena_plugins() {
$plugins[] = array(
'file' => 'plugins/FrxPDO.inc',
'class' => 'FrxPDO',
);
$plugins[] = array(
'file' => 'plugins/FrxOracle.inc',
'class' => 'FrxOracle',
);
$plugins[] = array(
'file' => 'plugins/FrxDrupal.inc',
'class' => 'FrxDrupal',
);
$plugins[] = array(
'file' => 'plugins/FrxFiles.inc',
'class' => 'FrxFiles',
);
$plugins[] = array(
'file' => 'plugins/FrxPostgres.inc',
'class' => 'FrxPostgres',
);
$plugins[] = array(
'file' => 'plugins/FrxMSSQL.inc',
'class' => 'FrxMSSQL',
);
return $plugins;
}
function forena_forena_controls() {
$controls[] = array(
'file' => 'plugins/FrxControls.inc',
'class' => 'FrxControls',
);
return $controls;
}
function forena_report_path() {
$report_path = variable_get('forena_report_repos', '');
if (!$report_path) {
$report_path = drupal_get_path('module', 'forena') . '/repos/reports';
}
return trim($report_path, '/');
}
function forena_current_user_uid() {
global $user;
return $user->uid;
}
function forena_current_user_name() {
global $user;
return $user->name;
}
function forena_filter($op, $delta = 0, $format = -1, $text = '') {
if ($op == 'list') {
return array(
0 => t('Insert Report'),
);
}
switch ($delta) {
case 0:
switch ($op) {
case 'description':
return t('Inserts a forena report');
case 'prepare':
return $text;
case 'process':
return _forena_filter_process($text);
case 'no cache':
return TRUE;
}
}
}
function _forena_filter_process($text = '') {
require_once 'forena.admin.inc';
$in_parms = array();
$in_parms = array_merge((array) $_GET, (array) $parms);
if (preg_match_all("/\\[report?:?([^\\]]+)\\]/i", $text, $match)) {
foreach ($match[1] as $idx => $value) {
$parms = array();
list($report_name, $parmsStr) = explode(':', $value);
$pairs = split(',', $parmsStr);
if ($pairs) {
foreach ($pairs as $pair) {
list($key, $value) = explode('=', $pair);
$parms[$key] = $value;
}
}
$parms = array_merge($parms, $in_parms);
$output = '';
$r = forena_get_report($report_name, $in_parms);
if ($r) {
$output .= $r
->render(null);
}
$find[] = $match[0][$idx];
$replace[] = $output;
}
return str_replace($find, $replace, $text);
}
return $text;
}
function forena_mail($key, &$message, $parms) {
switch ($key) {
case 'mailmerge':
$message['subject'] = $parms['subject'];
$body = $parms['body'];
$message['body'] = '<html><body>' . $body . '</body></html>';
$message['headers']['MIME-Version'] = '1.0';
$message['headers']['Content-Type'] = 'text/html;charset=utf-8';
break;
}
}