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

120
  $items['reports/%/view'] = array(
metzlerd's avatar
metzlerd committed
121
122
123
124
125
    'title' => 'View',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
    'file' => 'forena.common.inc',
    'access arguments' => array('design any report'),
metzlerd's avatar
metzlerd committed
126
  );
metzlerd's avatar
metzlerd committed
127

metzlerd's avatar
metzlerd committed
128
  $items['reports/%/edit'] = array(
129
     'title' => 'Edit',
130
131
     'page callback' => 'forena_report_preview',
     'page arguments' => array(1),
132
133
134
     'access arguments' => array('design any report'),
     'description' => 'Edit the layout of your report',
     'type' => MENU_LOCAL_TASK,
135
     'file' => 'forena.report.inc',
metzlerd's avatar
metzlerd committed
136
  );
137

metzlerd's avatar
metzlerd committed
138
139
  if (module_exists('locale')) {
    $items['reports/%/translations'] = array(
140
141
       'page callback' => 'forena_report_translations',
       'page arguments' => array(1),
142
       'title' => 'Translate',
metzlerd's avatar
metzlerd committed
143
       'file' => 'forena.report.inc',
144
145
       'access arguments' => array('design any report'),
       'type' => MENU_LOCAL_TASK,
metzlerd's avatar
metzlerd committed
146
147
    );
  }
metzlerd's avatar
metzlerd committed
148
  $items['reports/%/edit/parameters'] = array(
149
    'page callback' => 'drupal_get_form',
metzlerd's avatar
metzlerd committed
150
    'page arguments' => array('forena_report_parameters_form', 1),
151
    'title' => 'Parameters',
metzlerd's avatar
metzlerd committed
152
    'file' => 'forena.report.inc',
153
154
    'access arguments' => array('design any report'),
    'type' => MENU_LOCAL_TASK,
metzlerd's avatar
metzlerd committed
155
  );
156

metzlerd's avatar
metzlerd committed
157
158
159
160
161
162
163
164
165
166
  $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(
167
    'page callback' => 'drupal_get_form',
metzlerd's avatar
metzlerd committed
168
169
    'page arguments' => array('forena_report_add_parameter_form', 1),
    'file' => 'forena.report.inc',
170
171
172
    'title' => 'Parameters',
    'access arguments' => array('design any report'),
    'type' => MENU_CALLBACK,
metzlerd's avatar
metzlerd committed
173
  );
174

metzlerd's avatar
metzlerd committed
175
176
177
  $items['reports/%/edit/general'] = array(
    'title' => 'General',
    'page callback' => 'drupal_get_form',
metzlerd's avatar
metzlerd committed
178
    'page arguments' => array('forena_report_general_form',1),
metzlerd's avatar
metzlerd committed
179
180
    'access arguments' => array('design any report'),
    'description' => 'General Report Properties',
181
    'type' => MENU_LOCAL_TASK,
metzlerd's avatar
metzlerd committed
182
    'weight' => -1,
metzlerd's avatar
metzlerd committed
183
    'file' => 'forena.report.inc',
metzlerd's avatar
metzlerd committed
184
  );
185

metzlerd's avatar
metzlerd committed
186
  $items['reports/%/edit/layout'] = array(
187
188
    'title' => 'Layout',
    'page callback' => 'drupal_get_form',
metzlerd's avatar
metzlerd committed
189
    'page arguments' => array('forena_report_layout_form',1),
190
191
192
    'access arguments' => array('design any report'),
    'description' => 'Edit the layout of your report',
    'type' => MENU_LOCAL_TASK,
metzlerd's avatar
metzlerd committed
193
    'file' => 'forena.report.inc',
metzlerd's avatar
metzlerd committed
194
  );
metzlerd's avatar
metzlerd committed
195

David Metzler's avatar
Fixes    
David Metzler committed
196
  $items['reports/%/edit/edit-data/%/%'] = array(
197
198
199
200
201
202
203
   '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,
204
  );
metzlerd's avatar
metzlerd committed
205

206
207
208
209
210
211
212
213
214
215
  $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
216
  $items['reports/%/edit/select-data/%'] = array(
217
218
219
220
221
222
223
    '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
224
225
  );

