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

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

metzlerd's avatar
metzlerd committed
355
  $items['forena/fields/format/autocomplete'] = array(
metzlerd's avatar
metzlerd committed
356
357
358
     'page callback' => 'forena_fields_format_autocomplete',
     'access callback' => TRUE,
     'type' => MENU_CALLBACK,
metzlerd's avatar
metzlerd committed
359
  );
metzlerd's avatar
metzlerd committed
360

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

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


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

  return $items;
metzlerd's avatar
metzlerd committed
381
}
metzlerd's avatar
metzlerd committed
382

383
384
385
386
/**
 * Implements hook_admin_paths_alter().
 */
function forena_admin_paths_alter(&$paths) {
metzlerd's avatar
metzlerd committed
387
388
  $paths['reports/*/*'] = TRUE;
  $paths['reports/*/view'] = FALSE;
389
  $paths['reports/*/edit'] = FALSE;
metzlerd's avatar
metzlerd committed
390
  $paths['reports/add'] = TRUE;
391
392
393

}

metzlerd's avatar
metzlerd committed
394

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

metzlerd's avatar
metzlerd committed
423
424
425
426
427
  // Inlcude the base library
  require_once('forena.common.inc');
  switch ($delta) {
    case 'forena_reports':
      $block = array('subject' => 'My Reports',
metzlerd's avatar
metzlerd committed
428
            'content' => forena_my_reports_block());
metzlerd's avatar
metzlerd committed
429
430
431
      break;
  }
  return $block;
metzlerd's avatar
metzlerd committed
432

metzlerd's avatar
metzlerd committed
433
434
}

435

metzlerd's avatar
metzlerd committed
436
437
/**
 * Auto complete for categories
metzlerd's avatar
metzlerd committed
438
 *
metzlerd's avatar
metzlerd committed
439
440
441
442
 */

function forena_categories_autocomplete($string='') {
  require_once('forena.admin.inc');
metzlerd's avatar
metzlerd committed
443
  $categories = @forena_get_categories($string);
metzlerd's avatar
metzlerd committed
444
  print drupal_json_output($categories);
metzlerd's avatar
metzlerd committed
445
446
447
448
}



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

metzlerd's avatar
metzlerd committed
474
  print drupal_json_output($data_blocks);
metzlerd's avatar
metzlerd committed
475
476
}

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

function forena_xml($block_name, $parms=array()) {
metzlerd's avatar
metzlerd committed
497
498
499
500
501
  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);
502
503
}

metzlerd's avatar
metzlerd committed
504
505
506
507
508
509
/**
 * Auto complete for formats
 * @param $string
 * @return unknown_type
 */
