forena.admin.inc 26 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
  $output = '';
17
  $content = drupal_get_form('forena_sync_form');
18
  $sync_form = drupal_render($content);
19
  $links[] = array('href' => 'reports/add', 'title' => 'Create New Report' );
20
  $output .= theme('links',
21
22
23
24
    array(
      'links' => $links,
      'attributes' => array('class' => 'action-links'),
    )
metzlerd's avatar
metzlerd committed
25
  );
26
  // Add Data tables if it exists.
27
28
  $headers = array(t('category'), t('title'), t('name'), t('operation'));
  $result = Frx::File()->allReports();
29
  foreach ($result as $row) {
30
    $rpt = str_replace('/', '.', $row->name);
31
32
33
34
35
36
    if ($row->include) {
      $edit = l(t('Override'), 'reports/' . $rpt . '/edit');
    }
    else {
      $edit = l(t('Edit'), 'reports/' . $rpt . '/edit');
    }
metzlerd's avatar
metzlerd committed
37
    $clone = l(t('Clone'), 'reports/add/' . $rpt);
38
39
40
41
42
43
44
45
46
47
48
    // Determine the nature of the report delete link.
    if ($row->override) {
      $delete = l(t('Revert'), 'reports/' . $rpt . '/delete', array('query' => array('destination' => 'admin/structure/forena')));
    }
    else if (!$row->include) {
      $delete = l(t('Delete'), 'reports/' . $rpt . '/delete', array('query' => array('destination' => 'admin/structure/forena')));
    }
    else {
      $delete = '';
    }

49
50
    $title = l(t($row->cache['title']), 'reports/' . $rpt);
    $data[] = array($row->cache['category'], $title, $row->name, $edit . ' ' . $clone . ' ' . $delete);
51
  }
52
  $output .= '<div id="forena-reports-list">';
53
  $output .= theme('forena_data_table', array('header' => $headers, 'rows' => $data));
54
  $output .= '</div>';
55
  $output .= $sync_form;
56
57
58
  return $output;
}

59
/**
metzlerd's avatar
metzlerd committed
60
 * Remove the report from the database and file system.
61
62
 * @param string $report_name
 */
63
function forena_delete_report($report_name) {
64

65
  $filepath = $report_name . '.frx';
66
  $do = Frx::File()->delete($filepath);
67
68
}

metzlerd's avatar
metzlerd committed
69

70
71


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

93
  $element['format'][$name] = array(
metzlerd's avatar
metzlerd committed
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
    '#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
109
  return $element['format'];
metzlerd's avatar
metzlerd committed
110
}
111

metzlerd's avatar
metzlerd committed
112
113


114
/**
115
116
117
 * Report syncronization form
 * @param $formid
 * @param $form_state
118
 */
119
function forena_sync_form($formid, &$form_state) {
120
121
122
123

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

128
129
  $form['sync'] = array(
    '#type' => 'submit',
130
    '#value' => t('Revert'),
131
132
    '#submit' => array('forena_settings_sync_submit'),
  );
133
134

  return $form;
135
136
137
138
139
140
141
142

}

/**
 * Forena admin settings form
 *
 */
function forena_settings() {
143
  $skins = Frx::File()->skins();
144
145
146

  $report_path = forena_report_path();

147

148
149
150
  $form['forena_report_repos'] = array(
    '#type' => 'textfield',
    '#title' => t('Report Repository'),
metzlerd's avatar
metzlerd committed
151
152
153
154
155
156
    '#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,
  );

157
158
  $form['forena_last_report_path'] = array(
    '#type' => 'value',
159
    '#value' => variable_get('forena_report_repos', '')
metzlerd's avatar
metzlerd committed
160
161
  );

162
163
164
165
166
167
168
169
170
171
172
173
174
  if (module_exists('forena_query')) {
      $form['forena_query_data_path'] = array(
        '#type' => 'textfield',
        '#title' => t('Custom Data Block Repository'),
        '#description' => t('Indicate the directory that you want to use to save custom created data blocks using
            the forena query builder tool.  These will override any module delivered data blocks.  This needs to
            be a directory that is writable by the web user but should not be browsable by the web.  It defaults to
            '),
        '#default_value' => Frx::DataFile()->dir,
      );

      $form['forena_last_query_data_path'] = array(
          '#type' => 'value',
175
          '#value' =>  variable_get('forena_query_data_path', '')
176
177
178
      );

  }
179

180
  $form['forena_input_format'] = forena_filter_element(variable_get('forena_input_format', filter_default_format()), 'forena_input_format');
181
  $form['forena_default_form'] = array(
182
    '#type' => 'select',
183
    '#title' => t('Default report skin'),
184
    '#options' => $skins,
185
    '#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
186
    . ' Skins are basically css and javascript libraries added to your report.'),
metzlerd's avatar
metzlerd committed
187
    '#default_value' => variable_get('forena_default_form', ''),
metzlerd's avatar
metzlerd committed
188
    );
metzlerd's avatar
metzlerd committed
189

metzlerd's avatar
metzlerd committed
190
191
    $form =  system_settings_form($form);
    return $form;
metzlerd's avatar
metzlerd committed
192

193
}
194
195

