forena.module 40.4 KB
Newer Older
metzlerd's avatar
metzlerd committed
1
<?php
metzlerd's avatar
metzlerd committed
2
3
4

require_once 'Frx.inc';

5
/**
metzlerd's avatar
metzlerd committed
6
 * Implementation of hook_menu.
7
8
9
 *
 * @return array
 */
metzlerd's avatar
metzlerd committed
10
11
function forena_menu() {
  $items = array();
12
13
14

  // First build menu items.
  Frx::Menu()->addMenuItems($items);
15

16
  $items['admin/structure/forena'] = array(
17
18
19
    'type' => MENU_NORMAL_ITEM,
    'title' => 'Forena Reports',
    'description' => 'Build reports based on data in your sites databases.',
20
21
22
23
24
25
26
27
28
    'page callback' => 'forena_admin_reports',
    'access arguments' => array('design any report'),
    'file' => 'forena.admin.inc',
  );

  $items['admin/structure/forena/reports'] = array(
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'title' => 'Reports',
    'description' => 'Build reports based on data in your sites databases.',
29
30
31
32
33
    'page callback' => 'forena_admin_reports',
    'access arguments' => array('design any report'),
    'file' => 'forena.admin.inc',
  );

metzlerd's avatar
metzlerd committed
34
  $items['admin/config/content/forena'] = array(
metzlerd's avatar
metzlerd committed
35
    'page callback' => 'drupal_get_form',
metzlerd's avatar
metzlerd committed
36
37
38
    'page arguments' => array('forena_settings'),
    'title' => 'Forena Reports',
    'description' => 'Tell Forena where to store report files and how users should access them.',
metzlerd's avatar
metzlerd committed
39
    'access arguments' => array('administer forena reports'),
metzlerd's avatar
metzlerd committed
40
41
    'type' => MENU_NORMAL_ITEM,
    'file' => 'forena.admin.inc',
42
  );
metzlerd's avatar
metzlerd committed
43

44
  $items['admin/config/content/forena/general'] = array(
metzlerd's avatar
metzlerd committed
45
46
47
48
    'title' => 'General',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'file' => 'forena.admin.inc',
    'weight' => -10,
49
  );
metzlerd's avatar
metzlerd committed
50

51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
  $items['admin/config/content/forena/data/configure'] = array(
    'title' => 'Configure data source',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('forena_data_settings_edit'),
    'type' => MENU_CALLBACK,
    'file' => 'forena.admin.inc',
    'access arguments' => array('administer forena reports'),
  );

  $items['admin/config/content/forena/data/add'] = array(
    'title' => 'Add data source',
    'type' => MENU_CALLBACK,
    'page callback' => 'drupal_get_form',
    'page arguments' => array('forena_data_settings_edit'),
    'file' => 'forena.admin.inc',
    'access arguments' => array('administer forena reports'),
  );

  $items['admin/config/content/forena/data'] = array(
    'title' => 'Data',
    'page callback' => 'forena_data_settings',
    'type' => MENU_LOCAL_TASK,
    'file' => 'forena.admin.inc',
    'access arguments' => array('administer forena reports'),
  );

metzlerd's avatar
metzlerd committed
77
  $items['admin/config/content/forena/formats']  = array(
78
    'page callback' => 'drupal_get_form',
metzlerd's avatar
metzlerd committed
79
80
    'page arguments' => array('forena_doc_formats_settings'),
    'title' => 'Document Types',
81
    'access arguments' => array('administer forena reports'),
metzlerd's avatar
metzlerd committed
82
83
    'type' => MENU_LOCAL_TASK,
    'file' => 'forena.admin.inc',
84
  );
metzlerd's avatar
metzlerd committed
85

86
  $items['reports/%'] = array(
metzlerd's avatar
metzlerd committed
87
    'page callback' => 'forena_report',
metzlerd's avatar
metzlerd committed
88
89
90
91
92
    'page arguments' => array(1),
    'title' => 'Reports',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
    'file' => 'forena.common.inc',
metzlerd's avatar
metzlerd committed
93
  );
metzlerd's avatar
metzlerd committed
94

95
  $items['report_doc/%'] = array(
96
97
98
99
100
    'page callback' => 'forena_report',
    'page arguments' => array(1),
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
    'file' => 'forena.common.inc',
metzlerd's avatar
metzlerd committed
101
  );
102

103

104
  $items['reports/%/view'] = array(
metzlerd's avatar
metzlerd committed
105
106
107
108
109
    'title' => 'View',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
    'file' => 'forena.common.inc',
    'access arguments' => array('design any report'),
metzlerd's avatar
metzlerd committed
110
  );
metzlerd's avatar
metzlerd committed
111

metzlerd's avatar
metzlerd committed
112
  $items['reports/%/edit'] = array(
113
     'title' => 'Edit',
114
     'page callback' => 'drupal_get_form',
metzlerd's avatar
metzlerd committed
115
     'page arguments' => array( 'forena_general_form',1),
116
117
118
     'access arguments' => array('design any report'),
     'description' => 'Edit the layout of your report',
     'type' => MENU_LOCAL_TASK,
119
     'file' => 'forena.admin.inc',
metzlerd's avatar
metzlerd committed
120
  );
121

metzlerd's avatar
metzlerd committed
122
123
  if (module_exists('locale')) {
    $items['reports/%/translations'] = array(
124
125
       'page callback' => 'forena_report_translations',
       'page arguments' => array(1),
126
       'title' => 'Translate',
127
128
129
       'file' => 'forena.admin.inc',
       'access arguments' => array('design any report'),
       'type' => MENU_LOCAL_TASK,
metzlerd's avatar
metzlerd committed
130
131
    );
  }
132
  $items['reports/%/edit/params'] = array(
133
134
    'page callback' => 'drupal_get_form',
    'page arguments' => array('forena_admin_params_form', 1),
135
    'title' => 'Parameters',
136
    'file' => 'forena.admin.inc',
137
138
    'access arguments' => array('design any report'),
    'type' => MENU_LOCAL_TASK,
metzlerd's avatar
metzlerd committed
139
  );
140

141
142
  $items['reports/%/edit/params/add'] = array(
    'page callback' => 'drupal_get_form',
143
    'page arguments' => array('forena_add_param_form', 1),
144
145
146
147
    'file' => 'forena.admin.inc',
    'title' => 'Parameters',
    'access arguments' => array('design any report'),
    'type' => MENU_CALLBACK,
metzlerd's avatar
metzlerd committed
148
  );
149

metzlerd's avatar
metzlerd committed
150
151
152
153
154
155
156
157
158
159
  $items['reports/%/edit/general'] = array(
    'title' => 'General',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('forena_general_form',1),
    'access arguments' => array('design any report'),
    'description' => 'General Report Properties',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -1,
    'file' => 'forena.admin.inc',
  );
160

metzlerd's avatar
metzlerd committed
161
  $items['reports/%/edit/layout'] = array(
162
     'title' => 'Layout',
163
164
     'page callback' => 'drupal_get_form',
     'page arguments' => array('forena_layout_form',1),
metzlerd's avatar
metzlerd committed
165
166
     'access arguments' => array('design any report'),
     'description' => 'Edit the layout of your report',
metzlerd's avatar
metzlerd committed
167
168
     'type' => MENU_LOCAL_TASK,
     'weight' => 1,
169
     'file' => 'forena.admin.inc',
metzlerd's avatar
metzlerd committed
170
  );
metzlerd's avatar
metzlerd committed
171

metzlerd's avatar
metzlerd committed
172
  $items['reports/%/edit/data'] = array(
173
     'title' => 'Data',
174
175
     'page callback' => 'drupal_get_form',
     'page arguments' => array('forena_data_block_form', 1),
metzlerd's avatar
metzlerd committed
176
177
     'access arguments' => array('design any report'),
     'description' => 'Add a data block to your report',
178
     'file' => 'forena.admin.inc',
metzlerd's avatar
metzlerd committed
179
     'type' => MENU_LOCAL_TASK,
180

metzlerd's avatar
metzlerd committed
181
  );
metzlerd's avatar
metzlerd committed
182

metzlerd's avatar
metzlerd committed
183
  $items['reports/%/edit/data/add'] = array(
184
185
186
     'title' => 'Data',
     'file' => 'forena.admin.inc',
     'page callback' => 'drupal_get_form',
187
     'page arguments' => array('forena_add_data_block_form', 1),
188
189
190
     'access arguments' => array('design any report'),
     'description' => 'Add a data block to your report',
     'type' => MENU_CALLBACK,
metzlerd's avatar
metzlerd committed
191

metzlerd's avatar
metzlerd committed
192
193
  );
  $items['reports/%/edit/fields'] = array(
metzlerd's avatar
metzlerd committed
194
     'title' => 'Fields',
195
196
     'page callback' => 'drupal_get_form',
     'page arguments' => array('forena_fields_form',1),
metzlerd's avatar
metzlerd committed
197
198
     'access arguments' => array('design any report'),
     'description' => 'Edit the fields of your report',
199
     'file' => 'forena.admin.inc',
metzlerd's avatar
metzlerd committed
200
     'type' => MENU_LOCAL_TASK,
metzlerd's avatar
metzlerd committed
201
  );
metzlerd's avatar
metzlerd committed
202

metzlerd's avatar
metzlerd committed
203
  $items['reports/%/edit/format'] = array(
204
     'title' => 'Format',
205
206
     'page callback' => 'drupal_get_form',
     'page arguments' => array('forena_format_form', 1),
207
     'access arguments' => array('design any report'),
208
     'file' => 'forena.admin.inc',
209
210
     'description' => 'Style and document options',
     'type' => MENU_LOCAL_TASK,
metzlerd's avatar
metzlerd committed
211
  );
212

metzlerd's avatar
metzlerd committed
213
  $items['reports/%/delete'] = array(
214
     'title' => 'Remove Custom Report',
metzlerd's avatar
metzlerd committed
215
     'page callback' => 'drupal_get_form',
216
     'page arguments' => array('forena_delete_form', 1),
metzlerd's avatar
metzlerd committed
217
218
     'access arguments' => array('delete report'),
     'type' => MENU_NORMAL_ITEM,
metzlerd's avatar
metzlerd committed
219
     'file' => 'forena.admin.inc',
metzlerd's avatar
metzlerd committed
220
  );
metzlerd's avatar
metzlerd committed
221

metzlerd's avatar
metzlerd committed
222
  $items['reports/add'] = array(
223
     'title' => 'Create Report',
224
225
     'page callback' => 'drupal_get_form',
     'page arguments' => array('forena_add_report_form'),
metzlerd's avatar
metzlerd committed
226
227
     'access arguments' => array('create any report'),
     'description' => 'Create a new report',
228
     'file' => 'forena.admin.inc',
metzlerd's avatar
metzlerd committed
229
     'type' => MENU_NORMAL_ITEM,
metzlerd's avatar
metzlerd committed
230
231
  );
  $items['reports/%/add'] = array(
232
     'title' => 'Create Report',
233
234
     'page callback' => 'drupal_get_form',
     'page arguments' => array('forena_add_report_form', 1),
235
236
     'access arguments' => array('create any report'),
     'description' => 'Create a new report from a template',
237
     'file' => 'forena.admin.inc',
238
     'type' => MENU_CALLBACK,
metzlerd's avatar
metzlerd committed
239
  );
240

metzlerd's avatar
metzlerd committed
241
  $items['reports/%/translate'] = array(
242
243
244
     'title' => 'Translate Report',
     'page callback' => 'drupal_get_form',
     'page arguments' => array('forena_create_trans_form',1),
245
246
     'access arguments' => array('create any report'),
     'description' => 'Create a new report translation',
247
     'file' => 'forena.admin.inc',
248
     'type' => MENU_CALLBACK,
metzlerd's avatar
metzlerd committed
249
  );
metzlerd's avatar
metzlerd committed
250

251
252
  $items['forena'] = array(
    'page callback' => 'forena_user_reports',
metzlerd's avatar
metzlerd committed
253
254
255
    'page arguments' => array(),
    'title' => 'My Reports',
    'access arguments' => array('list reports'),
256
    'type' => MENU_NORMAL_ITEM,
metzlerd's avatar
metzlerd committed
257
  );
metzlerd's avatar
metzlerd committed
258

metzlerd's avatar
metzlerd committed
259
  $items['forena/xml/%'] = array(
260
261
   	'page callback' => 'forena_block_xml',
    'page arguments' => array(2),
262
   	'access arguments' => array('access forena block xml'),
263
264
    'file' => 'forena.common.inc',
   	'type' => MENU_CALLBACK,
metzlerd's avatar
metzlerd committed
265
  );
266

metzlerd's avatar
metzlerd committed
267
  $items['forena/fields/format/autocomplete'] = array(
metzlerd's avatar
metzlerd committed
268
269
270
     'page callback' => 'forena_fields_format_autocomplete',
     'access callback' => TRUE,
     'type' => MENU_CALLBACK,
metzlerd's avatar
metzlerd committed
271
  );
metzlerd's avatar
metzlerd committed
272

metzlerd's avatar
metzlerd committed
273
  $items['forena/data_block/autocomplete'] = array(
metzlerd's avatar
metzlerd committed
274
275
276
     'page callback' => 'forena_data_block_autocomplete',
     'access callback' => TRUE,
     'type' => MENU_CALLBACK,
metzlerd's avatar
metzlerd committed
277
  );
metzlerd's avatar
metzlerd committed
278

metzlerd's avatar
metzlerd committed
279
  $items['forena/reports/autocomplete'] = array(
280
281
282
     'page callback' => 'forena_reports_autocomplete',
     'access callback' => TRUE,
     'type' => MENU_CALLBACK,
metzlerd's avatar
metzlerd committed
283
  );
284
285


metzlerd's avatar
metzlerd committed
286
  $items['forena/categories/autocomplete'] = array(
metzlerd's avatar
metzlerd committed
287
288
289
     'page callback' => 'forena_categories_autocomplete',
     'access callback' => TRUE,
     'type' => MENU_CALLBACK,
metzlerd's avatar
metzlerd committed
290
  );
metzlerd's avatar
metzlerd committed
291
292

  return $items;
metzlerd's avatar
metzlerd committed
293
}
metzlerd's avatar
metzlerd committed
294

