forena.module 48.1 KB
Newer Older
metzlerd's avatar
metzlerd committed
1
<?php
David Metzler's avatar
Fixes    
David Metzler committed
2
require_once 'forena.data.inc';
metzlerd's avatar
metzlerd committed
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101

  $items['admin/structure/forena/data'] = array(
      'type' => MENU_LOCAL_TASK,
      'title' => 'Data',
      'access arguments' => array('design any report'),
      'page callback' => 'forena_display_blocks',
      'file' => 'forena.data.inc',
  );

  $items['forena/data'] = array(
      'page callback' => 'forena_data_preview',
      'access arguments' => array('design any report'),
      'file' => 'forena.data.inc',
      'type' => MENU_CALLBACK,
  );

102
  $items['reports/%'] = array(
metzlerd's avatar
metzlerd committed
103
    'page callback' => 'forena_report',
metzlerd's avatar
metzlerd committed
104
105
106
107
108
    'page arguments' => array(1),
    'title' => 'Reports',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
    'file' => 'forena.common.inc',
metzlerd's avatar
metzlerd committed
109
  );
metzlerd's avatar
metzlerd committed
110

111
  $items['report_doc/%'] = array(
112
113
114
115
116
    '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
117
  );
118

119

metzlerd's avatar
metzlerd committed
120
  $items['reports/%/edit'] = array(
121
     'title' => 'Edit',
122
123
     'page callback' => 'forena_report_preview',
     'page arguments' => array(1),
124
125
     'access arguments' => array('design any report'),
     'description' => 'Edit the layout of your report',
126
     'type' => MENU_CALLBACK,
127
     'file' => 'forena.report.inc',
metzlerd's avatar
metzlerd committed
128
  );
129

metzlerd's avatar
metzlerd committed
130
131
  if (module_exists('locale')) {
    $items['reports/%/translations'] = array(
132
133
       'page callback' => 'forena_report_translations',
       'page arguments' => array(1),
134
       'title' => 'Translate',
metzlerd's avatar
metzlerd committed
135
       'file' => 'forena.report.inc',
136
       'access arguments' => array('design any report'),
137
       'type' => MENU_CALLBACK,
metzlerd's avatar
metzlerd committed
138
139
    );
  }
metzlerd's avatar
metzlerd committed
140
  $items['reports/%/edit/parameters'] = array(
141
    'page callback' => 'drupal_get_form',
metzlerd's avatar
metzlerd committed
142
    'page arguments' => array('forena_report_parameters_form', 1),
143
    'title' => 'Parameters',
metzlerd's avatar
metzlerd committed
144
    'file' => 'forena.report.inc',
145
146
    'access arguments' => array('design any report'),
    'type' => MENU_LOCAL_TASK,
metzlerd's avatar
metzlerd committed
147
  );
148

metzlerd's avatar
metzlerd committed
149
150
151
152
153
154
155
156
157
158
  $items['reports/%/edit/edit-parameter'] = array(
      'page callback' => 'drupal_get_form',
      'page arguments' => array('forena_report_parameter_config_form', 1),
      'title' => 'Edit Parameter',
      'file' => 'forena.report.inc',
      'access arguments' => array('design any report'),
      'type' => MENU_CALLBACK,
  );

  $items['reports/%/edit/add-parameter'] = array(
159
    'page callback' => 'drupal_get_form',
metzlerd's avatar
metzlerd committed
160
161
    'page arguments' => array('forena_report_add_parameter_form', 1),
    'file' => 'forena.report.inc',
162
163
164
    'title' => 'Parameters',
    'access arguments' => array('design any report'),
    'type' => MENU_CALLBACK,
metzlerd's avatar
metzlerd committed
165
  );
166

metzlerd's avatar
metzlerd committed
167
168
169
  $items['reports/%/edit/general'] = array(
    'title' => 'General',
    'page callback' => 'drupal_get_form',
metzlerd's avatar
metzlerd committed
170
    'page arguments' => array('forena_report_general_form',1),
metzlerd's avatar
metzlerd committed
171
172
    'access arguments' => array('design any report'),
    'description' => 'General Report Properties',
173
    'type' => MENU_LOCAL_TASK,
metzlerd's avatar
metzlerd committed
174
    'weight' => -1,
metzlerd's avatar
metzlerd committed
175
    'file' => 'forena.report.inc',
metzlerd's avatar
metzlerd committed
176
  );
177

