forena.admin.inc 76.2 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
34
  $headers = array(t('title'), t('name'), t('category'), t('operation'));
  $result = db_query('SELECT * FROM {forena_reports} where language=:language ORDER BY category,title', array(':language' => $language->language));
  foreach ($result as $row) {
35
    $rpt = str_replace('/', '.', $row->report_name);
metzlerd's avatar
metzlerd committed
36
37
38
39
40
    $edit = l(t('Edit'), 'reports/' . $rpt . '/edit');
    $clone = l(t('Clone'), 'reports/add/' . $rpt);
    $delete = l(t('Delete'), 'reports/' . $rpt . '/delete', array('query' => array('destination' => 'admin/structure/reports')));
    $title = l(t($row->title), 'reports/' . $rpt);
    $data[] = array($title, $row->report_name, $row->category, $edit . ' ' . $clone . ' ' . $delete);
41
  }
42
  $output .= '<div id="forena-reports-list">';
43
  $output .= theme_table(array('header' => $headers, 'rows' => $data, 'attributes' => array('class' => array( 'dataTable-paged')), 'caption' => '', 'sticky' => TRUE, 'colgroups' => array(), 'empty' => ''));
44
  $output .= '</div>';
45
46
47
  return $output;
}

48
/**
metzlerd's avatar
metzlerd committed
49
 * Remove the report from the database and file system.
50
51
 * @param string $report_name
 */
metzlerd's avatar
metzlerd committed
52
function forena_delete_report($report_name, $delete_file = TRUE) {
53
  $report_path = forena_report_path();
54
55
56
  $language = 'en';


metzlerd's avatar
metzlerd committed
57
  $filepath = $report_path . '/' . $report_name . '.frx';
metzlerd's avatar
metzlerd committed
58
  $info = pathinfo($filepath);
metzlerd's avatar
metzlerd committed
59
  $do = TRUE;
metzlerd's avatar
metzlerd committed
60
  if (file_exists($filepath)) {
metzlerd's avatar
metzlerd committed
61
    chdir($info['dirname']);
62
    if ($delete_file) $do = unlink($info['basename']);
metzlerd's avatar
metzlerd committed
63
  }
64
  if (module_exists('locale')) {
65
    @list($tlang, $tname) = explode('/', $report_name, 2);
66
    if (array_key_exists($tlang, language_list())) {
67
68
      $report_name = $tname;
      $language = $tlang;
69
70
    }
  }
metzlerd's avatar
metzlerd committed
71
  if ($do) {
72
    db_delete('forena_reports')
metzlerd's avatar
metzlerd committed
73
74
75
    ->condition('report_name', $report_name)
    ->condition('language', $language)
    ->execute();
metzlerd's avatar
metzlerd committed
76
77
  }
  else {
metzlerd's avatar
metzlerd committed
78
    drupal_set_message(t('Unable to delete file %s', array('%s' => $info['basename'])), 'error');
79
80
81
  }
}

metzlerd's avatar
metzlerd committed
82

83
84


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

106
  $element['format'][$name] = array(
metzlerd's avatar
metzlerd committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
    '#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
122
  return $element['format'];
metzlerd's avatar
metzlerd committed
123
}
124

metzlerd's avatar
metzlerd committed
125
126
127
128
129
130

/**
 * Accepts the name of a file
 *
 * Returns an editor object of the file.
 *
metzlerd's avatar
metzlerd committed
131
 */
metzlerd's avatar
metzlerd committed
132
function forena_get_report_editor($report_name) {
metzlerd's avatar
metzlerd committed
133
  require_once('FrxEditor.inc');
metzlerd's avatar
metzlerd committed
134
135
136
137
138
139
140
  if ($report_name) {
    $r_text='';
    $report_path = forena_report_path();
    $filename = $report_path . '/' . $report_name . '.frx';
    if (file_exists($filename)) {
      $r_text = file_get_contents($filename);
    }
metzlerd's avatar
metzlerd committed
141
    $r = new FrxEditor($r_text);
metzlerd's avatar
metzlerd committed
142
143
144
    return $r;
  }
  else {
metzlerd's avatar
metzlerd committed
145
    return new FrxEditor();
metzlerd's avatar
metzlerd committed
146
147
148
  }
}

