forena.admin.inc 73.9 KB
Newer Older
1
<?php
metzlerd's avatar
metzlerd committed
2
// $Id$
metzlerd's avatar
metzlerd committed
3
4
/**
 * @file
metzlerd's avatar
metzlerd committed
5
 * Report administration forms and functions.
metzlerd's avatar
metzlerd committed
6
 */
metzlerd's avatar
metzlerd committed
7
require_once('forena.common.inc');
8

9
10
11
12
13
/**
 * Display reports to edit for admins in the structure menu
 * Enter description here ...
 */
function forena_admin_reports() {
14
  GLOBAL $language;
15
  $data = array();
16
  $links[] = array('href' => 'reports/add', 'title' => 'Create New Report' );
17
  $content = drupal_get_form('forena_sync_form');
18
19
  $output = drupal_render($content);
  $output .= theme('links',
20
21
22
23
    array(
      'links' => $links,
      'attributes' => array('class' => 'action-links'),
    )
metzlerd's avatar
metzlerd committed
24
  );
25
  // Add Data tables if it exists.
26

27
28
29
30
31
  drupal_add_css(drupal_get_path('module', 'forena') . '/forena.css');
  if (file_exists('sites/all/libraries/dataTables/media/js/jquery.dataTables.min.js')) {
    drupal_add_js(drupal_get_path('module', 'forena') . '/forena.admin.js');
    drupal_add_js('sites/all/libraries/dataTables/media/js/jquery.dataTables.min.js');
  }
32
33
  $headers = array(t('category'), t('title'), t('name'), t('operation'));
  $result = Frx::File()->allReports();
34
  foreach ($result as $row) {
35
    $rpt = str_replace('/', '.', $row->name);
36
37
38
39
40
41
    if ($row->include) {
      $edit = l(t('Override'), 'reports/' . $rpt . '/edit');
    }
    else {
      $edit = l(t('Edit'), 'reports/' . $rpt . '/edit');
    }
metzlerd's avatar
metzlerd committed
42
    $clone = l(t('Clone'), 'reports/add/' . $rpt);
43
44
45
46
47
48
49
50
51
52
53
    // Determine the nature of the report delete link.
    if ($row->override) {
      $delete = l(t('Revert'), 'reports/' . $rpt . '/delete', array('query' => array('destination' => 'admin/structure/forena')));
    }
    else if (!$row->include) {
      $delete = l(t('Delete'), 'reports/' . $rpt . '/delete', array('query' => array('destination' => 'admin/structure/forena')));
    }
    else {
      $delete = '';
    }

54
55
    $title = l(t($row->cache['title']), 'reports/' . $rpt);
    $data[] = array($row->cache['category'], $title, $row->name, $edit . ' ' . $clone . ' ' . $delete);
56
  }
57
  $output .= '<div id="forena-reports-list">';
58
  $output .= theme_table(array('header' => $headers, 'rows' => $data, 'attributes' => array('class' => array( 'dataTable-paged')), 'caption' => '', 'sticky' => TRUE, 'colgroups' => array(), 'empty' => ''));
59
  $output .= '</div>';
60
61
62
  return $output;
}

63
/**
metzlerd's avatar
metzlerd committed
64
 * Remove the report from the database and file system.
65
66
 * @param string $report_name
 */
67
function forena_delete_report($report_name) {
68

69
  $filepath = $report_name . '.frx';
70
  $do = Frx::File()->delete($filepath);
71
72
}

metzlerd's avatar
metzlerd committed
73

74
75


76
function forena_filter_element($fmt, $name) {
metzlerd's avatar
metzlerd committed
77
78
79
80
  global $user;
  $element['format'] =  array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
81
    '#collapsed' => $fmt!='',
metzlerd's avatar
metzlerd committed
82
83
    '#title' => t('Input formats'),
  );
84
  if (!$fmt) $fmt='full_html';
metzlerd's avatar
metzlerd committed
85
  // Get a list of formats that the current user has access to.
metzlerd's avatar
metzlerd committed
86
87
88
89
90
  $formats = filter_formats($user);
  foreach ($formats as $format) {
    $options[$format->format] = $format->name;
    $element['format']['guidelines'][$format->format] = array(
      '#theme' => 'filter_guidelines',
91
      '#required' => TRUE,
metzlerd's avatar
metzlerd committed
92
93
94
95
96
      '#format' => $format,
    );
  }
  $element['format']['guidelines']['#weight']=12;

97
  $element['format'][$name] = array(
metzlerd's avatar
metzlerd committed
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
    '#type' => 'select',
    '#title' => t('Text format'),
    '#options' => $options,
    '#default_value' => $fmt,
    '#access' => count($formats) > 1,
    '#weight' => 10,
    '#attributes' => array('class' => array('filter-list')),
  );

  $element['format']['help'] = array(
    '#type' => 'container',
    '#theme' => 'filter_tips_more_info',
    '#attributes' => array('class' => array('filter-help')),
    '#weight' => 11,
  );
metzlerd's avatar
metzlerd committed
113
  return $element['format'];
metzlerd's avatar
metzlerd committed
114
}
115

metzlerd's avatar
metzlerd committed
116
117


118
/**
119
120
121
 * Report syncronization form
 * @param $formid
 * @param $form_state
122
 */
123
function forena_sync_form($formid, &$form_state) {
124
125
126
127

  $form['sync_overwrite'] = array(
    '#type' => 'checkbox',
    '#title' => t('Revert all delivered reports to orignial'),
128
    '#required' => TRUE,
129
130
131
    '#description' => t('All customizations to module delivered reports will be lost.')
  );

132
133
  $form['sync'] = array(
    '#type' => 'submit',
134
    '#value' => t('Revert'),
135
136
    '#submit' => array('forena_settings_sync_submit'),
  );
137
138

  return $form;
139
140
141
142
143
144
145
146

}