metzlerd's avatar
metzlerd committed
178
  $items['reports/%/edit/layout'] = array(
179
180
    'title' => 'Layout',
    'page callback' => 'drupal_get_form',
metzlerd's avatar
metzlerd committed
181
    'page arguments' => array('forena_report_layout_form',1),
182
183
184
    'access arguments' => array('design any report'),
    'description' => 'Edit the layout of your report',
    'type' => MENU_LOCAL_TASK,
metzlerd's avatar
metzlerd committed
185
    'file' => 'forena.report.inc',
metzlerd's avatar
metzlerd committed
186
  );
metzlerd's avatar
metzlerd committed
187

David Metzler's avatar
Fixes    
David Metzler committed
188
  $items['reports/%/edit/edit-data/%/%'] = array(
189
190
191
192
193
194
195
   'title' => 'Edit Block',
   'file' => 'forena.report.inc',
   'page callback' => 'drupal_get_form',
   'page arguments' => array('forena_report_data_block_form', 1, 3, 4, 5, 'edit-data'),
   'access arguments' => array('design any report'),
   'description' => 'Add a data block to your report',
   'type' => MENU_CALLBACK,
196
  );
metzlerd's avatar
metzlerd committed
197

198
199
200
201
202
203
204
205
206
207
  $items['reports/%/edit/edit-field/%'] = array(
      'title' => 'Edit Block',
      'file' => 'forena.report.inc',
      'page callback' => 'drupal_get_form',
      'page arguments' => array('forena_report_field_form', 1, 4),
      'access arguments' => array('design any report'),
      'description' => 'Add a data block to your report',
      'type' => MENU_CALLBACK,
  );

David Metzler's avatar
David Metzler committed
208
  $items['reports/%/edit/select-data/%'] = array(
209
210
211
212
213
214
215
    'title' => 'Select Data Block',
    'file' => 'forena.report.inc',
    'page callback' => 'forena_select_block_list',
    'page arguments' => array(1, 4),
    'access arguments' => array('design any report'),
    'description' => 'Select the data blcok to add',
    'type' => MENU_CALLBACK,
David Metzler's avatar
Fixes    
David Metzler committed
216
217
  );

David Metzler's avatar
David Metzler committed
218
219

  $items['reports/%/edit/select-data/%/%'] = array(
220
221
222
223
224
225
226
    'title' => 'Select Data Block',
    'file' => 'forena.report.inc',
    'page callback' => 'forena_select_block_list',
    'page arguments' => array(1, 4, 5),
    'access arguments' => array('design any report'),
    'description' => 'Select the data blcok to add',
    'type' => MENU_CALLBACK,
David Metzler's avatar
Fixes    
David Metzler committed
227
228
229
  );

  $items['reports/%/edit/preview-data/%/%'] = array(
230
231
232
233
234
235
236
    'title' => 'Preview Data Block',
    'file' => 'forena.report.inc',
    'page callback' => 'forena_report_block_preview',
    'page arguments' => array(1, 4, 5),
    'access arguments' => array('design any report'),
    'description' => 'Select the data block to add',
    'type' => MENU_CALLBACK,
David Metzler's avatar
Fixes    
David Metzler committed
237
238
239
  );

  $items['reports/%/edit/preview-data/%/%/%'] = array(
240
241
242
243
244
245
246
247
    'title' => 'Preview Data Block',
    'file' => 'forena.report.inc',
    'page callback' => 'forena_report_block_preview',
    'page arguments' => array(1, 4, 5, 6),
    'access arguments' => array('design any report'),
    'description' => 'Select the data block to add',
    'type' => MENU_CALLBACK,
);
David Metzler's avatar
Fixes    
David Metzler committed
248

249
  $items['reports/%/edit/add-data/%'] = array(
250
251
252
253
254
255
256
    'title' => 'Data',
    'file' => 'forena.report.inc',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('forena_report_data_block_form', 1, 3, 4),
    'access arguments' => array('design any report'),
    'description' => 'Edit a data block on the report. ',
    'type' => MENU_CALLBACK,
metzlerd's avatar
metzlerd committed
257
  );
258

David Metzler's avatar
Fixes    
David Metzler committed
259
  $items['reports/%/edit/add-data/%/%'] = array(
260
261
262
263
264
265
266
    'title' => 'Add Data',
    'file' => 'forena.report.inc',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('forena_report_data_block_form', 1, 3, 4, 5),
    'access arguments' => array('design any report'),
    'description' => 'Edit a data block on the report. ',
    'type' => MENU_CALLBACK,
David Metzler's avatar
David Metzler committed
267
268
269
  );

  $items['reports/%/edit/prepend-data/%/%'] = array(
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
    'title' => 'Add Data',
    'file' => 'forena.report.inc',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('forena_report_data_block_form', 1, 3, 4, 5),
    'access arguments' => array('design any report'),
    'description' => 'Edit a data block on the report. ',
    'type' => MENU_CALLBACK,
  );

  $items['reports/%/edit/delete-data/%'] = array(
  	'title' => 'Delete Data',
    'file' => 'forena.report.inc',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('forena_report_delete_confirm', 1, 4),
    'access arguments' => array('design any report'),
    'description' => 'Delete this form',
    'type' => MENU_CALLBACK,
David Metzler's avatar
Fixes    
David Metzler committed
287
288
  );