295
296
297
298
/**
 * Implements hook_admin_paths_alter().
 */
function forena_admin_paths_alter(&$paths) {
metzlerd's avatar
metzlerd committed
299
300
301
  $paths['reports/*/*'] = TRUE;
  $paths['reports/*/view'] = FALSE;
  $paths['reports/add'] = TRUE;
302
303
304

}

metzlerd's avatar
metzlerd committed
305

306
307
308
309
310
311
312
313
314
315
316
function forena_library_file($library) {
  $libraries = array(
      'dataTables' => 'dataTables/media/js/jquery.dataTables.min.js',
      'mpdf' => 'mpdf/mpdf.php',
      'SVGGraph' => 'SVGGraph/SVGGraph.php',
      'prince' => 'prince/prince.php'
  );
  $path = isset($libraries[$library]) && file_exists('sites/all/libraries/' . $libraries[$library]) ? 'sites/all/libraries/' . $libraries[$library] : '';
  return $path;
}

metzlerd's avatar
metzlerd committed
317
/**
metzlerd's avatar
metzlerd committed
318
 * Implementation of hook_block_info
metzlerd's avatar
metzlerd committed
319
 */
metzlerd's avatar
metzlerd committed
320
function forena_block_info() {
metzlerd's avatar
metzlerd committed
321
  $blocks['forena_reports'] = array('info' => t('My reports'),
metzlerd's avatar
metzlerd committed
322
      'status' => 1,
metzlerd's avatar
metzlerd committed
323
      'region' => 'sidebar_first',
metzlerd's avatar
metzlerd committed
324
325
      'visibility' => 1,
      'pages' => 'forena',
metzlerd's avatar
metzlerd committed
326
327
  );
  return $blocks;
metzlerd's avatar
metzlerd committed
328
329
330
331
332
}
/**
 * Implementation of hook_block_view
 */