/**
 * Forena admin settings form
 *
 */
function forena_settings() {
147
  $skins = variable_get('forena_skins', array());
148
149
150

  $report_path = forena_report_path();

151

152
153
154
  $form['forena_report_repos'] = array(
    '#type' => 'textfield',
    '#title' => t('Report Repository'),
metzlerd's avatar
metzlerd committed
155
156
157
158
159
160
    '#description' => t('Indicate the directory that you want to use for your reports.  In order for you to ' .
                        'to be able to save reports, this directory should be writable by the web user. Relative' .
                        'paths should be entered relative to the base path of your drupal installation.'),
    '#default_value' => $report_path,
  );

161
162
163
  $form['forena_last_report_path'] = array(
    '#type' => 'value',
    '#value' => forena_report_path(),
metzlerd's avatar
metzlerd committed
164
165
  );

166

167
  $form['forena_input_format'] = forena_filter_element(variable_get('forena_input_format', filter_default_format()), 'forena_input_format');
168
  $form['forena_default_form'] = array(
169
    '#type' => 'select',
170
    '#title' => t('Default report skin'),
171
    '#options' => $skins,
172
    '#description' => t('Specify the default skin to be used.   New skins can be created by creating .skinfo files in your reports directory.'
metzlerd's avatar
metzlerd committed
173
    . ' Skins are basically css and javascript libraries added to your report.'),
metzlerd's avatar
metzlerd committed
174
    '#default_value' => variable_get('forena_default_form', ''),
metzlerd's avatar
metzlerd committed
175
    );
metzlerd's avatar
metzlerd committed
176

metzlerd's avatar
metzlerd committed
177
178
179
    $form =  system_settings_form($form);
    $form['#submit'][] = 'forena_settings_submit';
    return $form;
metzlerd's avatar
metzlerd committed
180

181
182
}
/**
183
 * Added submit handler to create directories and clear menu cache
184
185
186
187
 *
 * @param unknown_type $form
 * @param unknown_type $form_state
 */
metzlerd's avatar
metzlerd committed
188
189
function forena_settings_submit($form, &$form_state) {
  $values = $form_state['values'];
190
  $path = $values['forena_report_repos'];
metzlerd's avatar
metzlerd committed
191
  $src_dir = drupal_get_path('module', 'forena') . '/repos/reports';
192
  if ($path != $values['forena_last_report_path']) {
193
194
    if (!file_exists($path)) {
      try {
195

196
197
198
199
        if (file_exists($path)) {
          drupal_set_message(t('Created directory %s', array($path))) ;
        }
        mkdir($path);
200

201
202
203
204
205
      } catch (Exception $e) {
        forena_error(t('Unable to create report directory'), $e->getMessage());
        return;
      }
    }
metzlerd's avatar
metzlerd committed
206
  }
207
208
  forena_sync_reports();
}
metzlerd's avatar
metzlerd committed
209

210
function forena_settings_sync_submit($form, &$form_state) {
211
  forena_sync_reports($form_state['values']['sync_overwrite']);
212
  drupal_set_message(t('Report cache cleared'));
213
}
214

215
function forena_format_form($formid, $form_state, $report_name) {
metzlerd's avatar
metzlerd committed
216
  $desc = Frx::Menu()->parseURL($report_name);
217
  $name = $desc['name'];
218

219
220
221
  $filename = $desc['filename'];
  $format = isset($desc['format']) ? $desc['format'] : '';
  if ($desc['exists']) {
222
    $r = Frx::Editor($name);
223
224
225
226
227
    drupal_set_title($r->title);
    $form = array();
    $frx_options = $r->getOptions();
    $report_form = @$frx_options['form'];

228
    $doclist = Frx::Menu()->doc_formats;
229
230
    $skins[''] = t('Use Default');
    $skins = array_merge(variable_get('forena_skins', array()), $skins);
231

metzlerd's avatar
metzlerd committed
232
233
234
235
    $form['report_name'] = array(
       '#type' => 'value',
       '#value' => $name,
    );
236

metzlerd's avatar
metzlerd committed
237
    $form['form'] = array(
238
      '#type' => 'select',
239
      '#title' => t('Skin'),
240
      '#options' => $skins,
metzlerd's avatar
metzlerd committed
241
      '#default_value' => $report_form,
metzlerd's avatar
metzlerd committed
242
      '#description' => t('The page style of your report.  The {skin}.skinfo file specifies css and js file in your report.')
metzlerd's avatar
metzlerd committed
243
    );
244

metzlerd's avatar
metzlerd committed
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
    //begin checking doc generation options
    if ($r) $nodes = $r->simplexml->head->xpath('frx:docgen/frx:doc');
    if ($doclist) {
      $form['docgen'] = array(
       '#tree' => TRUE,
       '#type' => 'fieldset',
       '#title' => t('Document Options'),
       '#description' => t('These are document transformation options. Options selected will display as links in your report view.')
      );

      //build the options and default list
      $options = array();
      $default = array();
      foreach ($doclist as $value) {
        $options[$value] = strtoupper($value);
        $doc =  isset($r) ? $r->simplexml->head->xpath('frx:docgen/frx:doc[@type="' . $value . '"]') : '';
metzlerd's avatar
metzlerd committed
261
        if ($doc && array_search($value, $doclist)!==FALSE) {
metzlerd's avatar
metzlerd committed
262
          $default[$value] = $value;
263
        }
metzlerd's avatar
metzlerd committed
264
      }
265

metzlerd's avatar
metzlerd committed
266
267
268
269
270
271
272
      //display checkboxes
      $form['docgen']['docs'] = array(
        '#type' => 'checkboxes',
        '#description' => t('If no options are selected, the system will display all of the above as available for this report.'),
        '#options' => $options,
        '#default_value' => $default
      );
273

274
275
276
277
278
      $form['update'] = array(
        '#type' => 'submit',
        '#value' => 'Update',
      );

metzlerd's avatar
metzlerd committed
279
280
281
      $form['submit'] = array(
        '#type' => 'submit',
        '#value' => 'Save',
282
        '#submit' => array('forena_format_form_submit', 'forena_save_submit'),
metzlerd's avatar
metzlerd committed
283
      );
284
285
286
287
288

      $form['cancel'] = array(
        '#type' => 'submit',
        '#value' => 'Cancel',
        '#submit' => array('forena_save_cancel'),
metzlerd's avatar
metzlerd committed
289
290
      );

metzlerd's avatar
metzlerd committed
291
    }
292
293
294
295
296
  }
  return $form;
}

