forena.report.inc 48.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
/*
 * @file forena.report.inc
 * Include files containing form function for editinig reports
 */
function forena_add_report_form($formid, $form_state, $report_name='') {
  $name = '';
  $filename = '';
  $format = '';
  if ($report_name) {
    $desc = Frx::Menu()->parseURL($report_name);
    $name = $desc['name'];
    $filename = $desc['filename'];
    $format = @$desc['format'];
  }
  $form = array();
  global $language;
  //determine if this is an add new report request

  $r = Frx::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 {
      $lang = 'en';
    }
  }
  // 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());

metzlerd's avatar
metzlerd committed
47
48

  $form['title'] = array(
49
      '#type' => 'textfield',
metzlerd's avatar
metzlerd committed
50
51
52
53
54
55
      '#title' => t('Title'),
      '#default_value' => $title,
      '#required' => TRUE,
  );
  $form['report_name'] = array(
      '#type' => 'forena_machine_name',
56
57
58
59
60
61
      '#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,
metzlerd's avatar
metzlerd committed
62
      '#machine_name' => array('source' => array('report_name'), 'label' => 'Report_name'),
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
  );


  $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['clone_report_name'] = array(
      '#title' => t('Create from report'),
      '#type' => 'textfield',
      '#autocomplete_path' => 'forena/reports/autocomplete',
      '#default_value' => $name,
  );

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

}

function forena_validate_report_name($form, &$form_state) {
  $values = $form_state['values'];
  $pattern = "@^[A-Za-z0-9\/\_]$@";
  $save_report_name = $values['report_name'];
  if (preg_match($pattern, $save_report_name)) {
    form_set_error('report_name', t('Invalid characters in report name') );
  }
  //comparing the report names to see if they have changed.
  //If they have, making sure the new name does not already exist.
  $filename = $save_report_name . '.frx';
  if (Frx::File()->exists($filename, FALSE)) {
    form_set_error('reprot_name', t('The report ' . $save_report_name . ' already exists. Please enter another name.'));
  }
}

function forena_create_report_submit($form, &$form_state) {
  $values = $form_state['values'];
  $report_name = $values['report_name'];
  $desc = Frx::Menu()->parseURL($report_name);
  $report_name = $desc['name'];

  if ($values['report_name']) {
    $r = Frx::Editor($values['clone_report_name']);
    $r->report_name = $values['report_name'];
  }
  else {
    $r = Frx::Editor($values['report_name']);
  }
  // Title and category
  $r->setTitle($values['title']);
  $r->setCategory($values['category']);
  // Form options
  $options = array(
      'hidden' => $values['hidden'],
  );
  $r->setOptions($options);

  //determine redirection.
  $filename = $desc['filename'];

  $r->update();
  //if this is a new report then redirect to data blocks
  if ($values['report_name']) {
    $edit = '/edit';
  }
  else {
David Metzler's avatar
David Metzler committed
144
    $edit = '/edit/select-data/add-data';
145
146
147
148
149
150
151
152
153
154
  }
  $form_state['redirect']= $desc['i_link'] . $edit;

}

/**
 * Creates a simple save button to be used with other forms
 * @return multitype:string multitype:string  NULL
 */
function forena_report_save_form($form, &$form_state, $report_name) {
155
156
157
158
159
  $form['report_name'] = array(
  	'#type' => 'value',
    '#value' => $report_name,
  );

160
161
162
163
164
165
166
167
168
169
170
  $form['save'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
    '#submit' => array('forena_report_save_submit'),
  );

  $form['cancel'] = array(
  	'#type' => 'submit',
    '#value' => t('Cancel'),
    '#submit' => array('forena_report_save_cancel'),
  );
David Metzler's avatar
Fixes    
David Metzler committed
171
172
173
  $parms = $_GET;
  unset($parms['q']);
  $form_state['redirect'] = array("reports/$report_name", array('query' => $parms));
174
175
176
  return $form;
}

metzlerd's avatar
metzlerd committed
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/**
 * Form to confirm the delete of a form.
 * @param $form_state
 * @return unknown_type
 */
function forena_report_delete_form($form_state, $formid, $report_name) {
  // Parse names from url
  $name_in = $report_name;
  $desc = Frx::Menu()->parseURL($report_name);
  $report_name = $desc['name'];
  global $language;

  $link = $desc['link'];
  $form['report_name'] = array(
      '#type' => 'value',
      '#value' => $report_name
  );
  if (module_exists('locale')) {

    $options = array('all' => t('All Languages'),
        'single' => t('Only %lang', array('%lang' => $language->native)),
    );
    $form['delete_all_languages'] = array(
        '#type' => 'radios',
        '#title' => t('Remove custom reports in all languages?'),
        '#options' => $options,
        '#default_value' => 'single',
    );
    $form['base_name'] = array(
        '#type' => 'value',
        '#value' => $desc['base_name'],
    );
  }
  return confirm_form($form,
      t('Are you sure you want to remove %title?', array('%title' => $report_name)),
      isset($_GET['destination']) ? $_GET['destination'] : $link,
      t('This action cannot be undone.'),
      t('Remove'),
      t('Cancel')
  );
  return $form;
}


function forena_report_delete_form_submit($form, &$form_state) {
  $values = $form_state['values'];
  $report_name = $values['report_name'];
  if (isset($values['delete_all_languages']) && $values['delete_all_languages']=='all') {
    foreach (language_list() as $language) {
      $report_name = $language->language . '/' . $values['base_name'];
      $desc = Frx::Menu()->parseURL($report_name);
      Frx::Editor($report_name)->delete();
      forena_delete_report($desc['name']);
    }
    drupal_set_message(t('Deleted %s', array('%s' => $values['base_name'])));
  }
  else {
    drupal_set_message(t('Deleted %s', array('%s' => $report_name)));
    Frx::Editor($report_name)->delete();
    }
  menu_rebuild();
  $form_state['redirect'] = 'forena';
}




function forena_report_save_submit($form, &$form_state) {
245
246
247
  $report_name = $form_state['values']['report_name'];
  $r = Frx::Editor($report_name);
  $r->save();
metzlerd's avatar
metzlerd committed
248
249
  $r_link = $r->report_link;
  $form_state['redirect'] = array("$r_link", array('query' => Frx::parms()));
250
251
252
}