149
/**
150
151
152
 * Report syncronization form
 * @param $formid
 * @param $form_state
153
 */
154
function forena_sync_form($formid, &$form_state) {
155
156
157
158
159
160
161

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

162
163
164
165
166
  $form['sync'] = array(
    '#type' => 'submit',
    '#value' => t('Clear Cache'),
    '#submit' => array('forena_settings_sync_submit'),
  );
167
168

  return $form;
169
170
171
172
173
174
175
176

}

/**
 * Forena admin settings form
 *
 */
function forena_settings() {
177
  $skins = variable_get('forena_skins', array());
178
179
180

  $report_path = forena_report_path();

181

182
183
184
  $form['forena_report_repos'] = array(
    '#type' => 'textfield',
    '#title' => t('Report Repository'),
metzlerd's avatar
metzlerd committed
185
186
187
188
189
190
    '#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,
  );

191
192
193
  $form['forena_last_report_path'] = array(
    '#type' => 'value',
    '#value' => forena_report_path(),
metzlerd's avatar
metzlerd committed
194
195
  );

196

197
  $form['forena_input_format'] = forena_filter_element(variable_get('forena_input_format', filter_default_format()), 'forena_input_format');
198
  $form['forena_default_form'] = array(
199
    '#type' => 'select',
200
    '#title' => t('Default report skin'),
201
    '#options' => $skins,
202
    '#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
203
    . ' Skins are basically css and javascript libraries added to your report.'),
metzlerd's avatar
metzlerd committed
204
    '#default_value' => variable_get('forena_default_form', ''),
metzlerd's avatar
metzlerd committed
205
    );
metzlerd's avatar
metzlerd committed
206

metzlerd's avatar
metzlerd committed
207
208
209
    $form =  system_settings_form($form);
    $form['#submit'][] = 'forena_settings_submit';
    return $form;
metzlerd's avatar
metzlerd committed
210

211
212
}
/**
213
 * Added submit handler to create directories and clear menu cache
214
215
216
217
 *
 * @param unknown_type $form
 * @param unknown_type $form_state
 */
metzlerd's avatar
metzlerd committed
218
219
function forena_settings_submit($form, &$form_state) {
  $values = $form_state['values'];
220
  $path = $values['forena_report_repos'];
metzlerd's avatar
metzlerd committed
221
  $src_dir = drupal_get_path('module', 'forena') . '/repos/reports';
222
  if ($path != $values['forena_last_report_path']) {
223
224
    if (!file_exists($path)) {
      try {
225

226
227
228
229
        if (file_exists($path)) {
          drupal_set_message(t('Created directory %s', array($path))) ;
        }
        mkdir($path);
230

231
232
233
234
235
      } catch (Exception $e) {
        forena_error(t('Unable to create report directory'), $e->getMessage());
        return;
      }
    }
metzlerd's avatar
metzlerd committed
236
  }
237
238
  forena_sync_reports();
}
metzlerd's avatar
metzlerd committed
239

240
function forena_settings_sync_submit($form, &$form_state) {
241
  forena_sync_reports($form_state['values']['sync_overwrite']);
242
  drupal_set_message(t('Report cache cleared'));
243
}
244