function forena_format_form_submit($form, &$form_state) {
metzlerd's avatar
metzlerd committed
297
  $values = $form_state['values'];
298
299
  $name = $values['report_name'];

300
  $r = Frx::Editor();
301
302
303
304
305
306
307
308
  $options = array(
    'form' => $values['form']);
  $r->setOptions($options);


  // Doc gen settings.
  if (isset($form['docgen'])) {
    $docgen = array();
metzlerd's avatar
metzlerd committed
309
    if ($selected = array_filter(@$values['docgen']['docs'])) {
310
      if ($selected) foreach ($selected as $key => $value) {
metzlerd's avatar
metzlerd committed
311
        if ($value) $docgen[] = array('type' => $key);
312
313
314
315
316
      }
    }
    $r->setDocgen($docgen);
  }

317
  $r->update();
318
319

}
320

metzlerd's avatar
metzlerd committed
321
function forena_general_form($form, &$form_state, $report_name) {
metzlerd's avatar
metzlerd committed
322
  $name_in = $report_name;
metzlerd's avatar
metzlerd committed
323
  $desc = Frx::Menu()->parseURL($report_name);
metzlerd's avatar
metzlerd committed
324
325
326
  $name = $desc['name'];
  $filename = $desc['filename'];
  $format = @$desc['format'];
metzlerd's avatar
metzlerd committed
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
  $save_name = $name;
  //set the name to empty string for new reports
  $r = Frx::Editor($name);
  $title = (string)$r->title;
  drupal_set_title(filter_xss($r->title));
  $frx_options = $r->getOptions();
  $hidden = @$frx_options['hidden']=='1' ? 1 : 0;
  $report_form = @$frx_options['form'];
  $category = $r->getCategory();
  $menu = $r->getMenu();
  $cache = $r->getCache();
  $form['report_name'] = array(
    '#type' => 'value',
    '#value' => $name,
  );
metzlerd's avatar
metzlerd committed
342

metzlerd's avatar
metzlerd committed
343
344
345
346
347
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#default_value' => $title,
  );
348

metzlerd's avatar
metzlerd committed
349
350
351
352
  $form['visibility'] = array(
    '#type' => 'fieldset',
    '#title' => t('Visibility'),
  );
353

metzlerd's avatar
metzlerd committed
354
355
356
357
358
359
360
  $form['visibility']['category'] = array(
    '#type' => 'textfield',
    '#title' => t('Category'),
    '#default_value' => $category,
    '#autocomplete_path' => 'forena/categories/autocomplete',
    '#description' => t('The heading your report will be grouped under on the report list.'),
  );
metzlerd's avatar
metzlerd committed
361

metzlerd's avatar
metzlerd committed
362
363
364
365
366
367
  $form['visibility']['hidden'] = array(
    '#type' => 'checkbox',
    '#title' => t('Hidden'),
    '#default_value' => $hidden,
    '#description' => t('Hide your report from showing up on the report list.'),
  );
metzlerd's avatar
metzlerd committed
368

metzlerd's avatar
metzlerd committed
369
370
371
372
373
374
375
  $form['menu'] = array(
    '#type' => 'fieldset',
    '#title' => t('Menu'),
    '#tree' => TRUE,
    '#collapsible' => TRUE,
    '#collapsed' => empty($menu),
  );
376

metzlerd's avatar
metzlerd committed
377
378
379
380
381
382
  $form['menu']['path'] = array(
    '#type' => 'textfield',
    '#title' => t('Menu Path'),
    '#description' => t('Indicate site reletive path to menu.  Parameters may be embedded in the url using a :parm syntax (e.g. states/:state)'),
    '#default_value' => @$menu['path'],
  );
383

metzlerd's avatar
metzlerd committed
384
385
386
387
388
389
  $form['menu']['args'] = array(
    '#type' => 'textfield',
    '#title' => t('Additional Arguments'),
    '#description' => t('Indicate additonal parameters that should be extracted after the menu path using a :parm syntax (e.g. :parma/:parmb)'),
    '#default_value' => @$menu['args'],
  );
390

metzlerd's avatar
metzlerd committed
391
392
393
394
395
396
  $form['menu']['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Menu Title'),
    '#description' => t('Title of menu item.  Leave blank to use the report title as the menu title.'),
    '#default_value' => @$menu['title'],
  );
397

metzlerd's avatar
metzlerd committed
398
399
400
401
402
403
  $menu_options = array(
    'normal-item' => t('Normal'),
    'local-task' => t('Tab'),
    'default-local-taks' => t('Default Tab'),
    'callback' => t('Callback'),
  );
404