function forena_block_view($delta = 0) {
metzlerd's avatar
metzlerd committed
333

metzlerd's avatar
metzlerd committed
334
335
336
337
338
  // Inlcude the base library
  require_once('forena.common.inc');
  switch ($delta) {
    case 'forena_reports':
      $block = array('subject' => 'My Reports',
metzlerd's avatar
metzlerd committed
339
            'content' => forena_my_reports_block());
metzlerd's avatar
metzlerd committed
340
341
342
      break;
  }
  return $block;
metzlerd's avatar
metzlerd committed
343

metzlerd's avatar
metzlerd committed
344
345
}

346

metzlerd's avatar
metzlerd committed
347
348
/**
 * Auto complete for categories
metzlerd's avatar
metzlerd committed
349
 *
metzlerd's avatar
metzlerd committed
350
351
352
353
 */

function forena_categories_autocomplete($string='') {
  require_once('forena.admin.inc');
metzlerd's avatar
metzlerd committed
354
  $categories = @forena_get_categories($string);
metzlerd's avatar
metzlerd committed
355
  print drupal_json_output($categories);
metzlerd's avatar
metzlerd committed
356
357
358
359
}



metzlerd's avatar
metzlerd committed
360
361
362
363
364
/**
 * Auto complete for data blocks
 * @param $string
 * @return unknown_type
 */
