forena.module 41 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

267
268
269
270
271
272
273
  $items['forena/preview'] = array(
  	'page callback' => 'forena_data_preview',
     'access arguments' => array('create any report'),
     'file' => 'forena.admin.inc',
     'type' => MENU_CALLBACK,
  );

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

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

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


metzlerd's avatar
metzlerd committed
293
  $items['forena/categories/autocomplete'] = array(
metzlerd's avatar
metzlerd committed
294
295
296
     'page callback' => 'forena_categories_autocomplete',
     'access callback' => TRUE,
     'type' => MENU_CALLBACK,
metzlerd's avatar
metzlerd committed
297
  );
metzlerd's avatar
metzlerd committed
298
299

  return $items;
metzlerd's avatar
metzlerd committed
300
}
metzlerd's avatar
metzlerd committed
301

302
303
304
305
/**
 * Implements hook_admin_paths_alter().
 */
function forena_admin_paths_alter(&$paths) {
metzlerd's avatar
metzlerd committed
306
307
308
  $paths['reports/*/*'] = TRUE;
  $paths['reports/*/view'] = FALSE;
  $paths['reports/add'] = TRUE;
309
310
311

}

metzlerd's avatar
metzlerd committed
312

313
314
315
316
317
318
319
320
321
322
323
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
324
/**
metzlerd's avatar
metzlerd committed
325
 * Implementation of hook_block_info
metzlerd's avatar
metzlerd committed
326
 */
metzlerd's avatar
metzlerd committed
327
function forena_block_info() {
metzlerd's avatar
metzlerd committed
328
  $blocks['forena_reports'] = array('info' => t('My reports'),
metzlerd's avatar
metzlerd committed
329
      'status' => 1,
metzlerd's avatar
metzlerd committed
330
      'region' => 'sidebar_first',
metzlerd's avatar
metzlerd committed
331
332
      'visibility' => 1,
      'pages' => 'forena',
metzlerd's avatar
metzlerd committed
333
334
  );
  return $blocks;
metzlerd's avatar
metzlerd committed
335
336
337
338
339
}
/**
 * Implementation of hook_block_view
 */
function forena_block_view($delta = 0) {
metzlerd's avatar
metzlerd committed
340

metzlerd's avatar
metzlerd committed
341
342
343
344
345
  // Inlcude the base library
  require_once('forena.common.inc');
  switch ($delta) {
    case 'forena_reports':
      $block = array('subject' => 'My Reports',
metzlerd's avatar
metzlerd committed
346
            'content' => forena_my_reports_block());
metzlerd's avatar
metzlerd committed
347
348
349
      break;
  }
  return $block;
metzlerd's avatar
metzlerd committed
350

metzlerd's avatar
metzlerd committed
351
352
}

353

metzlerd's avatar
metzlerd committed
354
355
/**
 * Auto complete for categories
metzlerd's avatar
metzlerd committed
356
 *
metzlerd's avatar
metzlerd committed
357
358
359
360
 */

function forena_categories_autocomplete($string='') {
  require_once('forena.admin.inc');
metzlerd's avatar
metzlerd committed
361
  $categories = @forena_get_categories($string);
metzlerd's avatar
metzlerd committed
362
  print drupal_json_output($categories);
metzlerd's avatar
metzlerd committed
363
364
365
366
}



metzlerd's avatar
metzlerd committed
367
368
369
370
371
/**
 * Auto complete for data blocks
 * @param $string
 * @return unknown_type
 */
372
function forena_data_block_autocomplete() {
metzlerd's avatar
metzlerd committed
373
  require_once('forena.admin.inc');
374
375
376
377
378
379
380
381
382
383
  $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
384
  if ($data_blocks) {
385
    $temp = array_keys($data_blocks);
metzlerd's avatar
metzlerd committed
386
    $data_blocks = array_combine($temp, $temp);
metzlerd's avatar
metzlerd committed
387
388
  }
  else {
metzlerd's avatar
metzlerd committed
389
    $data_blocks = array();
metzlerd's avatar
metzlerd committed
390
  }
metzlerd's avatar
metzlerd committed
391

metzlerd's avatar
metzlerd committed
392
  print drupal_json_output($data_blocks);
metzlerd's avatar
metzlerd committed
393
394
}

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

function forena_xml($block_name, $parms=array()) {
metzlerd's avatar
metzlerd committed
415
416
417
418
419
  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);
420
421
}