metzlerd's avatar
metzlerd committed
289
  $items['reports/%/edit/format'] = array(
290
     'title' => 'Document Types',
291
     'page callback' => 'drupal_get_form',
metzlerd's avatar
metzlerd committed
292
     'page arguments' => array('forena_report_format_form', 1),
293
     'access arguments' => array('design any report'),
metzlerd's avatar
metzlerd committed
294
     'file' => 'forena.report.inc',
295
     'description' => 'Document Options',
296
     'type' => MENU_LOCAL_TASK,
metzlerd's avatar
metzlerd committed
297
  );
298

metzlerd's avatar
metzlerd committed
299
  $items['reports/%/delete'] = array(
300
     'title' => 'Remove Custom Report',
metzlerd's avatar
metzlerd committed
301
     'page callback' => 'drupal_get_form',
metzlerd's avatar
metzlerd committed
302
     'page arguments' => array('forena_report_delete_form', 1),
metzlerd's avatar
metzlerd committed
303
     'access arguments' => array('delete report'),
metzlerd's avatar
metzlerd committed
304
305
     'type' => MENU_CALLBACK,
     'file' => 'forena.report.inc',
metzlerd's avatar
metzlerd committed
306
  );
metzlerd's avatar
metzlerd committed
307

metzlerd's avatar
metzlerd committed
308
  $items['reports/add'] = array(
309
     'title' => 'Create Report',
310
311
     'page callback' => 'drupal_get_form',
     'page arguments' => array('forena_add_report_form'),
metzlerd's avatar
metzlerd committed
312
313
     'access arguments' => array('create any report'),
     'description' => 'Create a new report',
314
315
     'file' => 'forena.report.inc',
     'type' => MENU_SUGGESTED_ITEM,
metzlerd's avatar
metzlerd committed
316
  );
317

metzlerd's avatar
metzlerd committed
318
  $items['reports/%/add'] = array(
319
     'title' => 'Create Report',
320
321
     'page callback' => 'drupal_get_form',
     'page arguments' => array('forena_add_report_form', 1),
322
323
     'access arguments' => array('create any report'),
     'description' => 'Create a new report from a template',
324
     'file' => 'forena.admin.inc',
325
     'type' => MENU_CALLBACK,
metzlerd's avatar
metzlerd committed
326
  );
327

metzlerd's avatar
metzlerd committed
328
  $items['reports/%/translate'] = array(
329
330
331
     'title' => 'Translate Report',
     'page callback' => 'drupal_get_form',
     'page arguments' => array('forena_create_trans_form',1),
332
333
     'access arguments' => array('create any report'),
     'description' => 'Create a new report translation',
334
     'file' => 'forena.admin.inc',
335
     'type' => MENU_CALLBACK,
metzlerd's avatar
metzlerd committed
336
  );
metzlerd's avatar
metzlerd committed
337

338
339
  $items['forena'] = array(
    'page callback' => 'forena_user_reports',
metzlerd's avatar
metzlerd committed
340
341
342
    'page arguments' => array(),
    'title' => 'My Reports',
    'access arguments' => array('list reports'),
343
    'type' => MENU_NORMAL_ITEM,
metzlerd's avatar
metzlerd committed
344
  );
metzlerd's avatar
metzlerd committed
345

metzlerd's avatar
metzlerd committed
346
  $items['forena/xml/%'] = array(
347
348
   	'page callback' => 'forena_block_xml',
    'page arguments' => array(2),
349
   	'access arguments' => array('access forena block xml'),
350
351
    'file' => 'forena.common.inc',
   	'type' => MENU_CALLBACK,
metzlerd's avatar
metzlerd committed
352
  );
353

354
355
356
357
358
359
360
  $items['forena/json/%'] = array(
  	'page callback' => 'forena_json',
    'page arguments' => array(2),
    'access arguments' => array('access forena block xml'),
    'type' => MENU_CALLBACK,
  );

361