function forena_report_preview($report_name) {
metzlerd's avatar
metzlerd committed
253
  $r = Frx::Editor($report_name, TRUE);
254
255
  drupal_set_title($r->title);
  $content['save'] = drupal_get_form('forena_report_save_form', $report_name);
256
  $content['preview'] = $r->preview();
257
258
259
  return $content;
}

metzlerd's avatar
metzlerd committed
260
function forena_report_general_form($form, &$form_state, $report_name) {
261
262
263
264
265
266
  if (!$report_name) {
    $report_name = $form_state['storage']['report_name'];
  }
  else {
    $form_state['storage']['report_name'] = $report_name;
  }
metzlerd's avatar
metzlerd committed
267
268
269
270
271
272
273
274
275

  //set the name to empty string for new reports
  $r = Frx::Editor($report_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();
276
277
278
279
280
281
282
283
  if (isset($form_state['values']['menu'])) {
    $menu = $form_state['values']['menu'];
  }
  else {
    $menu = $r->getMenu();
    $menu['enabled'] = @$menu['path'] ? 1 : 0;
    if (!isset($menu['type'])) $menu['type'] = 'callback';
  }
metzlerd's avatar
metzlerd committed
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
  $cache = $r->getCache();
  $form['report_name'] = array(
      '#type' => 'value',
      '#value' => $report_name,
  );

  $form['title'] = array(
      '#type' => 'textfield',
      '#title' => t('Title'),
      '#default_value' => $title,
  );

  $form['visibility'] = array(
      '#type' => 'fieldset',
      '#title' => t('Visibility'),
  );

  $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.'),
  );

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

  $form['menu'] = array(
      '#type' => 'fieldset',
      '#title' => t('Menu'),
      '#tree' => TRUE,
      '#collapsible' => TRUE,
      '#collapsed' => empty($menu),
322
323
324
325
326
327
328
329
330
331
332
333
      '#prefix' => '<div id="forena-menu-wrapper">',
      '#suffix' => '</div>',
  );

  $form['menu']['enabled'] = array(
      '#type' => 'checkbox',
      '#title' => t('Provide a menu link'),
      '#default_value' => @$menu['enabled'],
      '#ajax' => array(
         'callback' => 'forena_report_general_menu_ajax',
         'wrapper' => 'forena-menu-wrapper',
      ),
metzlerd's avatar
metzlerd committed
334
335
336
337
338
  );

  $form['menu']['path'] = array(
      '#type' => 'textfield',
      '#title' => t('Menu Path'),
339
340
      '#access' => $menu['enabled'],
      '#description' => t('Indicate site relative path to menu.  Parameters may be embedded in the url using a :parm syntax (e.g. states/:state)'),
metzlerd's avatar
metzlerd committed
341
342
343
344
345
346
347
      '#default_value' => @$menu['path'],
  );

  $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)'),
348
      '#access' => $menu['enabled'],
metzlerd's avatar
metzlerd committed
349
350
351
352
353
354
      '#default_value' => @$menu['args'],
  );

  $menu_options = array(
      'normal-item' => t('Normal'),
      'local-task' => t('Tab'),
355
      'default-local-task' => t('Default Tab'),
metzlerd's avatar
metzlerd committed
356
357
358
359
360
361
362
      'callback' => t('Callback'),
  );

  $form['menu']['type'] = array(
      '#type' => 'select',
      '#title' => 'Type of menu to create',
      '#options' => $menu_options,
363
      '#access' => $menu['enabled'],
metzlerd's avatar
metzlerd committed
364
      '#default_value' => @$menu['type'],
365
366
367
368
369
370
371
372
373
374
375
376
377
      '#ajax' => array(
        'callback' => 'forena_report_general_menu_ajax',
  	    'wrapper' => 'forena-menu-wrapper'
      ),
  );

  $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.'),
      '#access' => $menu['enabled'] && $menu['type']!='callback',
      '#default_value' => @$menu['title'],
  );
metzlerd's avatar
metzlerd committed
378
  $options = menu_parent_options(menu_get_menus(), 0);
metzlerd's avatar
metzlerd committed
379
380
381
  $default = @$menu['menu_name'];
  if ($default && @$menu['plid']) {
    $default .= ':' . $menu['plid'];
382
  }
metzlerd's avatar
metzlerd committed
383

384
385
386
387
388
  $form['menu']['parent'] = array(
      '#type' => 'select',
      '#title' => t('Parent item'),
      '#default_value' => $default,
      '#options' => $options,
metzlerd's avatar
metzlerd committed
389
390
      '#empty_option' => t('Not Specified'),
      '#empty_value' => '',
391
392
393
394
395
396
397
398
      '#access' => $menu['enabled'] && $menu['type'] == 'normal-item',
      '#attributes' => array('class' => array('menu-parent-select')),
  );
  $form['menu']['weight'] = array(
      '#type' => 'weight',
      '#title' => t('Weight'),
      '#delta' => 50,
      '#access' => $menu['enabled']  && $menu['type']!= 'callback',
metzlerd's avatar
metzlerd committed
399
      '#default_value' => @$menu['weight'],
400
      '#description' => t('Menu links with smaller weights are displayed before links with larger weights.'),
metzlerd's avatar
metzlerd committed
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
  );

  $form['cache'] = array(
      '#type' => 'fieldset',
      '#title' => t('Cache'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#tree' => TRUE,
  );

  $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'],
  );

  $form['cache']['per_user'] = array(
      '#type' => 'checkbox',
      '#title' => t('Per User'),
      '#default_value' => @$cache['per_user'],
  );

  $form['cache']['per_doctype'] = array(
      '#type' => 'checkbox',
      '#title' => t('Per Document Type'),
      '#default_value' => @$cache['per_doctype'],
  );

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

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

  return $form;
}

445
function forena_report_general_menu_ajax($form) {
metzlerd's avatar
metzlerd committed
446
  drupal_get_messages('warning');
447
448
449
450
  return $form['menu'];
}


metzlerd's avatar
metzlerd committed
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
function forena_report_general_form_validate($form, &$form_state) {
  $values = $form_state['values'];
  if ($values['menu']['path']) {
    if (!valid_url(str_replace(':', '', $values['menu']['path']), FALSE)) {
      form_set_error('menu][path', t('Invalid Path'));
    }
  }
  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'));
  }
}