245
function forena_format_form($formid, $form_state, $report_name) {
metzlerd's avatar
metzlerd committed
246
  $desc = Frx::Menu()->parseURL($report_name);
247
  $name = $desc['name'];
248
  if (!$desc['exists']) {
metzlerd's avatar
metzlerd committed
249
250
    drupal_not_found();
    exit;
251
  }
252
253
254
255
256
257
258
259
260
261
  $filename = $desc['filename'];
  $format = isset($desc['format']) ? $desc['format'] : '';
  if ($desc['exists']) {
    $r = forena_get_report_editor($name);
    drupal_set_title($r->title);
    $form = array();
    $r = forena_get_report_editor($name);
    $frx_options = $r->getOptions();
    $report_form = @$frx_options['form'];

262
    $doclist = Frx::Menu()->doc_formats;
263
264
    $skins[''] = t('Use Default');
    $skins = array_merge(variable_get('forena_skins', array()), $skins);
265

metzlerd's avatar
metzlerd committed
266
267
268
269
    $form['report_name'] = array(
       '#type' => 'value',
       '#value' => $name,
    );
270

metzlerd's avatar
metzlerd committed
271
    $form['form'] = array(
272
      '#type' => 'select',
273
      '#title' => t('Skin'),
274
      '#options' => $skins,
metzlerd's avatar
metzlerd committed
275
      '#default_value' => $report_form,
metzlerd's avatar
metzlerd committed
276
      '#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
277
    );
278

metzlerd's avatar
metzlerd committed
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
    //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
295
        if ($doc && array_search($value, $doclist)!==FALSE) {
metzlerd's avatar
metzlerd committed
296
          $default[$value] = $value;
297
        }
metzlerd's avatar
metzlerd committed
298
      }
299

metzlerd's avatar
metzlerd committed
300
301
302
303
304
305
306
      //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
      );
307

metzlerd's avatar
metzlerd committed
308
309
310
311
312
      $form['submit'] = array(
        '#type' => 'submit',
        '#value' => 'Save',
      );
    }
313
314
315
316
317
  }
  return $form;
}

function forena_format_form_submit($form, &$form_state) {
metzlerd's avatar
metzlerd committed
318
  $values = $form_state['values'];
319
320
321
322
323
324
325
326
327
328
329
  $name = $values['report_name'];

  $r = forena_get_report_editor($name);
  $options = array(
    'form' => $values['form']);
  $r->setOptions($options);


  // Doc gen settings.
  if (isset($form['docgen'])) {
    $docgen = array();
metzlerd's avatar
metzlerd committed
330
    if ($selected = array_filter(@$values['docgen']['docs'])) {
331
      if ($selected) foreach ($selected as $key => $value) {
metzlerd's avatar
metzlerd committed
332
        if ($value) $docgen[] = array('type' => $key);
333
334
335
336
337
338
339
340
341
342
343
344
345
      }
    }
    $r->setDocgen($docgen);
  }

  if (forena_save_report($name, $r->asXML(), TRUE) == 1) {
    drupal_set_message(t('Your report, %s has been saved.', array('%s' => $name)));
  }
  else {
    drupal_set_message(t('There was an error saving your report, %s to the database.', array('%s' => $name)));
  }

}
346

347
348
349
350
351
/**
 * Form function for the edit report form
 * @param $form_state
 * @return the form
 */