metzlerd's avatar
metzlerd committed
362
  $items['forena/fields/format/autocomplete'] = array(
metzlerd's avatar
metzlerd committed
363
364
365
     'page callback' => 'forena_fields_format_autocomplete',
     'access callback' => TRUE,
     'type' => MENU_CALLBACK,
metzlerd's avatar
metzlerd committed
366
  );
metzlerd's avatar
metzlerd committed
367

metzlerd's avatar
metzlerd committed
368
  $items['forena/data_block/autocomplete'] = array(
metzlerd's avatar
metzlerd committed
369
370
371
     'page callback' => 'forena_data_block_autocomplete',
     'access callback' => TRUE,
     'type' => MENU_CALLBACK,
metzlerd's avatar
metzlerd committed
372
  );
metzlerd's avatar
metzlerd committed
373

metzlerd's avatar
metzlerd committed
374
  $items['forena/reports/autocomplete'] = array(
375
376
377
     'page callback' => 'forena_reports_autocomplete',
     'access callback' => TRUE,
     'type' => MENU_CALLBACK,
metzlerd's avatar
metzlerd committed
378
  );
379
380


metzlerd's avatar
metzlerd committed
381
  $items['forena/categories/autocomplete'] = array(
metzlerd's avatar
metzlerd committed
382
383
384
     'page callback' => 'forena_categories_autocomplete',
     'access callback' => TRUE,
     'type' => MENU_CALLBACK,
metzlerd's avatar
metzlerd committed
385
  );
metzlerd's avatar
metzlerd committed
386
387

  return $items;
metzlerd's avatar
metzlerd committed
388
}
metzlerd's avatar
metzlerd committed
389

390
391
392
393
/**
 * Implements hook_admin_paths_alter().
 */
function forena_admin_paths_alter(&$paths) {
metzlerd's avatar
metzlerd committed
394
395
  $paths['reports/*/*'] = TRUE;
  $paths['reports/*/view'] = FALSE;
396
  $paths['reports/*/edit'] = FALSE;
metzlerd's avatar
metzlerd committed
397
  $paths['reports/add'] = TRUE;
398
399
400

}

metzlerd's avatar
metzlerd committed
401

402
403
404
405
406
407
408
409
410
411
412
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
413
/**
metzlerd's avatar
metzlerd committed
414
 * Implementation of hook_block_info
metzlerd's avatar
metzlerd committed
415
 */
metzlerd's avatar
metzlerd committed
416
function forena_block_info() {
metzlerd's avatar
metzlerd committed
417
  $blocks['forena_reports'] = array('info' => t('My reports'),
metzlerd's avatar
metzlerd committed
418
      'status' => 1,
metzlerd's avatar
metzlerd committed
419
      'region' => 'sidebar_first',
metzlerd's avatar
metzlerd committed
420
421
      'visibility' => 1,
      'pages' => 'forena',
metzlerd's avatar
metzlerd committed
422
423
  );
  return $blocks;
metzlerd's avatar
metzlerd committed
424
425
426
427
428
}
/**
 * Implementation of hook_block_view
 */
function forena_block_view($delta = 0) {
metzlerd's avatar
metzlerd committed
429

metzlerd's avatar
metzlerd committed
430
431
432
433
434
  // Inlcude the base library
  require_once('forena.common.inc');
  switch ($delta) {
    case 'forena_reports':
      $block = array('subject' => 'My Reports',
metzlerd's avatar
metzlerd committed
435
            'content' => forena_my_reports_block());
metzlerd's avatar
metzlerd committed
436
437
438
      break;
  }
  return $block;
metzlerd's avatar
metzlerd committed
439

metzlerd's avatar
metzlerd committed
440
441
}

442

metzlerd's avatar
metzlerd committed
443
444
/**
 * Auto complete for categories
metzlerd's avatar
metzlerd committed
445
 *
metzlerd's avatar
metzlerd committed
446
447
448
449
 */

function forena_categories_autocomplete($string='') {
  require_once('forena.admin.inc');
metzlerd's avatar
metzlerd committed
450
  $categories = @forena_get_categories($string);
metzlerd's avatar
metzlerd committed
451
  print drupal_json_output($categories);
metzlerd's avatar
metzlerd committed
452
453
454
455
}



metzlerd's avatar
metzlerd committed
456
457
458
459
460
/**
 * Auto complete for data blocks
 * @param $string
 * @return unknown_type
 */