David Metzler's avatar
David Metzler committed
226
227

  $items['reports/%/edit/select-data/%/%'] = array(
228
229
230
231
232
233
234
    '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
235
236
237
  );

  $items['reports/%/edit/preview-data/%/%'] = array(
238
239
240
241
242
243
244
    '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
245
246
247
  );

  $items['reports/%/edit/preview-data/%/%/%'] = array(
248
249
250
251
252
253
254
255
    '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
256

257
  $items['reports/%/edit/add-data/%'] = array(
258
259
260
261
262
263
264
    '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
265
  );
266

David Metzler's avatar
Fixes    
David Metzler committed
267
  $items['reports/%/edit/add-data/%/%'] = array(
268
269
270
271
272
273
274
    '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
275
276
277
  );

  $items['reports/%/edit/prepend-data/%/%'] = array(
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
    '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
295
296
  );

metzlerd's avatar
metzlerd committed
297
  $items['reports/%/edit/format'] = array(
298
     'title' => 'Document Types',
299
     'page callback' => 'drupal_get_form',
metzlerd's avatar
metzlerd committed
300
     'page arguments' => array('forena_report_format_form', 1),
301
     'access arguments' => array('design any report'),
metzlerd's avatar
metzlerd committed
302
     'file' => 'forena.report.inc',
303
     'description' => 'Document Options',
304
     'type' => MENU_LOCAL_TASK,
metzlerd's avatar
metzlerd committed
305
  );
306

metzlerd's avatar
metzlerd committed
307
  $items['reports/%/delete'] = array(
308
     'title' => 'Remove Custom Report',
metzlerd's avatar
metzlerd committed
309
     'page callback' => 'drupal_get_form',
metzlerd's avatar
metzlerd committed
310
     'page arguments' => array('forena_report_delete_form', 1),
metzlerd's avatar
metzlerd committed
311
     'access arguments' => array('delete report'),
metzlerd's avatar
metzlerd committed
312
313
     'type' => MENU_CALLBACK,
     'file' => 'forena.report.inc',
metzlerd's avatar
metzlerd committed
314
  );
metzlerd's avatar
metzlerd committed
315

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

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

metzlerd's avatar
metzlerd committed
336
  $items['reports/%/translate'] = array(
337
338
339
     'title' => 'Translate Report',
     'page callback' => 'drupal_get_form',
     'page arguments' => array('forena_create_trans_form',1),
340
341
     'access arguments' => array('create any report'),
     'description' => 'Create a new report translation',
342
     'file' => 'forena.admin.inc',
343
     'type' => MENU_CALLBACK,
metzlerd's avatar
metzlerd committed
344
  );
metzlerd's avatar
metzlerd committed
345

346
347
  $items['forena'] = array(
    'page callback' => 'forena_user_reports',
metzlerd's avatar
metzlerd committed
348
349
350
    'page arguments' => array(),
    'title' => 'My Reports',
    'access arguments' => array('list reports'),
351
    'type' => MENU_NORMAL_ITEM,
metzlerd's avatar
metzlerd committed
352
  );
metzlerd's avatar
metzlerd committed
353

metzlerd's avatar
metzlerd committed
354
  $items['forena/xml/%'] = array(
355
356
   	'page callback' => 'forena_block_xml',
    'page arguments' => array(2),
357
   	'access arguments' => array('access forena block xml'),
358
359
    'file' => 'forena.common.inc',
   	'type' => MENU_CALLBACK,
metzlerd's avatar
metzlerd committed
360
  );
361

362

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

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

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


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

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

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

}

metzlerd's avatar
metzlerd committed
402

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

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

metzlerd's avatar
metzlerd committed
441
442
}

443

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

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



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

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

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

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

metzlerd's avatar
metzlerd committed
512
513
514
515
516
517
/**
 * Auto complete for formats
 * @param $string
 * @return unknown_type
 */