352
function forena_layout_form($form, $form_state, $report_name) {
metzlerd's avatar
metzlerd committed
353
  $name_in = $report_name;
metzlerd's avatar
metzlerd committed
354
  $desc = Frx::Menu()->parseURL($report_name);
metzlerd's avatar
metzlerd committed
355
  $name = $desc['name'];
356
  global $language;
metzlerd's avatar
metzlerd committed
357
  //determine if this is an add new report request
358

metzlerd's avatar
metzlerd committed
359
360
  $filename = $desc['filename'];
  $format = @$desc['format'];
361
  if ($name) {
362

363
364
    if ((isset($desc['exists']) && $desc['exists'])) {
      $save_name = $name;
metzlerd's avatar
metzlerd committed
365
      //set the name to empty string for new reports
366
      $r = forena_get_report_editor($name);
metzlerd's avatar
metzlerd committed
367

368
369
370
371
372
373
374
375
376
377
      $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 {
          $lang = 'en';
378
        }
metzlerd's avatar
metzlerd committed
379
      }
380
381
382
383
384
385
386
      drupal_set_title(filter_xss($r->title));
      // 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();
metzlerd's avatar
metzlerd committed
387
      $menu = $r->getMenu();
388
      $body = $r->simplexml->body->asXML();
389
      $css = @(string)$r->simplexml->head->style;
390

metzlerd's avatar
metzlerd committed
391
      $form = array();
metzlerd's avatar
metzlerd committed
392

393
      //array of xml attributes that are required to have a value
394
      $required = array('id' => TRUE, 'label' => TRUE);
metzlerd's avatar
metzlerd committed
395

396
      $form['report_name'] = array(
metzlerd's avatar
metzlerd committed
397
        '#type' => 'value',
metzlerd's avatar
metzlerd committed
398
        '#value' => $name,
399
      );
metzlerd's avatar
metzlerd committed
400

401
402
403
404
405
      $form['name_in'] = array(
        '#type' => 'value',
        '#value' => $name_in,
      );

406
407
408
      $form['attributes'] = array(
        '#type' => 'value',
        '#value' => $attributes,
409
      );
metzlerd's avatar
metzlerd committed
410

411
412
413
414
415
416
      $form['title'] = array(
        '#type' => 'textfield',
        '#title' => t('Title'),
        '#default_value' => $title,
      );

417
418
419
420
421
422
423
424
425
      $form['body'] = array(
        '#type' => 'text_format',
        '#title' => t('Body'),
        '#default_value' => $body,
        '#rows' => 25,
      '#format' => variable_get('forena_input_format', filter_default_format())
      );
      $form['visibility'] = array(
        '#type' => 'fieldset',
metzlerd's avatar
metzlerd committed
426
        '#title' => t('Visibility'),
427
428
429
      );

      $form['visibility']['category'] = array(
430
431
432
        '#type' => 'textfield',
        '#title' => t('Category'),
        '#default_value' => $category,
metzlerd's avatar
metzlerd committed
433
        '#autocomplete_path' => 'forena/categories/autocomplete',
metzlerd's avatar
metzlerd committed
434
        '#description' => t('The heading your report will be grouped under on the report list.'),
metzlerd's avatar
metzlerd committed
435
      );
metzlerd's avatar
metzlerd committed
436

437
      $form['visibility']['hidden'] = array(
438
        '#type' => 'checkbox',
metzlerd's avatar
metzlerd committed
439
        '#title' => t('Hidden'),
440
        '#default_value' => $hidden,
metzlerd's avatar
metzlerd committed
441
        '#description' => t('Hide your report from showing up on the report list.'),
442
443
      );

metzlerd's avatar
metzlerd committed
444
445
446
447
448
449
450
451
452
453
454
      $form['menu'] = array(
        '#type' => 'fieldset',
        '#title' => t('Menu'),
        '#tree' => TRUE,
        '#collapsible' => TRUE,
        '#collapsed' => empty($menu),
      );

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

459
460
461
462
463
464
465
      $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'],
      );

466
467
468
      $form['menu']['title'] = array(
        '#type' => 'textfield',
        '#title' => t('Menu Title'),
metzlerd's avatar
metzlerd committed
469
        '#description' => t('Title of menu item.  Leave blank to use the report title as the menu title.'),
470
        '#default_value' => @$menu['title'],
471
472
      );

metzlerd's avatar
metzlerd committed
473
474
475
476
477
478
479
480
481
482
483
      $menu_options = array(
        'normal-item' => t('Normal'),
        'local-task' => t('Tab'),
        'default-local-taks' => t('Default Tab'),
        'callback' => t('Callback'),
      );

      $form['menu']['type'] = array(
        '#type' => 'select',
        '#title' => 'Type of menu to create',
        '#options' => $menu_options,
484
        '#default_value' => @$menu['type'],
metzlerd's avatar
metzlerd committed
485
486
      );

487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
      $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,
      );

metzlerd's avatar
metzlerd committed
502
      $form['buttons']['save'] = array(
503
504
        '#type' => 'submit',
        '#value' => 'Save',
metzlerd's avatar
metzlerd committed
505
        '#submit' => array('forena_layout_form_submit'),
506
      );