function forena_fields_format_autocomplete($string='') {
metzlerd's avatar
metzlerd committed
510
511
  require_once('forena.common.inc');
  $matches = array();
metzlerd's avatar
metzlerd committed
512
  $formats = @FrxReportGenerator::instance()->supported_formats();
metzlerd's avatar
metzlerd committed
513
514
515

  if ($string == "*") {
    $matches = $formats;
metzlerd's avatar
metzlerd committed
516
    print drupal_json_output($matches);
metzlerd's avatar
metzlerd committed
517
518
519
520
521
522
523
524
    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
525
  print drupal_json_output($matches);
metzlerd's avatar
metzlerd committed
526
}
527

528
529
/**
 *  Calls forena_parameter_form
530
531
 *  in forena.common.inc
 */
metzlerd's avatar
metzlerd committed
532
function forena_parameters_report() {
533
  require_once('forena.admin.inc');
534
535
536
  $m = Frx::Menu();
  $name = $m->name;
  $filename = $m->filename;
metzlerd's avatar
metzlerd committed
537
  $format = $m->format;
metzlerd's avatar
metzlerd committed
538
539
540
  $report = forena_get_report($name);
  if ($report) {
    $r = forena_report_object();
metzlerd's avatar
metzlerd committed
541
542
    $o = drupal_get_form('forena_parameters_form');
    return $o;
metzlerd's avatar
metzlerd committed
543
544
545
  }
  else {
    drupal_not_found();
546
547
548
  }
}

549
function forena_parameter_form($formid, &$form_state, $parameters, $attributes= array()) {
metzlerd's avatar
metzlerd committed
550
  $parms = $_GET;
551
  // Set menu parms
metzlerd's avatar
metzlerd committed
552
  $menu_parms = Frx::Data()->getContext('menu-parms');
553
554
  if ($menu_parms) $form_state['storage']['menu-parms'] = $menu_parms;
  // Set Descriptors
555
  $desc = FrxData::instance()->getContext('report');
556
  $form_state['storage']['desc'] = $desc;
557
  $desc = $form_state['storage']['desc'];
558
  $report_name = @$desc['name'];
559

560
  $collapse = isset($attributes['collapsed']) ? $attributes['collapsed'] : FALSE;
metzlerd's avatar
metzlerd committed
561
562
563
  if (isset($form_state['values'])) {
    $collapse=FALSE;
    $parms = array_merge($parms, $form_state['values']['params']);
metzlerd's avatar
metzlerd committed
564
565
566
567
568
569
    // 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);

    }
570
    drupal_alter('forena_parameters', $report_name,  $parms);
metzlerd's avatar
metzlerd committed
571
572
    Frx::Data()->push($parms, 'parm');
  }
573
574
575
576
  $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');
577

metzlerd's avatar
metzlerd committed
578
579
  unset($parms['q']);
  $form = array();
metzlerd's avatar
metzlerd committed
580

581

metzlerd's avatar
metzlerd committed
582
  if ($parameters) {
583

584
    //drupal_alter('forena_parameters', $report_name,  $parms);
metzlerd's avatar
metzlerd committed
585
    if ($parameters) {
metzlerd's avatar
metzlerd committed
586

metzlerd's avatar
metzlerd committed
587
      $form['params'] = array(
metzlerd's avatar
metzlerd committed
588
          '#tree' => TRUE,
589
          '#title' => $title,
metzlerd's avatar
metzlerd committed
590
          '#type' => 'fieldset',
591
          '#collapsible' => $collapsible,
metzlerd's avatar
metzlerd committed
592
593
594
          '#collapsed' => $collapse,
          '#prefix' => '<div id="parameters-wrapper">',
          '#suffix' => '</div>',
metzlerd's avatar
metzlerd committed
595
596
597
      );

      foreach ($parameters as $node) {
metzlerd's avatar
metzlerd committed
598
599
        $add_null = FALSE;
        $list=array();
metzlerd's avatar
metzlerd committed
600
601
602
603
604
605
606
607
608
609
610
611
        $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
612

metzlerd's avatar
metzlerd committed
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
        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();

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

metzlerd's avatar
metzlerd committed
673
            }
674
            $ctl_attrs['#forena_date_format'] = @$options['date_parm_format'] ? $options['date_parm_format'] : 'Y-m-d';
metzlerd's avatar
metzlerd committed
675

metzlerd's avatar
metzlerd committed
676
677
678
679
680
681
            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
682

metzlerd's avatar
metzlerd committed
683
            $list=array();
metzlerd's avatar
metzlerd committed
684
685
686
687
688
            break;
          case 'checkbox':
            if (@$option_str['return_value']) {
              $ctl_attrs['#return_value'] = $options['return_value'];
            }
metzlerd's avatar
metzlerd committed
689
            $list=array();
metzlerd's avatar
metzlerd committed
690
691
692
693
            break;
          case 'radios':
            break;
          case 'hidden':
metzlerd's avatar
metzlerd committed
694
            $list=array();
metzlerd's avatar
metzlerd committed
695
696
697
            break;
          default:
            $type = 'textfield';
698
            $list = array();
metzlerd's avatar
metzlerd committed
699
        }
metzlerd's avatar
metzlerd committed
700

701
        if (isset($menu_parms[$id]) && $type!='hidden') {
metzlerd's avatar
metzlerd committed
702
703
          $disabled = TRUE;
        }
metzlerd's avatar
metzlerd committed
704

metzlerd's avatar
metzlerd committed
705
706
        //If a data_source attr was found then create an array of
        $form['params'][$id] = array(
metzlerd's avatar
metzlerd committed
707
708
709
            '#type' => $type,
            '#title' => ($label) ? t($label) : t($id),
            '#default_value' => $value,
710
            '#disabled' => $disabled,
metzlerd's avatar
metzlerd committed
711
712
            '#required' => $required,
            '#description' => t($desc),
metzlerd's avatar
metzlerd committed
713
        );
metzlerd's avatar
metzlerd committed
714

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

metzlerd's avatar
metzlerd committed
717
718
        if ($type == 'item') {
          $form['params'][$id]['#markup'] = $value;
metzlerd's avatar
metzlerd committed
719

metzlerd's avatar
metzlerd committed
720
        }
metzlerd's avatar
metzlerd committed
721

metzlerd's avatar
metzlerd committed
722
        if ($type == 'hidden') {
metzlerd's avatar
metzlerd committed
723
          $form['params'][$id]['#value'] = $value;
metzlerd's avatar
metzlerd committed
724
        }
metzlerd's avatar
metzlerd committed
725

metzlerd's avatar
metzlerd committed
726
727
728
        // Add class to parmeter form.
        if ($class) {
          $form['params'][$id]['#attributes'] = array(
metzlerd's avatar
metzlerd committed
729
              'class' => @explode(' ', $class),
metzlerd's avatar
metzlerd committed
730
731
          );
        }
metzlerd's avatar
metzlerd committed
732

metzlerd's avatar
metzlerd committed
733
734
735
        //if $list is not empty then push options
        //onto the array. options will cause an error for
        //textfield elements.
736
        if ($list || $type == 'select' || $type =='radios') {
737
738
          if ($add_null) {
            $prompt = @$options['prompt'];
metzlerd's avatar
metzlerd committed
739
            if (!$prompt) $prompt = $required ? '-' . t('select') .  '-' : '-' .t('none') . '-';
740
741
            $form['params'][$id]['#empty_option'] = $prompt ;
          }
metzlerd's avatar
metzlerd committed
742
743
744
          $form['params'][$id]['#options'] = $list;
          $form['params'][$id]['#multiple'] = $multiselect;
        }
metzlerd's avatar
metzlerd committed
745

metzlerd's avatar
metzlerd committed
746
747
        if ($ajax) {
          $form['params'][$id]['#ajax'] = array('callback' => 'forena_parameters_callback',
metzlerd's avatar
metzlerd committed
748
              'wrapper' => 'parameters-wrapper');
metzlerd's avatar
metzlerd committed
749
750
        }

metzlerd's avatar
metzlerd committed
751
752
753
754
755
756
757
      }

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

metzlerd's avatar
metzlerd committed
759
      $form['params']['submit'] = array(
metzlerd's avatar
metzlerd committed
760
          '#type' => 'submit',
761
          '#value' => $submit_value,
metzlerd's avatar
metzlerd committed
762
      );
763

metzlerd's avatar
metzlerd committed
764
    }