function forena_fields_format_autocomplete($string='') {
metzlerd's avatar
metzlerd committed
518
519
  require_once('forena.common.inc');
  $matches = array();
metzlerd's avatar
metzlerd committed
520
  $formats = @FrxReportGenerator::instance()->supported_formats();
metzlerd's avatar
metzlerd committed
521
522
523

  if ($string == "*") {
    $matches = $formats;
metzlerd's avatar
metzlerd committed
524
    print drupal_json_output($matches);
metzlerd's avatar
metzlerd committed
525
526
527
528
529
530
531
532
    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
533
  print drupal_json_output($matches);
metzlerd's avatar
metzlerd committed
534
}
535

536
537
/**
 *  Calls forena_parameter_form
538
539
 *  in forena.common.inc
 */
metzlerd's avatar
metzlerd committed
540
function forena_parameters_report() {
541
  require_once('forena.admin.inc');
542
543
544
  $m = Frx::Menu();
  $name = $m->name;
  $filename = $m->filename;
metzlerd's avatar
metzlerd committed
545
  $format = $m->format;
metzlerd's avatar
metzlerd committed
546
547
548
  $report = forena_get_report($name);
  if ($report) {
    $r = forena_report_object();
metzlerd's avatar
metzlerd committed
549
550
    $o = drupal_get_form('forena_parameters_form');
    return $o;
metzlerd's avatar
metzlerd committed
551
552
553
  }
  else {
    drupal_not_found();
554
555
556
  }
}

557
function forena_parameter_form($formid, &$form_state, $parameters, $attributes= array()) {
metzlerd's avatar
metzlerd committed
558
  $parms = $_GET;
559
  // Set menu parms
metzlerd's avatar
metzlerd committed
560
  $menu_parms = Frx::Data()->getContext('menu-parms');
561
562
  if ($menu_parms) $form_state['storage']['menu-parms'] = $menu_parms;
  // Set Descriptors
563
  $desc = FrxData::instance()->getContext('report');
564
  $form_state['storage']['desc'] = $desc;
565
  $desc = $form_state['storage']['desc'];
566
  $report_name = @$desc['name'];
567

568
  $collapse = isset($attributes['collapsed']) ? $attributes['collapsed'] : FALSE;
metzlerd's avatar
metzlerd committed
569
570
571
  if (isset($form_state['values'])) {
    $collapse=FALSE;
    $parms = array_merge($parms, $form_state['values']['params']);
metzlerd's avatar
metzlerd committed
572
573
574
575
576
577
    // 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);

    }
578
    drupal_alter('forena_parameters', $report_name,  $parms);
metzlerd's avatar
metzlerd committed
579
580
    Frx::Data()->push($parms, 'parm');
  }
581
582
583
584
  $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');
585

metzlerd's avatar
metzlerd committed
586
587
  unset($parms['q']);
  $form = array();
metzlerd's avatar
metzlerd committed
588

589

metzlerd's avatar
metzlerd committed
590
  if ($parameters) {
591

592
    //drupal_alter('forena_parameters', $report_name,  $parms);
metzlerd's avatar
metzlerd committed
593
    if ($parameters) {
metzlerd's avatar
metzlerd committed
594

metzlerd's avatar
metzlerd committed
595
      $form['params'] = array(
metzlerd's avatar
metzlerd committed
596
          '#tree' => TRUE,
597
          '#title' => $title,
metzlerd's avatar
metzlerd committed
598
          '#type' => 'fieldset',
599
          '#collapsible' => $collapsible,
metzlerd's avatar
metzlerd committed
600
601
602
          '#collapsed' => $collapse,
          '#prefix' => '<div id="parameters-wrapper">',
          '#suffix' => '</div>',
metzlerd's avatar
metzlerd committed
603
604
605
      );

      foreach ($parameters as $node) {
metzlerd's avatar
metzlerd committed
606
607
        $add_null = FALSE;
        $list=array();
metzlerd's avatar
metzlerd committed
608
609
610
611
612
613
614
615
616
617
618
619
        $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
620

metzlerd's avatar
metzlerd committed
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
        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();

636
637
638
639
640
641
        //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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
        //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
673
674
675
676
677
            $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) {
678
                $value = date('Y-m-d h:i', $datetime);
metzlerd's avatar
metzlerd committed
679
680
              }

metzlerd's avatar
metzlerd committed
681
            }
682
            $ctl_attrs['#forena_date_format'] = @$options['date_parm_format'] ? $options['date_parm_format'] : 'Y-m-d';
metzlerd's avatar
metzlerd committed
683

metzlerd's avatar
metzlerd committed
684
685
686
687
688
689
            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
690

metzlerd's avatar
metzlerd committed
691
            $list=array();
metzlerd's avatar
metzlerd committed
692
693
694
695
696
            break;
          case 'checkbox':
            if (@$option_str['return_value']) {
              $ctl_attrs['#return_value'] = $options['return_value'];
            }
metzlerd's avatar
metzlerd committed
697
            $list=array();
metzlerd's avatar
metzlerd committed
698
699
700
701
            break;
          case 'radios':
            break;
          case 'hidden':
metzlerd's avatar
metzlerd committed
702
            $list=array();
metzlerd's avatar
metzlerd committed
703
704
705
            break;
          default:
            $type = 'textfield';
706
            $list = array();
metzlerd's avatar
metzlerd committed
707
        }
metzlerd's avatar
metzlerd committed
708

metzlerd's avatar
metzlerd committed
709
710
711
        if (isset($menu_parms[$id]) && $type=='textfield') {
          $disabled = TRUE;
        }
metzlerd's avatar
metzlerd committed
712

metzlerd's avatar
metzlerd committed
713
714
        //If a data_source attr was found then create an array of
        $form['params'][$id] = array(
metzlerd's avatar
metzlerd committed
715
716
717
            '#type' => $type,
            '#title' => ($label) ? t($label) : t($id),
            '#default_value' => $value,
718
            '#disabled' => $disabled,
metzlerd's avatar
metzlerd committed
719
720
            '#required' => $required,
            '#description' => t($desc),
metzlerd's avatar
metzlerd committed
721
        );
metzlerd's avatar
metzlerd committed
722

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

metzlerd's avatar
metzlerd committed
725
726
        if ($type == 'item') {
          $form['params'][$id]['#markup'] = $value;
metzlerd's avatar
metzlerd committed
727

metzlerd's avatar
metzlerd committed
728
        }
metzlerd's avatar
metzlerd committed
729

metzlerd's avatar
metzlerd committed
730
        if ($type == 'hidden') {
metzlerd's avatar
metzlerd committed
731
          $form['params'][$id]['#value'] = $value;
metzlerd's avatar
metzlerd committed
732
        }
metzlerd's avatar
metzlerd committed
733

metzlerd's avatar
metzlerd committed
734
735
736
        // Add class to parmeter form.
        if ($class) {
          $form['params'][$id]['#attributes'] = array(
metzlerd's avatar
metzlerd committed
737
              'class' => @explode(' ', $class),
metzlerd's avatar
metzlerd committed
738
739
          );
        }
metzlerd's avatar
metzlerd committed
740

metzlerd's avatar
metzlerd committed
741
742
743
        //if $list is not empty then push options
        //onto the array. options will cause an error for
        //textfield elements.
744
        if ($list || $type == 'select' || $type =='radios') {
745
746
          if ($add_null) {
            $prompt = @$options['prompt'];
metzlerd's avatar
metzlerd committed
747
            if (!$prompt) $prompt = $required ? '-' . t('select') .  '-' : '-' .t('none') . '-';
748
749
            $form['params'][$id]['#empty_option'] = $prompt ;
          }
metzlerd's avatar
metzlerd committed
750
751
752
          $form['params'][$id]['#options'] = $list;
          $form['params'][$id]['#multiple'] = $multiselect;
        }
metzlerd's avatar
metzlerd committed
753

metzlerd's avatar
metzlerd committed
754
755
        if ($ajax) {
          $form['params'][$id]['#ajax'] = array('callback' => 'forena_parameters_callback',
metzlerd's avatar
metzlerd committed
756
              'wrapper' => 'parameters-wrapper');
metzlerd's avatar
metzlerd committed
757
758
        }

metzlerd's avatar
metzlerd committed
759
760
761
762
763
764
765
      }

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