metzlerd's avatar
metzlerd committed
507

metzlerd's avatar
metzlerd committed
508
      if (user_access('delete report')) {
metzlerd's avatar
metzlerd committed
509
510
511
512
513
        $form['buttons']['delete'] = array(
          '#type' => 'submit',
          '#value' => 'Delete',
          '#submit' => array('forena_edit_delete_submit'),
        );
metzlerd's avatar
metzlerd committed
514
      }
515
516
517
518
      return $form;
    }
    else {
      drupal_not_found();
519
      exit;
520
521
522
523
    }
  }
  else {
    drupal_not_found();
524
    exit;
metzlerd's avatar
metzlerd committed
525
  }
526
}
527

528
function forena_layout_form_validate($form, &$form_state) {
metzlerd's avatar
metzlerd committed
529
  $values = $form_state['values'];
530
531
532
533
534
535
  $body = $values['body']['value'];
  $doc_prefix = '<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE root [
    <!ENTITY nbsp "&#160;">
    ]>';
  if ($body) {
metzlerd's avatar
metzlerd committed
536
    $body_doc = new DOMDocument('1.0', 'UTF-8');
537
538
    $body_xml =  $doc_prefix . '<html xmlns:frx="urn:FrxReports">' . $body . '</html>';
    if (@$body_doc->loadXML($body_xml) === FALSE) {
metzlerd's avatar
metzlerd committed
539
      form_set_error('body', t('Invalid XHTML Document. Check for unclosed tags or stray &'));
540
541
    }
  }
metzlerd's avatar
metzlerd committed
542
543
544
545
546
  if ($values['menu']['path']) {
    if (!valid_url(str_replace(':', '', $values['menu']['path']), FALSE)) {
      form_set_error('menu][path', t('Invalid Path'));
    }
  }
547
}
548
549
550
551
552
553
554

/**
 * builds a string of the xml document,
 * submits it to forena_save_report.
 */
function forena_layout_form_submit($form, &$form_state) {
  $nodes = array();
metzlerd's avatar
metzlerd committed
555
  $rebuild_menu = FALSE;
556
557
558
559
560
561
562
563
564
565
566
  $values = $form_state['values'];
  $report_name = $values['report_name'];
  $r = forena_get_report_editor($report_name);
  // 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
567
568
569
570
571
  $menu = $r->getMenu();
  if ($values['menu']!= $menu) {
    $r->setMenu($values['menu']);
    $rebuild_menu = TRUE;
  }
572
573
574

  // Body
  $r->setBody($values['body']['value']);
575
576
  // CSS
  $r->setStyle($values['css']);
577
  // If there are no frx attributes in the body then replace them with the old values.
578
  $frx_nodes = $r->simplexml->xpath('body//*[@frx:*]');
579
580
581
582
583
584
585
586
587
  if (!$frx_nodes) {
    $r->save_attributes_by_id($values['attributes']);
  }

  //determine redirection.
  $report_path = forena_report_path();
  $filename = $report_path . '/' . $report_name . '.frx';

  if (forena_save_report($report_name, $r->asXML(), TRUE) == 1) {
metzlerd's avatar
metzlerd committed
588
    drupal_set_message(t('Your report, %s has been saved.', array('%s' => $report_name)));
589
  }
metzlerd's avatar
metzlerd committed
590
591
592
593
  // If we changed the menu we need to rebuild it.
  if ($rebuild_menu) {
    menu_rebuild();
  }
594
595
596
}


597
598
599
600
601
/**
 * Handle delete buttons from edit forms.
 * @return unknown_type
 */
function forena_edit_delete_submit($form, &$form_state) {
metzlerd's avatar
metzlerd committed
602
  $link = 'reports/' . $form_state['values']['name_in'] ;
603
604
605
606
607
608
609
610
611
612
613
614
615
616
  $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
617
    $desc = Frx::Menu()->parseURL($report_name);
618
    $name = $desc['name'];
619
620
621
    $filename = $desc['filename'];
    $format = @$desc['format'];
  }