461
function forena_data_block_autocomplete() {
metzlerd's avatar
metzlerd committed
462
  require_once('forena.admin.inc');
463
464
465
466
467
468
469
470
471
472
  $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
473
  if ($data_blocks) {
474
    $temp = array_keys($data_blocks);
metzlerd's avatar
metzlerd committed
475
    $data_blocks = array_combine($temp, $temp);
metzlerd's avatar
metzlerd committed
476
477
  }
  else {
metzlerd's avatar
metzlerd committed
478
    $data_blocks = array();
metzlerd's avatar
metzlerd committed
479
  }
metzlerd's avatar
metzlerd committed
480

metzlerd's avatar
metzlerd committed
481
  print drupal_json_output($data_blocks);
metzlerd's avatar
metzlerd committed
482
483
}

484
function forena_reports_autocomplete($string='', $string2='') {
metzlerd's avatar
metzlerd committed
485
  GLOBAL $language;
486
  $reports='';
487
488
489
  $link = '';
  // If we have two parameters.
  if ($string2) {
metzlerd's avatar
metzlerd committed
490
491
    $mode = $string;
    $string = $string2;
492
  }
493
494
  $string = '*' .$string .'*';
  $result = Frx::File()->allReports();
495
  foreach ($result as $row) {
496
497
    if (drupal_match_path($row->name, $string)) {
      $reports[$row->name] = $row->name . ' - ' . $row->cache['title'];
498
499
    }
  }
metzlerd's avatar
metzlerd committed
500
  return drupal_json_output($reports);
501
}
502
503

function forena_xml($block_name, $parms=array()) {
metzlerd's avatar
metzlerd committed
504
505
506
507
508
  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);
509
510
}

511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
/**
 * Get the raw data from a block if supported.
 * Note that this may return a simplexml object or a recordset or an
 * array depending on the support provided by the product.
 * @param unknown $block_name
 * @param unknown $parms
 * @return Ambigous <unknown, string, NULL>
 */
function forena_data($block_name, $parms=array()) {
  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, TRUE);
}

/**
 * Output forena data as JSON objects.
 * @param unknown $block_name
 * @param string $parms
 */
function forena_json($block_name, $parms= NULL) {
  $block_name = str_replace('.', '/', $block_name);
  if ($parms === NULL) {
    $parms = array_merge($_GET, $_POST);
    unset($_parms['q']);
  }
  $data = forena_data($block_name, $parms);
  $ret = array();
  foreach($data as $row) {
    $ret[] = $row;
  }
  drupal_json_output($ret);
}

metzlerd's avatar
metzlerd committed
546
547
548
549
550
551
/**
 * Auto complete for formats
 * @param $string
 * @return unknown_type
 */