metzlerd's avatar
metzlerd committed
767
      $form['params']['submit'] = array(
metzlerd's avatar
metzlerd committed
768
          '#type' => 'submit',
769
          '#value' => $submit_value,
metzlerd's avatar
metzlerd committed
770
      );
771

metzlerd's avatar
metzlerd committed
772
    }
metzlerd's avatar
metzlerd committed
773
774
    return $form;
  }
metzlerd's avatar
metzlerd committed
775
776
777
778
779
780
781
782
783
784
785
786
787
788


}


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

metzlerd's avatar
metzlerd committed
790
  if (isset($values['params'])) foreach ($values['params'] as $key => $value) {
791
792
793
794
795
796
797
    $ctl = $form['params'][$key];
    switch($ctl['#type']) {
      case 'date_popup':
      case 'date_select':
      case 'date_text':
        $datetime = @strtotime($value);
        if ($datetime) {
798
          $value = $values['params'][$key] = date($ctl['#forena_date_format'], $datetime);
799
800
801
802
803
        }
        break;

    }

metzlerd's avatar
metzlerd committed
804
    if (is_array($value)) {
805
      $values['params'][$key] = array();
metzlerd's avatar
metzlerd committed
806
      foreach ($value as $k => $val) {
807
808
        if ($val) {
          $values['params'][$key][] = $val;
metzlerd's avatar
metzlerd committed
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
        }
      }
    }
    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'];
}

834
835
836
837
838
/**
 * Implementation of hook_perm
 *
 * @return unknown
 */
metzlerd's avatar
metzlerd committed
839
function forena_permission() {
840
  $perms = array(
metzlerd's avatar
metzlerd committed
841
    'administer forena reports' => array('title' => t('Administer Forena Reports')),
842
    'access demo reports' => array('title' => t('Access Demo Reports')),
metzlerd's avatar
metzlerd committed
843
844
845
846
    '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')),
847
    'perform email merge' => array('title' => t('Peform email merge')),