622
623
624
625
  $form = array();
  global $language;
  //determine if this is an add new report request

626
627
628
629
630
631
632
633
634
635
  $r = forena_get_report_editor($name);
  $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
636
      $lang = 'en';
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
665
666
667
    }
  }
  // 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
668
    '#required' => TRUE,
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
  );

  $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',
689
    '#autocomplete_path' => 'forena/reports/autocomplete',
690
691
692
693
694
695
696
    '#default_value' => $name,
  );

  $form['save'] = array(
    '#type' => 'submit',
    '#value' => 'Create',
  );
697
698
699
700
701
  return $form;

}

function forena_add_report_form_validate($form, &$form_state) {
702
  $values = $form_state['values'];
703
  $regexp = "/^[A-Za-z0-9\/\_\-]*$/";
704
  $save_report_name = $values['save_report_name'];
705
  $desc = Frx::Menu()->parseURL($save_report_name);
metzlerd's avatar
metzlerd committed
706

707
708
  //comparing the report names to see if they have changed.
  //If they have, making sure the new name does not already exist.
metzlerd's avatar
metzlerd committed
709
710
711
712
713
  $report_path = forena_report_path();
  $filename = $report_path . '/' . $save_report_name . '.frx';
  if (@$desc['exists']) {
    form_set_error('save_report_name', t('The file ' . $save_report_name . ' already exists. Please enter another name.'));
  }
714
}
715

716
function forena_add_report_form_submit($form, &$form_state) {
717
  $values = $form_state['values'];
718
  $report_name = $values['save_report_name'];
metzlerd's avatar
metzlerd committed
719
  $desc = Frx::Menu()->parseURL($report_name);
720
  $report_name = $desc['name'];
721

722
  $r = forena_get_report_editor($values['report_name']);
723
724
725
726
727
728
  // Title and category
  $r->setTitle($values['title']);
  $r->setCategory($values['category']);
  // Form options
  $options = array(
    'hidden' => $values['hidden'],
729
  );
730
731
  $r->setOptions($options);

732
  //determine redirection.
733
  $filename = $desc['filename'];
metzlerd's avatar
metzlerd committed
734

735
  if (forena_save_report($report_name, $r->asXML(), TRUE) == 1) {
metzlerd's avatar
metzlerd committed
736
    drupal_set_message(t('Your report, %s has been saved.', array('%s' => $report_name)));
737
    //if this is a new report then redirect to data blocks
738
    if ($values['report_name']) {
metzlerd's avatar
metzlerd committed
739
      $edit = '/edit';
740
741
    }
    else {
metzlerd's avatar
metzlerd committed
742
      $edit = '/edit/data/add';
743
    }
metzlerd's avatar
metzlerd committed
744
    $form_state['redirect']= $desc['i_link'] . $edit;
745

metzlerd's avatar
metzlerd committed
746
  }
747
748
}
function forena_create_trans_form($formid, $form_state, $report_name) {
749
750
751
  $name = '';
  $filename = '';
  $format = '';
metzlerd's avatar
metzlerd committed
752
  $desc = Frx::Menu()->parseURL($report_name);
753
754
755
756
  $name = $desc['name'];
  $filename = $desc['filename'];
  $base_name = $desc['base_name'];
  $format = @$desc['format'];
757
758
  $form = array();
  global $language;
759
  $languages = language_list();
760
761
  //determine if this is an add new report request

762
763
764
765
  $r = forena_get_report_editor($name);
  $title = (string)$r->title;
  $lang = @$_GET['language'];
  if ($lang) {
metzlerd's avatar
metzlerd committed
766
    $language = $languages[$lang];
767
768
769
770
771
772
773
774
775
  }
  $form['base_name'] = array(
    '#type' => 'value',
    '#value' => $base_name
  );
  $form['report_name'] = array(
    '#type' => 'value',
    '#value' => $name,
  );
776

metzlerd's avatar
metzlerd committed
777
  foreach ($languages as $key => $obj) {
778
    $languages[$key] = $obj->native;
779
  }
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
  $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
797
    '#required' => TRUE,
798
799
800
801
802
803
804
805
806
  );


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