metzlerd's avatar
metzlerd committed
405
406
407
408
409
410
  $form['menu']['type'] = array(
    '#type' => 'select',
    '#title' => 'Type of menu to create',
    '#options' => $menu_options,
    '#default_value' => @$menu['type'],
  );
411

metzlerd's avatar
metzlerd committed
412
413
414
415
416
417
418
  $form['cache'] = array(
    '#type' => 'fieldset',
    '#title' => t('Cache'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#tree' => TRUE,
  );
419

metzlerd's avatar
metzlerd committed
420
421
422
423
424
425
  $form['cache']['duration'] = array(
    '#type' => 'textfield',
    '#title' => t('Duration'),
    '#description' => t('Specify a php strtotime relative date duration, (e.g. +1 hour, +2 days)'),
    '#default_value' => @$cache['duration'],
  );
426

metzlerd's avatar
metzlerd committed
427
428
429
430
431
  $form['cache']['per_user'] = array(
    '#type' => 'checkbox',
    '#title' => t('Per User'),
    '#default_value' => @$cache['per_user'],
  );
432

metzlerd's avatar
metzlerd committed
433
434
435
436
437
  $form['cache']['per_doctype'] = array(
    '#type' => 'checkbox',
    '#title' => t('Per Document Type'),
    '#default_value' => @$cache['per_doctype'],
  );
438

metzlerd's avatar
metzlerd committed
439
440
441
442
  $form['update'] = array(
    '#type' => 'submit',
    '#value' => 'Update',
  );
443

metzlerd's avatar
metzlerd committed
444
445
446
447
448
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Save',
    '#submit' => array('forena_general_form_submit', 'forena_save_submit'),
  );
449

metzlerd's avatar
metzlerd committed
450
451
452
453
454
  $form['cancel'] = array(
    '#type' => 'submit',
    '#value' => 'Cancel',
    '#submit' => array('forena_save_cancel'),
  );
455

metzlerd's avatar
metzlerd committed
456
  return $form;
457
}
458

metzlerd's avatar
metzlerd committed
459
function forena_general_form_validate($form, &$form_state) {
460
  $values = $form_state['values'];
metzlerd's avatar
metzlerd committed
461
462
463
464
465
  if ($values['menu']['path']) {
    if (!valid_url(str_replace(':', '', $values['menu']['path']), FALSE)) {
      form_set_error('menu][path', t('Invalid Path'));
    }
  }
466
467
468
469
470
471
472
473
  if ($values['cache']['duration']) {
    try {
      $time = @new DateTime($values['cache']['duration']);
    }
    catch (Exception $e) {
    }
    if (!$time) form_set_error('cache][duration' , t('Invalid Cache Duration'));
  }
474
}
475

metzlerd's avatar
metzlerd committed
476
function forena_general_form_submit($form, &$form_state) {
477
478
  $values = $form_state['values'];
  $report_name = $values['report_name'];
479
  $r = Frx::Editor();
metzlerd's avatar
metzlerd committed
480

481
482
483
484
485
486
487
488
  // Title and category
  $r->setTitle($values['title']);
  $r->setCategory($values['category']);
  // Form options
  $options = array(
    'hidden' => $values['hidden'],
  );
  $r->setOptions($options);
metzlerd's avatar
metzlerd committed
489
490
491
492
493
  $menu = $r->getMenu();
  if ($values['menu']!= $menu) {
    $r->setMenu($values['menu']);
    $rebuild_menu = TRUE;
  }
494

495
496
497
498
499
  $cache = $r->cache;
  if ($values['cache']!=$cache) {
    $r->setCache($values['cache']);
  }

metzlerd's avatar
metzlerd committed
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
  $r->update();
}

/**
 * Form function for the edit report form
 * @param $form_state
 * @return the form
 */
function forena_layout_form($form, &$form_state, $report_name) {
  $desc = Frx::Menu()->parseURL($report_name);
  $name = $desc['name'];
  $r = Frx::Editor($name);
  drupal_set_title(filter_xss($r->title));
  // Need to get all option attributes
  $frx_options = $r->getOptions();
  $report_form = @$frx_options['form'];
  $attributes = $r->get_attributes_by_id();
  $body = $r->simplexml->body->asXML();
  $css = @(string)$r->simplexml->head->style;

  //array of xml attributes that are required to have a value
  $required = array('id' => TRUE, 'label' => TRUE);

  $form['report_name'] = array(
    '#type' => 'value',
    '#value' => $name,
  );

  $form['attributes'] = array(
    '#type' => 'value',
    '#value' => $attributes,
  );


  $form['body'] = array(
    '#type' => 'text_format',
    '#title' => t('Body'),
    '#default_value' => $body,
    '#rows' => 25,
  '#format' => variable_get('forena_input_format', filter_default_format())
  );

  $form['style'] = array(
    '#type' => 'fieldset',
    '#title' => t('CSS Styles'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );

  $form['style']['css'] = array(
    '#type' => 'textarea',
    '#default_value' => $css,
    '#description' => t('Specify small css snipets that can be used in the reports.'),
    '#rows' => 10,
    '#cols' => 80,
  );

  $form['buttons']['update'] = array(
    '#type' => 'submit',
    '#value' => 'Update',
    '#submit' => array('forena_layout_form_submit'),
  );

  $form['buttons']['save'] = array(
    '#type' => 'submit',
    '#value' => 'Save',
    '#submit' => array('forena_layout_form_submit', 'forena_save_submit'),
  );

  $form['buttons']['cancel'] = array(
    '#type' => 'submit',
    '#value' => 'Cancel',
    '#submit' => array('forena_save_cancel'),
  );  return $form;

  if (user_access('delete report')) {
    $form['buttons']['delete'] = array(
      '#type' => 'submit',
      '#value' => 'Delete',
      '#submit' => array('forena_edit_delete_submit'),
    );
  }
  return $form;
}

function forena_layout_form_validate($form, &$form_state) {
  $values = $form_state['values'];
  $body = $values['body']['value'];
  $doc_prefix = '<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE root [
    <!ENTITY nbsp "&#160;">
    ]>';
  if ($body) {
    $body_doc = new DOMDocument('1.0', 'UTF-8');
    $body_xml =  $doc_prefix . '<html xmlns:frx="urn:FrxReports">' . $body . '</html>';
    if (@$body_doc->loadXML($body_xml) === FALSE) {
      form_set_error('body', t('Invalid XHTML Document. Check for unclosed tags or stray &'));
    }
  }

}