function forena_fields_format_autocomplete($string='') {
metzlerd's avatar
metzlerd committed
552
553
  require_once('forena.common.inc');
  $matches = array();
metzlerd's avatar
metzlerd committed
554
  $formats = @FrxReportGenerator::instance()->supported_formats();
metzlerd's avatar
metzlerd committed
555
556
557

  if ($string == "*") {
    $matches = $formats;
metzlerd's avatar
metzlerd committed
558
    print drupal_json_output($matches);
metzlerd's avatar
metzlerd committed
559
560
561
562
563
564
565
566
    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
567
  print drupal_json_output($matches);
metzlerd's avatar
metzlerd committed
568
}
569

570
571
/**
 *  Calls forena_parameter_form
572
573
 *  in forena.common.inc
 */
metzlerd's avatar
metzlerd committed
574
function forena_parameters_report() {
575
  require_once('forena.admin.inc');
576
577
578
  $m = Frx::Menu();
  $name = $m->name;
  $filename = $m->filename;
metzlerd's avatar
metzlerd committed
579
  $format = $m->format;
metzlerd's avatar
metzlerd committed
580
581
582
  $report = forena_get_report($name);
  if ($report) {
    $r = forena_report_object();
metzlerd's avatar
metzlerd committed
583
584
    $o = drupal_get_form('forena_parameters_form');
    return $o;
metzlerd's avatar
metzlerd committed
585
586
587
  }
  else {
    drupal_not_found();
588
589
590
  }
}

591
function forena_parameter_form($formid, &$form_state, $parameters, $attributes= array()) {
metzlerd's avatar
metzlerd committed
592
  $parms = $_GET;
593
  // Set menu parms
metzlerd's avatar
metzlerd committed
594
  $menu_parms = Frx::Data()->getContext('menu-parms');
595
596
  if ($menu_parms) $form_state['storage']['menu-parms'] = $menu_parms;
  // Set Descriptors
597
  $desc = FrxData::instance()->getContext('report');
598
  $form_state['storage']['desc'] = $desc;
599
  $desc = $form_state['storage']['desc'];
600
  $report_name = @$desc['name'];
601

602
  $collapse = isset($attributes['collapsed']) ? $attributes['collapsed'] : FALSE;
metzlerd's avatar
metzlerd committed
603
604
605
  if (isset($form_state['values'])) {
    $collapse=FALSE;
    $parms = array_merge($parms, $form_state['values']['params']);
metzlerd's avatar
metzlerd committed
606
607
608
609
610
611
    // 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);

    }
612
    drupal_alter('forena_parameters', $report_name,  $parms);
metzlerd's avatar
metzlerd committed
613
614
    Frx::Data()->push($parms, 'parm');
  }
615
616
617
618
  $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');
619

metzlerd's avatar
metzlerd committed
620
621
  unset($parms['q']);
  $form = array();
metzlerd's avatar
metzlerd committed
622

623

metzlerd's avatar
metzlerd committed
624
  if ($parameters) {
625

626
    //drupal_alter('forena_parameters', $report_name,  $parms);
metzlerd's avatar
metzlerd committed
627
    if ($parameters) {
metzlerd's avatar
metzlerd committed
628

metzlerd's avatar
metzlerd committed
629
      $form['params'] = array(
metzlerd's avatar
metzlerd committed
630
          '#tree' => TRUE,
631
          '#title' => $title,
metzlerd's avatar
metzlerd committed
632
          '#type' => 'fieldset',
633
          '#collapsible' => $collapsible,
metzlerd's avatar
metzlerd committed
634
635
636
          '#collapsed' => $collapse,
          '#prefix' => '<div id="parameters-wrapper">',
          '#suffix' => '</div>',
metzlerd's avatar
metzlerd committed
637
638
639
      );

      foreach ($parameters as $node) {
metzlerd's avatar
metzlerd committed
640
641
        $add_null = FALSE;
        $list=array();
metzlerd's avatar
metzlerd committed
642
643
644
645
646
647
648
649
650
651
652
653
        $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
654

metzlerd's avatar
metzlerd committed
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
        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();

670
671
672
673
674
675
        //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
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
        //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
707
708
709
710
711
            $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) {
712
                $value = date('Y-m-d h:i', $datetime);
metzlerd's avatar
metzlerd committed
713
714
              }

metzlerd's avatar
metzlerd committed
715
            }
716
            $ctl_attrs['#forena_date_format'] = @$options['date_parm_format'] ? $options['date_parm_format'] : 'Y-m-d';
metzlerd's avatar
metzlerd committed
717

metzlerd's avatar
metzlerd committed
718
719
720
721
722
723
            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
724

metzlerd's avatar
metzlerd committed
725
            $list=array();
metzlerd's avatar
metzlerd committed
726
727
728
729
730
            break;
          case 'checkbox':
            if (@$option_str['return_value']) {
              $ctl_attrs['#return_value'] = $options['return_value'];
            }
metzlerd's avatar
metzlerd committed
731
            $list=array();
metzlerd's avatar
metzlerd committed
732
733
734
735
            break;
          case 'radios':
            break;
          case 'hidden':
metzlerd's avatar
metzlerd committed
736
            $list=array();
metzlerd's avatar
metzlerd committed
737
738
739
            break;
          default:
            $type = 'textfield';
740
            $list = array();
metzlerd's avatar
metzlerd committed
741
        }
metzlerd's avatar
metzlerd committed
742

743
        if (isset($menu_parms[$id]) && $type!='hidden') {
metzlerd's avatar
metzlerd committed
744
745
          $disabled = TRUE;
        }
metzlerd's avatar
metzlerd committed
746

metzlerd's avatar
metzlerd committed
747
748
        //If a data_source attr was found then create an array of
        $form['params'][$id] = array(
metzlerd's avatar
metzlerd committed
749
750
751
            '#type' => $type,
            '#title' => ($label) ? t($label) : t($id),
            '#default_value' => $value,
752
            '#disabled' => $disabled,
metzlerd's avatar
metzlerd committed
753
754
            '#required' => $required,
            '#description' => t($desc),
metzlerd's avatar
metzlerd committed
755
        );
metzlerd's avatar
metzlerd committed
756

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

metzlerd's avatar
metzlerd committed
759
760
        if ($type == 'item') {
          $form['params'][$id]['#markup'] = $value;
metzlerd's avatar
metzlerd committed
761

metzlerd's avatar
metzlerd committed
762
        }
metzlerd's avatar
metzlerd committed
763

metzlerd's avatar
metzlerd committed
764
        if ($type == 'hidden') {
metzlerd's avatar
metzlerd committed
765
          $form['params'][$id]['#value'] = $value;
metzlerd's avatar
metzlerd committed
766
        }
metzlerd's avatar
metzlerd committed
767

metzlerd's avatar
metzlerd committed
768
769
770
        // Add class to parmeter form.
        if ($class) {
          $form['params'][$id]['#attributes'] = array(
metzlerd's avatar
metzlerd committed
771
              'class' => @explode(' ', $class),
metzlerd's avatar
metzlerd committed
772
773
          );
        }
metzlerd's avatar
metzlerd committed
774

metzlerd's avatar
metzlerd committed
775
776
777
        //if $list is not empty then push options
        //onto the array. options will cause an error for
        //textfield elements.
778
        if ($list || $type == 'select' || $type =='radios') {
779
780
          if ($add_null) {
            $prompt = @$options['prompt'];
metzlerd's avatar
metzlerd committed
781
            if (!$prompt) $prompt = $required ? '-' . t('select') .  '-' : '-' .t('none') . '-';
782
783
            $form['params'][$id]['#empty_option'] = $prompt ;
          }
metzlerd's avatar
metzlerd committed
784
785
786
          $form['params'][$id]['#options'] = $list;
          $form['params'][$id]['#multiple'] = $multiselect;
        }
metzlerd's avatar
metzlerd committed
787

metzlerd's avatar
metzlerd committed
788
789
        if ($ajax) {
          $form['params'][$id]['#ajax'] = array('callback' => 'forena_parameters_callback',
metzlerd's avatar
metzlerd committed
790
              'wrapper' => 'parameters-wrapper');
metzlerd's avatar
metzlerd committed
791
792
        }

metzlerd's avatar
metzlerd committed
793
794
795
796
797
798
799
      }

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