function forena_report_general_form_submit($form, &$form_state) {
  $values = $form_state['values'];
  $report_name = $values['report_name'];
471
  $r = Frx::Editor($report_name);
metzlerd's avatar
metzlerd committed
472
473
474
475
476
477
478
479
480
481
  $r_link = $r->report_link;

  // Title and category
  $r->setTitle($values['title']);
  $r->setCategory($values['category']);
  // Form options
  $options = array(
      'hidden' => $values['hidden'],
  );
  $r->setOptions($options);
482

metzlerd's avatar
metzlerd committed
483
  $menu = $r->getMenu();
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
  $menu['enabled'] = (int)(bool) @$menu['path'];
  $new_menu = $form_state['values']['menu'];
  $new_menu= $values['menu'];
  // Preprocess the menu
  if (!$values['menu']['enabled']) {
    $new_menu = array_fill_keys(array_keys($new_menu), '');
  }
  elseif ($new_menu['type']!=='normal-item') {
    $new_menu['plid'] = '';
    $new_menu['menu_name'] = '';
  }
  else {
    @list($new_menu['menu_name'], $new_menu['plid']) = explode(':', $new_menu['parent'], 2);
    unset($new_menu['parent']);
  }
  unset($menu['enabled']);

  // If the menu is different, save it.
metzlerd's avatar
metzlerd committed
502
  if ($values['menu']!= $menu) {
503
    $r->setMenu($new_menu);
metzlerd's avatar
metzlerd committed
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
  }

  $cache = $r->cache;
  if ($values['cache']!=$cache) {
    $r->setCache($values['cache']);
  }

  $r->update();
  $form_state['redirect'] = array("$r_link/edit", array('query' => Frx::parms()));
}

function forena_report_format_form($formid, $form_state, $report_name) {
  $r = Frx::Editor($report_name);

  drupal_set_title($r->title);
  $form = array();

  $doclist = Frx::Menu()->doc_formats;

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

  //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 . '"]') : '';
      if ($doc && array_search($value, $doclist)!==FALSE) {
        $default[$value] = $value;
      }
    }

    //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
    );

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

    );

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

function forena_report_format_form_submit($form, &$form_state) {
  $values = $form_state['values'];
  $report_name = $values['report_name'];
  $r = Frx::Editor($report_name);
  $r_link = $r->report_link;
577

metzlerd's avatar
metzlerd committed
578
579
580
581


  // Doc gen settings.
  if (isset($form['docgen'])) {
582
    $r->setDocgen($values['docgen']['docs']);
metzlerd's avatar
metzlerd committed
583
584
585
586
587
588
589
  }

  $r->update();
  $form_state['redirect'] = array("$r_link/edit", array('query' => Frx::parms()));

}

590
591
592
593
594
595
596

/**
 * A form to preview and add data blocks to an existing report
 * @param $formid String representation fo form
 * @param $form_state Array of block type
 * @return unknown_type
 */
David Metzler's avatar
David Metzler committed
597
function forena_report_data_block_form($formid, &$form_state, $report_name, $action, $block_name, $id='') {
598
599
600
601
  $config = array();
  $r = Frx::Editor($report_name);
  // List of templates
  $templates = $r->templateOptions();
602
603
604
605
606
607
  if (!isset($form_state['storage'])) {
    $form_state['storage']['id'] = $id;
    $parms = $_GET;
    unset($parms['q']);
    $form_state['storage']['parms'] = $parms;
  }
608
609
610
  $id = isset($form_state['storage']['id']) ? $form_state['storage']['id'] : $id;
  $template_class = isset($form_state['values']['template_class']) ? $form_state['values']['template_class'] : 'FrxTable';
  if (isset($form_state['values']['config'])) {
611
    $config = array_merge($form_state['storage']['config'], $form_state['values']['config']);
612
  }
metzlerd's avatar
metzlerd committed
613
  elseif ($id && $action != 'add-data') {
614
615
    $template_class = $r->scrapeBlockConfig($id, $config);
  }
616
  $form_state['storage']['config'] = $config;
617
618
  //The default submit handler
  //If someone presses enter, this handler will execute
David Metzler's avatar
David Metzler committed
619
620
621
622
  $form['action'] = array(
    '#type' => 'value',
    '#value' => $action,
  );
623
624

  $form['add'] = array(
David Metzler's avatar
David Metzler committed
625
626
627
628
    '#type' => 'submit',
    '#value' => 'Add',
    '#submit' => array('forena_report_add_template_submit'),
    '#access' => $action=='add-data',
629
630
631
  );

  $form['update'] = array(
David Metzler's avatar
David Metzler committed
632
633
634
635
636
637
638
639
640
641
642
    '#type' => 'submit',
    '#value' => 'Update',
    '#submit' => array('forena_report_edit_template_submit'),
    '#access' => $action=='edit-data',
  );

  $form['prepend'] = array(
    '#type' => 'submit',
    '#value' => 'Insert',
    '#submit' => array('forena_report_prepend_template_submit'),
    '#access' => $action=='prepend-data',
643
644
645
  );

  $form['cancel'] = array(
David Metzler's avatar
David Metzler committed
646
647
648
    '#type' => 'submit',
    '#value' => 'Cancel',
    '#submit' => array('forena_update_cancel'),
649
  );
David Metzler's avatar
David Metzler committed
650

651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
  $form['report_name'] = array(
  	'#type' => 'value',
    '#value' => $report_name,
  );

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

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

  $form['template_class'] = array(
    '#type' => 'select',
    '#title' => t('Template'),
    '#options' => $templates,
    '#default_value' => $template_class,
    '#ajax' => array(
  	   'callback' => 'forena_template_callback',
        'wrapper' => 'forena-template-wrapper'
     ),
  );

  $form['template']['#prefix'] = '<div id="forena-template-wrapper">';
  $form['template']['#suffix'] = '</div>';
  $form['template']['config'] = array(
      '#type' => 'fieldset',
      '#title' => 'Settings',
      '#collapsible'=>TRUE,
      "#tree" => TRUE,
  );

686
  // Generate Data block with preview
687
  $parms = $form_state['storage']['parms'];
688
  $r = Frx::Editor('__report_block_preview__');
689
  $r->setEditorParms($parms);
690
  $r->addBlock($block_name, $template_class, $config);
691
  $form['template']['config'] = array_merge($form['template']['config'], $r->templateConfigForm($template_class, $config));
David Metzler's avatar
Fixes    
David Metzler committed
692
  $r->update();
693
  $preview = $r->preview($parms);
694
695
696
697
698

  $form['template']['preview_button'] = array('#type' => 'button', '#value' => t('Preview'));



David Metzler's avatar
Fixes    
David Metzler committed
699
700
701
702
  if (!$preview['#has_data']) {
    $path = "reports/$report_name/edit/preview-data/$action/$block_name";
    if ($id) $path .= "/$id";
    $options = array();
metzlerd's avatar
metzlerd committed
703
    if ($parms) $options['query'] = Frx::parms();
David Metzler's avatar
Fixes    
David Metzler committed
704
705
    drupal_goto("$path", $options);
  }
706
  $form['template']['preview']['content'] = $preview['content'];
707
708
709
710
711
  $r->cancel();

  return $form;
}