/**
 * builds a string of the xml document,
 * submits it to forena_save_report.
 */
function forena_layout_form_submit($form, &$form_state) {
  $nodes = array();
  $rebuild_menu = FALSE;
  $values = $form_state['values'];
  $report_name = $values['report_name'];
  $r = Frx::Editor();


614
615
  // Body
  $r->setBody($values['body']['value']);
616
617
  // CSS
  $r->setStyle($values['css']);
618
  // If there are no frx attributes in the body then replace them with the old values.
619
  $frx_nodes = $r->simplexml->xpath('body//*[@frx:*]');
620
621
622
623
  if (!$frx_nodes) {
    $r->save_attributes_by_id($values['attributes']);
  }

624
  $r->update();
metzlerd's avatar
metzlerd committed
625
  // If we changed the menu we need to rebuild it.
626

627
628
629
}


630
631
632
633
634
/**
 * Handle delete buttons from edit forms.
 * @return unknown_type
 */
function forena_edit_delete_submit($form, &$form_state) {
metzlerd's avatar
metzlerd committed
635
  $link = 'reports/' . $form_state['values']['name_in'] ;
636
637
638
639
640
641
642
643
644
645
646
647
648
649
  $destination = '';
  if (isset($_REQUEST['destination'])) {
    $destination = drupal_get_destination();
    unset($_REQUEST['destination']);
  }
  $form_state['redirect'] = array('path' => $link . '/delete', 'query' => array('destination' => $destination));
}


function forena_add_report_form($formid, $form_state, $report_name='') {
  $name = '';
  $filename = '';
  $format = '';
  if ($report_name) {
metzlerd's avatar
metzlerd committed
650
    $desc = Frx::Menu()->parseURL($report_name);
651
    $name = $desc['name'];
652
653
654
    $filename = $desc['filename'];
    $format = @$desc['format'];
  }
655
656
657
658
  $form = array();
  global $language;
  //determine if this is an add new report request

659
  $r = Frx::Editor($name);
660
661
662
663
664
665
666
667
668
  $title = (string)$r->title;
  if (module_exists('locale')) {
    @list($tlang,  $tsave_name) = explode('/', $name, 2);
    // FInd out if the starting name of the report is an installed language.
    if (array_key_exists($tlang, language_list() )) {
      $lang = $tlang;
      $save_name = $tsave_name;
    }
    else {
metzlerd's avatar
metzlerd committed
669
      $lang = 'en';
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
    }
  }
  // Need to get all option attributes
  $frx_options = $r->getOptions();
  $hidden = @$frx_options['hidden']=='1' ? 1 : 0;
  $report_form = @$frx_options['form'];
  $attributes = $r->get_attributes_by_id();
  $category = $r->getCategory();
  $body = $r->simplexml->body->asXML();


  //array of xml attributes that are required to have a value
  $required = array('id' => TRUE, 'label' => TRUE);
  //list of supported document formats
  $doclist = variable_get('forena_doc_formats', array());

  $form['save_report_name'] = array(
    '#type' => 'textfield',
    '#title' => t('Report Name'),
    '#description' => t('Enter only letters, numbers, and special characters:  - _ /
                         <br/>White space is not permitted.
                         Create a directory using the format: (directory name) / (report name). Save multiple reports to the same directory
                         by referencing the same name.'),
    '#required' => TRUE,
  );


  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#default_value' => $title,
metzlerd's avatar
metzlerd committed
701
    '#required' => TRUE,
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
  );

  $form['category'] = array(
    '#type' => 'textfield',
    '#title' => t('Category'),
    '#default_value' => $category,
    '#autocomplete_path' => 'forena/categories/autocomplete',
    '#description' => t('The heading your report will be grouped under on the report list.'),
  );

  $form['hidden'] = array(
    '#type' => 'checkbox',
    '#title' => t('Hidden'),
    '#default_value' => $hidden,
    '#description' => t('Hide your report from showing up on the report list.'),
  );

  $form['report_name'] = array(
    '#title' => t('Create from report'),
    '#type' => 'textfield',
722
    '#autocomplete_path' => 'forena/reports/autocomplete',
723
724
725
726
727
728
729
    '#default_value' => $name,
  );

  $form['save'] = array(
    '#type' => 'submit',
    '#value' => 'Create',
  );
730
731
732
733
734
  return $form;

}

function forena_add_report_form_validate($form, &$form_state) {
735
  $values = $form_state['values'];
736
  $regexp = "/^[A-Za-z0-9\/\_\-]*$/";
737
  $save_report_name = $values['save_report_name'];
metzlerd's avatar
metzlerd committed
738

739
740
  //comparing the report names to see if they have changed.
  //If they have, making sure the new name does not already exist.
741
742
743
  $filename = $save_report_name . '.frx';
  if (Frx::File()->exists($filename, FALSE)) {
    form_set_error('save_report_name', t('The report ' . $save_report_name . ' already exists. Please enter another name.'));
metzlerd's avatar
metzlerd committed
744
  }
745
}
746

