[ Index ] |
|
Code source de SPIP Agora 1.4 |
1 <?php 2 /***************************************************** 3 * This file is part of Agora, web based content management system. 4 * 5 * Agora is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation; version 2 of the License. 8 * 9 * Agora is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details (file "COPYING"). 13 * 14 * Copyright © Arnaud Martin, Antoine Pitrou et Philippe Rivière. 15 * List of authors detailed in "copyright_fr.html" file. 16 * E-mail : agora@sig.premier-ministre.gouv.fr 17 * Web site : http://www.agora.gouv.fr 18 *****************************************************/ 19 // Ce fichier ne sera execute qu'une fois 20 if (defined("_ECRIRE_INC_OBJET_BASE")) 21 return; 22 23 define("_ECRIRE_INC_OBJET_BASE", "1"); 24 25 class _Abstract { 26 function abstract_error ($str) { 27 die ("<h4>" . $str . "<br />" . _T('info_contact_developpeur'). "</h4>"); 28 } 29 30 function fun_abstract () { 31 $this->abstract_error(_T('avis_erreur_fonction_contexte')); 32 } 33 34 function _Abstract () { 35 $this->fun_abstract(); 36 } 37 } 38 39 class ObjectCacheInstance extends _Abstract { 40 // Variable values (array) 41 var $fast_vars; 42 var $slow_vars; 43 44 // Variable status 45 var $fast_vars_loaded = false; 46 var $slow_vars_loaded = false; 47 48 // Is modified ? 49 var $dirty = false; 50 51 function ObjectCacheInstance () { 52 $this->fast_vars = array(); 53 $this->slow_vars = array(); 54 } 55 } 56 57 class _ObjectFactory extends _Abstract { 58 // Factory ID 59 var $id_factory; 60 61 // Object class name (for instantiation) 62 var $object_class; 63 64 // SQL table name/pattern 65 var $sql_table; 66 var $sql_id; 67 68 // Plain array 69 var $fast_vars_list, $nb_fast_vars; 70 var $slow_vars_list, $nb_slow_vars; 71 72 // Associative array 73 var $fast_vars_array; 74 var $slow_vars_array; 75 76 // SQL field names 77 var $fast_vars_sql; 78 var $slow_vars_sql; 79 80 // Object cache 81 var $cache; 82 83 // --------------------------------------------------------- 84 85 // 86 // Init factory helper variables and constants 87 // 88 function init_factory ($id_factory) { 89 $this->id_factory = $id_factory; 90 91 // Store different representations of fast vars 92 if (is_array($this->fast_vars_list)) { 93 reset ($this->fast_vars_list); 94 95 while (list($key, $val) = each($this->fast_vars_list)) { 96 $this->fast_vars_array[$val] = $val; 97 $this->fast_vars_sql[] = $this->sql_table . '.' . $val; 98 } 99 100 $this->fast_vars_sql = join(', ', $this->fast_vars_sql); 101 $this->nb_fast_vars = count($this->fast_vars_list); 102 } 103 else 104 $this->nb_fast_vars = 0; 105 106 // Store different representations of slow vars 107 if (is_array($this->slow_vars_list)) { 108 reset ($this->slow_vars_list); 109 110 while (list($key, $val) = each($this->slow_vars_list)) { 111 $this->slow_vars_array[$val] = $val; 112 $this->slow_vars_sql[] = $this->sql_table . '.' . $val; 113 } 114 115 $this->slow_vars_sql = join(', ', $this->slow_vars_sql); 116 $this->nb_slow_vars = count($this->slow_vars_list); 117 } 118 else 119 $this->nb_slow_vars = 0; 120 121 // Default value for object id in database 122 if (!$this->sql_id) { 123 $this->sql_id = 'id_' . strtolower($this->object_class); 124 } 125 } 126 127 // 128 // Object management methods 129 // 130 131 function new_object ($id) { 132 $this->fun_abstract(); 133 } 134 135 function create_object_cache_instance ($id) { 136 if (!($g = $this->cache[$id])) { 137 $g = '_' . $this->object_class . '_' . $id; 138 $GLOBALS[$g] = new ObjectCacheInstance; 139 $this->cache[$id] = $g; 140 } 141 142 return $g; 143 } 144 145 // Create a new alias for an object 146 // (aliases are the only way by which user code sees an object) 147 function create_object_alias ($id) { 148 $class = $this->object_class; 149 $alias = new $class; 150 $alias->init_object($this->id_factory, $id); 151 return $alias; 152 } 153 154 // Get field of an object (by ID) 155 function get_object_field ($id, $name) { 156 $g = $this->cache[$id]; 157 158 if ($v = $this->fast_vars_array[$name]) { 159 if (!$GLOBALS[$g]->fast_vars_loaded) 160 $this->load_object_id($id, true); 161 return $GLOBALS[$g]->fast_vars[$v]; 162 } 163 else if ($v = $this->slow_vars_array[$name]) { 164 if (!$GLOBALS[$g]->slow_vars_loaded) 165 $this->load_object_id($id, false); 166 return $GLOBALS[$g]->slow_vars[$v]; 167 } 168 else { 169 $this->abstract_error( 170 _T('avis_champ_incorrect_type_objet', array('name' => $name, 'type' => $this->object_class))); 171 } 172 } 173 174 // Set field of an object (by ID) 175 function set_object_field ($id, $name, $value) { 176 $g = $this->cache[$id]; 177 178 if ($v = $this->fast_vars_array[$name]) { 179 if (!$GLOBALS[$g]->fast_vars_loaded) 180 $this->load_object_id($id, true); 181 182 $GLOBALS[$g]->fast_vars[$v] = $value; 183 $GLOBALS[$g]->dirty = true; 184 } 185 else if ($v = $this->slow_vars_array[$name]) { 186 if (!$GLOBALS[$g]->slow_vars_loaded) 187 $this->load_object_id($id, false); 188 189 $GLOBALS[$g]->slow_vars[$v] = $value; 190 $GLOBALS[$g]->dirty = true; 191 } 192 else { 193 $this->abstract_error(_T('avis_champ_incorrect_type_objet', array('name' => $name)). $this->object_class); 194 } 195 } 196 197 // 198 // Load object by SQL query 199 // 200 function load_object_sql ($query, $fast, $multiple = false) { 201 $cols = $this->fast_vars_sql; 202 203 if (!$fast && $this->slow_vars_sql) { 204 if ($cols) 205 $cols .= ', '; 206 $cols .= $this->slow_vars_sql; 207 } 208 209 // Replace generic names by actual ones 210 $query = ereg_replace('#cols', $cols, $query); 211 $query = ereg_replace('#table', $this->sql_table, $query); 212 $query = ereg_replace('#id', $this->sql_table . '.' . $this->sql_id, $query); 213 echo "<br />transformation --> $query<br /><br /><br />"; 214 $result = spip_query($query); 215 216 // If multiple results expected, create a result array 217 if ($multiple) 218 $r = array(); 219 220 if ($result) 221 while ($row = spip_fetch_array($result)) { 222 $id = $row[$this->sql_id]; 223 $g = $this->create_object_cache_instance($id); 224 225 // Read fast vars 226 for ($i = 0; $i < $this->nb_fast_vars; $i++) { 227 $var = $this->fast_vars_list[$i]; 228 $GLOBALS[$g]->fast_vars[$var] = $row[$var]; 229 } 230 231 $GLOBALS[$g]->fast_vars_loaded = true; 232 233 if (!$fast) { 234 // Read slow vars 235 for ($i = 0; $i < $this->nb_slow_vars; $i++) { 236 $var = $this->slow_vars_list[$i]; 237 $GLOBALS[$g]->slow_vars[$var] = $row[$var]; 238 } 239 $GLOBALS[$g]->slow_vars_loaded = true; 240 } 241 if ($multiple) 242 $r[$id] = $id; 243 else 244 break; 245 } 246 247 if ($multiple) 248 return $r; 249 } 250 251 // 252 // Load object by ID 253 // 254 function load_object_id ($id, $fast = true) { 255 $query = "SELECT #cols FROM #table WHERE #id=$id"; 256 echo "<br />transformation --> $query<br /><br /><br />"; 257 $this->load_object_sql($query, $fast); 258 } 259 260 // 261 // Fetch object only if not in cache 262 // 263 function fetch_object_id ($id, $fast = true) { 264 if ($g = $this->cache[$id]) { 265 if (!$GLOBALS[$g]->dirty) 266 return; 267 } 268 else { 269 $g = $this->create_object_cache_instance($id); 270 } 271 272 $this->load_object_id($id, $fast); 273 } 274 275 // 276 // Create new object 277 // 278 function create_object () { 279 static $new_id = 0; 280 $id = 'new_' . (++$new_id); 281 $g = $this->create_object_cache_instance($id); 282 $GLOBALS[$g]->dirty = true; 283 $GLOBALS[$g]->fast_vars_loaded = true; 284 $GLOBALS[$g]->slow_vars_loaded = true; 285 $this->new_object($id); 286 return $id; 287 } 288 289 // 290 // Main load function : fetch object by generic criterium 291 // 292 function fetch_object ($critere, $fast = true) { 293 echo "<br />--> $critere<br /><br /><br />"; 294 295 if ($critere == 'new') { 296 // create object 297 $id = $this->create_object(); 298 } 299 else if ($critere > 0) { 300 // get object by id 301 $id = intval($critere); 302 $this->fetch_object_id($id, $fast); 303 } 304 else { 305 // get object list by sql 306 return $this->load_object_sql($critere, $fast, true); 307 } 308 309 return $this->create_object_alias($id); 310 } 311 312 // 313 // Main save function : update object by ID 314 // 315 function update_object ($id) { 316 $g = $this->cache[$id]; 317 318 if ($GLOBALS[$g]->dirty) { 319 // generate INSERT query (penser au addslashes) 320 } 321 } 322 } 323 324 class _Object extends _Abstract { 325 // Factory ID 326 var $id_factory; 327 328 // Object ID 329 var $id = 0; 330 331 function init_object ($id_factory, $id = 0) { 332 $this->id_factory = $id_factory; 333 334 if ($id) 335 $this->id = $id; 336 } 337 338 function get ($var) { 339 return $GLOBALS[$this->id_factory]->get_object_field($this->id, $var); 340 } 341 342 function set ($var, $value) { 343 return $GLOBALS[$this->id_factory]->set_object_field($this->id, $var, $value); 344 } 345 346 function commit () { 347 return $GLOBALS[$this->id_factory]->update_object($this->id); 348 } 349 } 350 351 // 352 // Create a factory of a given type, and register it 353 // 354 355 function add_factory ($type) { 356 global $factories; 357 $class = ucfirst($type). 'Factory'; 358 $id_factory = $type . '_factory'; 359 $GLOBALS[$id_factory] = new $class; 360 $GLOBALS[$id_factory]->init_factory($id_factory); 361 return $id_factory; 362 } 363 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sat Feb 24 14:40:03 2007 | par Balluche grâce à PHPXref 0.7 |