You are here

function forena_query_builder_form in Forena Reports 7.3

1 string reference to 'forena_query_builder_form'
forena_query_menu in ./forena_query.module
Implementation of hook_menu

File

./forena_query.inc, line 7

Code

function forena_query_builder_form($formid, &$form_state) {

  // Define an empty block
  $access = TRUE;
  $default_storage = array(
    'block' => array(),
    'block_name' => '',
    'parameters' => array(),
    'provider' => '',
    'builder' => array(
      'columns' => array(),
      'block' => '',
      'where' => array(
        'op' => 'AND',
        'filter' => array(
          array(
            'field' => '',
            'op' => '',
            'value' => '',
          ),
        ),
      ),
      'group' => array(),
      'order' => array(),
    ),
    'fields' => array(),
    'step' => 'block',
    'preview' => '',
    'new' => TRUE,
  );

  // Work with the existing block or load default
  if (!isset($form_state['storage'])) {
    $form_state['storage'] = $default_storage;
  }
  $data =& $form_state['storage'];
  $block = $data['block'];
  $step = $form_state['storage']['step'];

  /**
   * Block select portion of ofrm
   */

  // First stage is to either load an existing block or create a new one
  if ($step == 'block') {
    $form['block'] = array(
      '#type' => 'textfield',
      '#autocomplete_path' => 'forena/data_block/autocomplete',
      '#title' => 'Data Block',
      '#required' => TRUE,
      '#description' => t('Select an exising block or create a new one derived from the selected block.'),
    );
    $form['load_include'] = array(
      '#type' => 'submit',
      '#validate' => array(
        'forena_query_validate_block',
      ),
      '#submit' => array(
        'forena_query_load_include',
        'forena_query_preview',
      ),
      '#value' => t('New'),
    );
    $form['load'] = array(
      '#type' => 'submit',
      '#validate' => array(
        'forena_query_validate_block',
      ),
      '#submit' => array(
        'forena_query_load_block',
        'forena_query_preview',
      ),
      '#value' => t('Load'),
    );
  }
  else {
    $access = forena_query_access_repository($form_state['storage']['block_name']);

    /**
     * Section of form for finally saving the report.
     */
    if ($access && $form_state['storage']['new']) {
      $form['save_as_name'] = array(
        '#type' => 'textfield',
        '#title' => t('Name') . ' ' . $form_state['storage']['provider'] . '/',
        '#description' => t('Name of block to save'),
      );
    }
    if (@$form_state['storage']['delete']) {
      $form['question'] = array(
        '#type' => 'markup',
        '#markup' => '<p>Are you sure you want to delete ' . $data['block_name'] . '?</p>',
      );
      $form['confirm'] = array(
        '#type' => 'submit',
        '#submit' => array(
          'forena_query_delete_block',
        ),
        '#value' => t('Yes'),
      );
      $form['cancel'] = array(
        '#type' => 'submit',
        '#submit' => array(
          'forena_query_delete_cancel',
        ),
        '#value' => t('No'),
      );
      return $form;
    }
    if ($step == 'builder' || $step == 'sql') {
      $form['access'] = array(
        '#title' => t('Data Security'),
        '#description' => t('Value depends on the security for the data block,
          but for drupal data, this is a right as defined by the module provided.  '),
        '#type' => 'textfield',
        '#required' => TRUE,
        '#disabled' => !user_access('forena data security') || @$block['locked'],
        '#default_value' => $block['access'],
      );
    }

    /**
     * Edit query builder clause portion of form.
     */
    if ($step == 'builder') {
      $form['where'] = array(
        '#tree' => TRUE,
      );
      $form['where'] = array(
        '#type' => 'fieldset',
        '#title' => 'Filter Criteria',
        '#tree' => TRUE,
      );
      _forena_query_filter_ctl($data['builder']['where'], $form['where'], $form_state['storage']['fields']);
      $form['sql'] = array(
        '#type' => 'submit',
        '#value' => t('SQL'),
        '#submit' => array(
          'forena_query_update_builder',
          'forena_query_sql_view',
        ),
      );
    }

    /**
     * Edit SQL directly.
     */
    if ($step == 'sql') {
      if ($block && $block['type'] == 'sql') {
        $form['file'] = array(
          '#type' => 'textarea',
          '#title' => t('Source'),
          '#disabled' => @$block['locked'],
          '#default_value' => @$block['file'],
        );
        if (@$block['builder']) {
          $form['builder_view'] = array(
            '#type' => 'submit',
            '#value' => t('Query Builder'),
            '#submit' => array(
              'forena_query_builder_view',
            ),
          );
        }
      }
    }
    if (@$block['tokens']) {
      $form['parameters'] = array(
        '#title' => 'Parameters',
        '#type' => 'fieldset',
        '#tree' => TRUE,
      );
      foreach ($block['tokens'] as $name) {
        $ctl = array(
          '#type' => 'textfield',
          '#title' => check_plain($name),
        );
        $form['parameters'][$name] = $ctl;
      }
    }
    if (!@$form_state['storage']['new'] && $step != $block) {
      $form['block'] = array(
        '#type' => 'value',
        '#value' => $data['block_name'],
      );
      $form['load'] = array(
        '#type' => 'submit',
        '#submit' => array(
          'forena_query_load_block',
          'forena_query_preview',
        ),
        '#value' => t('Reload'),
      );
    }
    if (!@$block['locked'] && $access) {
      $form['save'] = array(
        '#type' => 'submit',
        '#value' => 'Save',
        '#validate' => array(
          'forena_query_validate_new_block',
        ),
        '#submit' => array(
          'forena_query_update',
          'forena_query_save_block',
        ),
      );
      if (!$data['new']) {
        $form['delete'] = array(
          '#type' => 'submit',
          '#value' => t('Delete'),
          '#submit' => array(
            'forena_query_try_delete',
          ),
        );
      }
    }
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => 'Preview',
      '#submit' => array(
        'forena_query_update',
        'forena_query_preview',
      ),
    );
    $form['cancel'] = array(
      '#type' => 'submit',
      '#value' => 'Close',
      '#submit' => array(
        'forena_query_close',
      ),
      '#limit_validation_errors' => array(),
    );
    $form['preview'] = array(
      '#markup' => @$form_state['storage']['preview'],
    );
  }
  return $form;
}