You are here

function forena_data_block_form_submit in Forena Reports 7

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

The Preview submit handler for forena_add_block_form Renders datablock into a report

_state

Parameters

$form:

Return value

unknown_type

1 string reference to 'forena_data_block_form_submit'
forena_data_block_form in ./forena.admin.inc
A form to preview and add data blocks to an existing report

File

./forena.admin.inc, line 1000

Code

function forena_data_block_form_submit($form, &$form_state) {
  $values = $form_state['values'];
  $data_block = $values['data_block'];
  $template = $values['templates'];
  $params = isset($values['params']) ? $values['params'] : array();
  $name = $values['report_name'];
  $where_clause = $values['where_clause'];
  $r = forena_get_report($name);
  $form_state['storage']['param_values'] = $params;
  $form_state['storage']['data_block'] = $data_block;
  $form_state['storage']['template'] = $template;
  $form_state['storage']['where_clause'] = $where_clause;
  $form_state['rebuild'] = TRUE;
  if ($data_block) {

    //get xml from data block

    //check for and store parameters
    $block_info = forena_load_block($data_block, $where_clause);
    if ($block_info) {

      //if there were not any parameters passed. Use the report parameters
      if (!$params) {
        $rpt_params = $r->parameters;
        if ($rpt_params) {
          foreach ($block_info['tokens'] as $index => $id) {
            $params[$id] = $rpt_params[$id]['value'];
          }
        }
      }

      //now invoke the data provider with the correct params
      $xml = forena_invoke_data_provider($data_block, $params, $where_clause);
    }
  }
  if ($xml) {

    //create an array of columns
    $rows = $xml
      ->xpath('//row');
    $column_array = array();
    foreach ($rows as $columns) {
      foreach ($columns as $name => $value) {
        $column_array[$name] = $name;
      }
    }

    //create xml from template object
    $template_obj = forena_get_templates($template);
    if ($template_obj) {
      $body = $template_obj
        ->{$template}($column_array, $data_block, $where_clause);
    }
    $rpt_xml = '<html xmlns:frx="urn:FrxReports"><body>' . $body . '</body></html>';

    //render the xml
    $output = forena_render_report($rpt_xml, NULL, $params);
    $form_state['storage']['output'] = $output;
    $form_state['storage']['template_body'] = $body;
  }
  else {

    //there wasn't any xml returned. clear the storage
    $form_state['storage']['output'] = '';
  }
  $form_state['storage']['parameters'] = $block_info['tokens'];
}