function forena_settings_validate($form, &$form_state) {
metzlerd's avatar
metzlerd committed
196
  $values = $form_state['values'];
197
  $path = $values['forena_report_repos'];
198
  if ($path != $values['forena_last_report_path']) {
199
200
201
    if (!file_exists($path)) {
      try {
        if (file_exists($path)) {
202
          drupal_set_message(t('Created directory %s', array('%s' => $path))) ;
203
204
        }
        mkdir($path);
205

206
207
208
209
      } catch (Exception $e) {
        forena_error(t('Unable to create report directory'), $e->getMessage());
      }
    }
metzlerd's avatar
metzlerd committed
210
  }
211
212
213
214
215
216
217
218
219
220
221
  if (!file_exists($path) || !is_writable($path)) {
    form_set_error('forena_report_repos', 'Report Directory must be writable by the web user');
  }
  if ($values['forena_query_data_path']) {
    $path = $values['forena_query_data_path'];
    if ($path != $values['forena_last_query_data_path']) {
      if (!file_exists($path)) {
        try {

          mkdir($path);
          if (file_exists($path)) {
222
            drupal_set_message(t('Created directory %s', array('%s' => $path))) ;
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
          }

        } catch (Exception $e) {
          forena_error(t('Unable to create data directory'), $e->getMessage());
        }
      }
    }
    if (!file_exists($path) || !is_writable($path)) {
      form_set_error('forena_query_data_path', 'Data Directory must be writable by the web user');
    }
  }


}


/**
 * Added submit handler to create directories and clear menu cache
 *
 * @param unknown_type $form
 * @param unknown_type $form_state
 */
function forena_settings_submit($form, &$form_state) {
246
}
metzlerd's avatar
metzlerd committed
247

248
function forena_settings_sync_submit($form, &$form_state) {
249
  forena_sync_reports($form_state['values']['sync_overwrite']);
250
}
251

metzlerd's avatar
metzlerd committed
252
253
254
255
256
/**
 * Form function for the edit report form
 * @param $form_state
 * @return the form
 */
257
258


259
260
261
262
263
/**
 * Handle delete buttons from edit forms.
 * @return unknown_type
 */
function forena_edit_delete_submit($form, &$form_state) {
metzlerd's avatar
metzlerd committed
264
  $link = 'reports/' . $form_state['values']['name_in'] ;
265
266
267
268
269
270
271
272
273
  $destination = '';
  if (isset($_REQUEST['destination'])) {
    $destination = drupal_get_destination();
    unset($_REQUEST['destination']);
  }
  $form_state['redirect'] = array('path' => $link . '/delete', 'query' => array('destination' => $destination));
}


274
function forena_create_trans_form($formid, $form_state, $report_name) {
275
276
277
  $name = '';
  $filename = '';
  $format = '';
metzlerd's avatar
metzlerd committed
278
  $desc = Frx::Menu()->parseURL($report_name);
279
280
281
282
  $name = $desc['name'];
  $filename = $desc['filename'];
  $base_name = $desc['base_name'];
  $format = @$desc['format'];
283
284
  $form = array();
  global $language;
285
  $languages = language_list();
286
287
  //determine if this is an add new report request

288
  $r = Frx::Editor($name);
289
290
291
  $title = (string)$r->title;
  $lang = @$_GET['language'];
  if ($lang) {
metzlerd's avatar
metzlerd committed
292
    $language = $languages[$lang];
293
294
295
296
297
298
299
300
301
  }
  $form['base_name'] = array(
    '#type' => 'value',
    '#value' => $base_name
  );
  $form['report_name'] = array(
    '#type' => 'value',
    '#value' => $name,
  );
302

metzlerd's avatar
metzlerd committed
303
  foreach ($languages as $key => $obj) {
304
    $languages[$key] = $obj->native;
305
  }
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
  $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
323
    '#required' => TRUE,
324
325
326
327
328
329
330
331
332
  );


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