747
function forena_add_report_form_submit($form, &$form_state) {
748
  $values = $form_state['values'];
749
  $report_name = $values['save_report_name'];
metzlerd's avatar
metzlerd committed
750
  $desc = Frx::Menu()->parseURL($report_name);
751
  $report_name = $desc['name'];
752

753
754
755
756
757
758
759
  if ($values['report_name']) {
    $r = Frx::Editor($values['report_name']);
    $r->report_name = $values['save_report_name'];
  }
  else {
    $r = Frx::Editor($values['save_report_name']);
  }
760
761
762
763
764
765
  // Title and category
  $r->setTitle($values['title']);
  $r->setCategory($values['category']);
  // Form options
  $options = array(
    'hidden' => $values['hidden'],
766
  );
767
768
  $r->setOptions($options);

769
  //determine redirection.
770
  $filename = $desc['filename'];
metzlerd's avatar
metzlerd committed
771

772
773
774
775
776
777
778
  $r->update();
  //if this is a new report then redirect to data blocks
  if ($values['report_name']) {
    $edit = '/edit';
  }
  else {
    $edit = '/edit/data/add';
metzlerd's avatar
metzlerd committed
779
  }
780
781
  $form_state['redirect']= $desc['i_link'] . $edit;

782
783
}
function forena_create_trans_form($formid, $form_state, $report_name) {
784
785
786
  $name = '';
  $filename = '';
  $format = '';
metzlerd's avatar
metzlerd committed
787
  $desc = Frx::Menu()->parseURL($report_name);
788
789
790
791
  $name = $desc['name'];
  $filename = $desc['filename'];
  $base_name = $desc['base_name'];
  $format = @$desc['format'];
792
793
  $form = array();
  global $language;
794
  $languages = language_list();
795
796
  //determine if this is an add new report request

797
  $r = Frx::Editor($name);
798
799
800
  $title = (string)$r->title;
  $lang = @$_GET['language'];
  if ($lang) {
metzlerd's avatar
metzlerd committed
801
    $language = $languages[$lang];
802
803
804
805
806
807
808
809
810
  }
  $form['base_name'] = array(
    '#type' => 'value',
    '#value' => $base_name
  );
  $form['report_name'] = array(
    '#type' => 'value',
    '#value' => $name,
  );
811

metzlerd's avatar
metzlerd committed
812
  foreach ($languages as $key => $obj) {
813
    $languages[$key] = $obj->native;
814
  }
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
  $form['report_lang'] = array(
    '#type' => 'value',
    '#value' => $lang,
  );
  $def_lang = $lang ? $lang : 'en';
  $form['save_report_language'] = array(
    '#type' => 'select',
    '#title' => t('Language'),
    '#options' => $languages,
    '#default_value' => $def_lang,
  );


  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#default_value' => $title,
metzlerd's avatar
metzlerd committed
832
    '#required' => TRUE,
833
834
835
836
837
838
839
840
841
  );


  $form['save'] = array(
    '#type' => 'submit',
    '#value' => 'Create',
  );
  return $form;

842
843
844
845
}

function forena_create_trans_form_submit($form, &$form_state) {
  $values = $form_state['values'];
846
  $base_name = $values['base_name'];
metzlerd's avatar
metzlerd committed
847
  $new_name = $values['save_report_language'] . '/' . $base_name;
848

metzlerd's avatar
metzlerd committed
849
  $desc = Frx::Menu()->parseURL($new_name);
850
851
  $filename = $desc['filename'];
  $report_name = $desc['name'];
852

David Metzler's avatar
David Metzler committed
853
  $r = Frx::Editor($base_name);
854
855
856

  // Title and category
  $r->setTitle($values['title']);
metzlerd's avatar
metzlerd committed
857

metzlerd's avatar
metzlerd committed
858
  //determine redirection.
859

860
  if (Frx::File()->exists($filename, FALSE)) {
metzlerd's avatar
metzlerd committed
861
    drupal_set_message(t('Report %s already exists', array('%s' => $new_name)), 'error');
862
    return;
metzlerd's avatar
metzlerd committed
863
  }
David Metzler's avatar
David Metzler committed
864
865
866
867
868
  else {
    $r->rename($new_name);
    drupal_set_message(t('Translation,  %s has been created. Switch languages to translate.', array('%s' => $values['title'])));
    $form_state['redirect']= array( $desc['i_link'] . '/edit/layout');
  }
metzlerd's avatar
metzlerd committed
869

870
  //if this is a new report then redirect to data blocks
871
}
872

873
874


875
876
877
878
/*
 * administer the settings for document format options
 */
function forena_doc_formats_settings() {
879
  // Invoke doc_type hook to see which document types are there.
880
  $supported_doctypes = Frx::documentTypes();
881

882
883
  $form['forena_doc_formats'] = array(
    '#type' => 'checkboxes',
884
    '#title' => t('Allowed Document Formats'),
885
886
887
888
    '#default_value' => variable_get('forena_doc_formats', $supported_doctypes),
    '#description' => t('check your desired document format'),
    '#options' => $supported_doctypes,
  );
889
890
891
892

  $form['forena_doc_defaults'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Default Document Formats'),
893
    '#default_value' => variable_get('forena_doc_defaults', array()),
894
895
896
897
    '#description' => t('check your desired document format'),
    '#options' => $supported_doctypes,
  );

898
899
900
  $form['forena_email_override'] = array(
    '#type' => 'checkbox',
    '#title' => 'Run email merges in test mode' ,
metzlerd's avatar
metzlerd committed
901
    '#default_value' => variable_get('forena_email_override', FALSE),
902
903
    '#description' => t('When this box is checked emails are sent to the currently logged in user.  Useful for testing environments.'),
  );
904
  $form['forena_email_input_format'] = forena_filter_element(variable_get('forena_email_input_format', 'full_text'), 'forena_email_input_format');
905
  $form['forena_email_input_format']['#title'] = t('Email Input Format');
906

907
908
  return system_settings_form($form);
}
909