365
function forena_data_block_autocomplete() {
metzlerd's avatar
metzlerd committed
366
  require_once('forena.admin.inc');
367
368
369
370
371
372
373
374
375
376
  $args = func_get_args();
  $search = implode('/', $args);
  if (strlen($search) < 2) {
    $search .= '*';
  }
  else {
    $search = "*$search*";
  }

  $data_blocks = Frx::DataFile()->userBlocks($search);
metzlerd's avatar
metzlerd committed
377
  if ($data_blocks) {
378
    $temp = array_keys($data_blocks);
metzlerd's avatar
metzlerd committed
379
    $data_blocks = array_combine($temp, $temp);
metzlerd's avatar
metzlerd committed
380
381
  }
  else {
metzlerd's avatar
metzlerd committed
382
    $data_blocks = array();
metzlerd's avatar
metzlerd committed
383
  }
metzlerd's avatar
metzlerd committed
384

metzlerd's avatar
metzlerd committed
385
  print drupal_json_output($data_blocks);
metzlerd's avatar
metzlerd committed
386
387
}

388
function forena_reports_autocomplete($string='', $string2='') {
metzlerd's avatar
metzlerd committed
389
  GLOBAL $language;
390
  $reports='';
391
392
393
  $link = '';
  // If we have two parameters.
  if ($string2) {
metzlerd's avatar
metzlerd committed
394
395
    $mode = $string;
    $string = $string2;
396
  }
397
398
  $string = '*' .$string .'*';
  $result = Frx::File()->allReports();
399
  foreach ($result as $row) {
400
401
    if (drupal_match_path($row->name, $string)) {
      $reports[$row->name] = $row->name . ' - ' . $row->cache['title'];
402
403
    }
  }
metzlerd's avatar
metzlerd committed
404
  return drupal_json_output($reports);
405
}
406
407

function forena_xml($block_name, $parms=array()) {
metzlerd's avatar
metzlerd committed
408
409
410
411
412
  include_once('forena.common.inc');
  drupal_alter('forena_parameters', $block_name, $parms );
  //now invoke the data provider with the correct params
  Frx::Data()->push($parms, 'parm');
  return Frx::RepoMan()->data($block_name);
413
414
}

metzlerd's avatar
metzlerd committed
415
416
417
418
419
420
/**
 * Auto complete for formats
 * @param $string
 * @return unknown_type
 */
function forena_fields_format_autocomplete($string='') {
metzlerd's avatar
metzlerd committed
421
422
  require_once('forena.common.inc');
  $matches = array();
metzlerd's avatar
metzlerd committed
423
  $formats = @FrxReportGenerator::instance()->supported_formats();
metzlerd's avatar
metzlerd committed
424
425
426

  if ($string == "*") {
    $matches = $formats;
metzlerd's avatar
metzlerd committed
427
    print drupal_json_output($matches);
metzlerd's avatar
metzlerd committed
428
429
430
431
432
433
434
435
    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;
    }
  }
metzlerd's avatar
metzlerd committed
436
  print drupal_json_output($matches);
metzlerd's avatar
metzlerd committed
437
}
438

439
440
/**
 *  Calls forena_parameter_form
441
442
 *  in forena.common.inc
 */
metzlerd's avatar
metzlerd committed
443
function forena_parameters_report() {
444
  require_once('forena.admin.inc');
445
446
447
  $m = Frx::Menu();
  $name = $m->name;
  $filename = $m->filename;
metzlerd's avatar
metzlerd committed
448
  $format = $m->format;
metzlerd's avatar
metzlerd committed
449
450
451
  $report = forena_get_report($name);
  if ($report) {
    $r = forena_report_object();
metzlerd's avatar
metzlerd committed
452
453
    $o = drupal_get_form('forena_parameters_form');
    return $o;
metzlerd's avatar
metzlerd committed
454
455
456
  }
  else {
    drupal_not_found();
457
458
459
  }
}