712
713
714
function forena_report_data_block_form_validate($form, &$form_state) {
  $r = Frx::Editor($form_state['values']['report_name']);
  $template_class = $form_state['values']['template_class'];
715
716
717
  $config = array_merge($form_state['storage']['config'], $form_state['values']['config']);
  $errors = $r->templateConfigFormValidate($template_class, $config, 'template][config][');
  $form_state['storage']['config'] = $config;
718
719
720
721
722
  if ($errors) foreach ($errors as $key => $message) {
    form_set_error($key, $message);
  }
}

723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
/**
 * Ajax callback to refresh the template data
 * @param unknown $form
 * @param unknown $form_state
 * @return unknown
 */
function forena_template_callback($form, &$form_state) {
  return $form['template'];
}

/**
 * Edit an existing id based on the template.
 * @param unknown $form
 * @param unknown $form_state
 */
function forena_report_edit_template_submit($form, &$form_state){
  extract($form_state['values']);
  $r = Frx::Editor($report_name);
741
  $parms = $form_state['storage']['parms'];
742
  $config = array_merge($form_state['storage']['config'], $config);
743
744
  $r = Frx::Editor($report_name);
  $r->setEditorParms($parms);
745
746
  $r->applyTemplate($id, $template_class, $config);
  $r->update();
metzlerd's avatar
metzlerd committed
747
  $form_state['redirect'] = array("reports/$report_name/edit", array('query' => Frx::parms()));
748
749
750
751
}

function forena_report_add_template_submit($form, &$form_state) {
  extract($form_state['values']);
752
  $parms = $form_state['storage']['parms'];
753
  $config = array_merge($form_state['storage']['config'], $config);
754
755
  $r = Frx::Editor($report_name);
  $r->setEditorParms($parms);
metzlerd's avatar
metzlerd committed
756
  Frx::Editor($report_name)->addBlock($block_name, $template_class, $config, $id);
757
  Frx::Editor() ->update();
metzlerd's avatar
metzlerd committed
758
  $form_state['redirect'] = array("reports/$report_name/edit", array('query' => Frx::parms()));
David Metzler's avatar
Fixes    
David Metzler committed
759
  }
760

David Metzler's avatar
David Metzler committed
761
762
763
764
765
766
767
768
769
770
771
772
function forena_report_prepend_template_submit($form, &$form_state) {
  extract($form_state['values']);
  $parms = $form_state['storage']['parms'];
  $r = Frx::Editor($report_name);
  $r->setEditorParms($parms);
  Frx::Editor($report_name)->prependBlock($block_name, $template_class, (array)$config, $id);
  Frx::Editor() ->update();
  $parms = $_GET;
  unset($parms['q']);
  $form_state['redirect'] = array("reports/$report_name/edit", array('query' => $parms));
}

metzlerd's avatar
metzlerd committed
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797

function forena_report_translations($name_in='') {
  $desc = Frx::Menu()->parseURL($name_in);
  global $language;
  $cur_lang = $language->language=='en' ? '' : $language->language;
  $name = $desc['name'];
  $filename = $desc['filename'];
  $languages = language_list();
  @list($lang, $base_name) = explode('/', $name, 2);
  if (array_key_exists($lang, $languages)) {
    $name = $base_name;
  }
  elseif ($cur_lang &&  Frx::File()->exists( $cur_lang . '/' . str_replace('.', '/', $name_in) . '.frx')) {
    $name_in = $cur_lang . '.' . $name_in;
  }
  $data = array();
  $headers = array(t('Language'), t('Title'), t('Last Modified'), t('Operation'));
  // Generate list of all existing tranlsations and their status.
  foreach ($languages as $key => $language) {
    $i_name = ($key=='en') ? $name : $key . '/' . $name;
    $i_link = $key . '.' . $name;
    if (Frx::File()->exists($i_name . '.frx')) {
      $modified = format_date(filemtime(Frx::File()->path($i_name . '.frx')));
      $r = Frx::Editor($i_name);
      $title = $r->title;
metzlerd's avatar
metzlerd committed
798
      $edit = '';// l(t('Edit'), 'reports/' . str_replace('/', '.', $i_link) . '/edit', array('query' => array('target_language' => $key)));
metzlerd's avatar
metzlerd committed
799
800
801
802
    }
    else {
      $modified = '';
      $title ='';
metzlerd's avatar
metzlerd committed
803
      $edit = l(t('Add'), 'reports/' . $name_in . '/translate', array('query' => array('target_language' => $key)));
metzlerd's avatar
metzlerd committed
804
805
806
807
808
809
810
811
812
813
    }
    $data[] = array($language->native, $title, $modified, $edit);

  }

  return theme_table(array('header' => $headers, 'rows' => $data, 'attributes' => array(), 'caption' => '', 'sticky' => TRUE, 'colgroups' => array(), 'empty' => ''));
}



814
815
function forena_update_cancel($form, &$form_state) {
  extract($form_state['values']);
metzlerd's avatar
metzlerd committed
816
  $form_state['redirect'] = array("reports/$report_name/edit", array('query' => Frx::parms()));
David Metzler's avatar
Fixes    
David Metzler committed
817
  }
818
819
820
821

function forena_report_save_cancel($form, &$form_state) {
  extract($form_state['values']);
  Frx::Editor($report_name)->cancel();
822
  $form_state['redirect'] = array("reports/$report_name", array('query' => Frx::parms()));
823
824
}