807
808
809
810
}

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

metzlerd's avatar
metzlerd committed
814
  $desc = Frx::Menu()->parseURL($new_name);
815
816
  $filename = $desc['filename'];
  $report_name = $desc['name'];
817
818
819
820
821

  $r = forena_get_report_editor($values['report_name']);

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

metzlerd's avatar
metzlerd committed
823
  //determine redirection.
824
825

  if (file_exists($filename)) {
metzlerd's avatar
metzlerd committed
826
    drupal_set_message(t('Report %s already exists', array('%s' => $new_name)), 'error');
827
    return;
metzlerd's avatar
metzlerd committed
828
  }
metzlerd's avatar
metzlerd committed
829

830
  if (forena_save_report($report_name, $r->asXML(), TRUE) == 1) {
metzlerd's avatar
metzlerd committed
831
    drupal_set_message(t('Translation,  %s has been created. Switch languages to translate.', array('%s' => $values['title'])));
metzlerd's avatar
metzlerd committed
832
    //if this is a new report then redirect to data blocks
metzlerd's avatar
metzlerd committed
833
    $form_state['redirect']= array( $desc['i_link'] . '/edit/layout');
metzlerd's avatar
metzlerd committed
834
  }
835
}
836

837
838


839
840
841
842
/*
 * administer the settings for document format options
 */
function forena_doc_formats_settings() {
843
  // Invoke doc_type hook to see which document types are there.
844
  $supported_doctypes = Frx::documentTypes();
845

846
847
  $form['forena_doc_formats'] = array(
    '#type' => 'checkboxes',
848
    '#title' => t('Allowed Document Formats'),
849
850
851
852
    '#default_value' => variable_get('forena_doc_formats', $supported_doctypes),
    '#description' => t('check your desired document format'),
    '#options' => $supported_doctypes,
  );
853
854
855
856

  $form['forena_doc_defaults'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Default Document Formats'),
857
    '#default_value' => variable_get('forena_doc_defaults', array()),
858
859
860
861
    '#description' => t('check your desired document format'),
    '#options' => $supported_doctypes,
  );

862
863
864
  $form['forena_email_override'] = array(
    '#type' => 'checkbox',
    '#title' => 'Run email merges in test mode' ,
metzlerd's avatar
metzlerd committed
865
    '#default_value' => variable_get('forena_email_override', FALSE),
866
867
    '#description' => t('When this box is checked emails are sent to the currently logged in user.  Useful for testing environments.'),
  );
868
  $form['forena_email_input_format'] = forena_filter_element(variable_get('forena_email_input_format', 'full_text'), 'forena_email_input_format');
869
  $form['forena_email_input_format']['#title'] = t('Email Input Format');
870

871
872
  return system_settings_form($form);
}
873

874
875
876
877
/*
 * administer the settings for document format options
 */
function forena_data_settings() {
878
  $repos = Frx::RepoMan()->repositories;
879
  $r_list = array();
metzlerd's avatar
metzlerd committed
880
881
  $headers = array(t('Name'), t('Description'), t('Path'), t('Operation'));
  foreach ($repos as $name => $r) {
882
    $r_list[] = array(
metzlerd's avatar
metzlerd committed
883
884
885
886
    $name,
    $r['title'],
    $r['path'],
    l(t('configure'), 'admin/config/content/forena/data/configure/' . $name)
887
888
889
890
    );

  }

metzlerd's avatar
metzlerd committed
891
892
  $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' => ''));
893
894
895
896
  return $output;
}

function forena_data_settings_edit($form, &$form_state, $source=-1) {
897
  global $databases;
898
899
900
901
902
903
904
905
906
  $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
907
          'data provider' => 'FrxDrupal',
908
          'database' => 'default',
909
910
          'access callback' => 'user_access',
          'user callback' => 'forena_current_user_id'
metzlerd's avatar
metzlerd committed
911
912
          ),
          );
