forena.module 46.6 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
193
    'access arguments' => array('design any report'),
    'description' => 'Edit the layout of your report',
    'type' => MENU_LOCAL_TASK,
    'weight' => 1,
metzlerd's avatar
metzlerd committed
194
    'file' => 'forena.report.inc',
metzlerd's avatar
metzlerd committed
195
  );
metzlerd's avatar
metzlerd committed
196

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

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

David Metzler's avatar
David Metzler committed
227
228

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

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

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

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

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

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

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

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

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

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

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

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

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

363

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

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

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


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

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

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

}

metzlerd's avatar
metzlerd committed
403

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

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

metzlerd's avatar
metzlerd committed
442
443
}

444

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

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



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

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

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

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

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

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

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

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

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

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

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

590

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

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

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

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

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

637
638
639
640
641
642
        //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
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
673
        //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
674
675
676
677
678
            $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) {
679
                $value = date('Y-m-d h:i', $datetime);
metzlerd's avatar
metzlerd committed
680
681
              }

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

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

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

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

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

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

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

metzlerd's avatar
metzlerd committed
729
        }
metzlerd's avatar
metzlerd committed
730

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

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

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

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

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

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

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

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


}


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

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

    }

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

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

858

metzlerd's avatar
metzlerd committed
859

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

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

903

904
905
906
907
908
909
/**
 * 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
 */
910
function forena_report($name_in, $parms = NULL, $print = TRUE) {
911
  require_once('forena.common.inc');
metzlerd's avatar
metzlerd committed
912
  $desc = Frx::Menu()->parseURL($name_in);
913
914
  $time = @new DateTime($value);
  $desc['time'] = date_format($time, 'Y-m-d H:i:s');
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
926
  $content = Frx::Editor($name_in, FALSE)->report($parms, $print, FALSE);

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
937
938
  if (!$print) {
    return drupal_render($content);
  }
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.
998
999
1000
1001
 */
function forena_check_all_access($checks) {
  // Check each callback function to see if we have an error.
  $access = FALSE;
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
  // 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) {