333
334
335
336
}

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

metzlerd's avatar
metzlerd committed
340
  $desc = Frx::Menu()->parseURL($new_name);
341
342
  $filename = $desc['filename'];
  $report_name = $desc['name'];
343

David Metzler's avatar
David Metzler committed
344
  $r = Frx::Editor($base_name);
345
346
347

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

metzlerd's avatar
metzlerd committed
349
  //determine redirection.
350

351
  if (Frx::File()->exists($filename, FALSE)) {
metzlerd's avatar
metzlerd committed
352
    drupal_set_message(t('Report %s already exists', array('%s' => $new_name)), 'error');
353
    return;
metzlerd's avatar
metzlerd committed
354
  }
David Metzler's avatar
David Metzler committed
355
356
357
358
359
  else {
    $r->rename($new_name);
    drupal_set_message(t('Translation,  %s has been created. Switch languages to translate.', array('%s' => $values['title'])));
    $form_state['redirect']= array( $desc['i_link'] . '/edit/layout');
  }
metzlerd's avatar
metzlerd committed
360

361
  //if this is a new report then redirect to data blocks
362
}
363

364
365


366
367
368
369
/*
 * administer the settings for document format options
 */
function forena_doc_formats_settings() {
370
  // Invoke doc_type hook to see which document types are there.
371
  $supported_doctypes = Frx::documentTypes();
372

373
374
  $form['forena_doc_formats'] = array(
    '#type' => 'checkboxes',
375
    '#title' => t('Allowed Document Formats'),
376
377
378
379
    '#default_value' => variable_get('forena_doc_formats', $supported_doctypes),
    '#description' => t('check your desired document format'),
    '#options' => $supported_doctypes,
  );
380
381
382
383

  $form['forena_doc_defaults'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Default Document Formats'),
384
    '#default_value' => variable_get('forena_doc_defaults', array()),
385
386
387
388
    '#description' => t('check your desired document format'),
    '#options' => $supported_doctypes,
  );

389
390
391
  $form['forena_email_override'] = array(
    '#type' => 'checkbox',
    '#title' => 'Run email merges in test mode' ,
metzlerd's avatar
metzlerd committed
392
    '#default_value' => variable_get('forena_email_override', FALSE),
393
394
    '#description' => t('When this box is checked emails are sent to the currently logged in user.  Useful for testing environments.'),
  );
395
  $form['forena_email_input_format'] = forena_filter_element(variable_get('forena_email_input_format', 'full_text'), 'forena_email_input_format');
396
  $form['forena_email_input_format']['#title'] = t('Email Input Format');
397

398
399
  return system_settings_form($form);
}
400

401
402
403
404
/*
 * administer the settings for document format options
 */
function forena_data_settings() {
405
  $repos = Frx::RepoMan()->repositories;
406
  $r_list = array();
metzlerd's avatar
metzlerd committed
407
408
  $headers = array(t('Name'), t('Description'), t('Path'), t('Operation'));
  foreach ($repos as $name => $r) {
409
    $r_list[] = array(
metzlerd's avatar
metzlerd committed
410
411
412
413
    $name,
    $r['title'],
    $r['path'],
    l(t('configure'), 'admin/config/content/forena/data/configure/' . $name)
414
415
416
417
    );

  }

metzlerd's avatar
metzlerd committed
418
419
  $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' => ''));
420
421
422
423
  return $output;
}

function forena_data_settings_edit($form, &$form_state, $source=-1) {
424
  global $databases;
425
426
427
428
429
430
431
432
433
  $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
434
          'data provider' => 'FrxDrupal',
435
          'database' => 'default',
436
437
          'access callback' => 'user_access',
          'user callback' => 'forena_current_user_id'
metzlerd's avatar
metzlerd committed
438
439
          ),
          );