David Metzler's avatar
David Metzler committed
825
function forena_select_block_list($report_name, $action='add-data', $id='') {
David Metzler's avatar
Fixes    
David Metzler committed
826
827
828
829
830
831
  $header = array(t('Block'), t('Access'), t('Keywords'));
  $blocks = Frx::DataFile()->userBlocks();
  $data=array();

  foreach ($blocks as $block_name => $block) {
    $blck = str_replace('/', '.', $block_name);
David Metzler's avatar
David Metzler committed
832
    $path = "reports/$report_name/edit/preview-data/$action/$blck";
David Metzler's avatar
Fixes    
David Metzler committed
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
    // Build the table.
    if ($id) $path .= "/$id";
    $data[] = array(
      l($block_name, $path),
      @$block->cache['access'],
      @$block->cache['keywords']
    );
  }

  // Theme the table
  $content['data_blocks'] = array('#markup' => theme('forena_data_table', array('header' => $header, 'rows' => $data)));
  return $content;
}

/**
 * Preview the block
 * @param unknown $report_name
 * @param unknown $block_name
 * @param string $id
 * @return multitype:string unknown multitype:string unknown  multitype:string  multitype:string Ambigous <The, string, A, Optional>
 */
function forena_report_block_select_form($form, &$form_state, $report_name, $action, $block_name, $id='') {
  $form['action'] = array(
      '#type' => 'value',
      '#value' => $action,
  );

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

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

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

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Select'),
  );
  return $form;
}

function forena_report_block_select_form_submit($form, &$form_state) {
  //Instantiate form variables
  extract($form_state['values']);
  $parms = $_GET;
  unset($parms['q']);
  $path = "reports/$report_name/edit/$action/$block_name";
  if ($id) $path = "$path/$id";
  $form_state['redirect'] = array($path, array('query' => $parms));
}
/**
 * Page to allow selection of a data block with parameters.
 * @param unknown $report_name
 * @param unknown $block_name
 * @param string $id
 * @return Ambigous <unknown, multitype:multitype:NULL  Ambigous <string, multitype:multitype:multitype:string   multitype:multitype:string unknown   > Ambigous <The, boolean, string> >
 */
function forena_report_block_preview($report_name, $action, $block_name, $id='') {
  $parms = $_GET;
  unset($parms['q']);
  $preview = forena_data_block_preview($block_name, $parms);
  if (@trim($preview['content']['#markup'])) {
    $content['select'] = drupal_get_form('forena_report_block_select_form', $report_name, $action, $block_name, $id);
  }
  else {
    drupal_set_message(t('Sample data is required in order to correctly generate a template.  Try changing parameters'), 'warning');
  }
  $content['preview'] = $preview;
  return $content;
}

912

913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
function forena_report_delete_confirm($form, $form_state, $report_name, $id) {
  $parms = $_GET;
  unset($parms['q']);

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

  $form['id'] = array(
    '#type' => 'value',
    '#value' => $id,
  );
  $path = array('path' => "reports/$report_name/edit", 'options' => array('query' => $parms));
  $form = confirm_form(
    $form,
    t('Are you sure you would like to remove the %s content', array('%s' => $id)),
    $path,
    'Detail blocks contain inside this block will also be deleted.',
    'Yes', 'No'
  );
  return $form;
}

function forena_report_delete_confirm_submit($form, &$form_state) {
  extract($form_state['values']);

  // Delete the block
  $r = Frx::Editor($report_name);
  $r->deleteNode($id);
  $r->update();


  $parms = $_GET;
  unset($parms['q']);
  $form_state['redirect'] = array("reports/$report_name/edit", array('query' => $parms));
}

951
952
953
954
955
956
957
958
/**
 * Form field translation editor.
 * @param unknown $form
 * @param unknown $form_state
 * @param unknown $report_name
 * @param unknown $id
 * @return multitype:string multitype:string  Ambigous <The, string, A, Optional>
 */
959
960
function forena_report_field_form($form, &$form_state, $report_name, $id) {
  $r = Frx::Editor($report_name);
metzlerd's avatar
metzlerd committed
961
  $id = urldecode($id);
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
  $form['report_name'] = array(
  	'#type' => 'value',
    '#value' => $report_name,
  );
  $field = $r->frxReport->getField($id);
  $form['field'] = array('#tree' => TRUE);

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

  $form['field']['id_label'] = array(
  	'#type' => 'item',
    '#markup' => $id,
    '#title' => t('token'),
  );

  $form['field']['format'] = array(
      '#type' => 'textfield',
      '#title' => t('format'),
      '#default_value' => $field['format'],
      '#size' => 30,
      '#autocomplete_path' => 'forena/fields/format/autocomplete',
      '#description' => t('Format a date and time field by entering the name of a supported format function. Enter a "*" to see all available formats.')
  );

  $form['field']['format-string'] = array(
      '#type' => 'textfield',
      '#title' => t('format-string'),
      '#default_value' => $field['format-string'],
      '#size' => 30,
      '#description' => t('The display type of your format.')
  );

  $form['field']['link'] = array(
      '#type' => 'textfield',
      '#title' => t('link'),
      '#default_value' => $field['link'],
      '#size' => 100,
      '#maxlength' => 256,
      '#description' => t('Create a link that incorporates this field, e.g "profile/{field_name}" will create a link to this field_name\'s profile. *Note the field must be wrapped in {}.')
  );

  $form['field']['add-query'] = array(
      '#type' => 'checkbox',
      '#title' => t('Pass through url parameters'),
      '#default_value' => $field['add-query'],
      '#description' => t('Automatically pass through url parameters on this link'),
  );

  $form['field']['rel'] = array(
      '#type' => 'textfield',
      '#title' => t('rel') ,
      '#description' => t('Relationship attribute to apply to the link.'),
      '#size' => 100,
      '#maxlength' => 256,
      '#default_value' => $field['rel'],
  );

  $form['field']['class'] = array(
      '#type' => 'textfield',
      '#title' => t('class') ,
      '#description' => t('Class applied to be applied to the link.'),
      '#size' => 100,
      '#maxlength' => 256,
      '#default_value' => $field['class'],
  );

  $form['field']['target'] = array(
      '#type' => 'textfield',
      '#title' => t('target'),
      '#default_value' => $field['target'],
      '#size' => 30,
      '#description' => t('Link target eg. _BLANK,  Targets that begin with "popup" will be opened in a new window using javascript.')
  );

  $form['field']['default'] = array(
      '#type' => 'textfield',
      '#title' => t('default value'),
      '#default_value' => $field['default'],
      '#size' => 30,
      '#description' => t('The value to be displayed in the report when no value exists.')
  );

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

  $form['cancel'] = array(
    '#type' => 'submit',
    '#value' => t('Cancel'),
1055
    '#submit' => array('forena_update_cancel'),
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
  );

  return $form;
}