metzlerd's avatar
metzlerd committed
801
      $form['params']['submit'] = array(
metzlerd's avatar
metzlerd committed
802
          '#type' => 'submit',
803
          '#value' => $submit_value,
metzlerd's avatar
metzlerd committed
804
      );
805

metzlerd's avatar
metzlerd committed
806
    }
metzlerd's avatar
metzlerd committed
807
808
    return $form;
  }
metzlerd's avatar
metzlerd committed
809
810
811
812
813
814
815
816
817
818
819
820
821
822


}


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

metzlerd's avatar
metzlerd committed
824
  if (isset($values['params'])) foreach ($values['params'] as $key => $value) {
825
826
827
828
829
830
831
    $ctl = $form['params'][$key];
    switch($ctl['#type']) {
      case 'date_popup':
      case 'date_select':
      case 'date_text':
        $datetime = @strtotime($value);
        if ($datetime) {
832
          $value = $values['params'][$key] = date($ctl['#forena_date_format'], $datetime);
833
834
835
836
837
        }
        break;

    }

metzlerd's avatar
metzlerd committed
838
    if (is_array($value)) {
839
      $values['params'][$key] = array();
metzlerd's avatar
metzlerd committed
840
      foreach ($value as $k => $val) {
841
842
        if ($val) {
          $values['params'][$key][] = $val;
metzlerd's avatar
metzlerd committed
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
        }
      }
    }
    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'];
}

868
869
870
871
872
/**
 * Implementation of hook_perm
 *
 * @return unknown
 */
metzlerd's avatar
metzlerd committed
873
function forena_permission() {
874
  $perms = array(
metzlerd's avatar
metzlerd committed
875
876
877
878
879
    'administer forena reports' => array('title' => t('Administer Forena Reports')),
    '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')),
880
    'perform email merge' => array('title' => t('Peform email merge')),
881
    'access forena block xml' => array('title' => t('Access data from data blocks directly'), 'description' => t('Useful for ajax calls to data blocks')),
metzlerd's avatar
metzlerd committed
882
  );
883
884
885
886
  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
887
  return $perms;
888
889
}

890

metzlerd's avatar
metzlerd committed
891

892
function forena_user_reports($category = '') {
metzlerd's avatar
metzlerd committed
893
  require_once('forena.common.inc');
metzlerd's avatar
metzlerd committed
894
  $output='';
895
  $reports = forena_get_user_reports($category);
metzlerd's avatar
metzlerd committed
896
  $report_repos = variable_get('forena_path', 'reports');
897
898
899
  if (!$reports) {
    $output = 'No Reports Found';
  }
metzlerd's avatar
metzlerd committed
900
  $links = '';
metzlerd's avatar
metzlerd committed
901
  foreach ($reports as $category => $reports) {
metzlerd's avatar
metzlerd committed
902
    $links .= '<li><a href="#' . urlencode($category) . '">' . $category . '</a></li> ';
903
    $output .= '<h3 id="' . urlencode($category) . '">' . $category . '</h3>';
metzlerd's avatar
metzlerd committed
904
905
906
    $output .= '<ul>';
    foreach ($reports as $r) {
      $output .= '<li>' . l($r['title'], $report_repos . '/' . str_replace('/', '.', $r['report_name'])) . '</li>';
907
    }
metzlerd's avatar
metzlerd committed
908
    $output .= '</ul>';
metzlerd's avatar
metzlerd committed
909
  }
metzlerd's avatar
metzlerd committed
910
  return $output;
metzlerd's avatar
metzlerd committed
911
912
}