440
441
    }
    else {
442
443
      Frx::RepoMan()->repository($source);
      $repos = Frx::RepoMan()->repositories;
444
      $r = $repos[$source];
davidmetzler's avatar
davidmetzler committed
445
      // Remove teh object from the data.
446
      unset($r['data']);
447
448
449
      $form_state['storage'] = array(
        'name' => $source,
        'title' => $r['title'],
davidmetzler's avatar
davidmetzler committed
450
        'path' => @$r['path'],
451
        'config' => $r,
452
453
454
455
456
457
      );

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

davidmetzler's avatar
davidmetzler committed
459
  $locked = !($adding || (@$config['source'] == 'user'));
460
461
462
463
464
465
466
467
468
  $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
469
  );
470
471
472
473

  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
davidmetzler's avatar
davidmetzler committed
474
    '#required' => TRUE,
475
476
477
478
479
480
481
482
483
484
485
    '#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
486
487
488
489
490
491
492
  $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'],
  );

493
494
495
  $form['path'] = array(
    '#type' => 'textfield',
    '#title' => t('Path'),
davidmetzler's avatar
davidmetzler committed
496
    '#required' => TRUE,
497
    '#disabled' => $locked,
498
499
500
501
    '#description' => t('Directory containing data block files.'),
    '#default_value' => @$data['path'],
  );

davidmetzler's avatar
davidmetzler committed
502
503
504
505
506
507
508
509
510
  $user_options = array(
    '' => 'None',
    'forena_current_user_id' => 'UID',
    'forena_current_user_name' => 'User name',
  );

  $form['user_callback'] = array(
    '#type' => 'select',
    '#title' =>  'Current user',
511
    '#disabled' => $locked,
davidmetzler's avatar
davidmetzler committed
512
513
    '#description' => t('Can be refererenced as :current_user in each data block.'),
    '#options' => $user_options,
514
    '#default_value' => @$config['user callback'],
davidmetzler's avatar
davidmetzler committed
515
516
517
518
    '#disabled' => $locked,
  );


519
520
  // Access method list
  $access = array(
davidmetzler's avatar
davidmetzler committed
521
    'callback' => t('Use drupal permissions'),
metzlerd's avatar
metzlerd committed
522
    'block' => t('Match values provided by a data block.'),
523
524
525
526
527
  );

  $form['access_method'] = array(
    '#type' => 'select',
    '#options' => $access,
528
    '#disabled' => $locked,
davidmetzler's avatar
davidmetzler committed
529
530
    '#title' => t('Data security method'),
    '#default_value' => empty($config['access block']) ? 'callback' : 'block',
metzlerd's avatar
metzlerd committed
531
    '#description' => t('Specify how the ACCESS defined for a data block is to be interpreted.'),
532
533
534
    '#ajax' => array(
      'callback' => 'forena_access_info_callback',
      'wrapper' => 'access-details',
metzlerd's avatar
metzlerd committed
535
  ),
536
537
538
539
540
541
  );

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

  switch (!empty($values['access_method']) ? $values['access_method'] : $form['access_method']['#default_value']) {
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
    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'],
      );
563
564
565
566
567
568
569
570
  }



  // Driver list
  $drivers = array(
    'FrxDrupal' => t('Drupal'),
    'FrxOracle' => t('Oracle Database'),
metzlerd's avatar
metzlerd committed
571
    'FrxPDO' => t('PDO other than Drupal'),
572
573
574
575
576
    'FrxPostgres' => t('Postgres Database'),
    'FrxMSSQL' => t('MSSQL Database'),
    'FrxFiles' => t('XML Files'),
  );

davidmetzler's avatar
davidmetzler committed
577
  $form['data_provider'] = array(
578
579
580
581
582
    '#type' => 'select',
    '#title' => t('Driver'),
    '#description' => t('Forena data connection type'),
    '#options' => $drivers,
    '#disabled' => $locked,
davidmetzler's avatar
davidmetzler committed
583
    '#default_value' => $config['data provider'],
584
585
586
    '#ajax' => array(
      'callback' => 'forena_connection_info_callback',
      'wrapper' => 'conn-div',
metzlerd's avatar
metzlerd committed
587
  ),
588
589
590
591
  );

  $form['connection'] = array(
    '#type' => 'fieldset',
davidmetzler's avatar
davidmetzler committed
592
    '#tree' => TRUE,
593
594
595
596
    '#title' => 'Connection info',
   '#prefix' =>  '<div id="conn-div">',
   '#suffix' => '</div>',
  );
davidmetzler's avatar
davidmetzler committed
597
  $data_provider = (!empty($form_state['values']['data_provider']) ? $form_state['values']['data_provider'] : $config['data provider']);