function forena_report_field_form_submit($form, &$form_state) {
  $values = $form_state['values'];
  $name = $values['report_name'];

  $r = Frx::Editor($name);
  /*now build the fields*/
  $fields = $values['field'];
1068

1069
1070
1071
1072
1073
1074
1075
  $r->setFields(array($fields));
  $r->update();
  $parms = Frx::parms();
  $form_state['redirect'] = array("reports/$name/edit", array('query' => $parms));

}

metzlerd's avatar
metzlerd committed
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087

/**
 * Implementation of hook_form
 * Edits a single parameter config
 * @param unknown $formid
 * @param unknown $form_state
 * @param $report_name string name of report to edit
 * @param $id string name of parameter to edit
 * @return multitype:multitype:boolean string  multitype:string  multitype:string multitype:string   multitype:string Ambigous <An, string>  multitype:string unknown
 */
function forena_report_parameter_config_form($formid, $form_state, $report_name, $id) {

1088
  $r = Frx::Editor($report_name);
metzlerd's avatar
metzlerd committed
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134


  // Set up basic control types.
  $control_types= array('textfield' => 'Text',
      'hidden' => 'Hidden',
      'radios' => 'Radios',
      'checkbox' => 'Checkbox',
      'checkboxes' => 'Checkboxes',
      'select' => 'Select',
      'selectajax' => 'Select with Refresh',
      'multiselect' => 'Multi-Select',
      'multiselectajax' => 'Multi-select With Refresh',
  );

  // Supported module specific control types.
  if (module_exists('date_api')) {
    $control_types['date_select'] =  'Date Select';
  }
  if (module_exists('date_popup')) {
    $control_types['date_popup'] = 'Date Popup';
  }

  drupal_set_title($r->title);
  $form = array();

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

  $parameters = $r->frxReport->parametersArray();

  if (!isset($parameters[$id])) {
    drupal_not_found();
    drupal_exit();
  }
  $parm = $parameters[$id];

  //make a subtree for each param
  $form['parm'] = array('#tree' => TRUE);

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

metzlerd's avatar
metzlerd committed
1135
1136
1137
1138
1139
1140
1141
  $form['parm']['id_label'] = array(
  	'#type' => 'item',
    '#title' => t('Name'),
    '#markup' => $id,
    '#description' => t('The machine friendly name of the parameter as used in the query or report.  ')
  );

metzlerd's avatar
metzlerd committed
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
  $form['parm']['label'] = array(
    '#type' => 'textfield',
    '#title' => t('label'),
    '#default_value' => @$parm['label'],
    '#required' => FALSE,
    '#description' => t('A descriptive name of the parameter to be displayed on a form.')
  );

  $form['parm']['require'] = array(
      '#type' => 'radios',
      '#title' => t('require'),
      '#default_value' => @$parm['require'] ? $parm['require'] : "0",
      '#options' => array("1" => t('Yes'), "0" => t('No')),
      '#description' => t('Requires a value for this parameter to display the report. If there is not a default value, the user will be prompted to enter a value.')
  );

  $form['parm']['default'] = array(
    '#type' => 'textfield',
    '#title' => 'default value',
    '#default_value' => @$parm['default'],
    '#description' => t('The value of the parameter for the initial view of the report.')
  );

  $form['parm']['desc'] = array(
      '#type' => 'textfield',
      '#title' => t('description'),
      '#default_value' => @$parm['desc'],
      '#description' => t('Enter a helpful description about this parameter. This will display on the form when the user is prompted to enter a parameter.'),
  );

  $form['parm']['type'] = array(
      '#type' => 'select',
      '#title' => t('Input Control Type'),
      '#default_value' => @$parm['type'],
      '#options' => $control_types,
      '#description' => t('Enter a helpful description about this parameter. This will display on the form when the user is prompted to enter a parameter.'),
  );

  $form['parm']['data_source'] = array(
      '#type' => 'textfield',
1182
      '#title' => t('Source Data'),
metzlerd's avatar
metzlerd committed
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
      '#default_value' => @$parm['data_source'],
      '#autocomplete_path' => 'forena/data_block/autocomplete',
      '#description' => t('Data block to be used for select, multiselect, and checkboxes types'),
  );

  $form['parm']['data_field'] = array(
      '#type' => 'textfield',
      '#title' => t('Data Field'),
      '#default_value' => @$parm['data_field'],
      '#description' => t('Column in data block to be used data of select boxes and radio buttons. '),
  );

  $form['parm']['label_field'] = array(
      '#type' => 'textfield',
      '#title' => t('Label Field'),
      '#default_value' => @$parm['label_field'],
      '#description' => t('Column in data block to be used for the label for selet boxes and radio buttons.')
  );

  $form['parm']['options'] = array(
      '#type' => 'textfield',
      '#title' => t('Options'),
      '#default_value' => @$parm['options'],
      '#description' => t('Used to configure the control type above options depend on the control type above.')
  );

  $form['parm']['class'] = array(
      '#type' => 'textfield',
      '#title' => t('Class'),
      '#description' => t('Specify the html class you wish to apply to this parameter'),
      '#default_value' => @$parm['class'],
  );

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

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

}

function forena_report_parameter_config_form_submit($formid, &$form_state) {
  $r = Frx::Editor($form_state['values']['report_name']);
1232
  $parameters = $r->frxReport->parametersArray();
metzlerd's avatar
metzlerd committed
1233
  $id = $form_state['values']['parm']['id'];
metzlerd's avatar
metzlerd committed
1234
1235
  $parameters[$id] = array_merge($parameters[$id], $form_state['values']['parm']);
  $r->setParameters($parameters);
metzlerd's avatar
metzlerd committed
1236
1237
  $r->update();
  $r_link = $r->report_link;
metzlerd's avatar
metzlerd committed
1238
  $form_state['redirect'] = array("$r_link/edit/parameters", array('query' => Frx::parms()));
metzlerd's avatar
metzlerd committed
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
}