913
914
915
/**
 * Tries to load data tables libary and other libraries that help.
 */
916
function forena_include_data_tables() {
metzlerd's avatar
metzlerd committed
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
  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);
    }
  }
933
934
}

935

936
937
938
939
940
941
/**
 * 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
 */
David Metzler's avatar
David Metzler committed
942
function forena_report($name_in, $parms = NULL, $print = TRUE, $filename='') {
943
  require_once('forena.common.inc');
metzlerd's avatar
metzlerd committed
944
  $desc = Frx::Menu()->parseURL($name_in);
945
946
  $time = @new DateTime($value);
  $desc['time'] = date_format($time, 'Y-m-d H:i:s');
947
  $desc['basepath'] = base_path();
948
  $desc['path'] = $desc['basepath'] . Frx::File()->directory(Frx::File()->path($desc['filename'], TRUE));
949
950
  $desc['theme'] = $desc['basepath'] . drupal_get_path('theme',$GLOBALS['theme']);
  $desc['q'] = $_GET['q'];
951
  if (!$desc['exists']) {
952
    watchdog('error', 'Report %s not found', array('%s' => $name_in), WATCHDOG_ERROR);
metzlerd's avatar
metzlerd committed
953
954
955
956
    if($print) {
      drupal_not_found();
      exit;
    }
957
    return '';
958
  }
metzlerd's avatar
metzlerd committed
959
  FrxData::instance()->setContext('cookie', $_COOKIE);
960
  FrxData::instance()->setContext('report', $desc);
961
  $report_name = $desc['name'];
962
  // Load dataTable plugin if possible.
metzlerd's avatar
metzlerd committed
963
  //forena_include_data_tables();
David Metzler's avatar
David Metzler committed
964
  $content = Frx::Editor($name_in, FALSE)->report($parms, $print, FALSE, $filename);
965

966
  $m_path = drupal_get_path('module', 'forena');
967
  if ($content) {
968
    // This has been deprecated, but we'll eave it in for now.
metzlerd's avatar
metzlerd committed
969
    if ($print) drupal_add_js($m_path . '/forena.js');
970
971
972
    $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
973
    if ($print) drupal_add_js(array('forena' => $forena_js), 'setting');
974
  }
metzlerd's avatar
metzlerd committed
975
976
977
  if (!$print && is_array($content)) {
    return drupal_render($content['content']);
  }
978
  return $content;
979

980

981
982
}

983
/**
metzlerd's avatar
metzlerd committed
984
 * Render report as an inlcude.  Don't perform the output fuctnions for the document
985
986
987
 * @param $report_name String
 * @param $parms array of parameters to include
 */
metzlerd's avatar
metzlerd committed
988
function forena_report_include($report_name, $parms=array()) {
metzlerd's avatar
metzlerd committed
989
990
991
992
993
994
995
996
997
998
999
1000
  $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
1001
  $r->render($format, FALSE);
metzlerd's avatar
metzlerd committed
1002
1003
1004
1005
1006
1007

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

1010
1011

/**
1012
 * Menu callback for rendering the forena report.
1013
 */
1014
1015
function forena_report_menu_callback() {
  $args = func_get_args();
metzlerd's avatar
metzlerd committed
1016
1017
1018
1019
1020
  $path = array_shift($args);
  $report_name = array_shift($args);
  $parms = $_GET;
  unset($parms['q']);
  $menu_parms = array();
1021
  $tokens = Frx::Menu()->tokens($path);
metzlerd's avatar
metzlerd committed
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
  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;
1032
1033
1034
}

/**
1035
1036
 * Used in hook_menu to test access to menu items.
 * @param $checks array key value of callbacks and args to check.
1037
1038
1039
1040
 */
function forena_check_all_access($checks) {
  // Check each callback function to see if we have an error.
  $access = FALSE;
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
  // 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;
1055
1056
      }
    }
1057
1058
  }
  return $access;
1059