You are here

function forena_add_data_block_form in Forena Reports 7.3

Same name and namespace in other branches
  1. 6.2 forena.admin.inc \forena_add_data_block_form()
  2. 7.2 forena.admin.inc \forena_add_data_block_form()
1 string reference to 'forena_add_data_block_form'
forena_menu in ./forena.module
Implementation of hook_menu.

File

./forena.admin.inc, line 1440

Code

function forena_add_data_block_form($formid, &$form_state, $report_name) {

  // If no step is set then we're visiting this form for the first time.
  if (!isset($form_state['storage']['step'])) {
    $desc = $form_state['storage']['desc'] = Frx::Menu()
      ->parseURL($report_name);
    $filename = $form_state['storage']['filename'] = $desc['filename'];
    $format = isset($desc['format']) ? $desc['format'] : '';
    $r = forena_get_report_editor($desc['name']);
    $form_state['storage']['title'] = $r->title;
    if (!@$desc['exists']) {
      drupal_not_found();
      return;
    }
    $step = $form_state['storage']['step'] = 'select_block';
  }
  else {
    $desc = $form_state['storage']['desc'];
    $step = $form_state['storage']['step'];
  }

  // General report data variables;
  drupal_set_title($form_state['storage']['title']);
  $form = array();
  $template_array = FrxReportGenerator::instance()
    ->supported_templates();
  @($default_template = isset($form_state['values']['templates']) ? $form_state['values']['templates'] : $form_state['storage']['template']);
  $template = $default_template ? $default_template : 'FrxTable';
  $template_obj = FrxReportGenerator::instance()
    ->get_templates($template);
  $params = @$form_state['storage']['parameters'];
  $param_values = @$form_state['storage']['parms'];
  $form = array();
  $form['report_name'] = array(
    '#type' => 'value',
    '#value' => $desc['name'],
  );
  $form['return'] = array(
    '#type' => 'value',
    '#value' => $desc['link'] . '/edit/data',
  );
  if ($step == 'select_block') {
    $form['data_block'] = array(
      '#type' => 'textfield',
      '#title' => t('Data Block'),
      '#default_value' => @$form_state['storage']['data_block'],
      '#autocomplete_path' => 'forena/data_block/autocomplete',
      '#required' => TRUE,
      '#description' => t('Enter a data block, a preview will appear below'),
    );
    $form['parameters'] = array(
      '#type' => 'textfield',
      '#title' => t('Parameter data'),
      '#description' => t('Enter parameters to be used for the block as they would appear on a url'),
      '#default_value' => @$form_state['storage']['parm_values'],
    );

    //If there are parameters in the data block allow the user to input data

    //Check if there is already a parameter of the same name in the existing report. If there is, use the report value.
    if ($params) {
      $rpt_params = array();

      //returns a 2 dimensional array of report parameters from the report
      $param_values = @$form_state['values']['parameters'];
      $form['parms'] = array(
        '#tree' => TRUE,
        '#title' => 'Parameters',
        '#type' => 'fieldset',
        '#collapsible' => TRUE,
        '#collapsed' => @$form_state['storage']['xml'] ? TRUE : FALSE,
      );
      foreach ($params as $param) {
        $form['parms'][$param] = array(
          '#type' => 'textfield',
          '#title' => t($param),
          '#default_value' => isset($param_values[$param]) ? $param_values[$param] : '',
        );
      }
    }
    $form['preview'] = array(
      '#type' => 'submit',
      '#value' => 'Preview',
      '#validate' => array(
        'forena_data_select_block_validate',
      ),
      '#submit' => array(
        'forena_data_select_block_submit',
      ),
    );
  }

  //Template selection
  if ($step == 'select_template') {
    $form['templates'] = array(
      '#type' => 'select',
      '#title' => t('Templates'),
      '#default_value' => $template,
      '#options' => $template_array,
      '#required' => TRUE,
      '#description' => t('Select a template to preview the data block in.'),
    );
    forena_template_ajax($form['templates']);
    $form['template'] = array(
      '#prefix' => '<div id="template-wrapper">',
      '#suffix' => '</div>',
    );
    $config = isset($form_state['values']['template_data']) ? @$form_state['values']['template_data']['config'] : @$form_state['storage']['config'];
    $config['block'] = $form_state['storage']['data_block'];
    $xml = $form_state['storage']['xml'];
    if ($xml) {
      try {
        $xml = @new SimpleXMLElement($xml);
      } catch (Exception $e) {
        drupal_set_message(t('Could not create xml from %s.  Verify that all columns names are valid XML tag names and have no spaces or other special characters.', array(
          '%s' => $form_state['storage']['data_block'],
        )), 'error', FALSE);
      }

      //create xml from template object
      $template_obj = FrxReportGenerator::instance()
        ->get_templates($template);
      if ($template_obj) {
        if ($template_obj) {
          $form['template']['template_data'] = array(
            '#type' => 'fieldset',
            '#title' => t('Template Settings'),
            '#tree' => TRUE,
          );
          $form['template']['template_data']['config'] = $template_obj
            ->config_form($config, $xml);
        }
        $template_obj
          ->generate($xml, $config);
        $body = $template_obj
          ->template();
        $rpt_xml = $template_obj
          ->asXML();
        $parms = $form_state['storage']['parms'];
        $output = forena_render_report($rpt_xml, NULL, $parms, NULL, FALSE);

        //render the xml
        $form_state['storage']['output'] = $output;
        $form_state['storage']['template_body'] = $body;
      }
    }
    $form['template']['back'] = array(
      '#type' => 'submit',
      '#value' => t('Back'),
      '#submit' => array(
        'forena_data_block_back',
      ),
    );
    if (@$form_state['storage']['output'] != '') {
      $form['template']['add'] = array(
        '#type' => 'submit',
        '#value' => 'Add',
        '#submit' => array(
          'forena_data_block_add',
        ),
      );
    }
    $form['template']['preview'] = array(
      '#type' => 'submit',
      '#value' => 'Preview',
      '#submit' => array(
        'forena_data_select_template_submit',
      ),
    );
    if (isset($form_state['storage']['output'])) {
      $form['template']['output_text'] = array(
        '#type' => 'markup',
        '#markup' => '<p>' . $form_state['storage']['output'] . '</p>',
      );
    }
    $form['cancel'] = array(
      '#type' => 'submit',
      '#value' => 'Cancel',
      '#submit' => array(
        'forena_add_cancel',
      ),
    );
  }
  return $form;
}