910
911
912
913
/*
 * administer the settings for document format options
 */
function forena_data_settings() {
914
  $repos = Frx::RepoMan()->repositories;
915
  $r_list = array();
metzlerd's avatar
metzlerd committed
916
917
  $headers = array(t('Name'), t('Description'), t('Path'), t('Operation'));
  foreach ($repos as $name => $r) {
918
    $r_list[] = array(
metzlerd's avatar
metzlerd committed
919
920
921
922
    $name,
    $r['title'],
    $r['path'],
    l(t('configure'), 'admin/config/content/forena/data/configure/' . $name)
923
924
925
926
    );

  }

metzlerd's avatar
metzlerd committed
927
928
  $output = '<ul class="action-links"><li>' . l(t('Add data source'), 'admin/config/content/forena/data/add') . '</li></ul>';
  $output .= theme_table(array('header' => $headers, 'rows' => $r_list, 'attributes' => array(), 'caption' => '', 'sticky' => TRUE, 'colgroups' => array(), 'empty' => ''));
929
930
931
932
  return $output;
}

function forena_data_settings_edit($form, &$form_state, $source=-1) {
933
  global $databases;
934
935
936
937
938
939
940
941
942
  $adding = ($source === -1);
  if (!@$form_state['storage']) {
    if ($adding) {
      $form_state['storage'] = array(
        'name' => '',
        'title' => '',
        'path' => '',
        'config' => array(
          'source' => 'user',
davidmetzler's avatar
davidmetzler committed
943
          'data provider' => 'FrxDrupal',
944
          'database' => 'default',
945
946
          'access callback' => 'user_access',
          'user callback' => 'forena_current_user_id'
metzlerd's avatar
metzlerd committed
947
948
          ),
          );
949
950
    }
    else {
951
952
      Frx::RepoMan()->repository($source);
      $repos = Frx::RepoMan()->repositories;
953
      $r = $repos[$source];
davidmetzler's avatar
davidmetzler committed
954
      // Remove teh object from the data.
955
      unset($r['data']);
956
957
958
      $form_state['storage'] = array(
        'name' => $source,
        'title' => $r['title'],
davidmetzler's avatar
davidmetzler committed
959
        'path' => @$r['path'],
960
        'config' => $r,
961
962
963
964
965
966
      );

    }
  }
  $data = $form_state['storage'];
  $config = $data['config'];
davidmetzler's avatar
davidmetzler committed
967

davidmetzler's avatar
davidmetzler committed
968
  $locked = !($adding || (@$config['source'] == 'user'));
969
970
971
972
973
974
975
976
977
  $values = @$form_state['values'];

  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Name'),
    '#description' => t('Machine readable name.  Used in referencing all data used by this source. must should not contain any special characters or spaces.'),
    '#disabled' => !$adding,
    '#default_value' => $data['name'],
    '#required' => TRUE,
metzlerd's avatar
metzlerd committed
978
  );
979
980
981
982

  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
davidmetzler's avatar
davidmetzler committed
983
    '#required' => TRUE,
984
985
986
987
988
989
990
991
992
993
994
    '#description' => t('Human readable name that describes the data source.  This primarily occurs in error messages where the data source cannot be accessed.'),
    '#default_value' => $data['title'],
  );

  $form['enabled'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enabled'),
    '#description' => t('Disabling will cause all queries to return no data.'),
    '#default_value' => @$data['enabled']!==0,
  );

davidmetzler's avatar
davidmetzler committed
995
996
997
998
999
1000
1001
  $form['debug'] = array(
    '#type' => 'checkbox',
    '#title' => t('Debug'),
    '#description' => t('Write information to the screen and logs for each query executed.'),
    '#default_value' => @$config['debug'],
  );

1002
1003
1004
  $form['path'] = array(
    '#type' => 'textfield',
    '#title' => t('Path'),
davidmetzler's avatar
davidmetzler committed
1005
    '#required' => TRUE,
1006
    '#disabled' => $locked,
1007
1008
1009
1010
    '#description' => t('Directory containing data block files.'),
    '#default_value' => @$data['path'],
  );

davidmetzler's avatar
davidmetzler committed
1011
1012
1013
1014
1015
1016
1017
1018
1019
  $user_options = array(
    '' => 'None',
    'forena_current_user_id' => 'UID',
    'forena_current_user_name' => 'User name',
  );

  $form['user_callback'] = array(
    '#type' => 'select',
    '#title' =>  'Current user',
1020
    '#disabled' => $locked,
davidmetzler's avatar
davidmetzler committed
1021
1022
    '#description' => t('Can be refererenced as :current_user in each data block.'),
    '#options' => $user_options,
1023
    '#default_value' => @$config['user callback'],
davidmetzler's avatar
davidmetzler committed
1024
1025
1026
1027
    '#disabled' => $locked,
  );