598

davidmetzler's avatar
davidmetzler committed
599
600
601
602
603
  // Common controls used in mulitple providers.
  $uri = array(
    '#type' => 'textfield',
    '#title' => t('uri'),
    '#descripton' => t('Connection string: see appropriate php documentation for more details.'),
604
    '#default_value' => @$config['uri'],
davidmetzler's avatar
davidmetzler committed
605
606
607
608
609
610
611
612
613
614
615
616
617
    '#required' => TRUE,
    '#disabled' => $locked,
  );

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

davidmetzler's avatar
davidmetzler committed
620
  switch ($data_provider) {
davidmetzler's avatar
davidmetzler committed
621
    case 'FrxDrupal':
622
623
624
625
      $db_list = array_combine(array_keys($databases), array_keys($databases));
      $form['connection']['database'] = array(
        '#type' => 'select',
        '#title' => t('Database'),
626
        '#disabled' => $locked,
627
628
        '#default_value' => @$config['database'],
        '#options' => $db_list,
davidmetzler's avatar
davidmetzler committed
629
630
631
632
        '#markup' => 'Determined by Drupal settings.php file',
      );
      break;
    case 'FrxMSSQL':
633
634
635
      $form['connection']['uri'] = $uri;
      $form['connection']['user'] = $user;
      $form['connection']['new_password'] = $password;
636
637
638
639
640
641
642
      $form['connection']['database'] = array(
        '#type' => 'textfield',
        '#disabled' => $locked,
        '#title' => t('Database'),
        '#default_value' => $config['database'],
        '#required' => TRUE,
      );
davidmetzler's avatar
davidmetzler committed
643
644
645
646
647
648
649
      $form['connection']['mssql_xml'] = array(
        '#type' => 'checkbox',
        '#disabled' => $locked,
        '#title' => t('Microsoft SQL native XML'),
        '#description' => t('Use for XML auto queries to generate XML.'),
        '#default_value' => $config['mssql_xml'],
      );
metzlerd's avatar
metzlerd committed
650
      break;
davidmetzler's avatar
davidmetzler committed
651
    case 'FrxOracle':
652
      $form['connection']['uri'] = $uri;
davidmetzler's avatar
davidmetzler committed
653
654
      $form['connection']['user'] = $user;
      $form['connection']['new_password'] = $password;
655
656
657
658
659
660
661
662
      $form['connection']['character_set'] = array(
        '#type' => 'textfield',
        '#title' => t('Character Set'),
        '#disabled' => $locked,
        '#description' => t('Leave blank for default character set'),
        '#default_value' => @$config['character_set'],

      );
davidmetzler's avatar
davidmetzler committed
663
664
665
666
667
668
669
670
671
672
      $form['connection']['oracle_xml'] = array(
        '#type' => 'checkbox',
        '#title' => t('Oracle native XML'),
        '#disabled' => $locked,
        '#description' => t('Use the function provided with Forena to generate XML.  Requires installing a function into the database'),
        '#default_value' => $config['oracle_xml'],
      );
      break;
    case 'FrxPDO':
      $form['connection']['uri'] = $uri;
metzlerd's avatar
metzlerd committed
673
674
675
      $form['connection']['user'] = $user;
      $form['connection']['new_password'] = $password;
      break;
davidmetzler's avatar
davidmetzler committed
676
    case 'FrxPostgres':
677
678
      $form['connection']['uri'] = $uri;
      $form['connection']['new_password'] = $password;
davidmetzler's avatar
davidmetzler committed
679
680
681
682
683
684
685
686
687
688
      $form['connection']['postgres_xml'] = array(
        '#type' => 'checkbox',
        '#title' => t('Postgres native XML'),
        '#disabled' => $locked,
        '#default_value' => @$config['postgres_xml'],
        '#description' => t('Use Postgres native XML support.  Requires Posgres 8.3 or better'),
      );

    default:
      $form['connection']['uri'] = $uri;
689
690
691
692
693
  }

  $form['save'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
694
    '#submit' => array('forena_data_settings_save'),
davidmetzler's avatar
davidmetzler committed
695
696
  );

697

davidmetzler's avatar
davidmetzler committed
698
699
700
701
  $form['delete'] = array(
    "#type" => 'submit',
    '#value' => t('Delete'),
    '#submit' => array('forena_data_settings_delete'),
702
703
704
705
706
  );

  return $form;
}

707
708