metzlerd's avatar
metzlerd committed
422
423
424
425
426
427
/**
 * Auto complete for formats
 * @param $string
 * @return unknown_type
 */
function forena_fields_format_autocomplete($string='') {
metzlerd's avatar
metzlerd committed
428
429
  require_once('forena.common.inc');
  $matches = array();
metzlerd's avatar
metzlerd committed
430
  $formats = @FrxReportGenerator::instance()->supported_formats();
metzlerd's avatar
metzlerd committed
431
432
433

  if ($string == "*") {
    $matches = $formats;
metzlerd's avatar
metzlerd committed
434
    print drupal_json_output($matches);
metzlerd's avatar
metzlerd committed
435
436
437
438
439
440
441
442
    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
443
  print drupal_json_output($matches);
metzlerd's avatar
metzlerd committed
444
}
445

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

467
function forena_parameter_form($formid, &$form_state, $parameters, $attributes= array()) {
metzlerd's avatar
metzlerd committed
468
  $parms = $_GET;
469
  // Set menu parms
metzlerd's avatar
metzlerd committed
470
  $menu_parms = Frx::Data()->getContext('menu-parms');
471
472
  if ($menu_parms) $form_state['storage']['menu-parms'] = $menu_parms;
  // Set Descriptors
473
  $desc = FrxData::instance()->getContext('report');
474
475
  if ($desc) $form_state['storage']['desc'] = $desc;
  $desc = $form_state['storage']['desc'];
476
  $report_name = @$desc['name'];
477

478
  $collapse = isset($attributes['collapsed']) ? $attributes['collapsed'] : FALSE;
metzlerd's avatar
metzlerd committed
479
480
481
  if (isset($form_state['values'])) {
    $collapse=FALSE;
    $parms = array_merge($parms, $form_state['values']['params']);
metzlerd's avatar
metzlerd committed
482
483
484
485
486
487
    // 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);

    }
488
    drupal_alter('forena_parameters', $report_name,  $parms);
metzlerd's avatar
metzlerd committed
489
490
    Frx::Data()->push($parms, 'parm');
  }
491
492
493
494
  $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');
495

metzlerd's avatar
metzlerd committed
496
497
  unset($parms['q']);
  $form = array();
metzlerd's avatar
metzlerd committed
498

499

metzlerd's avatar
metzlerd committed
500
  if ($parameters) {
501

502
    //drupal_alter('forena_parameters', $report_name,  $parms);
metzlerd's avatar
metzlerd committed
503
    if ($parameters) {
metzlerd's avatar
metzlerd committed
504

metzlerd's avatar
metzlerd committed
505
      $form['params'] = array(
metzlerd's avatar
metzlerd committed
506
          '#tree' => TRUE,
507
          '#title' => $title,
metzlerd's avatar
metzlerd committed
508
          '#type' => 'fieldset',
509
          '#collapsible' => $collapsible,
metzlerd's avatar
metzlerd committed
510
511
512
          '#collapsed' => $collapse,
          '#prefix' => '<div id="parameters-wrapper">',
          '#suffix' => '</div>',
metzlerd's avatar
metzlerd committed
513
514
515
      );

      foreach ($parameters as $node) {
metzlerd's avatar
metzlerd committed
516
517
        $add_null = FALSE;
        $list=array();
metzlerd's avatar
metzlerd committed
518
519
520
521
522
523
524
525
526
527
528
529
        $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
530

metzlerd's avatar
metzlerd committed
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
        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();

546
547
548
549
550
551
        //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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
        //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
583
584
585
586
587
            $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) {
588
                $value = date('Y-m-d h:i', $datetime);
metzlerd's avatar
metzlerd committed
589
590
              }

metzlerd's avatar
metzlerd committed
591
            }
592
            $ctl_attrs['#forena_date_format'] = @$options['date_parm_format'] ? $options['date_parm_format'] : 'Y-m-d';
metzlerd's avatar
metzlerd committed
593

metzlerd's avatar
metzlerd committed
594
595
596
597
598
599
            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
600

metzlerd's avatar
metzlerd committed
601
            $list=array();
metzlerd's avatar
metzlerd committed
602
603
604
605
606
            break;
          case 'checkbox':
            if (@$option_str['return_value']) {
              $ctl_attrs['#return_value'] = $options['return_value'];
            }
metzlerd's avatar
metzlerd committed
607
            $list=array();
metzlerd's avatar
metzlerd committed
608
609
610
611
            break;
          case 'radios':
            break;
          case 'hidden':
metzlerd's avatar
metzlerd committed
612
            $list=array();
metzlerd's avatar
metzlerd committed
613
614
615
            break;
          default:
            $type = 'textfield';
616
            $list = array();
metzlerd's avatar
metzlerd committed
617
        }
metzlerd's avatar
metzlerd committed
618

metzlerd's avatar
metzlerd committed
619
620
621
        if (isset($menu_parms[$id]) && $type=='textfield') {
          $disabled = TRUE;
        }
metzlerd's avatar
metzlerd committed
622

metzlerd's avatar
metzlerd committed
623
624
        //If a data_source attr was found then create an array of
        $form['params'][$id] = array(
metzlerd's avatar
metzlerd committed
625
626
627
            '#type' => $type,
            '#title' => ($label) ? t($label) : t($id),
            '#default_value' => $value,
628
            '#disabled' => $disabled,
metzlerd's avatar
metzlerd committed
629
630
            '#required' => $required,
            '#description' => t($desc),
metzlerd's avatar
metzlerd committed
631
        );
metzlerd's avatar
metzlerd committed
632

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

metzlerd's avatar
metzlerd committed
635
636
        if ($type == 'item') {
          $form['params'][$id]['#markup'] = $value;
metzlerd's avatar
metzlerd committed
637

metzlerd's avatar
metzlerd committed
638
        }
metzlerd's avatar
metzlerd committed
639

metzlerd's avatar
metzlerd committed
640
        if ($type == 'hidden') {
metzlerd's avatar
metzlerd committed
641
          $form['params'][$id]['#value'] = $value;
metzlerd's avatar
metzlerd committed
642
        }
metzlerd's avatar
metzlerd committed
643

metzlerd's avatar
metzlerd committed
644
645
646
        // Add class to parmeter form.
        if ($class) {
          $form['params'][$id]['#attributes'] = array(
metzlerd's avatar
metzlerd committed
647
              'class' => @explode(' ', $class),
metzlerd's avatar
metzlerd committed
648
649
          );
        }
metzlerd's avatar
metzlerd committed
650

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

metzlerd's avatar
metzlerd committed
664
665
        if ($ajax) {
          $form['params'][$id]['#ajax'] = array('callback' => 'forena_parameters_callback',
metzlerd's avatar
metzlerd committed
666
              'wrapper' => 'parameters-wrapper');
metzlerd's avatar
metzlerd committed
667
668
        }

metzlerd's avatar
metzlerd committed
669
670
671
672
673
674
675
      }

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

