[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/form_builder/includes/ -> form_builder.cache.inc (source)

   1  <?php
   2  
   3  /**
   4   * @file form_builder.cache.inc
   5   * Functions for temporary storage of form builder structures while editing.
   6   */
   7  
   8  /**
   9   * Load a form configuration cache.
  10   *
  11   * @param $form_type
  12   *   The type of form being edited.
  13   * @param $form_id
  14   *   The unique identifier for the form type.
  15   * @return
  16   *   A FAPI array if a cache entry was found. Boolean FALSE if an entry does not
  17   *   yet exist. Note that an empty FAPI array may exist, so be sure to use
  18   *   strict comparison (===) when checking return values.
  19   */
  20  function form_builder_cache_load($form_type, $form_id, $sid = NULL, $reset = FALSE) {
  21    static $data;
  22    $sid = isset($sid) ? $sid : session_id();
  23  
  24    if ($reset) {
  25      $data = NULL;
  26    }
  27  
  28    if (!isset($data) && isset($form_type) && isset($form_id)) {
  29      $data = db_result(db_query("SELECT data FROM {form_builder_cache} WHERE type = '%s' AND form_id = '%s' AND sid = '%s'", $form_type, $form_id, $sid));
  30  
  31      if ($data) {
  32        $data = unserialize($data);
  33      }
  34    }
  35  
  36    return $data;
  37  }
  38  
  39  /**
  40   * Save a form builder cache based on the form structure.
  41   */
  42  function form_builder_cache_save($form_type, $form_id, $form, $sid = NULL) {
  43    $sid = isset($sid) ? $sid : session_id();
  44  
  45    if (form_builder_cache_load($form_type, $form_id, $sid) === FALSE) {
  46      $result = db_query("INSERT INTO {form_builder_cache} (sid, type, form_id, updated, data) VALUES ('%s', '%s', '%s', %d, '%s')", $sid, $form_type, $form_id, time(), serialize($form));
  47    }
  48    else {
  49      $result = db_query("UPDATE {form_builder_cache} SET data = '%s', updated = %d WHERE type = '%s' AND form_id = '%s' AND sid = '%s'", serialize($form), time(), $form_type, $form_id, $sid);
  50    }
  51  
  52    // Ensure caches are fresh for any retrievals made this request.
  53    form_builder_cache_load(NULL, NULL, NULL, TRUE);
  54  
  55    return $result;
  56  }
  57  
  58  function form_builder_cache_delete($form_type, $form_id, $sid = NULL) {
  59    $sid = isset($sid) ? $sid : session_id();
  60  
  61    return db_query("DELETE FROM {form_builder_cache} WHERE type = '%s' AND form_id = '%s' AND sid = '%s'", $form_type, $form_id, $sid);
  62  }
  63  
  64  /**
  65   * Delete outdated cache entries.
  66   *
  67   * @param $expire_threshold
  68   *   The maximum amount of time allowed for entries to be kept, in seconds.
  69   */
  70  function form_builder_cache_purge($expire_threshold = NULL) {
  71    $expire_threshold = isset($expire_threshold) ? $expire_threshold : ini_get('session.cache_expire');
  72  
  73    return db_query("DELETE FROM {form_builder_cache} WHERE updated < %d", time() - $expire_threshold);
  74  }
  75  
  76  /**
  77   * Compare the cached form with the original and return all changed elements.
  78   *
  79   * @return
  80   *   An array of elements keyed by the element_id of the changed element. Each
  81   *   value contains an array of the 'original' and 'modified' elements.
  82   */
  83  function form_builder_cache_difference($form_type, $form_id, $exclude_weight = TRUE) {
  84    if (($modified_form = form_builder_cache_load($form_type, $form_id)) && $modified_form !== FALSE) {
  85      $original_form = form_builder_load_form($form_type, $form_id);
  86      $original_ids = form_builder_get_element_ids($original_form);
  87      $modified_ids = form_builder_get_element_ids($modified_form);
  88  
  89      // Find the union of IDs in both arrays.
  90      $element_ids = array_keys(array_flip($original_ids) + array_flip($modified_ids));
  91  
  92      // Build a list of all elements that have changed.
  93      $differences = array();
  94      foreach ($element_ids as $element_id) {
  95        $original = form_builder_get_element($original_form, $element_id);
  96        $modified = form_builder_get_element($modified_form, $element_id);
  97  
  98        if ($exclude_weight) {
  99          if (isset($original['#weight'])) {
 100            unset($original['#weight']);
 101          }
 102          if (isset($modified['#weight'])) {
 103            unset($modified['#weight']);
 104          }
 105        }
 106  
 107        if ($original != $modified) {
 108          $differences[$element_id] = array(
 109            'original' => $original,
 110            'modified' => $modified,
 111          );
 112        }
 113      }
 114  
 115      return $differences;
 116    }
 117    return FALSE;
 118  }
 119  
 120  /**
 121   * Retrieve a single field from a form cache..
 122   */
 123  function form_builder_cache_field_load($form_type, $form_id, $element_id, $sid = NULL, $reset = FALSE) {
 124    $sid = isset($sid) ? $sid : session_id();
 125  
 126    $form = form_builder_cache_load($form_type, $form_id, $sid, $reset);
 127    return form_builder_get_element($form, $element_id);
 128  }
 129  
 130  /**
 131   * Add or update a single field in a form builder cache.
 132   */
 133  function form_builder_cache_field_save($form_type, $form_id, $element, $sid = NULL) {
 134    $sid = isset($sid) ? $sid : session_id();
 135  
 136    $form = form_builder_cache_load($form_type, $form_id, $sid);
 137    form_builder_set_element($form, $element);
 138    form_builder_cache_save($form_type, $form_id, $form, $sid);
 139  }
 140  
 141  /**
 142   * Remove a single field from a form builder cache.
 143   */
 144  function form_builder_cache_field_delete($form_type, $form_id, $element_id, $sid = NULL) {
 145    $sid = isset($sid) ? $sid : session_id();
 146  
 147    $form = form_builder_cache_load($form_type, $form_id, $sid);
 148    form_builder_unset_element($form, $element_id);
 149    form_builder_cache_save($form_type, $form_id, $form, $sid);
 150  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7