Skip to content
forena.admin.inc 75.2 KiB
Newer Older
David Metzler's avatar
David Metzler committed
// $Id$
David Metzler's avatar
David Metzler committed
/**
 * @file
David Metzler's avatar
David Metzler committed
 * Report administration forms and functions.
David Metzler's avatar
David Metzler committed
 */
David Metzler's avatar
David Metzler committed
require_once('forena.common.inc');
/**
 * Display reports to edit for admins in the structure menu
 * Enter description here ...
 */
function forena_admin_reports() {
  $data = array();
  $links[] = array('href' => 'reports/add', 'title' => 'Create New Report' );
  $content = drupal_get_form('forena_sync_form');
  $output = drupal_render($content);
  $output .= theme('links',
    array(
      'links' => $links,
      'attributes' => array('class' => 'action-links'),
    )
David Metzler's avatar
David Metzler committed
  );
  // Add Data tables if it exists.
  drupal_add_css(drupal_get_path('module', 'forena') . '/forena.css');
  if (file_exists('sites/all/libraries/dataTables/media/js/jquery.dataTables.min.js')) {
    drupal_add_js(drupal_get_path('module', 'forena') . '/forena.admin.js');
    drupal_add_js('sites/all/libraries/dataTables/media/js/jquery.dataTables.min.js');
  }
  $headers = array(t('title'), t('name'), t('category'), t('operation'));
  $result = db_query('SELECT * FROM {forena_reports} where language=:language ORDER BY category,title', array(':language' => $language->language));
  foreach ($result as $row) {
    $rpt = str_replace('/', '.', $row->report_name);
David Metzler's avatar
David Metzler committed
    $edit = l(t('Edit'), 'reports/' . $rpt . '/edit');
    $clone = l(t('Clone'), 'reports/add/' . $rpt);
    $delete = l(t('Delete'), 'reports/' . $rpt . '/delete', array('query' => array('destination' => 'admin/structure/reports')));
    $title = l(t($row->title), 'reports/' . $rpt);
    $data[] = array($title, $row->report_name, $row->category, $edit . ' ' . $clone . ' ' . $delete);
  $output .= '<div id="forena-reports-list">';
  $output .= theme_table(array('header' => $headers, 'rows' => $data, 'attributes' => array('class' => array( 'dataTable-paged')), 'caption' => '', 'sticky' => TRUE, 'colgroups' => array(), 'empty' => ''));
David Metzler's avatar
David Metzler committed
 * Remove the report from the database and file system.
function forena_delete_report($report_name, $delete_file = TRUE) {
  $report_path = forena_report_path();
David Metzler's avatar
David Metzler committed
  $filepath = $report_path . '/' . $report_name . '.frx';
David Metzler's avatar
David Metzler committed
  $info = pathinfo($filepath);
  $do = TRUE;
  if (file_exists($filepath)) {
David Metzler's avatar
David Metzler committed
    chdir($info['dirname']);
    if ($delete_file) $do = unlink($info['basename']);
  if (module_exists('locale')) {
    @list($tlang, $tname) = explode('/', $report_name, 2);
    if (array_key_exists($tlang, language_list())) {
      $report_name = $tname;
      $language = $tlang;
  if ($do) {
    db_delete('forena_reports')
David Metzler's avatar
David Metzler committed
    ->condition('report_name', $report_name)
    ->condition('language', $language)
    ->execute();
  }
  else {
metzlerd's avatar
metzlerd committed
    drupal_set_message(t('Unable to delete file %s', array('%s' => $info['basename'])), 'error');
function forena_filter_element($fmt, $name) {
David Metzler's avatar
David Metzler committed
  global $user;
  $element['format'] =  array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
David Metzler's avatar
David Metzler committed
    '#title' => t('Input formats'),
  );
  if (!$fmt) $fmt='full_html';
David Metzler's avatar
David Metzler committed
  // Get a list of formats that the current user has access to.
David Metzler's avatar
David Metzler committed
  $formats = filter_formats($user);
  foreach ($formats as $format) {
    $options[$format->format] = $format->name;
    $element['format']['guidelines'][$format->format] = array(
      '#theme' => 'filter_guidelines',
David Metzler's avatar
David Metzler committed
      '#format' => $format,
    );
  }
  $element['format']['guidelines']['#weight']=12;

  $element['format'][$name] = array(
David Metzler's avatar
David Metzler committed
    '#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,
  );
David Metzler's avatar
David Metzler committed
  return $element['format'];
David Metzler's avatar
David Metzler committed
}

/**
 * Accepts the name of a file
 *
 * Returns an editor object of the file.
 *
David Metzler's avatar
David Metzler committed
 */
function forena_get_report_editor($report_name) {
  require_once('FrxEditor.inc');
  if ($report_name) {
    $r_text='';
    $report_path = forena_report_path();
    $filename = $report_path . '/' . $report_name . '.frx';
    if (file_exists($filename)) {
      $r_text = file_get_contents($filename);
    }
    $r = new FrxEditor($r_text);
    return $r;
  }
  else {
    return new FrxEditor();
 * Report syncronization form
 * @param $formid
 * @param $form_state
function forena_sync_form($formid, &$form_state) {

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

  $form['sync'] = array(
    '#type' => 'submit',
    '#value' => t('Clear Cache'),
    '#submit' => array('forena_settings_sync_submit'),
  );

}

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

  $report_path = forena_report_path();

  $form['forena_report_repos'] = array(
    '#type' => 'textfield',
    '#title' => t('Report Repository'),
David Metzler's avatar
David Metzler committed
    '#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,
  );

  $form['forena_last_report_path'] = array(
    '#type' => 'value',
    '#value' => forena_report_path(),
David Metzler's avatar
David Metzler committed
  );

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

David Metzler's avatar
David Metzler committed
    $form =  system_settings_form($form);
    $form['#submit'][] = 'forena_settings_submit';
    return $form;
David Metzler's avatar
David Metzler committed

 * Added submit handler to create directories and clear menu cache
 *
 * @param unknown_type $form
 * @param unknown_type $form_state
 */
David Metzler's avatar
David Metzler committed
function forena_settings_submit($form, &$form_state) {
  $values = $form_state['values'];
David Metzler's avatar
David Metzler committed
  $src_dir = drupal_get_path('module', 'forena') . '/repos/reports';
  if ($path != $values['forena_last_report_path']) {
        if (file_exists($path)) {
          drupal_set_message(t('Created directory %s', array($path))) ;
        }
        mkdir($path);
      } catch (Exception $e) {
        forena_error(t('Unable to create report directory'), $e->getMessage());
        return;
      }
    }
David Metzler's avatar
David Metzler committed
  }
David Metzler's avatar
David Metzler committed

function forena_settings_sync_submit($form, &$form_state) {
  forena_sync_reports($form_state['values']['sync_overwrite']);
  drupal_set_message(t('Report cache cleared'));
function forena_format_form($formid, $form_state, $report_name) {
  $desc = Frx::Menu()->parseURL($report_name);
  $name = $desc['name'];
David Metzler's avatar
David Metzler committed
    drupal_not_found();
    exit;
  $filename = $desc['filename'];
  $format = isset($desc['format']) ? $desc['format'] : '';
  if ($desc['exists']) {
    $r = forena_get_report_editor($name);
    drupal_set_title($r->title);
    $form = array();
    $r = forena_get_report_editor($name);
    $frx_options = $r->getOptions();
    $report_form = @$frx_options['form'];

    $doclist = Frx::Menu()->doc_formats;
    $skins[''] = t('Use Default');
    $skins = array_merge(variable_get('forena_skins', array()), $skins);
    $form['report_name'] = array(
       '#type' => 'value',
       '#value' => $name,
    );
    $form['form'] = array(
      '#type' => 'select',
      '#title' => t('Skin'),
      '#options' => $skins,
      '#default_value' => $report_form,
metzlerd's avatar
metzlerd committed
      '#description' => t('The page style of your report.  The {skin}.skinfo file specifies css and js file in your report.')
    //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['submit'] = array(
        '#type' => 'submit',
        '#value' => 'Save',
      );
    }
  }
  return $form;
}

function forena_format_form_submit($form, &$form_state) {
David Metzler's avatar
David Metzler committed
  $values = $form_state['values'];
  $name = $values['report_name'];

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


  // Doc gen settings.
  if (isset($form['docgen'])) {
    $docgen = array();
metzlerd's avatar
metzlerd committed
    if ($selected = array_filter(@$values['docgen']['docs'])) {
      if ($selected) foreach ($selected as $key => $value) {
David Metzler's avatar
David Metzler committed
        if ($value) $docgen[] = array('type' => $key);
      }
    }
    $r->setDocgen($docgen);
  }

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

}
/**
 * Form function for the edit report form
 * @param $form_state
 * @return the form
 */
function forena_layout_form($form, $form_state, $report_name) {
David Metzler's avatar
David Metzler committed
  $name_in = $report_name;
  $desc = Frx::Menu()->parseURL($report_name);
David Metzler's avatar
David Metzler committed
  $name = $desc['name'];
  //determine if this is an add new report request
David Metzler's avatar
David Metzler committed
  $filename = $desc['filename'];
  $format = @$desc['format'];
    if ((isset($desc['exists']) && $desc['exists'])) {
      $save_name = $name;
David Metzler's avatar
David Metzler committed
      //set the name to empty string for new reports
      $r = forena_get_report_editor($name);
David Metzler's avatar
David Metzler committed

      $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';
      drupal_set_title(filter_xss($r->title));
      // Need to get all option attributes
      $frx_options = $r->getOptions();
      $hidden = @$frx_options['hidden']=='1' ? 1 : 0;
      $report_form = @$frx_options['form'];
      $attributes = $r->get_attributes_by_id();
      $category = $r->getCategory();
      $menu = $r->getMenu();
      $body = $r->simplexml->body->asXML();
      $css = @(string)$r->simplexml->head->style;
David Metzler's avatar
David Metzler committed
      $form = array();
David Metzler's avatar
David Metzler committed

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

      $form['report_name'] = array(
        '#type' => 'value',
David Metzler's avatar
David Metzler committed
        '#value' => $name,
David Metzler's avatar
David Metzler committed

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

      $form['attributes'] = array(
        '#type' => 'value',
        '#value' => $attributes,
David Metzler's avatar
David Metzler committed

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

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

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

      $form['visibility']['hidden'] = array(
        '#title' => t('Hidden'),
David Metzler's avatar
David Metzler committed
        '#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),
      );

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

      $form['menu']['title'] = array(
        '#type' => 'textfield',
        '#title' => t('Menu Title'),
metzlerd's avatar
metzlerd committed
        '#description' => t('Title of menu item.  Leave blank to use the report title as the menu title.'),
        '#default_value' => @$menu['title'],
      $menu_options = array(
        'normal-item' => t('Normal'),
        'local-task' => t('Tab'),
        'default-local-taks' => t('Default Tab'),
        'callback' => t('Callback'),
      );

      $form['menu']['type'] = array(
        '#type' => 'select',
        '#title' => 'Type of menu to create',
        '#options' => $menu_options,
        '#default_value' => @$menu['type'],
      $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']['save'] = array(
        '#type' => 'submit',
        '#value' => 'Save',
David Metzler's avatar
David Metzler committed
        '#submit' => array('forena_layout_form_submit'),
David Metzler's avatar
David Metzler committed

      if (user_access('delete report')) {
David Metzler's avatar
David Metzler committed
        $form['buttons']['delete'] = array(
          '#type' => 'submit',
          '#value' => 'Delete',
          '#submit' => array('forena_edit_delete_submit'),
        );
      return $form;
    }
    else {
      drupal_not_found();
David Metzler's avatar
David Metzler committed
  }
function forena_layout_form_validate($form, &$form_state) {
David Metzler's avatar
David Metzler committed
  $values = $form_state['values'];
  $body = $values['body']['value'];
  $doc_prefix = '<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE root [
    <!ENTITY nbsp "&#160;">
    ]>';
  if ($body) {
David Metzler's avatar
David Metzler committed
    $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) {
David Metzler's avatar
David Metzler committed
      form_set_error('body', t('Invalid XHTML Document. Check for unclosed tags or stray &'));
  if ($values['menu']['path']) {
    if (!valid_url(str_replace(':', '', $values['menu']['path']), FALSE)) {
      form_set_error('menu][path', t('Invalid Path'));
    }
  }

/**
 * builds a string of the xml document,
 * submits it to forena_save_report.
 */
function forena_layout_form_submit($form, &$form_state) {
  $nodes = array();
  $rebuild_menu = FALSE;
  $values = $form_state['values'];
  $report_name = $values['report_name'];
  $r = forena_get_report_editor($report_name);
  // Title and category
  $r->setTitle($values['title']);
  $r->setCategory($values['category']);
  // Form options
  $options = array(
    'hidden' => $values['hidden'],
  );
  $r->setOptions($options);
  $menu = $r->getMenu();
  if ($values['menu']!= $menu) {
    $r->setMenu($values['menu']);
    $rebuild_menu = TRUE;
  }

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

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

  if (forena_save_report($report_name, $r->asXML(), TRUE) == 1) {
metzlerd's avatar
metzlerd committed
    drupal_set_message(t('Your report, %s has been saved.', array('%s' => $report_name)));
  // If we changed the menu we need to rebuild it.
  if ($rebuild_menu) {
    menu_rebuild();
  }
/**
 * Handle delete buttons from edit forms.
 * @return unknown_type
 */
function forena_edit_delete_submit($form, &$form_state) {
David Metzler's avatar
David Metzler committed
  $link = 'reports/' . $form_state['values']['name_in'] ;
  $destination = '';
  if (isset($_REQUEST['destination'])) {
    $destination = drupal_get_destination();
    unset($_REQUEST['destination']);
  }
  $form_state['redirect'] = array('path' => $link . '/delete', 'query' => array('destination' => $destination));
}


function forena_add_report_form($formid, $form_state, $report_name='') {
  $name = '';
  $filename = '';
  $format = '';
  if ($report_name) {
    $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 = forena_get_report_editor($name);
  $title = (string)$r->title;
  if (module_exists('locale')) {
    @list($tlang,  $tsave_name) = explode('/', $name, 2);
    // FInd out if the starting name of the report is an installed language.
    if (array_key_exists($tlang, language_list() )) {
      $lang = $tlang;
      $save_name = $tsave_name;
    }
    else {
David Metzler's avatar
David Metzler committed
      $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());

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


  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#default_value' => $title,
metzlerd's avatar
metzlerd committed
    '#required' => TRUE,
  );

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

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

  $form['report_name'] = array(
    '#title' => t('Create from report'),
    '#type' => 'textfield',
    '#autocomplete_path' => 'forena/reports/autocomplete',
    '#default_value' => $name,
  );

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

}

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

  //comparing the report names to see if they have changed.
  //If they have, making sure the new name does not already exist.
David Metzler's avatar
David Metzler committed
  $report_path = forena_report_path();
  $filename = $report_path . '/' . $save_report_name . '.frx';
  if (@$desc['exists']) {
    form_set_error('save_report_name', t('The file ' . $save_report_name . ' already exists. Please enter another name.'));
  }
function forena_add_report_form_submit($form, &$form_state) {
  $values = $form_state['values'];
  $report_name = $values['save_report_name'];
  $desc = Frx::Menu()->parseURL($report_name);
  $r = forena_get_report_editor($values['report_name']);
  // Title and category
  $r->setTitle($values['title']);
  $r->setCategory($values['category']);
  // Form options
  $options = array(
    'hidden' => $values['hidden'],
  //determine redirection.
  $filename = $desc['filename'];
David Metzler's avatar
David Metzler committed

  if (forena_save_report($report_name, $r->asXML(), TRUE) == 1) {
metzlerd's avatar
metzlerd committed
    drupal_set_message(t('Your report, %s has been saved.', array('%s' => $report_name)));
    //if this is a new report then redirect to data blocks
    if ($values['report_name']) {
David Metzler's avatar
David Metzler committed
      $edit = '/edit';
David Metzler's avatar
David Metzler committed
      $edit = '/edit/data/add';
metzlerd's avatar
metzlerd committed
    $form_state['redirect']= $desc['i_link'] . $edit;
David Metzler's avatar
David Metzler committed
  }
}
function forena_create_trans_form($formid, $form_state, $report_name) {
  $name = '';
  $filename = '';
  $format = '';
  $desc = Frx::Menu()->parseURL($report_name);
  $name = $desc['name'];
  $filename = $desc['filename'];
  $base_name = $desc['base_name'];
  $format = @$desc['format'];
  $form = array();
  global $language;
  //determine if this is an add new report request

  $r = forena_get_report_editor($name);
  $title = (string)$r->title;
  $lang = @$_GET['language'];
  if ($lang) {
David Metzler's avatar
David Metzler committed
    $language = $languages[$lang];
  }
  $form['base_name'] = array(
    '#type' => 'value',
    '#value' => $base_name
  );
  $form['report_name'] = array(
    '#type' => 'value',
    '#value' => $name,
  );
metzlerd's avatar
metzlerd committed
  foreach ($languages as $key => $obj) {
    $languages[$key] = $obj->native;
  $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
    '#required' => TRUE,
  );


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

}

function forena_create_trans_form_submit($form, &$form_state) {
  $values = $form_state['values'];
  $base_name = $values['base_name'];
metzlerd's avatar
metzlerd committed
  $new_name = $values['save_report_language'] . '/' . $base_name;
  $desc = Frx::Menu()->parseURL($new_name);
  $filename = $desc['filename'];
  $report_name = $desc['name'];

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

  // Title and category
  $r->setTitle($values['title']);
David Metzler's avatar
David Metzler committed
  //determine redirection.

  if (file_exists($filename)) {
    drupal_set_message(t('Report %s already exists', array('%s' => $new_name)), 'error');
David Metzler's avatar
David Metzler committed

  if (forena_save_report($report_name, $r->asXML(), TRUE) == 1) {
metzlerd's avatar
metzlerd committed
    drupal_set_message(t('Translation,  %s has been created. Switch languages to translate.', array('%s' => $values['title'])));
David Metzler's avatar
David Metzler committed
    //if this is a new report then redirect to data blocks
    $form_state['redirect']= array( $desc['i_link'] . '/edit/layout');
David Metzler's avatar
David Metzler committed
  }
/*
 * administer the settings for document format options
 */
function forena_doc_formats_settings() {
  // Invoke doc_type hook to see which document types are there.
  $supported_doctypes = Frx::documentTypes();
  $form['forena_doc_formats'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Allowed Document Formats'),
    '#default_value' => variable_get('forena_doc_formats', $supported_doctypes),
    '#description' => t('check your desired document format'),
    '#options' => $supported_doctypes,
  );

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

  $form['forena_email_override'] = array(
    '#type' => 'checkbox',
    '#title' => 'Run email merges in test mode' ,
metzlerd's avatar
metzlerd committed
    '#default_value' => variable_get('forena_email_override', FALSE),
    '#description' => t('When this box is checked emails are sent to the currently logged in user.  Useful for testing environments.'),
  );
  $form['forena_email_input_format'] = forena_filter_element(variable_get('forena_email_input_format', 'full_text'), 'forena_email_input_format');
  $form['forena_email_input_format']['#title'] = t('Email Input Format');
  return system_settings_form($form);
}
/*
 * administer the settings for document format options
 */
function forena_data_settings() {
  $repos = Frx::RepoMan()->repositories;
metzlerd's avatar
metzlerd committed
  $headers = array(t('Name'), t('Description'), t('Path'), t('Operation'));
  foreach ($repos as $name => $r) {
David Metzler's avatar
David Metzler committed
    $name,
    $r['title'],
    $r['path'],
    l(t('configure'), 'admin/config/content/forena/data/configure/' . $name)
metzlerd's avatar
metzlerd committed
  $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' => ''));
  return $output;
}

function forena_data_settings_edit($form, &$form_state, $source=-1) {
  $adding = ($source === -1);
  if (!@$form_state['storage']) {
    if ($adding) {
      $form_state['storage'] = array(
        'name' => '',
        'title' => '',
        'path' => '',
        'config' => array(
          'source' => 'user',
          'data provider' => 'FrxDrupal',
          'database' => 'default',
          'access callback' => 'user_access',
          'user callback' => 'forena_current_user_id'
David Metzler's avatar
David Metzler committed
          ),
          );
      Frx::RepoMan()->repository($source);
      $repos = Frx::RepoMan()->repositories;
davidmetzler's avatar
davidmetzler committed
      // Remove teh object from the data.
      unset($r['data']);
      $form_state['storage'] = array(
        'name' => $source,
        'title' => $r['title'],
        'path' => @$r['path'],
        'config' => $r,
      );

    }
  }
  $data = $form_state['storage'];
  $config = $data['config'];
  $locked = !($adding || (@$config['source'] == 'user'));
  $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,
David Metzler's avatar
David Metzler committed
  );

  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#required' => TRUE,
    '#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,
  );

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

  $form['path'] = array(
    '#type' => 'textfield',
    '#title' => t('Path'),
    '#required' => TRUE,
    '#description' => t('Directory containing data block files.'),
    '#default_value' => @$data['path'],
  );

  $user_options = array(
    '' => 'None',
    'forena_current_user_id' => 'UID',
    'forena_current_user_name' => 'User name',
  );

  $form['user_callback'] = array(
    '#type' => 'select',
    '#title' =>  'Current user',
    '#description' => t('Can be refererenced as :current_user in each data block.'),
    '#options' => $user_options,
    '#default_value' => @$config['user callback'],
    '#disabled' => $locked,
  );


  // Access method list
  $access = array(
    'callback' => t('Use drupal permissions'),
metzlerd's avatar
metzlerd committed
    'block' => t('Match values provided by a data block.'),
  );

  $form['access_method'] = array(
    '#type' => 'select',
    '#options' => $access,
    '#title' => t('Data security method'),
    '#default_value' => empty($config['access block']) ? 'callback' : 'block',
metzlerd's avatar
metzlerd committed
    '#description' => t('Specify how the ACCESS defined for a data block is to be interpreted.'),
    '#ajax' => array(
      'callback' => 'forena_access_info_callback',
      'wrapper' => 'access-details',