davidmetzler's avatar
davidmetzler committed
709
710
711
712
713
/**
 * Submit handler to cause save events to happen.
 * Enter description here ...
 */
function forena_data_settings_save($form, &$form_state) {
714
715
  $values = $form_state['values'];
  $name = $values['name'];
davidmetzler's avatar
davidmetzler committed
716
  $config = $form_state['storage']['config'];
davidmetzler's avatar
davidmetzler committed
717
718
719
  $config['path'] = @$values['path'];
  $config['data provider'] = $values['data_provider'];
  $config['user callback'] = $values['user_callback'];
davidmetzler's avatar
davidmetzler committed
720
721
  $config['debug'] = $values['debug'];
  if (@$values['connection']['new_password']) {
722
    $values['connection']['password'] = $values['connection']['new_password'];
davidmetzler's avatar
davidmetzler committed
723
724
  }
  if (isset($values['connection']['new_password'])) unset($values['connection']['new_password']);
725
  if (is_array(@$values['connection'])) $config = array_merge($config, @$values['connection']);
metzlerd's avatar
metzlerd committed
726
  if ($values['access_method']=='callback') {
727
    $config['access callback'] = empty($values['access_callback']) ? 'user_access' : $values['access_callback'];
metzlerd's avatar
metzlerd committed
728
    if (isset($config['access block'])) unset($config['access block']);
davidmetzler's avatar
davidmetzler committed
729
730
  }
  else {
731
    $config['access block'] = $values['access_block'];
davidmetzler's avatar
davidmetzler committed
732
733
  }
  $config_str = serialize($config);
davidmetzler's avatar
davidmetzler committed
734
  $result = db_query('SELECT * FROM {forena_repositories} WHERE repository = :name', array(':name' => $name));
davidmetzler's avatar
davidmetzler committed
735
736

  if ($result) drupal_set_message(t('Data connection settings saved'));
davidmetzler's avatar
davidmetzler committed
737
738
  if ($repos = $result->fetchObject()) {
    db_update('forena_repositories')
metzlerd's avatar
metzlerd committed
739
    ->fields(array(
davidmetzler's avatar
davidmetzler committed
740
741
        'title' => $values['title'],
        'enabled' => $values['enabled'],
davidmetzler's avatar
davidmetzler committed
742
        'config' => $config_str,
metzlerd's avatar
metzlerd committed
743
744
745
    ))
    ->condition('repository', $name, '=')
    ->execute();
746
747
  }
  else {
748
    $form_state['#redirect'] = 'admin/forena/content/data';
davidmetzler's avatar
davidmetzler committed
749
    db_insert('forena_repositories')
metzlerd's avatar
metzlerd committed
750
    ->fields(array(
davidmetzler's avatar
davidmetzler committed
751
752
753
        'repository' => $name,
        'title' => $values['title'],
        'enabled' => $values['enabled'],
davidmetzler's avatar
davidmetzler committed
754
        'config' => $config_str,
metzlerd's avatar
metzlerd committed
755
756
    ))
    ->execute();
757
  }
davidmetzler's avatar
davidmetzler committed
758
759
760
761
762
763
764
765
766
767
  $form_state['redirect'] = 'admin/config/content/forena/data';
}

/**
 * Delete the function
 * Enter description here ...
 * @param unknown_type $form
 * @param unknown_type $form_state
 */
function forena_data_settings_delete($form, &$form_state) {
768
769
770
  if (!@$form_state['storage]']['locked']) {
    $form_state['redirect'] = 'admin/config/content/forena/data';
    db_delete('forena_repositories')
metzlerd's avatar
metzlerd committed
771
772
    ->condition('repository', $form_state['values']['name'], '=')
    ->execute();
773
  }
davidmetzler's avatar
davidmetzler committed
774

775
776
777
778
779
780
781
782
783
784
785
786
}

/**
 * Generates the controls for external databases.
 * Enter description here ...
 */
function forena_connection_info_callback($form, $form_state) {
  return $form['connection'];

}

function forena_access_info_callback($form, $form_state) {
787
  return $form['access_details'];
788
789
}

790

791

792
/**
metzlerd's avatar
metzlerd committed
793
 * Clean xhtml
794
795
796
797
 *
 * @param unknown_type $xhtml
 * @return unknown
 */