460
function forena_parameter_form($formid, &$form_state, $parameters, $attributes= array()) {
metzlerd's avatar
metzlerd committed
461
  $parms = $_GET;
462
  // Set menu parms
metzlerd's avatar
metzlerd committed
463
  $menu_parms = Frx::Data()->getContext('menu-parms');
464
465
  if ($menu_parms) $form_state['storage']['menu-parms'] = $menu_parms;
  // Set Descriptors
466
  $desc = FrxData::instance()->getContext('report');
467
468
  if ($desc) $form_state['storage']['desc'] = $desc;
  $desc = $form_state['storage']['desc'];
469
  $report_name = @$desc['name'];
470

471
  $collapse = isset($attributes['collapsed']) ? $attributes['collapsed'] : FALSE;
metzlerd's avatar
metzlerd committed
472
473
474
  if (isset($form_state['values'])) {
    $collapse=FALSE;
    $parms = array_merge($parms, $form_state['values']['params']);
metzlerd's avatar
metzlerd committed
475
476
477
478
479
480
    // In the case of ahah, we need to restore menu parameters from the form state.
    if (isset($form_state['storage']['menu-parms'])) {
      $menu_parms = $form_state['storage']['menu-parms'];
      $parms = array_merge($menu_parms,$parms);

    }
481
    drupal_alter('forena_parameters', $report_name,  $parms);
metzlerd's avatar
metzlerd committed
482
483
    Frx::Data()->push($parms, 'parm');
  }
484
485
486
487
  $template = @$attributes['template'];
  $collapsible = isset($attributes['collapsible']) ? $attributes['collapsible'] : TRUE;
  $title = isset($attributes['title']) ? $attributes['title'] : t('Parameters');
  $submit_value = isset($attributes['submit']) ? $attributes['submit'] : t('Submit');
488

metzlerd's avatar
metzlerd committed
489
490
  unset($parms['q']);
  $form = array();
metzlerd's avatar
metzlerd committed
491

492

metzlerd's avatar
metzlerd committed
493
  if ($parameters) {
494

495
    //drupal_alter('forena_parameters', $report_name,  $parms);
metzlerd's avatar
metzlerd committed
496
    if ($parameters) {
metzlerd's avatar
metzlerd committed
497

metzlerd's avatar
metzlerd committed
498
      $form['params'] = array(
metzlerd's avatar
metzlerd committed
499
          '#tree' => TRUE,
500
          '#title' => $title,
metzlerd's avatar
metzlerd committed
501
          '#type' => 'fieldset',
502
          '#collapsible' => $collapsible,
metzlerd's avatar
metzlerd committed
503
504
505
          '#collapsed' => $collapse,
          '#prefix' => '<div id="parameters-wrapper">',
          '#suffix' => '</div>',
metzlerd's avatar
metzlerd committed
506
507
508
      );

      foreach ($parameters as $node) {
metzlerd's avatar
metzlerd committed
509
510
        $add_null = FALSE;
        $list=array();
metzlerd's avatar
metzlerd committed
511
512
513
514
515
516
517
518
519
520
521
522
        $disabled = FALSE;
        $label = @(string)$node['label'];
        $id = @(string)$node['id'];
        $data_source = @(string)$node['data_source'];
        $data_field = @(string)$node['data_field'];
        $class = @(string)$node['class'];
        $type = @(string)$node['type'];
        $option_str = @(string)$node['options'];
        $options = array();
        if ($option_str) {
          parse_str($option_str, $options);
        }
metzlerd's avatar
metzlerd committed
523

metzlerd's avatar
metzlerd committed
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
        if (isset($parms[$id])) {
          $value = $parms[$id];
          $multi_value=(array)$parms[$id];
        }
        else {
          $value = @(string)$node['default'];
          $multi_value = array();
          if (strpos($value, '|')!==FALSE) $multi_value = explode('|', $value);
        }
        $desc =  @(string)$node['desc'];
        $label_field = @(string)$node['label_field'];

        @(strcmp((string)$node['require'], "1") == 0) ? $required = TRUE : $required = FALSE;
        $ctl_attrs = array();

539
540
541
542
543
544
        //returned values filtered against data_field attr.
        if ($data_source) {
          $list = Frx::RepoMan()->dataBlockParams($data_source, $data_field, $label_field);
          if (!$required && $add_null) $list = array('' => '') + $list;
        }

metzlerd's avatar
metzlerd committed
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
        //Determine the form element type to be displayed
        //If select or radios is chosen then begin a $list array for display values.
        $multiselect = FALSE;
        $ajax = FALSE;
        $add_null = FALSE;
        switch ($type) {
          case 'multiselect':
            $type = 'select';
            $multiselect = TRUE;
            $value = $multi_value;
            break;
          case 'multiselectajax':
            $type = 'select';
            $multiselect = TRUE;
            $value = $multi_value;
            $ajax = TRUE;
            break;
          case 'checkboxes':
            $value = $multi_value;
            break;
          case 'selectajax':
            $ajax = TRUE;
            $type = 'select';
            $add_null = TRUE;
            break;
          case 'select':
            $add_null = TRUE;
            break;
          case 'date_text':
          case 'date_select':
          case 'date_popup':
metzlerd's avatar
metzlerd committed
576
577
578
579
580
            $options['date_format'] = @$options['date_format'] ? $options['date_format'] : 'Y-m-d';
            $ctl_attrs['#date_format'] = $options['date_format'];
            if ($value){
              $datetime = @strtotime($value);
              if ($datetime) {
581
                $value = date('Y-m-d h:i', $datetime);
metzlerd's avatar
metzlerd committed
582
583
              }

metzlerd's avatar
metzlerd committed
584
            }
585
            $ctl_attrs['#forena_date_format'] = @$options['date_parm_format'] ? $options['date_parm_format'] : 'Y-m-d';
metzlerd's avatar
metzlerd committed
586

metzlerd's avatar
metzlerd committed
587
588
589
590
591
592
            if (@$options['date_year_range']) {
              $ctl_attrs['#date_year_range'] = $options['date_year_range'];
            }
            if (@$options['date_label_position']) {
              $ctl_attrs['#date_label_position'] = $options['date_label_position'];
            }
metzlerd's avatar
metzlerd committed
593

metzlerd's avatar
metzlerd committed
594
            $list=array();
metzlerd's avatar
metzlerd committed
595
596
597
598
599
            break;
          case 'checkbox':
            if (@$option_str['return_value']) {
              $ctl_attrs['#return_value'] = $options['return_value'];
            }
metzlerd's avatar
metzlerd committed
600
            $list=array();
metzlerd's avatar
metzlerd committed
601
602
603
604
            break;
          case 'radios':
            break;
          case 'hidden':
metzlerd's avatar
metzlerd committed
605
            $list=array();
metzlerd's avatar
metzlerd committed
606
607
608
            break;
          default:
            $type = 'textfield';
609
            $list = array();
metzlerd's avatar
metzlerd committed
610
        }
metzlerd's avatar
metzlerd committed
611

metzlerd's avatar
metzlerd committed
612
613
614
        if (isset($menu_parms[$id]) && $type=='textfield') {
          $disabled = TRUE;
        }
metzlerd's avatar
metzlerd committed
615

metzlerd's avatar
metzlerd committed
616
617
        //If a data_source attr was found then create an array of
        $form['params'][$id] = array(
metzlerd's avatar
metzlerd committed
618
619
620
            '#type' => $type,
            '#title' => ($label) ? t($label) : t($id),
            '#default_value' => $value,
621
            '#disabled' => $disabled,
metzlerd's avatar
metzlerd committed
622
623
            '#required' => $required,
            '#description' => t($desc),
metzlerd's avatar
metzlerd committed
624
        );
metzlerd's avatar
metzlerd committed
625

metzlerd's avatar
metzlerd committed
626
        $form['params'][$id] = array_merge($form['params'][$id], $ctl_attrs);
metzlerd's avatar
metzlerd committed
627

metzlerd's avatar
metzlerd committed
628
629
        if ($type == 'item') {
          $form['params'][$id]['#markup'] = $value;
metzlerd's avatar
metzlerd committed
630

metzlerd's avatar
metzlerd committed
631
        }
metzlerd's avatar
metzlerd committed
632

metzlerd's avatar
metzlerd committed
633
        if ($type == 'hidden') {
metzlerd's avatar
metzlerd committed
634
          $form['params'][$id]['#value'] = $value;
metzlerd's avatar
metzlerd committed
635
        }
metzlerd's avatar
metzlerd committed
636

metzlerd's avatar
metzlerd committed
637
638
639
        // Add class to parmeter form.
        if ($class) {
          $form['params'][$id]['#attributes'] = array(
metzlerd's avatar
metzlerd committed
640
              'class' => @explode(' ', $class),
metzlerd's avatar
metzlerd committed
641
642
          );
        }
metzlerd's avatar
metzlerd committed
643

metzlerd's avatar
metzlerd committed
644
645
646
        //if $list is not empty then push options
        //onto the array. options will cause an error for
        //textfield elements.
647
        if ($list || $type == 'select' || $type =='radios') {
648
649
          if ($add_null) {
            $prompt = @$options['prompt'];
metzlerd's avatar
metzlerd committed
650
            if (!$prompt) $prompt = $required ? '-' . t('select') .  '-' : '-' .t('none') . '-';
651
652
            $form['params'][$id]['#empty_option'] = $prompt ;
          }
metzlerd's avatar
metzlerd committed
653
654
655
          $form['params'][$id]['#options'] = $list;
          $form['params'][$id]['#multiple'] = $multiselect;
        }
metzlerd's avatar
metzlerd committed
656

metzlerd's avatar
metzlerd committed
657
658
        if ($ajax) {
          $form['params'][$id]['#ajax'] = array('callback' => 'forena_parameters_callback',
metzlerd's avatar
metzlerd committed
659
              'wrapper' => 'parameters-wrapper');
metzlerd's avatar
metzlerd committed
660
661
        }

metzlerd's avatar
metzlerd committed
662
663
664
665
666
667
668
      }

      if ($template) {
        $form['params']['#forena-template'] = $template;
        $form['params']['#theme'] = 'forena_fieldset_template';
        _forena_set_inline_theme($form['params']);
      }
669

metzlerd's avatar
metzlerd committed
670
      $form['params']['submit'] = array(
metzlerd's avatar
metzlerd committed
671
          '#type' => 'submit',
672
          '#value' => $submit_value,
metzlerd's avatar
metzlerd committed
673
      );
674

metzlerd's avatar
metzlerd committed
675
    }
metzlerd's avatar
metzlerd committed
676
677
    return $form;
  }
metzlerd's avatar
metzlerd committed
678
679
680
681
682
683
684
685
686
687
688
689
690
691


}