metzlerd's avatar
metzlerd committed
765
766
    return $form;
  }
metzlerd's avatar
metzlerd committed
767
768
769
770
771
772
773
774
775
776
777
778
779
780


}


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

metzlerd's avatar
metzlerd committed
782
  if (isset($values['params'])) foreach ($values['params'] as $key => $value) {
783
784
785
786
787
788
789
    $ctl = $form['params'][$key];
    switch($ctl['#type']) {
      case 'date_popup':
      case 'date_select':
      case 'date_text':
        $datetime = @strtotime($value);
        if ($datetime) {
790
          $value = $values['params'][$key] = date($ctl['#forena_date_format'], $datetime);
791
792
793
794
795
        }
        break;

    }

metzlerd's avatar
metzlerd committed
796
    if (is_array($value)) {
797
      $values['params'][$key] = array();
metzlerd's avatar
metzlerd committed
798
      foreach ($value as $k => $val) {
799
800
        if ($val) {
          $values['params'][$key][] = $val;
metzlerd's avatar
metzlerd committed
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
        }
      }
    }
    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'];
}

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

849

metzlerd's avatar
metzlerd committed
850

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

872
873
874
/**
 * Tries to load data tables libary and other libraries that help.
 */
875
function forena_include_data_tables() {
metzlerd's avatar
metzlerd committed
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
  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);
    }
  }
892
893
}

894

895
896
897
898
899
900
/**
 * 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
901
function forena_report($name_in, $parms = NULL, $print = TRUE, $filename='') {
902
  require_once('forena.common.inc');
metzlerd's avatar
metzlerd committed
903
  $desc = Frx::Menu()->parseURL($name_in);
904
905
  $time = @new DateTime($value);
  $desc['time'] = date_format($time, 'Y-m-d H:i:s');
906
  $desc['basepath'] = base_path();
907
  if (!$desc['exists']) {
908
    watchdog('error', 'Report %s not found', array('%s' => $name_in), WATCHDOG_ERROR);
metzlerd's avatar
metzlerd committed
909
910
911
912
    if($print) {
      drupal_not_found();
      exit;
    }
913
    return '';
914
  }
metzlerd's avatar
metzlerd committed
915
916

  FrxData::instance()->setContext('cookie', $_COOKIE);
917
  FrxData::instance()->setContext('report', $desc);
918
  $report_name = $desc['name'];
919
  // Load dataTable plugin if possible.
metzlerd's avatar
metzlerd committed
920
  //forena_include_data_tables();
David Metzler's avatar
David Metzler committed
921
  $content = Frx::Editor($name_in, FALSE)->report($parms, $print, FALSE, $filename);
922

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

937

938
939
}

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

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

967
968

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

/**
992
993
 * Used in hook_menu to test access to menu items.
 * @param $checks array key value of callbacks and args to check.
994
995
996
997
 */
function forena_check_all_access($checks) {
  // Check each callback function to see if we have an error.
  $access = FALSE;