forena.admin.inc 26.5 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

60

metzlerd's avatar
metzlerd committed
61

62
63


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

85
  $element['format'][$name] = array(
metzlerd's avatar
metzlerd committed
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
    '#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
101
  return $element['format'];
metzlerd's avatar
metzlerd committed
102
}
103

metzlerd's avatar
metzlerd committed
104
105


106
/**
107
108
109
 * Report syncronization form
 * @param $formid
 * @param $form_state
110
 */
111
function forena_sync_form($formid, &$form_state) {
112
113
114
115

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

120
121
  $form['sync'] = array(
    '#type' => 'submit',
122
    '#value' => t('Revert'),
123
124
    '#submit' => array('forena_settings_sync_submit'),
  );
125
126

  return $form;
127
128
129
130
131
132
133
134

}

/**
 * Forena admin settings form
 *
 */
function forena_settings() {
135
  $skins = Frx::File()->skins();
136
137
138

  $report_path = forena_report_path();

139

140
141
142
  $form['forena_report_repos'] = array(
    '#type' => 'textfield',
    '#title' => t('Report Repository'),
143
144
    '#description' => t('Indicate the directory that you want to use for storing your report sources (frx files). In order for you ' .
                        'to be able to save report sources, this directory should be writable by the web user. Relative ' .
metzlerd's avatar
metzlerd committed
145
146
147
148
                        'paths should be entered relative to the base path of your drupal installation.'),
    '#default_value' => $report_path,
  );

149
150
  $form['forena_last_report_path'] = array(
    '#type' => 'value',
151
    '#value' => variable_get('forena_report_repos', '')
metzlerd's avatar
metzlerd committed
152
153
  );

154
155
156
157
  if (module_exists('forena_query')) {
      $form['forena_query_data_path'] = array(
        '#type' => 'textfield',
        '#title' => t('Custom Data Block Repository'),
158
159
160
        '#description' => t('Indicate the directory that you want to use to save custom created data blocks (SQLs or
            XMLs) 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.'),
161
162
163
164
165
        '#default_value' => Frx::DataFile()->dir,
      );

      $form['forena_last_query_data_path'] = array(
          '#type' => 'value',
166
          '#value' =>  variable_get('forena_query_data_path', '')
167
168
169
      );

  }
170

171
  $form['forena_input_format'] = forena_filter_element(variable_get('forena_input_format', filter_default_format()), 'forena_input_format');
172
  $form['forena_default_form'] = array(
173
    '#type' => 'select',
174
    '#title' => t('Default report skin'),
175
    '#options' => $skins,
176
    '#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
177
    . ' Skins are basically css and javascript libraries added to your report.'),
metzlerd's avatar
metzlerd committed
178
    '#default_value' => variable_get('forena_default_form', ''),
metzlerd's avatar
metzlerd committed
179
    );
metzlerd's avatar
metzlerd committed
180

metzlerd's avatar
metzlerd committed
181
182
    $form =  system_settings_form($form);
    return $form;
metzlerd's avatar
metzlerd committed
183

184
}
185
186

function forena_settings_validate($form, &$form_state) {
metzlerd's avatar
metzlerd committed
187
  $values = $form_state['values'];
188
  $path = $values['forena_report_repos'];
189
  if ($path != $values['forena_last_report_path']) {
190
191
    if (!file_exists($path)) {
      try {
192
        mkdir($path);
193
        if (file_exists($path)) {
194
          drupal_set_message(t('Created Report Directory %s', array('%s' => $path))) ;
195
196
        }
      } catch (Exception $e) {
197
        Frx::error(t('Unable to create Report Directory'), $e->getMessage());
198
199
      }
    }
metzlerd's avatar
metzlerd committed
200
  }
201
  if (!file_exists($path)) {
202
    form_set_error('forena_report_repos', t('Report Directory (') . $path . t(') does not exist'));
203
204
205
  }
  else {
    if (!is_writable($path)) {
206
      form_set_error('forena_report_repos', t('Report Directory (') . $path . t(') must be writable by the web user'));
207
    }
208
  }
metzlerd's avatar
metzlerd committed
209
  if (isset($values['forena_query_data_path'])) {
210
211
212
213
214
215
    $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)) {
216
            drupal_set_message(t('Created Custom Data Block Directory %s', array('%s' => $path))) ;
217
218
          }
        } catch (Exception $e) {
219
          Frx::error(t('Unable to create Custom Data Block Directory'), $e->getMessage());
220
221
222
        }
      }
    }