848
    '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
849
  );
850
851
852
853
  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
854
  return $perms;
855
856
}

857

metzlerd's avatar
metzlerd committed
858

859
function forena_user_reports($category = '') {
metzlerd's avatar
metzlerd committed
860
  require_once('forena.common.inc');
metzlerd's avatar
metzlerd committed
861
  $output='';
862
  $reports = forena_get_user_reports($category);
metzlerd's avatar
metzlerd committed
863
  $report_repos = variable_get('forena_path', 'reports');
864
865
866
  if (!$reports) {
    $output = 'No Reports Found';
  }
metzlerd's avatar
metzlerd committed
867
  $links = '';
metzlerd's avatar
metzlerd committed
868
  foreach ($reports as $category => $reports) {
metzlerd's avatar
metzlerd committed
869
    $links .= '<li><a href="#' . urlencode($category) . '">' . $category . '</a></li> ';
870
    $output .= '<h3 id="' . urlencode($category) . '">' . $category . '</h3>';
metzlerd's avatar
metzlerd committed
871
872
873
    $output .= '<ul>';
    foreach ($reports as $r) {
      $output .= '<li>' . l($r['title'], $report_repos . '/' . str_replace('/', '.', $r['report_name'])) . '</li>';
874
    }
metzlerd's avatar
metzlerd committed
875
    $output .= '</ul>';
metzlerd's avatar
metzlerd committed
876
  }
metzlerd's avatar
metzlerd committed
877
  return $output;
metzlerd's avatar
metzlerd committed
878
879
}

880
881
882
/**
 * Tries to load data tables libary and other libraries that help.
 */
883
function forena_include_data_tables() {
metzlerd's avatar
metzlerd committed
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
  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);
    }
  }
900
901
}

902

903
904
905
906
907
908
/**
 * 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
 */
909
function forena_report($name_in, $parms = NULL, $print = TRUE) {
910
  require_once('forena.common.inc');
metzlerd's avatar
metzlerd committed
911
  $desc = Frx::Menu()->parseURL($name_in);
912
913
  $time = @new DateTime($value);
  $desc['time'] = date_format($time, 'Y-m-d H:i:s');
914
  $desc['basepath'] = base_path();
915
  if (!$desc['exists']) {
916
917
    drupal_set_message(t('Report %s not found', array('%s' => $name_in)), 'error', FALSE);
    return '';
918
  }
metzlerd's avatar
metzlerd committed
919
920

  FrxData::instance()->setContext('cookie', $_COOKIE);
921
  FrxData::instance()->setContext('report', $desc);
922
  $report_name = $desc['name'];
923
  // Load dataTable plugin if possible.
metzlerd's avatar
metzlerd committed
924
  //forena_include_data_tables();
925
  $content = Frx::Editor($name_in, $print)->report($parms, $print, FALSE);
926

927
  $m_path = drupal_get_path('module', 'forena');
928
  if ($content) {
929
    // This has been deprecated, but we'll eave it in for now.
metzlerd's avatar
metzlerd committed
930
    if ($print) drupal_add_js($m_path . '/forena.js');
931
932
933
    $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
934
    if ($print) drupal_add_js(array('forena' => $forena_js), 'setting');
935
  }
936
  if (!$print) {
937
    return drupal_render($content['content']);
938
  }
939
  return $content;
940

941

942
943
}

944
/**
metzlerd's avatar
metzlerd committed
945
 * Render report as an inlcude.  Don't perform the output fuctnions for the document
946
947
948
 * @param $report_name String
 * @param $parms array of parameters to include
 */
metzlerd's avatar
metzlerd committed
949
function forena_report_include($report_name, $parms=array()) {
metzlerd's avatar
metzlerd committed
950
951
952
953
954
955
956
957
958
959
960
961
  $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
962
  $r->render($format, FALSE);
metzlerd's avatar
metzlerd committed
963
964
965
966
967
968

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

971
972

/**
973
 * Menu callback for rendering the forena report.
974
 */
975
976
function forena_report_menu_callback() {
  $args = func_get_args();
metzlerd's avatar
metzlerd committed
977
978
979
980
981
  $path = array_shift($args);
  $report_name = array_shift($args);
  $parms = $_GET;
  unset($parms['q']);
  $menu_parms = array();
982
  $tokens = Frx::Menu()->tokens($path);
metzlerd's avatar
metzlerd committed
983
984
985
986
987
988
989
990
991
992
  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;
993
994
995
}

/**
996
997
 * Used in hook_menu to test access to menu items.
 * @param $checks array key value of callbacks and args to check.