metzlerd's avatar
metzlerd committed
677
      $form['params']['submit'] = array(
metzlerd's avatar
metzlerd committed
678
          '#type' => 'submit',
679
          '#value' => $submit_value,
metzlerd's avatar
metzlerd committed
680
      );
681

metzlerd's avatar
metzlerd committed
682
    }
metzlerd's avatar
metzlerd committed
683
684
    return $form;
  }
metzlerd's avatar
metzlerd committed
685
686
687
688
689
690
691
692
693
694
695
696
697
698


}


/**
 *
 * 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']);
699

metzlerd's avatar
metzlerd committed
700
  if (isset($values['params'])) foreach ($values['params'] as $key => $value) {
701
702
703
704
705
706
707
    $ctl = $form['params'][$key];
    switch($ctl['#type']) {
      case 'date_popup':
      case 'date_select':
      case 'date_text':
        $datetime = @strtotime($value);
        if ($datetime) {
708
          $value = $values['params'][$key] = date($ctl['#forena_date_format'], $datetime);
709
710
711
712
713
        }
        break;

    }

metzlerd's avatar
metzlerd committed
714
    if (is_array($value)) {
715
      $values['params'][$key] = array();
metzlerd's avatar
metzlerd committed
716
      foreach ($value as $k => $val) {
717
718
        if ($val) {
          $values['params'][$key][] = $val;
metzlerd's avatar
metzlerd committed
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
        }
      }
    }
    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'];
}

744
745
746
747
748
/**
 * Implementation of hook_perm
 *
 * @return unknown
 */
metzlerd's avatar
metzlerd committed
749
function forena_permission() {
750
  $perms = array(
metzlerd's avatar
metzlerd committed
751
    'administer forena reports' => array('title' => t('Administer Forena Reports')),
752
    'access demo reports' => array('title' => t('Access Demo Reports')),
metzlerd's avatar
metzlerd committed
753
754
755
756
    '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')),