223
    if (!file_exists($path)) {
224
      form_set_error('forena_query_data_path', t('Custom Data Block Directory (') . $path . t(') does not exist'));
225
226
227
    }
    else {
      if (!is_writable($path)) {
228
	form_set_error('forena_query_data_path', t('Custom Data Block Directory (') . $path . t(') must be writable by the web user'));
229
      }
230
231
232
233
234
235
236
237
238
239
240
241
    }
  }
}


/**
 * 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) {
242
}
metzlerd's avatar
metzlerd committed
243

244
function forena_settings_sync_submit($form, &$form_state) {
245
  forena_sync_reports($form_state['values']['sync_overwrite']);
246
}
247

metzlerd's avatar
metzlerd committed
248
249
250
251
252
/**
 * Form function for the edit report form
 * @param $form_state
 * @return the form
 */
253
254


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


270
function forena_create_trans_form($formid, $form_state, $report_name) {
271
272
273
  $name = '';
  $filename = '';
  $format = '';
metzlerd's avatar
metzlerd committed
274
  $desc = Frx::Menu()->parseURL($report_name);
metzlerd's avatar
metzlerd committed
275
276


277
278
  $form = array();
  global $language;
279
  $languages = language_list();
280
281
  //determine if this is an add new report request

metzlerd's avatar
metzlerd committed
282
283
  $r = Frx::Editor($report_name, FALSE);
  $base_name = $r->desc['base_name'];
284
  $title = (string)$r->title;
metzlerd's avatar
metzlerd committed
285
  $lang = @$_GET['target_language'];
286
  if ($lang) {
metzlerd's avatar
metzlerd committed
287
    $language = $languages[$lang];
288
289
290
291
292
293
294
295
296
  }
  $form['base_name'] = array(
    '#type' => 'value',
    '#value' => $base_name
  );
  $form['report_name'] = array(
    '#type' => 'value',
    '#value' => $name,
  );
297

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


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

328
329
330
}

function forena_create_trans_form_submit($form, &$form_state) {
metzlerd's avatar
metzlerd committed
331
332
  global $language;
  $language = language_default();
333
  $values = $form_state['values'];
334
  $base_name = $values['base_name'];
metzlerd's avatar
metzlerd committed
335
  $new_name = $values['save_report_language'] . '/' . $base_name;
metzlerd's avatar
metzlerd committed
336
337
  $r = Frx::Editor($base_name, TRUE);
  $link = $r->report_link;
338

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

metzlerd's avatar
metzlerd committed
344
  //determine redirection.
345

346
  if (Frx::File()->exists($filename, FALSE)) {
metzlerd's avatar
metzlerd committed
347
    drupal_set_message(t('Report %s already exists', array('%s' => $new_name)), 'error');
348
    return;
metzlerd's avatar
metzlerd committed
349
  }
David Metzler's avatar
David Metzler committed
350
  else {
metzlerd's avatar
metzlerd committed
351
352
353
    // Title and category
    $r->setTitle($values['title']);
    $r->update();
David Metzler's avatar
David Metzler committed
354
355
    $r->rename($new_name);
    drupal_set_message(t('Translation,  %s has been created. Switch languages to translate.', array('%s' => $values['title'])));
metzlerd's avatar
metzlerd committed
356
357
358
    //$r->cancel();
    $r->save();
    $form_state['redirect']= $link . '/edit';
David Metzler's avatar
David Metzler committed
359
  }
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
    '#required' => TRUE,
  );

  $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
617
  );
davidmetzler's avatar
davidmetzler committed
618

