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