metzlerd's avatar
metzlerd committed
798
799
800
801
function forena_clean_xhtml($xhtml) {
  $ret = $xhtml;
  // If tidy is installed lets clean the html using that.
  if (is_callable('tidy_repair_string')) {
802
    $config = array(
metzlerd's avatar
metzlerd committed
803
      'doctype'            => 'omit',
metzlerd's avatar
metzlerd committed
804
      'indent-spaces'             => 2,
metzlerd's avatar
metzlerd committed
805
806
807
      'indent' => 'auto',
      'input-xml'          => TRUE,
      'output-xml'         => TRUE,
metzlerd's avatar
metzlerd committed
808
      'indent-attributes'  => FALSE,
809
      'indent-spaces'      => 2,
metzlerd's avatar
metzlerd committed
810
      'add-xml-space'      => TRUE,
811
812
      'wrap'               => 135 );
    $ret = tidy_repair_string($xhtml, $config, 'utf8');
metzlerd's avatar
metzlerd committed
813
814
  }
  else {
815
    $ret = str_replace('&nbsp;', '&#160;', $ret);
816
  }
metzlerd's avatar
metzlerd committed
817
  return $ret;
818
}
819

metzlerd's avatar
metzlerd committed
820
/**
821
822
823
824
 *  Prettifies an XML string into a human-readable and indented work of art
 *  @param string $xml The XML as a string
 *  @param boolean $html_output True if the output should be escaped (for use in HTML)
 */
metzlerd's avatar
metzlerd committed
825
function forena_xmlpp($xml, $html_output=FALSE) {
metzlerd's avatar
metzlerd committed
826
827
828
829
  $xml_obj = new SimpleXMLElement($xml);
  $level = 4;
  $indent = 0; // current indentation level
  $pretty = array();
metzlerd's avatar
metzlerd committed
830

metzlerd's avatar
metzlerd committed
831
832
  // get an array containing each XML element
  $xml = explode("\n", preg_replace('/>\s*</', ">\n<", $xml_obj->asXML()));
833

metzlerd's avatar
metzlerd committed
834
835
836
837
  // shift off opening XML tag if present
  if (count($xml) && preg_match('/^<\?\s*xml/', $xml[0])) {
    $pretty[] = array_shift($xml);
  }
metzlerd's avatar
metzlerd committed
838

metzlerd's avatar
metzlerd committed
839
840
841
842
843
844
845
846
847
  foreach ($xml as $el) {
    if (preg_match('/^<([\w])+[^>\/]*>$/U', $el)) {
      // opening tag, increase indent
      $pretty[] = str_repeat(' ', $indent) . $el;
      $indent += $level;
    }
    else {
      if (preg_match('/^<\/.+>$/', $el)) {
        $indent -= $level;  // closing tag, decrease indent
metzlerd's avatar
metzlerd committed
848
      }
metzlerd's avatar
metzlerd committed
849
850
      if ($indent < 0) {
        $indent += $level;
851
      }
metzlerd's avatar
metzlerd committed
852
      $pretty[] = str_repeat(' ', $indent) . $el;
metzlerd's avatar
metzlerd committed
853
    }
metzlerd's avatar
metzlerd committed
854
855
856
  }
  $xml = implode("\n", $pretty);
  return ($html_output) ? htmlentities($xml) : $xml;
857
}
metzlerd's avatar
metzlerd committed
858
859
860
861

/**
 * Auto complete function for categories
 * Checks access for security as well.
metzlerd's avatar
metzlerd committed
862
 *
metzlerd's avatar
metzlerd committed
863
864
865
866
 * @param $string = string to be matched against categories
 * @return An array containing all matching categories
 */
function forena_get_categories($string='') {
867
868
  $data = Frx::File()->reportsByCategory();
  $categories = array_keys($data);
metzlerd's avatar
metzlerd committed
869
  return $categories;
metzlerd's avatar
metzlerd committed
870
}
metzlerd's avatar
metzlerd committed
871

872
function forena_template_ajax(&$form_element) {
metzlerd's avatar
metzlerd committed
873
  $form_element['#ajax'] = array(
metzlerd's avatar
metzlerd committed
874
875
        'callback' => 'forena_template_info_callback',
        'wrapper' => 'template-wrapper',
metzlerd's avatar
metzlerd committed
876
  );
metzlerd's avatar
metzlerd committed
877
878
879
880
881
882
}

function forena_query_string($var) {
  return drupal_http_build_query($var);
}

metzlerd's avatar
metzlerd committed
883