913
914
    }
    else {
915
916
      Frx::RepoMan()->repository($source);
      $repos = Frx::RepoMan()->repositories;
917
      $r = $repos[$source];
davidmetzler's avatar
davidmetzler committed
918
      // Remove teh object from the data.
919
      unset($r['data']);
920
921
922
      $form_state['storage'] = array(
        'name' => $source,
        'title' => $r['title'],
davidmetzler's avatar
davidmetzler committed
923
        'path' => @$r['path'],
924
        'config' => $r,
925
926
927
928
929
930
      );

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

davidmetzler's avatar
davidmetzler committed
932
  $locked = !($adding || (@$config['source'] == 'user'));
933
934
935
936
937
938
939
940
941
  $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
942
  );
943
944
945
946

  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
davidmetzler's avatar
davidmetzler committed
947
    '#required' => TRUE,
948
949
950
951
952
953
954
955
956
957
958
    '#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
959
960
961
962
963
964
965
  $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'],
  );

966
967
968
  $form['path'] = array(
    '#type' => 'textfield',
    '#title' => t('Path'),
davidmetzler's avatar
davidmetzler committed
969
    '#required' => TRUE,
970
    '#disabled' => $locked,
971
972
973
974
    '#description' => t('Directory containing data block files.'),
    '#default_value' => @$data['path'],
  );

davidmetzler's avatar
davidmetzler committed
975
976
977
978
979
980
981
982
983
  $user_options = array(
    '' => 'None',
    'forena_current_user_id' => 'UID',
    'forena_current_user_name' => 'User name',
  );

  $form['user_callback'] = array(
    '#type' => 'select',
    '#title' =>  'Current user',
984
    '#disabled' => $locked,
davidmetzler's avatar
davidmetzler committed
985
986
    '#description' => t('Can be refererenced as :current_user in each data block.'),
    '#options' => $user_options,
987
    '#default_value' => @$config['user callback'],
davidmetzler's avatar
davidmetzler committed
988
989
990
991
    '#disabled' => $locked,
  );


992
993
  // Access method list
  $access = array(
davidmetzler's avatar
davidmetzler committed
994
    'callback' => t('Use drupal permissions'),
metzlerd's avatar
metzlerd committed
995
    'block' => t('Match values provided by a data block.'),
996
997
998
999
1000
  );

  $form['access_method'] = array(
    '#type' => 'select',
    '#options' => $access,
1001
    '#disabled' => $locked,
davidmetzler's avatar
davidmetzler committed
1002
1003
    '#title' => t('Data security method'),
    '#default_value' => empty($config['access block']) ? 'callback' : 'block',
metzlerd's avatar
metzlerd committed
1004
    '#description' => t('Specify how the ACCESS defined for a data block is to be interpreted.'),
1005
1006
1007
    '#ajax' => array(
      'callback' => 'forena_access_info_callback',
      'wrapper' => 'access-details',
metzlerd's avatar
metzlerd committed
1008
  ),
1009
1010
1011
1012
1013
1014
  );

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

  switch (!empty($values['access_method']) ? $values['access_method'] : $form['access_method']['#default_value']) {
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
    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'],
      );
1036
1037
1038
1039
1040
1041
1042
1043
  }



  // Driver list
  $drivers = array(
    'FrxDrupal' => t('Drupal'),
    'FrxOracle' => t('Oracle Database'),
metzlerd's avatar
metzlerd committed
1044
    'FrxPDO' => t('PDO other than Drupal'),
1045
1046
1047
1048
1049
    'FrxPostgres' => t('Postgres Database'),
    'FrxMSSQL' => t('MSSQL Database'),
    'FrxFiles' => t('XML Files'),
  );

davidmetzler's avatar
davidmetzler committed
1050
  $form['data_provider'] = array(
1051
1052
1053
1054
1055
    '#type' => 'select',
    '#title' => t('Driver'),
    '#description' => t('Forena data connection type'),
    '#options' => $drivers,
    '#disabled' => $locked,