1028
1029
  // Access method list
  $access = array(
davidmetzler's avatar
davidmetzler committed
1030
    'callback' => t('Use drupal permissions'),
metzlerd's avatar
metzlerd committed
1031
    'block' => t('Match values provided by a data block.'),
1032
1033
1034
1035
1036
  );

  $form['access_method'] = array(
    '#type' => 'select',
    '#options' => $access,
1037
    '#disabled' => $locked,
davidmetzler's avatar
davidmetzler committed
1038
1039
    '#title' => t('Data security method'),
    '#default_value' => empty($config['access block']) ? 'callback' : 'block',
metzlerd's avatar
metzlerd committed
1040
    '#description' => t('Specify how the ACCESS defined for a data block is to be interpreted.'),
1041
1042
1043
    '#ajax' => array(
      'callback' => 'forena_access_info_callback',
      'wrapper' => 'access-details',
metzlerd's avatar
metzlerd committed
1044
  ),
1045
1046
1047
1048
1049
1050
  );

  $form['access_details'] = array(
    '#type' => 'fieldset',
    '#prefix' => '<div id="access-details">',
    '#suffix' => '</div>',
davidmetzler's avatar
davidmetzler committed
1051
    '#title' => t('Details'),
1052
1053
1054
  ) ;

  switch (!empty($values['access_method']) ? $values['access_method'] : $form['access_method']['#default_value']) {
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
    case 'block':
      $form['access_details']['access_block'] = array(
        '#type' => 'textfield',
        '#title' => 'Data block providing permissions list',
        '#disabled' => $locked,
        '#autocomplete_path' => 'forena/data_block/autocomplete',
        '#description' => t('The datablock to be used to interpret permissions.  This should return a single column of permissions based on the current user.   May be provided by another repository.'),
        '#default_value' => @$config['access block'],
      );
      break;
    default:
      $form['access_details']['access_callback'] = array(
        '#type' => 'item',
        '#title' => 'Access callback',
        '#disabled' => $locked,
        '#markup' => @$config['access callback'],
      );
1072
1073
1074
1075
1076
1077
1078
1079
  }



  // Driver list
  $drivers = array(
    'FrxDrupal' => t('Drupal'),
    'FrxOracle' => t('Oracle Database'),
metzlerd's avatar
metzlerd committed
1080
    'FrxPDO' => t('PDO other than Drupal'),
1081
1082
1083
1084
1085
    'FrxPostgres' => t('Postgres Database'),
    'FrxMSSQL' => t('MSSQL Database'),
    'FrxFiles' => t('XML Files'),
  );

davidmetzler's avatar
davidmetzler committed
1086
  $form['data_provider'] = array(
1087
1088
1089
1090
1091
    '#type' => 'select',
    '#title' => t('Driver'),
    '#description' => t('Forena data connection type'),
    '#options' => $drivers,
    '#disabled' => $locked,
davidmetzler's avatar
davidmetzler committed
1092
    '#default_value' => $config['data provider'],
1093
1094
1095
    '#ajax' => array(
      'callback' => 'forena_connection_info_callback',
      'wrapper' => 'conn-div',
metzlerd's avatar
metzlerd committed
1096
  ),
1097
1098
1099
1100
  );

  $form['connection'] = array(
    '#type' => 'fieldset',
davidmetzler's avatar
davidmetzler committed
1101
    '#tree' => TRUE,
1102
1103
1104
1105
    '#title' => 'Connection info',
   '#prefix' =>  '<div id="conn-div">',
   '#suffix' => '</div>',
  );
davidmetzler's avatar
davidmetzler committed
1106
  $data_provider = (!empty($form_state['values']['data_provider']) ? $form_state['values']['data_provider'] : $config['data provider']);
1107

davidmetzler's avatar
davidmetzler committed
1108
1109
1110
1111
1112
  // Common controls used in mulitple providers.
  $uri = array(
    '#type' => 'textfield',
    '#title' => t('uri'),
    '#descripton' => t('Connection string: see appropriate php documentation for more details.'),
1113
    '#default_value' => @$config['uri'],
davidmetzler's avatar
davidmetzler committed
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
    '#required' => TRUE,
    '#disabled' => $locked,
  );

  $user = array(
    '#type' => 'textfield',
    '#title' => t('User'),
    '#default_value' => @$config['user'],
  );
  $password= array(
    '#type' => 'password',
    '#title' => t('Password'),
    '#default_value' => @$config['password'],
metzlerd's avatar
metzlerd committed
1127
  );
davidmetzler's avatar
davidmetzler committed
1128

davidmetzler's avatar
davidmetzler committed
1129
  switch ($data_provider) {
davidmetzler's avatar
davidmetzler committed
1130
    case 'FrxDrupal':
1131
1132
1133
1134
      $db_list = array_combine(array_keys($databases), array_keys($databases));
      $form['connection']['database'] = array(
        '#type' => 'select',
        '#title' => t('Database'),
1135
        '#disabled' => $locked,
1136
1137
        '#default_value' => @$config['database'],
        '#options' => $db_list,
davidmetzler's avatar
davidmetzler committed
1138
1139
1140
1141
        '#markup' => 'Determined by Drupal settings.php file',
      );
      break;
    case 'FrxMSSQL':
1142
1143
1144
      $form['connection']['uri'] = $uri;
      $form['connection']['user'] = $user;
      $form['connection']['new_password'] = $password;
1145
1146
1147
1148
1149
1150
1151
      $form['connection']['database'] = array(
        '#type' => 'textfield',
        '#disabled' => $locked,
        '#title' => t('Database'),
        '#default_value' => $config['database'],
        '#required' => TRUE,
      );
davidmetzler's avatar
davidmetzler committed
1152
1153
1154
1155
1156
1157
1158
      $form['connection']['mssql_xml'] = array(
        '#type' => 'checkbox',
        '#disabled' => $locked,
        '#title' => t('Microsoft SQL native XML'),
        '#description' => t('Use for XML auto queries to generate XML.'),
        '#default_value' => $config['mssql_xml'],
      );