/**
 *
 * gets the values from the params form
 * redirects to the report page with the values in
 * the querystring.
 */
function forena_parameter_form_submit($form, &$form_state) {
  $values = $form_state['values'];
  unset($values['params']['submit']);
692

metzlerd's avatar
metzlerd committed
693
  if (isset($values['params'])) foreach ($values['params'] as $key => $value) {
694
695
696
697
698
699
700
    $ctl = $form['params'][$key];
    switch($ctl['#type']) {
      case 'date_popup':
      case 'date_select':
      case 'date_text':
        $datetime = @strtotime($value);
        if ($datetime) {
701
          $value = $values['params'][$key] = date($ctl['#forena_date_format'], $datetime);
702
703
704
705
706
        }
        break;

    }

metzlerd's avatar
metzlerd committed
707
    if (is_array($value)) {
708
      $values['params'][$key] = array();
metzlerd's avatar
metzlerd committed
709
      foreach ($value as $k => $val) {
710
711
        if ($val) {
          $values['params'][$key][] = $val;
metzlerd's avatar
metzlerd committed
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
        }
      }
    }
    else {
      if (strpos($value, '|')!==FALSE) {
        $values['params'][$key] = explode('|', $value);
      }
      elseif ($value==='' || $value===NULL) {
        unset($values['params'][$key]);
      }
    }
  }
  $form_state['redirect']= array($_GET['q'], array('query' => @$values['params']));
}

