You are here

function forena_fields_form in Forena Reports 6

Same name and namespace in other branches
  1. 6.2 forena.admin.inc \forena_fields_form()
  2. 7 forena.admin.inc \forena_fields_form()
  3. 7.2 forena.admin.inc \forena_fields_form()
  4. 7.3 forena.admin.inc \forena_fields_form()

Parameters

$form_state:

Return value

a form to edit the fields of the report

1 string reference to 'forena_fields_form'
forena_fields_report in ./forena.module
Calls forena_fields_form in forena.admin.inc

File

./forena.admin.inc, line 647

Code

function forena_fields_form($form_state) {
  $desc = forena_report_desc();
  $name = $desc['name'];
  $filename = $desc['filename'];
  $format = $desc['format'];
  if ($name) {
    if (file_exists($filename)) {
      $r = forena_get_report_editor($name);
      drupal_set_title($r->title);
      $regexp = FRX_TOKEN_EXP;
      $match = array(
        "{",
        "}",
      );

      /*search the body, looking for {}*/
      $body = $r->simplexml->body
        ->asXML();
      $head = $r->simplexml->head;
      $fields = array();
      preg_match_all($regexp, $body, $out);
      foreach ($out as $key => $value) {
        foreach ($value as $el) {
          $clean_element = str_replace($match, "", $el);
          $fields[$clean_element] = $clean_element;
        }
      }
      $form = array();
      $form['report_name'] = array(
        '#type' => 'value',
        '#value' => $name,
      );
      $form['fields'] = array(
        '#tree' => TRUE,
      );

      /*Now check the fields in the body against the xml*/
      $i = 0;
      foreach ($fields as $field) {
        $i++;
        $field_ids[$i] = $field;
        $form['fields'][$i] = array(
          '#tree' => TRUE,
          '#type' => 'fieldset',
          '#title' => $field,
          '#collapsible' => TRUE,
          '#collapsed' => TRUE,
        );
        $path = 'frx:fields/frx:field[@id="' . $field . '"]';
        $node = $head
          ->xpath($path);
        $attr = array();
        $default = '';
        if ($node) {
          $attr = $node[0];
          $default = (string) $node[0];
        }
        $form['fields'][$i]['id'] = array(
          '#type' => 'value',
          '#value' => $field,
        );
        $form['fields'][$i]['format'] = array(
          '#type' => 'textfield',
          '#title' => t('format'),
          '#default_value' => @$attr['format'],
          '#size' => 30,
          '#autocomplete_path' => 'forena/fields/format/autocomplete',
          '#description' => t('Format a date and time field by entering the name of a supported format function. Enter a "*" to see all available formats.'),
        );
        $form['fields'][$i]['format-string'] = array(
          '#type' => 'textfield',
          '#title' => t('format-string'),
          '#default_value' => @$attr['format-string'],
          '#size' => 30,
          '#description' => t('The display type of your format.'),
        );
        $form['fields'][$i]['link'] = array(
          '#type' => 'textfield',
          '#title' => t('link'),
          '#default_value' => @$attr['link'],
          '#size' => 100,
          '#maxlength' => 256,
          '#description' => t('Create a link that incorporates this field, e.g "profile/{field_name}" will create a link to this field_name\'s profile. *Note the field must be wrapped in {}.'),
        );
        $form['fields'][$i]['target'] = array(
          '#type' => 'textfield',
          '#title' => t('target'),
          '#default_value' => @$attr['target'],
          '#size' => 30,
          '#description' => t('Link target eg. _BLANK'),
        );
        $form['fields'][$i]['default'] = array(
          '#type' => 'textfield',
          '#title' => t('default value'),
          '#default_value' => $default,
          '#size' => 30,
          '#description' => t('The value to be displayed in the report in the place of the field.'),
        );
      }
      $form['submit'] = array(
        '#type' => 'submit',
        '#value' => 'Save',
      );
      return $form;
    }
    else {
      drupal_not_found();
    }
  }
  else {
    drupal_not_found();
  }
}