function forena_report_parameters_form($form, &$form_state, $report_name) {
  $r = Frx::Editor($report_name);
  $r_link = $r->report_link;
  if (!isset($form_state['storage']['parameters']))  {
    $form_state['storage']['parameters'] = $r->frxReport->parametersArray();

  }
  $parameters = $form_state['storage']['parameters'];
1250
  $links = array();
metzlerd's avatar
metzlerd committed
1251
  $links[] = array('href' => $r->report_link . '/edit/add-parameter', 'title' => 'Add Parameter' );
1252
1253
1254
1255
1256
1257
1258
1259
1260
  $form['add_link'] = array(
      '#type' => 'markup',
      '#markup' => theme('links',
          array(
              'links' => $links,
              'attributes' => array('class' => 'action-links'),
          )
      )
  );
metzlerd's avatar
metzlerd committed
1261
1262
  $i=0;
  $form['parameters'] = array('#tree' => TRUE);
1263
  foreach ($parameters as $parameter) {
metzlerd's avatar
metzlerd committed
1264
    $id = urlencode($parameter['id']);
1265
    $ctl = array();
metzlerd's avatar
metzlerd committed
1266
1267
1268
1269
1270
    $ctl['id_label'] = array(
        '#type' => 'item',
        '#title' => 'id',
        '#title_display' => 'invisible',
        '#markup' => check_plain($parameter['id']) . $r->l_icon("$r_link/edit/edit-parameter/$id", 'configure.png', 'Configure Parameter'),
1271
        );
metzlerd's avatar
metzlerd committed
1272

1273
1274
1275
    $ctl['label'] = array(
    	'#type' => 'textfield',
      '#size' => 30,
metzlerd's avatar
metzlerd committed
1276
1277
1278
      '#default_value' => @$parameter['label'],
      '#title' => t('Label or Prompt'),
      '#title_display' => 'invisible',
1279
1280
1281
1282
1283
    );

    $ctl['default'] = array(
    	'#type' => 'textfield',
      '#size' => 30,
metzlerd's avatar
metzlerd committed
1284
1285
1286
      '#title' => t('Default Value'),
      '#default_value' => @$parameter['default'],
      '#title_display' => 'invisible',
1287
1288
    );

metzlerd's avatar
metzlerd committed
1289
1290
1291
1292
1293
    $ctl['actions'] = array(
    	'#type' => 'actions',
      '#title' => t('Actions'),
      '#title_display' => 'invisible',
    );
1294

metzlerd's avatar
metzlerd committed
1295
1296
1297
1298
1299
    $ctl['actions']['remove'] = array(
    	'#type' => 'submit',
      '#value' => t('Remove'),
      '#submit' => array('forena_report_parameters_remove_submit'),
      '#name' => 'remove-' . $parameter['id'],
1300
1301
    );

metzlerd's avatar
metzlerd committed
1302
1303
1304
1305
1306
1307
1308
1309
    $ctl['weight'] = array(
    	'#type' => 'weight',
      '#title' => 'Weight',
      '#default_value' => $i,
      '#title_display' => 'invisible',
    );
    $i++;
    $form['parameters'][$parameter['id']] = $ctl;
1310
1311
  }

metzlerd's avatar
metzlerd committed
1312
1313
1314
1315
1316
  $form['report_name'] = array(
  	'#type' => 'value',
    '#value' => $report_name,
  );

1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
  $form['update'] = array(
  	'#type' => 'submit',
    '#value' => t('Update'),
  );

  $form['cancel'] = array(
  	'#type' => 'submit',
    '#value' => t('Cancel'),
    '#submit' => array('forena_update_cancel'),
  );

metzlerd's avatar
metzlerd committed
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
  return $form;

}

function forena_report_parameters_form_submit($form, &$form_state) {
  $parms = array();
  $new_parameters = $form_state['values']['parameters'];
  $parm_weights = array();
  foreach ($new_parameters as $id => $parm) {
    $parm_weights[$id] = $parm['weight'];
  }
  asort($parm_weights);
  // Reorder by weight;

  foreach ($parm_weights as $id => $weight) {
    $parms[$id] = array_merge($form_state['storage']['parameters'][$id], $new_parameters[$id]);
  }

  // Now replace parameters
  $r = Frx::Editor($form_state['values']['report_name']);
  $r_link = $r->report_link;
  $r->setParameters($parms);
  $r->update();

  $form_state['redirect'] = array("$r_link/edit", array('query' => Frx::parms()));
}

/**
 * Removes a parameter from the form until update is performed.
 * @param unknown $form
 * @param unknown $form_state
 */
function forena_report_parameters_remove_submit($form, &$form_state) {
  $new_parameters = $form_state['values']['parameters'];
  $form_state['rebuild'] = TRUE;
  $key = $form_state['triggering_element']['#parents'][1];
  unset($form_state['storage']['parameters'][$key]);
  unset($form_state['input']['parameters'][$key]);
}


function forena_report_add_parameter_form($form, &$form_state, $report_name) {

  $r = Frx::Editor($report_name);
  //drupal_set_title($r->title);
  $form = array();

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

  $form['parameters'] = array('#tree' => TRUE);
metzlerd's avatar
metzlerd committed
1381
1382
1383
1384
1385
  $form['parameters']['label'] = array(
  	'#type' => 'textfield',
    '#title' => t("Label"),
    '#description' => t('The Label to display to the user. ')
  );
metzlerd's avatar
metzlerd committed
1386
1387

  $form['parameters']['id'] = array(
metzlerd's avatar
metzlerd committed
1388
      '#type' => 'forena_machine_name',
metzlerd's avatar
metzlerd committed
1389
1390
      '#title' => t('id'),
      '#required' => TRUE,
metzlerd's avatar
metzlerd committed
1391
      '#description' => t('The machine friendly name of the as used in queries or reports.')
metzlerd's avatar
metzlerd committed
1392
1393
  );

metzlerd's avatar
metzlerd committed
1394

metzlerd's avatar
metzlerd committed
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
  $form['add'] = array(
      '#type' => 'submit',
      '#value' => t('Add'),
  );

  $form['cancel'] = array(
      '#type' => 'submit',
      '#value' => t('Cancel'),
      '#submit' => array('forena_update_cancel'),
  );

  return $form;
}