757
    'perform email merge' => array('title' => t('Peform email merge')),
758
    '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
759
  );
760
761
762
763
  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
764
  return $perms;
765
766
}

767

metzlerd's avatar
metzlerd committed
768

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

790
791
792
/**
 * Tries to load data tables libary and other libraries that help.
 */
793
function forena_include_data_tables() {
metzlerd's avatar
metzlerd committed
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
  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);
    }
  }
810
811
}

812
813
814
815
816
817
/**
 * 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
 */
818
function forena_report($name_in, $parms = array(), $print = TRUE) {
819
  require_once('forena.common.inc');
metzlerd's avatar
metzlerd committed
820
  $desc = Frx::Menu()->parseURL($name_in);
821
822
  $time = @new DateTime($value);
  $desc['time'] = date_format($time, 'Y-m-d H:i:s');
823
  if (!$desc['exists']) {
824
825
    drupal_set_message(t('Report %s not found', array('%s' => $name_in)), 'error', FALSE);
    return '';
826
  }
metzlerd's avatar
metzlerd committed
827
828

  FrxData::instance()->setContext('cookie', $_COOKIE);
829
  FrxData::instance()->setContext('report', $desc);
830
  $report_name = $desc['name'];
831
  // Load dataTable plugin if possible.
metzlerd's avatar
metzlerd committed
832
  //forena_include_data_tables();
833
  $output = FrxReportGenerator::instance()->report($name_in, $parms, $print);
834
  $m_path = drupal_get_path('module', 'forena');
835
836
  if ($output) {
    // This has been deprecated, but we'll eave it in for now.
metzlerd's avatar
metzlerd committed
837
    if ($print) drupal_add_js($m_path . '/forena.js');
838
839
840
    $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
841
    if ($print) drupal_add_js(array('forena' => $forena_js), 'setting');
842
    if (!$print && is_array($output)) $output = drupal_render($output);
843
844
    return $output;
  }
845

846

847
848
}

849
/**
metzlerd's avatar
metzlerd committed
850
 * Render report as an inlcude.  Don't perform the output fuctnions for the document
851
852
853
 * @param $report_name String
 * @param $parms array of parameters to include
 */
metzlerd's avatar
metzlerd committed
854
function forena_report_include($report_name, $parms=array()) {
metzlerd's avatar
metzlerd committed
855
856
857
858
859
860
861
862
863
864
865
866
  $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
867
  $r->render($format, FALSE);
metzlerd's avatar
metzlerd committed
868
869
870
871
872
873

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

876
877

/**
878
 * Menu callback for rendering the forena report.
879
 */
880
881
function forena_report_menu_callback() {
  $args = func_get_args();
metzlerd's avatar
metzlerd committed
882
883
884
885
886
  $path = array_shift($args);
  $report_name = array_shift($args);
  $parms = $_GET;
  unset($parms['q']);
  $menu_parms = array();
887
  $tokens = Frx::Menu()->tokens($path);
metzlerd's avatar
metzlerd committed
888
889
890
891
892
893
894
895
896
897
  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;
898
899
900
}

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

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

939
940


metzlerd's avatar
metzlerd committed
941
942
943
/**
 * Render report with some data
 *
944
 * @param string $report
945
946
 * @param string $format
 * @param mixed $data
metzlerd's avatar
metzlerd committed
947
948
 * @return unknown
 */
949
function forena_render_report($report, $format='', $data='', $options= array(), $print = TRUE) {
metzlerd's avatar
metzlerd committed
950
951
  require_once('forena.common.inc');
  $o = forena_report_object($report, $data);
952
  $output = $o->render($format, $print);
metzlerd's avatar
metzlerd committed
953
954

  //If a format was requested render a custom non-drupal document
955
  if ($format && $format != 'web') {
956
    $output = FrxReportGenerator::instance()->generate_doc($format, $output, $options, $print);
metzlerd's avatar
metzlerd committed
957
  }
958
  return $output;
metzlerd's avatar
metzlerd committed
959
}
960

961
/**
962
 * Self register plugins with forena.
963
 *
964
 */
metzlerd's avatar
metzlerd committed
965
function forena_forena_plugins() {
966
967
  $path = drupal_get_path('module', 'forena');

968
  $plugins['FrxPDO'] = array('file' => 'plugins/FrxPDO.inc',