davidmetzler's avatar
davidmetzler committed
619
  switch ($data_provider) {
davidmetzler's avatar
davidmetzler committed
620
    case 'FrxDrupal':
621
622
623
624
      $db_list = array_combine(array_keys($databases), array_keys($databases));
      $form['connection']['database'] = array(
        '#type' => 'select',
        '#title' => t('Database'),
625
        '#disabled' => $locked,
626
627
        '#default_value' => @$config['database'],
        '#options' => $db_list,
davidmetzler's avatar
davidmetzler committed
628
629
630
631
        '#markup' => 'Determined by Drupal settings.php file',
      );
      break;
    case 'FrxMSSQL':
632
633
634
      $form['connection']['uri'] = $uri;
      $form['connection']['user'] = $user;
      $form['connection']['new_password'] = $password;
635
636
637
638
639
640
641
      $form['connection']['database'] = array(
        '#type' => 'textfield',
        '#disabled' => $locked,
        '#title' => t('Database'),
        '#default_value' => $config['database'],
        '#required' => TRUE,
      );
davidmetzler's avatar
davidmetzler committed
642
643
644
645
646
647
648
      $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
649
      break;
davidmetzler's avatar
davidmetzler committed
650
    case 'FrxOracle':
651
      $form['connection']['uri'] = $uri;
davidmetzler's avatar
davidmetzler committed
652
653
      $form['connection']['user'] = $user;
      $form['connection']['new_password'] = $password;
654
655
656
657
658
659
660
661
      $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
662
663
664
665
666
      $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'),
667
        '#default_value' => @$config['oracle_xml'],
davidmetzler's avatar
davidmetzler committed
668
669
670
671
      );
      break;
    case 'FrxPDO':
      $form['connection']['uri'] = $uri;
metzlerd's avatar
metzlerd committed
672
673
674
      $form['connection']['user'] = $user;
      $form['connection']['new_password'] = $password;
      break;
davidmetzler's avatar
davidmetzler committed
675
    case 'FrxPostgres':
676
677
      $form['connection']['uri'] = $uri;
      $form['connection']['new_password'] = $password;
davidmetzler's avatar
davidmetzler committed
678
679
680
681
682
683
684
685
686
687
      $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;
688
689
690
691
692
  }

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

696

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

  return $form;
}

706
707


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

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

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

/**
 * 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) {
786
  return $form['access_details'];
787
788
}

789

790

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

metzlerd's avatar
metzlerd committed
819
/**
820
821
822
823
 *  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
824
function forena_xmlpp($xml, $html_output=FALSE) {
metzlerd's avatar
metzlerd committed
825
826
827
828
  $xml_obj = new SimpleXMLElement($xml);
  $level = 4;
  $indent = 0; // current indentation level
  $pretty = array();
metzlerd's avatar
metzlerd committed
829

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

metzlerd's avatar
metzlerd committed
833
834
835
836
  // 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
837

metzlerd's avatar
metzlerd committed
838
839
840
841
842
843
844
845
846
  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
847
      }
metzlerd's avatar
metzlerd committed
848
849
      if ($indent < 0) {
        $indent += $level;
850
      }
metzlerd's avatar
metzlerd committed
851
      $pretty[] = str_repeat(' ', $indent) . $el;
metzlerd's avatar
metzlerd committed
852
    }
metzlerd's avatar
metzlerd committed
853
854
855
  }
  $xml = implode("\n", $pretty);
  return ($html_output) ? htmlentities($xml) : $xml;
856
}
metzlerd's avatar
metzlerd committed
857
858
859
860

/**
 * Auto complete function for categories
 * Checks access for security as well.
metzlerd's avatar
metzlerd committed
861
 *
metzlerd's avatar
metzlerd committed
862
863
864
865
 * @param $string = string to be matched against categories
 * @return An array containing all matching categories
 */
function forena_get_categories($string='') {
866
  $data = Frx::File()->reportsByCategory();
867
868
869
870
871
872
873
874
875
876
877
878
879
  if ($string) {
    $categories = array();
    foreach (array_keys($data) as $cat) {
      if (strpos(strtolower($cat), strtolower(trim($string))) === 0) {
        $categories[] = $cat;
      }
    }

  }
  else {
    $categories = array_keys($data);
  }
  $categories = array_combine($categories, $categories);
metzlerd's avatar
metzlerd committed
880
  return $categories;
metzlerd's avatar
metzlerd committed
881
}
metzlerd's avatar
metzlerd committed
882

883
function forena_template_ajax(&$form_element) {
metzlerd's avatar
metzlerd committed
884
  $form_element['#ajax'] = array(
metzlerd's avatar
metzlerd committed
885
886
        'callback' => 'forena_template_info_callback',
        'wrapper' => 'template-wrapper',
metzlerd's avatar
metzlerd committed
887
  );
metzlerd's avatar
metzlerd committed
888
889
890
891
892
893
}

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

metzlerd's avatar
metzlerd committed
894