function forena_report_add_parameter_form_submit($form, &$form_state) {
  $values = $form_state['values'];
  $report_name = $values['report_name'];
  $param = $values['parameters'];
  $r = Frx::Editor($report_name);
  $r_link = $r->report_link;
  //add the new parms
  $r->addParameters($param['id']);
  $r->update();
  $form_state['redirect'] = array("$r_link/edit/parameters", array('query' => Frx::parms()));
1419
1420
}

metzlerd's avatar
metzlerd committed
1421
1422
1423
1424
1425
/**
 * Theme function for the draggable parameters form.
 * @param unknown $variables
 * @return Ambigous <string, A, boolean>
 */
1426
function theme_forena_report_parameters_form($variables) {
metzlerd's avatar
metzlerd committed
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
  $form = &$variables['form'];
  $headers = array();
  $rows = array();
  // Iterate the controls of the table giving some labels.
  foreach (element_children($form['parameters']) as $e_key) {
    $form['parameters'][$e_key]['weight']['#attributes']['class'] = array('parameters-weight');
    $row = array();
    foreach (element_children($form['parameters'][$e_key]) as $ctl) {
      if (isset($form['parameters'][$e_key][$ctl]['#title'])) {
        $headers[$ctl] = $form['parameters'][$e_key][$ctl]['#title'];
      }
      $row[] = drupal_render($form['parameters'][$e_key][$ctl]);
    }
    $rows[] = array('data' => $row, 'class' => array('draggable'));
  }
  $headers = array_values($headers);
  $output = theme('table', array('header' => $headers, 'rows' => $rows, 'attributes' => array('id' => 'forena-parameters')));
  // Render the rest of the children.
  $output .= drupal_render_children($form);
  drupal_add_tabledrag('forena-parameters', 'order', 'group', 'parameters-weight');
1447
1448
1449
  return $output;
}

1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
function __forena_element_theme(&$element) {

}

function theme_forena_element_draggable($variables) {
  $output = '';
  $element = $variables['element'];
  $id = $element['#draggable_id'];
  $headers = array();
  $rows = array();
  foreach (element_children($element) as $e_key) {
    $element[$e_key]['weight']['#attributes']['class'] = array($id . '-weight');
    $row = array();
    foreach (element_children($element[$e_key]) as $ctl) {
      if (isset($element[$e_key][$ctl]['#title'])) {
        $headers[$ctl] = $element[$e_key][$ctl]['#title'];
        $element[$e_key][$ctl]['#title_display'] = 'invisible';
1467
1468
        $e = drupal_render($element[$e_key][$ctl]);
        $row[] = $e;
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
      }



    }
    $rows[] = array('data' => $row, 'class' => array('draggable'));
  }
  $output = theme('table', array('header' => $headers, 'rows' => $rows, 'attributes' => array('id' => $id)));
  drupal_add_tabledrag($id, 'order', 'group', $id . '-weight');
  //$output .= drupal_render_children($element);
  return $output;
}

1482

metzlerd's avatar
metzlerd committed
1483
1484
1485
1486
1487
1488
1489
1490
function forena_report_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'];
David Metzler's avatar
David Metzler committed
1491
1492
  $filter = variable_get('forena_input_format', filter_default_format());
  if (isset($frx_options['input_format'])) $filter = $frx_options['input_format'];
1493

metzlerd's avatar
metzlerd committed
1494
1495
1496
1497
1498
1499
1500
1501
  $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',
metzlerd's avatar
metzlerd committed
1502
      '#value' => $report_name,
metzlerd's avatar
metzlerd committed
1503
1504
1505
  );


1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
  // Skin control
  $frx_options = $r->getOptions();
  $report_form = @$frx_options['form'];
  $skins[''] = t('Use Default');
  $skins = array_merge(variable_get('forena_skins', array()), $skins);
  $form['form'] = array(
      '#type' => 'select',
      '#title' => t('Skin'),
      '#options' => $skins,
      '#default_value' => $report_form,
      '#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
1520
1521
1522
1523
1524
  $form['body'] = array(
      '#type' => 'text_format',
      '#title' => t('Body'),
      '#default_value' => $body,
      '#rows' => 25,
David Metzler's avatar
David Metzler committed
1525
      '#format' => $filter,
metzlerd's avatar
metzlerd committed
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
  );

  $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_report_layout_form_submit'),
  );

  $form['buttons']['cancel'] = array(
      '#type' => 'submit',
      '#value' => 'Cancel',
metzlerd's avatar
metzlerd committed
1552
      '#submit' => array('forena_update_cancel'),
metzlerd's avatar
metzlerd committed
1553
1554
1555
1556
1557
1558
1559
1560
  );  return $form;

  return $form;
}

function forena_report_layout_form_validate($form, &$form_state) {
  $values = $form_state['values'];
  $body = $values['body']['value'];
1561

metzlerd's avatar
metzlerd committed
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
  $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_report_layout_form_submit($form, &$form_state) {
  $nodes = array();
  $rebuild_menu = FALSE;
  $values = $form_state['values'];
  $report_name = $values['report_name'];
  $r = Frx::Editor();
  $r_link = $r->report_link;

David Metzler's avatar
David Metzler committed
1588
1589
1590
1591
1592
1593
  $options = array();
  $filter = variable_get('forena_input_format', filter_default_format());
  if (isset($values['body']['format']) && $values['body']['format'] != $filter) {
    $options['input_format'] = $values['body']['format'];
    $r->setOptions($options);
  }
1594

metzlerd's avatar
metzlerd committed
1595
1596
1597
1598
1599
1600
1601
1602
  // Body
  $r->setBody($values['body']['value']);
  // CSS
  $r->setStyle($values['css']);
  $r->update();
  // If we changed the menu we need to rebuild it.
  $form_state['redirect'] = array("$r_link/edit", array('query' => Frx::parms()));
}
1603

1604

metzlerd's avatar
metzlerd committed
1605
1606
1607
1608
1609
/**
 * Remove the report from the database and file system.
 * @param string $report_name
 */
function forena_delete_report($report_name) {
1610

metzlerd's avatar
metzlerd committed
1611
1612
1613
  $filepath = $report_name . '.frx';
  $do = Frx::File()->delete($filepath);
}