You are here

class ParameterForm in Forena Reports 8

Hierarchy

Expanded class hierarchy of ParameterForm

1 file declares its use of ParameterForm
TestingAppService.php in tests/src/Unit/Mock/TestingAppService.php

File

src/Form/ParameterForm.php, line 18

Namespace

Drupal\forena\Form
View source
class ParameterForm extends FormBase {
  use FrxAPI;
  public function getFormID() {
    return 'forena_parameter_form';
  }
  public function buildForm(array $form, FormStateInterface $form_state, $parameters = NULL) {
    $parms = $_GET;

    //$storage = $form_state->getStorage();

    // Set menu parms
    $menu_parms = $this
      ->getDataContext('menu-parms');
    if ($menu_parms) {
      $storage['menu-parms'] = $menu_parms;
    }

    // Set Descriptors
    $values = $form_state
      ->getValues();
    $collapse = isset($attributes['collapsed']) ? $attributes['collapsed'] : FALSE;
    if ($values) {
      $collapse = FALSE;
      $parms = array_merge($parms, $values['params']);

      // In the case of ahah, we need to restore menu parameters from the form state.
      if (isset($storage['menu-parms'])) {
        $menu_parms = $storage['menu-parms'];
        $parms = array_merge($menu_parms, $parms);
      }
      $this
        ->app()
        ->alter('forena_parameters', $report_name, $parms);
      $this
        ->pushData($parms, 'parm');
    }
    $template = @$attributes['template'];
    $collapsible = isset($attributes['collapsible']) ? $attributes['collapsible'] : TRUE;
    $title = isset($attributes['title']) ? $attributes['title'] : $this
      ->t('Parameters');
    $submit_value = isset($attributes['submit']) ? $attributes['submit'] : $this
      ->t('Submit');
    unset($parms['q']);
    $form = array();
    if ($parameters) {
      $this
        ->app()
        ->alter('forena_parameters', $report_name, $parms);
      if ($parameters) {
        $form['parms'] = array(
          '#tree' => TRUE,
          '#title' => $title,
          '#type' => 'details',
          '#collapsible' => $collapsible,
          '#collapsed' => $collapse,
          '#prefix' => '<div id="parameters-wrapper">',
          '#suffix' => '</div>',
        );
        foreach ($parameters as $node) {
          $add_null = FALSE;
          $list = array();
          $disabled = FALSE;
          $label = @(string) $node['label'];
          $id = @(string) $node['id'];
          $data_source = @(string) $node['data_source'];
          $data_field = @(string) $node['data_field'];
          $class = @(string) $node['class'];
          $type = @(string) $node['type'];
          $option_str = @(string) $node['options'];
          $options = array();
          if ($option_str) {
            parse_str($option_str, $options);
          }
          if (isset($parms[$id])) {
            $value = $parms[$id];
            $multi_value = (array) $parms[$id];
          }
          else {
            $value = @(string) $node['default'];
            if (strpos($value, '|') !== FALSE) {
              $multi_value = explode('|', $value);
            }
            elseif ($value) {
              $multi_value = (array) $value;
            }
            else {
              $multi_value = array();
            }
          }
          $desc = @(string) $node['desc'];
          $label_field = @(string) $node['label_field'];
          @(strcmp((string) $node['require'], "1") == 0) ? $required = TRUE : ($required = FALSE);
          $ctl_attrs = array();

          //returned values filtered against data_field attr.
          if ($data_source) {
            $list = DataManager::instance()
              ->dataBlockParams($data_source, $data_field, $label_field);
            if (!$required && $add_null) {
              $list = array(
                '' => '',
              ) + $list;
            }
          }

          //Determine the form element type to be displayed

          //If select or radios is chosen then begin a $list array for display values.
          $multiselect = FALSE;
          $ajax = FALSE;
          $add_null = FALSE;
          switch ($type) {
            case 'multiselect':
              $type = 'select';
              $multiselect = TRUE;
              $value = $multi_value;
              break;
            case 'multiselectajax':
              $type = 'select';
              $multiselect = TRUE;
              $value = $multi_value;
              $ajax = TRUE;
              break;
            case 'checkboxes':
              $value = $multi_value;
              break;
            case 'selectajax':
              $ajax = TRUE;
              $type = 'select';
              $add_null = TRUE;
              break;
            case 'select':
              $add_null = TRUE;
              break;
            case 'date_text':
            case 'date_select':
            case 'date_popup':
              $options['date_format'] = @$options['date_format'] ? $options['date_format'] : 'Y-m-d';
              $ctl_attrs['#date_format'] = $options['date_format'];
              if ($value) {
                $datetime = @strtotime($value);
                if ($datetime) {
                  $value = date('Y-m-d h:i', $datetime);
                }
              }
              $ctl_attrs['#forena_date_format'] = @$options['date_parm_format'] ? $options['date_parm_format'] : 'Y-m-d';
              if (@$options['date_year_range']) {
                $ctl_attrs['#date_year_range'] = $options['date_year_range'];
              }
              if (@$options['date_label_position']) {
                $ctl_attrs['#date_label_position'] = $options['date_label_position'];
              }
              $list = array();
              break;
            case 'checkbox':
              if (@$option_str['return_value']) {
                $ctl_attrs['#return_value'] = $options['return_value'];
              }
              $list = array();
              break;
            case 'radios':
              break;
            case 'hidden':
              $list = array();
              break;
            default:
              $type = 'textfield';
              $list = array();
          }
          if (isset($menu_parms[$id]) && $type != 'hidden') {
            $disabled = TRUE;
          }

          //If a data_source attr was found then create an array of
          $form['parms'][$id] = array(
            '#type' => $type,
            '#title' => $label ? $this
              ->t($label) : $this
              ->t($id),
            '#default_value' => $value,
            '#disabled' => $disabled,
            '#required' => $required,
            '#description' => $this
              ->t($desc),
          );
          $form['parms'][$id] = array_merge($form['parms'][$id], $ctl_attrs);
          if ($type == 'item') {
            $form['parms'][$id]['#markup'] = $value;
          }
          if ($type == 'hidden') {
            $form['parms'][$id]['#value'] = $value;
          }

          // Add class to parmeter form.
          if ($class) {
            $form['parms'][$id]['#attributes'] = array(
              'class' => @explode(' ', $class),
            );
          }

          //if $list is not empty then push options

          //onto the array. options will cause an error for

          //textfield elements.
          if ($list || $type == 'select' || $type == 'radios') {
            if ($add_null) {
              $prompt = @$options['prompt'];
              if (!$prompt) {
                $prompt = $required ? '-' . $this
                  ->t('select') . '-' : '-' . t('none') . '-';
              }
              $form['parms'][$id]['#empty_option'] = $prompt;
            }
            $form['parms'][$id]['#options'] = $list;
            $form['parms'][$id]['#multiple'] = $multiselect;
          }
          if ($ajax) {
            $form['parms'][$id]['#ajax'] = array(
              'callback' => 'forena_parameters_callback',
              'wrapper' => 'parameters-wrapper',
            );
          }
        }
        if ($template) {
          $form['parms']['#forena-template'] = $template;
          $form['parms']['#theme'] = 'forena_fieldset_template';
          _forena_set_inline_theme($form['parms']);
        }
        $form['parms']['submit'] = array(
          '#type' => 'submit',
          '#value' => $submit_value,
        );
      }

      //$form_state->setStorage($storage);
      return $form;
    }
    else {
      return NULL;
    }
  }
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state
      ->getValues();
    unset($values['parms']['submit']);
    if (isset($values['parms'])) {
      foreach ($values['parms'] as $key => $value) {
        $ctl = $form['parms'][$key];
        switch ($ctl['#type']) {
          case 'date_popup':
          case 'date_select':
          case 'date_text':
            $datetime = @strtotime($value);
            if ($datetime) {
              $value = $values['parms'][$key] = date($ctl['#forena_date_format'], $datetime);
            }
            break;
        }
        if (is_array($value)) {
          $values['parms'][$key] = array();
          foreach ($value as $k => $val) {
            if ($val) {
              $values['parms'][$key][] = $val;
            }
          }
        }
        else {
          if (strpos($value, '|') !== FALSE) {
            $values['parms'][$key] = explode('|', $value);
          }
          elseif ($value === '' || $value === NULL) {
            unset($values['parms'][$key]);
          }
        }
      }
    }
    $path = \Drupal::service('path.current')
      ->getPath();
    $url = Url::fromUserInput($path, [
      'query' => $values['parms'],
    ]);
    $form_state
      ->setRedirectUrl($url);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create 87
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
FormInterface::getFormId public function Returns a unique string identifying the form. 236
FrxAPI::app public function Returns containing application service
FrxAPI::currentDataContext public function Get the current data context.
FrxAPI::currentDataContextArray public function
FrxAPI::dataManager public function Returns the data manager service
FrxAPI::dataService public function Return Data Service
FrxAPI::documentManager public function Returns the fornea document manager
FrxAPI::error public function Report an error
FrxAPI::getDataContext public function Get the context of a specific id.
FrxAPI::getDocument public function Get the current document
FrxAPI::getReportFileContents public function Load the contents of a file in the report file system.
FrxAPI::innerXML function Enter description here... 1
FrxAPI::popData public function Pop data off of the stack.
FrxAPI::pushData public function Push data onto the Stack
FrxAPI::report public function Run a report with a particular format. 1
FrxAPI::reportFileSystem public function Get the current report file system.
FrxAPI::setDataContext public function Set Data context by id.
FrxAPI::setDocument public function Change to a specific document type.
FrxAPI::skins public function Get list of skins.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
ParameterForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
ParameterForm::getFormID public function
ParameterForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.