/**
 * Ajax form callback function
 * Enter description here ...
 * @param unknown_type $form
 * @param unknown_type $form_state
 */
function forena_parameters_callback($form, &$form_state) {
  return $form['params'];
}

737
738
739
740
741
/**
 * Implementation of hook_perm
 *
 * @return unknown
 */
metzlerd's avatar
metzlerd committed
742
function forena_permission() {
743
  $perms = array(
metzlerd's avatar
metzlerd committed
744
    'administer forena reports' => array('title' => t('Administer Forena Reports')),
745
    'access demo reports' => array('title' => t('Access Demo Reports')),
metzlerd's avatar
metzlerd committed
746
747
748
749
    'list reports' => array('title' => t('List reports')),
    'create any report' => array('title' => t('Create a report')),
    'design any report' => array('title' => t('Design reports')),
    'delete report' => array('title' => t('Delete reports')),
750
    'perform email merge' => array('title' => t('Peform email merge')),
751
    'access forena block xml' => array('title' => t('Access xml from data blocks directly'), 'description' => t('Useful for ajax calls to data blocks')),
metzlerd's avatar
metzlerd committed
752
  );
753
754
755
756
  foreach (Frx::RepoMan()->repositories as $repos => $conf) {
    $name = $conf['title'] ? $conf['title'] : $repos;
    $perms['access '. $repos . ' data'] = array('title' =>  'Access ' . $name . ' Data');
  }
metzlerd's avatar
metzlerd committed
757
  return $perms;
758
759
}

760

metzlerd's avatar
metzlerd committed
761

762
function forena_user_reports($category = '') {
metzlerd's avatar
metzlerd committed
763
  require_once('forena.common.inc');
metzlerd's avatar
metzlerd committed
764
  $output='';
765
  $reports = forena_get_user_reports($category);
metzlerd's avatar
metzlerd committed
766
  $report_repos = variable_get('forena_path', 'reports');
767
768
769
  if (!$reports) {
    $output = 'No Reports Found';
  }
metzlerd's avatar
metzlerd committed
770
  $links = '';
metzlerd's avatar
metzlerd committed
771
  foreach ($reports as $category => $reports) {
metzlerd's avatar
metzlerd committed
772
    $links .= '<li><a href="#' . urlencode($category) . '">' . $category . '</a></li> ';
773
    $output .= '<h3 id="' . urlencode($category) . '">' . $category . '</h3>';
metzlerd's avatar
metzlerd committed
774
775
776
    $output .= '<ul>';
    foreach ($reports as $r) {
      $output .= '<li>' . l($r['title'], $report_repos . '/' . str_replace('/', '.', $r['report_name'])) . '</li>';
777
    }
metzlerd's avatar
metzlerd committed
778
    $output .= '</ul>';
metzlerd's avatar
metzlerd committed
779
  }
metzlerd's avatar
metzlerd committed
780
  return $output;
metzlerd's avatar
metzlerd committed
781
782
}

783
784
785
/**
 * Tries to load data tables libary and other libraries that help.
 */
786
function forena_include_data_tables() {
metzlerd's avatar
metzlerd committed
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
  static $init = FALSE;
  if (!$init) {
    $init = TRUE;
    $lib = 'sites/all/libraries/dataTables/media/js/jquery.dataTables.min.js';
    if (file_exists($lib)) {
      drupal_add_js($lib);
    }
    $lib = 'sites/all/libraries/FixedColumns/media/js/FixedColumns.min.js';
    if (file_exists($lib)) {
      drupal_add_js($lib);
    }
    $lib = 'sites/all/libraries/FixedHeader/js/FixedHeader.min.js';
    if (file_exists($lib)) {
      drupal_add_js($lib);
    }
  }
803
804
}

805
806
807
808
809
810
/**
 * Load and render a report based on a drupal path.
 * In this function the arglist is used to get the full path to the report.
 *
 * @return unknown
 */
