示例#1
0
 /**
  * Gets the value of a user state variable and sets it in the session
  *
  * This is the same as the method in JApplication except that this also can optionally
  * force you back to the first page when a filter has changed
  *
  * @param   string   $key        The key of the user state variable.
  * @param   string   $request    The name of the variable passed in a request.
  * @param   string   $default    The default value for the variable if not found. Optional.
  * @param   string   $type       Filter for the variable, for valid values see {@link \JFilterInput::clean()}. Optional.
  * @param   boolean  $resetPage  If true, the limitstart in request is set to zero
  *
  * @return  array The request user state.
  */
 public function getUserStateFromRequest($key, $request, $default = null, $type = 'none', $resetPage = true)
 {
     /** @var \JApplicationCms $app */
     $app = $this->container->get('app');
     $input = $app->input;
     $oldState = $app->getUserState($key);
     $currentState = !is_null($oldState) ? $oldState : $default;
     $newState = $input->get($request, null, $type);
     /*
      * In RAD, filter & search is array with default elements,
      * so we can't directly compare them with empty value.
      * Here prepare some default value to compare them.
      */
     // Remove empty values from input, because session registry will remove empty values too.
     if ($request == 'filter' && is_array($newState)) {
         $newState = ArrayHelper::filterRecursive($newState, 'strlen');
     }
     // Add default field name '*' if we clear filter bar.
     if ($request == 'search' && '' === (string) ArrayHelper::getValue($currentState, 'field')) {
         $currentState['field'] = '*';
     }
     // Now compare them, and set start to 0 if there has any differences.
     if ($newState && $currentState != $newState && $resetPage) {
         $input->set('limitstart', 0);
     }
     // Save the new value only if it is set in this request.
     if ($newState !== null) {
         $app->setUserState($key, $newState);
     } else {
         $newState = $currentState;
     }
     return $newState;
 }