[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/views/includes/ -> form.inc (source)

   1  <?php
   2  // $Id: form.inc,v 1.11.2.1 2010/01/05 01:33:46 merlinofchaos Exp $
   3  
   4  /**
   5   * @file form.inc
   6   * Views' replacements for Drupal's form functions.
   7   *
   8   */
   9  function _drupal_build_form($form_id, &$form_state) {
  10    // Ensure that we have some defaults.
  11  
  12    // These are defaults only; if already set they will not be overridden.
  13    $form_state += array('storage' => NULL, 'submitted' => FALSE, 'input' => $_POST, 'method' => 'post');
  14  
  15    $args = isset($form_state['args']) ? $form_state['args'] : array();
  16    $cacheable = FALSE;
  17  
  18    if (isset($_SESSION['batch_form_state'])) {
  19      // We've been redirected here after a batch processing : the form has
  20      // already been processed, so we grab the post-process $form_state value
  21      // and move on to form display. See _batch_finished() function.
  22      $form_state = $_SESSION['batch_form_state'];
  23      unset($_SESSION['batch_form_state']);
  24    }
  25    else {
  26      // If the incoming $_POST contains a form_build_id, we'll check the
  27      // cache for a copy of the form in question. If it's there, we don't
  28      // have to rebuild the form to proceed. In addition, if there is stored
  29      // form_state data from a previous step, we'll retrieve it so it can
  30      // be passed on to the form processing code.
  31      if (isset($_POST['form_id']) && $_POST['form_id'] == $form_id && !empty($_POST['form_build_id'])) {
  32        $form = form_get_cache($_POST['form_build_id'], $form_state);
  33        if (!empty($form['#no_cache']) || empty($form)) {
  34          unset($form);
  35        }
  36      }
  37  
  38      // If the previous bit of code didn't result in a populated $form
  39      // object, we're hitting the form for the first time and we need
  40      // to build it from scratch.
  41      if (!isset($form)) {
  42        $form_state['post'] = $form_state['input'];
  43        // Use a copy of the function's arguments for manipulation
  44        $args_temp = $args;
  45        $args_temp[0] = &$form_state;
  46        array_unshift($args_temp, $form_id);
  47  
  48        $form = call_user_func_array('drupal_retrieve_form', $args_temp);
  49        $form_build_id = 'form-' . md5(mt_rand());
  50        $form['#build_id'] = $form_build_id;
  51  
  52        if ($form_state['method'] == 'get' && !isset($form['#method'])) {
  53          $form['#method'] = 'get';
  54        }
  55  
  56        drupal_prepare_form($form_id, $form, $form_state);
  57        // Store a copy of the unprocessed form for caching and indicate that it
  58        // is cacheable if #cache will be set.
  59        $original_form = $form;
  60        $cacheable = TRUE;
  61        unset($form_state['post']);
  62      }
  63      $form['#post'] = $form_state['input'];
  64  
  65      // Now that we know we have a form, we'll process it (validating,
  66      // submitting, and handling the results returned by its submission
  67      // handlers. Submit handlers accumulate data in the form_state by
  68      // altering the $form_state variable, which is passed into them by
  69      // reference.
  70      drupal_process_form_new($form_id, $form, $form_state);
  71  
  72      // If we were told not to redirect, but not told to re-render, return
  73      // here.
  74      if (!empty($form_state['executed']) && empty($form_state['rerender'])) {
  75        return;
  76      }
  77  
  78      if ($cacheable && !empty($form['#cache']) && empty($form['#no_cache'])) {
  79        // Caching is done past drupal_process_form so #process callbacks can
  80        // set #cache. By not sending the form state, we avoid storing
  81        // $form_state['storage'].
  82        form_set_cache($form_build_id, $original_form, NULL);
  83      }
  84    }
  85  
  86    // Most simple, single-step forms will be finished by this point --
  87    // drupal_process_form() usually redirects to another page (or to
  88    // a 'fresh' copy of the form) once processing is complete. If one
  89    // of the form's handlers has set $form_state['redirect'] to FALSE,
  90    // the form will simply be re-rendered with the values still in its
  91    // fields.
  92    //
  93    // If $form_state['storage'] or $form_state['rebuild'] have been
  94    // set by any submit or validate handlers, however, we know that
  95    // we're in a complex multi-part process of some sort and the form's
  96    // workflow is NOT complete. We need to construct a fresh copy of
  97    // the form, passing in the latest $form_state in addition to any
  98    // other variables passed into drupal_get_form().
  99  
 100    if (!empty($form_state['rebuild']) || !empty($form_state['storage'])) {
 101      $form = drupal_rebuild_form_new($form_id, $form_state, $args);
 102    }
 103  
 104    // If we haven't redirected to a new location by now, we want to
 105    // render whatever form array is currently in hand.
 106    return drupal_render_form($form_id, $form);
 107  }
 108  
 109  /**
 110   * Views' replacement of drupal_rebuild_form.
 111   *
 112   * This change merely respects a form's wishes not to be cached.
 113   */
 114  function drupal_rebuild_form_new($form_id, &$form_state, $args, $form_build_id = NULL) {
 115    // Remove the first argument. This is $form_id.when called from
 116    // drupal_get_form and the original $form_state when called from some AHAH
 117    // callback. Neither is needed. After that, put in the current state.
 118    $args[0] = &$form_state;
 119    // And the form_id.
 120    array_unshift($args, $form_id);
 121    $form = call_user_func_array('drupal_retrieve_form', $args);
 122  
 123    if (!isset($form_build_id)) {
 124      // We need a new build_id for the new version of the form.
 125      $form_build_id = 'form-' . md5(mt_rand());
 126    }
 127    $form['#build_id'] = $form_build_id;
 128    drupal_prepare_form($form_id, $form, $form_state);
 129  
 130    if (empty($form['#no_cache'])) {
 131      // Now, we cache the form structure so it can be retrieved later for
 132      // validation. If $form_state['storage'] is populated, we'll also cache
 133      // it so that it can be used to resume complex multi-step processes.
 134      form_set_cache($form_build_id, $form, $form_state);
 135    }
 136  
 137    // Originally this called drupal_process_form, but all that happens there
 138    // is form_builder and then submission; and the rebuilt form is not
 139    // allowed to submit. Therefore, just do this:
 140    $form['#post'] = $form_state['input'];
 141    $form = form_builder($form_id, $form, $form_state);
 142  
 143    return $form;
 144  }
 145  
 146  /**
 147   * Views' replacement for drupal_process_form that accepts commands
 148   * not to redirect, as well as forcing processing of 'get' method forms.
 149   */
 150  function drupal_process_form_new($form_id, &$form, &$form_state) {
 151    // submitting, and handling the results returned by its submission
 152    // handlers. Submit handlers accumulate data in the form_state by
 153    // altering the $form_state variable, which is passed into them by
 154    // reference.
 155    $form_state['values'] = array();
 156  
 157    // With $_GET, these forms are always submitted.
 158    if ($form_state['method'] == 'get') {
 159      if (!isset($form['#post']['form_build_id'])) {
 160        $form['#post']['form_build_id'] = $form['#build_id'];
 161      }
 162      if (!isset($form['#post']['form_id'])) {
 163        $form['#post']['form_id'] = $form_id;
 164      }
 165      if (!isset($form['#post']['form_token']) && isset($form['#token'])) {
 166        $form['#post']['form_token'] = drupal_get_token($form['#token']);
 167      }
 168    }
 169  
 170    $form = form_builder($form_id, $form, $form_state);
 171    // Only process the form if it is programmed or the form_id coming
 172    // from the POST data is set and matches the current form_id.
 173  
 174    if ((!empty($form['#programmed'])) || (!empty($form['#post']) && (isset($form['#post']['form_id']) && ($form['#post']['form_id'] == $form_id)))) {
 175      drupal_validate_form_new($form_id, $form, $form_state);
 176  
 177      // form_clean_id() maintains a cache of element IDs it has seen,
 178      // so it can prevent duplicates. We want to be sure we reset that
 179      // cache when a form is processed, so scenerios that result in
 180      // the form being built behind the scenes and again for the
 181      // browser don't increment all the element IDs needlessly.
 182      form_clean_id(NULL, TRUE);
 183  
 184      if ((!empty($form_state['submitted'])) && !form_get_errors() && empty($form_state['rebuild'])) {
 185        $form_state['redirect'] = NULL;
 186        form_execute_handlers('submit', $form, $form_state);
 187  
 188        // We'll clear out the cached copies of the form and its stored data
 189        // here, as we've finished with them. The in-memory copies are still
 190        // here, though.
 191        if (variable_get('cache', CACHE_DISABLED) == CACHE_DISABLED && !empty($form_state['values']['form_build_id'])) {
 192          cache_clear_all('form_' . $form_state['values']['form_build_id'], 'cache_form');
 193          cache_clear_all('storage_' . $form_state['values']['form_build_id'], 'cache_form');
 194        }
 195  
 196        // If batches were set in the submit handlers, we process them now,
 197        // possibly ending execution. We make sure we do not react to the batch
 198        // that is already being processed (if a batch operation performs a
 199        // drupal_execute).
 200        if ($batch =& batch_get() && !isset($batch['current_set'])) {
 201          // The batch uses its own copies of $form and $form_state for
 202          // late execution of submit handers and post-batch redirection.
 203          $batch['form'] = $form;
 204          $batch['form_state'] = $form_state;
 205          $batch['progressive'] = !$form['#programmed'];
 206          batch_process();
 207          // Execution continues only for programmatic forms.
 208          // For 'regular' forms, we get redirected to the batch processing
 209          // page. Form redirection will be handled in _batch_finished(),
 210          // after the batch is processed.
 211        }
 212  
 213        // If no submit handlers have populated the $form_state['storage']
 214        // bundle, and the $form_state['rebuild'] flag has not been set,
 215        // we're finished and should redirect to a new destination page
 216        // if one has been set (and a fresh, unpopulated copy of the form
 217        // if one hasn't). If the form was called by drupal_execute(),
 218        // however, we'll skip this and let the calling function examine
 219        // the resulting $form_state bundle itself.
 220        if (!$form['#programmed'] && empty($form_state['rebuild']) && empty($form_state['storage'])) {
 221          if (!empty($form_state['no_redirect'])) {
 222            $form_state['executed'] = TRUE;
 223          }
 224          else {
 225            drupal_redirect_form($form, $form_state['redirect']);
 226          }
 227        }
 228      }
 229    }
 230  }
 231  
 232  /**
 233   * The original version of drupal_validate_form does not have an override for
 234   * the static check to only validate a form id once. Unfortunately, we need
 235   * to be able to overridet his.
 236   */
 237  function drupal_validate_form_new($form_id, $form, &$form_state) {
 238    static $validated_forms = array();
 239  
 240    if (isset($validated_forms[$form_id]) && empty($form_state['must_validate'])) {
 241      return;
 242    }
 243  
 244    // If the session token was set by drupal_prepare_form(), ensure that it
 245    // matches the current user's session.
 246    if (isset($form['#token'])) {
 247      if (!drupal_valid_token($form_state['values']['form_token'], $form['#token'])) {
 248        // Setting this error will cause the form to fail validation.
 249        form_set_error('form_token', t('Validation error, please try again. If this error persists, please contact the site administrator.'));
 250      }
 251    }
 252  
 253    _form_validate($form, $form_state, $form_id);
 254    $validated_forms[$form_id] = TRUE;
 255  }
 256  
 257  /**
 258   * Process callback to add dependency to form items.
 259   *
 260   * Usage:
 261   *
 262   * On any form item, add
 263   * - @code '#process' => 'views_process_dependency' @endcode
 264   * - @code '#dependency' => array('id-of-form-without-the-#' => array(list, of, values, that, make, this, gadget, visible)); @endcode
 265   */
 266  function views_process_dependency($element, $edit, &$form_state, &$form) {
 267    static $dependencies;
 268    if (isset($element['#dependency']) && !isset($dependencies[$element['#id']])) {
 269      if (!isset($element['#dependency_count'])) {
 270        $element['#dependency_count'] = 1;
 271      }
 272      if (!empty($form_state['ajax'])) {
 273        $form_state['js settings']['viewsAjax']['formRelationships'][$element['#id']] = array('num' => $element['#dependency_count'], 'values' => $element['#dependency']);
 274      }
 275      else {
 276        views_add_js('dependent');
 277        $options['viewsAjax']['formRelationships'][$element['#id']] = array('num' => $element['#dependency_count'], 'values' => $element['#dependency']);
 278        drupal_add_js($options, 'setting');
 279      }
 280      $dependencies[$element['#id']] = TRUE;
 281    }
 282  
 283    return $element;
 284  }
 285  
 286  /**
 287   * #process callback to see if we need to check_plain() the options.
 288   *
 289   * Since FAPI is inconsistent, the #options are sanitized for you in all cases
 290   * _except_ checkboxes. We have form elements that are sometimes 'select' and
 291   * sometimes 'checkboxes', so we need decide late in the form rendering cycle
 292   * if the options need to be sanitized before they're rendered. This callback
 293   * inspects the type, and if it's still 'checkboxes', does the sanitation.
 294   */
 295  function views_process_check_options($element, $edit, &$form_state, &$form) {
 296    if ($element['#type'] == 'checkboxes' || $element['#type'] == 'checkbox') {
 297      $element['#options'] = array_map('check_plain', $element['#options']);
 298    }
 299    return $element;
 300  }
 301  


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7