811
function forena_report($name_in, $parms = array(), $print = TRUE) {
812
  require_once('forena.common.inc');
metzlerd's avatar
metzlerd committed
813
  $desc = Frx::Menu()->parseURL($name_in);
814
815
  $time = @new DateTime($value);
  $desc['time'] = date_format($time, 'Y-m-d H:i:s');
816
  if (!$desc['exists']) {
817
818
    drupal_set_message(t('Report %s not found', array('%s' => $name_in)), 'error', FALSE);
    return '';
819
  }
metzlerd's avatar
metzlerd committed
820
821

  FrxData::instance()->setContext('cookie', $_COOKIE);
822
  FrxData::instance()->setContext('report', $desc);
823
  $report_name = $desc['name'];
824
  // Load dataTable plugin if possible.
metzlerd's avatar
metzlerd committed
825
  //forena_include_data_tables();
826
  $output = FrxReportGenerator::instance()->report($name_in, $parms, $print);
827
  $m_path = drupal_get_path('module', 'forena');
828
829
  if ($output) {
    // This has been deprecated, but we'll eave it in for now.
metzlerd's avatar
metzlerd committed
830
    if ($print) drupal_add_js($m_path . '/forena.js');
831
832
833
    $forena_js = array();
    $forena_js['form'] = preg_replace('/[^\w\-]+/u', '_', Frx::Skin()->name);
    $forena_js['report'] = preg_replace('/[^\w\-]+/u', '_', $name_in);
metzlerd's avatar
metzlerd committed
834
    if ($print) drupal_add_js(array('forena' => $forena_js), 'setting');
835
    if (!$print && is_array($output)) $output = drupal_render($output);
836
837
    return $output;
  }
838

839

840
841
}

842
/**
metzlerd's avatar
metzlerd committed
843
 * Render report as an inlcude.  Don't perform the output fuctnions for the document
844
845
846
 * @param $report_name String
 * @param $parms array of parameters to include
 */
metzlerd's avatar
metzlerd committed
847
function forena_report_include($report_name, $parms=array()) {
metzlerd's avatar
metzlerd committed
848
849
850
851
852
853
854
855
856
857
858
859
  $output = '';
  require_once 'forena.common.inc';
  $desc= Frx::Menu()->parseURL($report_name);
  $name = $desc['name'];

  $r = @FrxReportGenerator::instance()->get_report($name, $parms);
  if (!$r || !$r->rpt_xml) {
    return '';
  }
  //check for default parameters
  $r->processParameters();
  $format = $desc['format'];
metzlerd's avatar
metzlerd committed
860
  $r->render($format, FALSE);
metzlerd's avatar
metzlerd committed
861
862
863
864
865
866

  $o =  Frx::Document($format);
  if ($o) {
    $output = $o->render($r, $format,  array());
  }
  return $output;
867
868
}

869
870

/**
871
 * Menu callback for rendering the forena report.
872
 */
873
874
function forena_report_menu_callback() {
  $args = func_get_args();
metzlerd's avatar
metzlerd committed
875
876
877
878
879
  $path = array_shift($args);
  $report_name = array_shift($args);
  $parms = $_GET;
  unset($parms['q']);
  $menu_parms = array();
880
  $tokens = Frx::Menu()->tokens($path);
metzlerd's avatar
metzlerd committed
881
882
883
884
885
886
887
888
889
890
  if ($args && $tokens) {
    foreach ($args as $i=> $value) {
      @$menu_parms[$tokens[$i]] = $value;
    }
  }

  Frx::Data()->setContext('menu-parms', $menu_parms);
  $parms = array_merge($parms, $menu_parms);
  $output = forena_report($report_name, $parms);
  if ($output) return $output;
891
892
893
}

/**
894
895
 * Used in hook_menu to test access to menu items.
 * @param $checks array key value of callbacks and args to check.
896
897
898
899
 */
function forena_check_all_access($checks) {
  // Check each callback function to see if we have an error.
  $access = FALSE;
900
901
902
903
904
905
906
907
908
909
910
911
912
913
  // Check each callback function to see if we have an error.
  if ($checks) foreach ($checks 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;
914
915
      }
    }
916
917
  }
  return $access;
918
919
}

metzlerd's avatar
metzlerd committed
920
921
922
923
924
/**
 * Callback for setting international titles
 * @param $report_name string name of report
 * @param $use_menu_title boolean indicate wether to use menu title attribute.
 */
925
926
function forena_report_title_callback($report_name, $use_menu_title=TRUE) {
  $title = '';
927
928
  $info = Frx::File()->getReportCacheInfo($report_name);
  $title = $use_menu_title && $info && isset($info->cache['menu']['title']) ? $info->cache['menu']['title']:  $info->cache['title'];
929
930
931
  return $title;
}

932
933


metzlerd's avatar
metzlerd committed
934
935
936
/**
 * Render report with some data
 *
937
 * @param string $report
938
939
 * @param string $format
 * @param mixed $data
metzlerd's avatar
metzlerd committed
940
941
 * @return unknown
 */
942
function forena_render_report($report, $format='', $data='', $options= array(), $print = TRUE) {
metzlerd's avatar
metzlerd committed
943
944
  require_once('forena.common.inc');
  $o = forena_report_object($report, $data);
945
  $output = $o->render($format, $print);
metzlerd's avatar
metzlerd committed
946
947

  //If a format was requested render a custom non-drupal document
948
  if ($format && $format != 'web') {
949
    $output = FrxReportGenerator::instance()->generate_doc($format, $output, $options, $print);
metzlerd's avatar
metzlerd committed
950
  }
951
  return $output;
metzlerd's avatar
metzlerd committed
952
}
953

954
/**
955
 * Self register plugins with forena.
956
 *
957
 */
metzlerd's avatar
metzlerd committed
958
function forena_forena_plugins() {
959
960
  $path = drupal_get_path('module', 'forena');

961
  $plugins['FrxPDO'] = array('file' => 'plugins/FrxPDO.inc',
962
                     'parent' => 'FrxDataSource',
metzlerd's avatar
metzlerd committed
963
                     'class' => 'FrxPDO',
metzlerd's avatar