00001 <?php
00015 class Model_Base
00016 {
00017 var $db = null;
00018 var $web = null;
00019 var $cache = null;
00020 var $depends = null;
00021 var $plugins = null;
00022
00036 var $context = null;
00037
00038
00039
00040
00041
00045 var $table = null;
00049 var $pk = 'id';
00053 var $default_sort = 'id';
00057 var $per_page = 50;
00065 var $enable_cache = false;
00066
00098 var $files = null;
00099
00104 function Model_Base()
00105 {
00106 $this->db =& Registry::get('pronto:db:main');
00107 $this->cache =& Registry::get('pronto:cache');
00108 $this->web =& Registry::get('pronto:web');
00109 $this->validator =& Registry::get('pronto:validator');
00110
00111
00112
00113 if(is_null($this->table) || empty($this->table)) {
00114 $this->table = strtolower(substr(get_class($this),1)).'s';
00115 }
00116
00117
00118 $this->plugins =& Registry::get('pronto:plugins');
00119
00120 if(method_exists($this, '__init__')) {
00121 $this->__init__();
00122 }
00123 }
00124
00130 function depend($model)
00131 {
00132 foreach(func_get_args() as $name) {
00133 if(isset($this->depends->$name)) continue;
00134 $this->depends->$name =& Factory::model($name);
00135 }
00136 }
00137
00142 function &import_plugin($name)
00143 {
00144 foreach(func_get_args() as $name) {
00145 $class =& Factory::plugin($name, 'page');
00146 if($class === false) {
00147 trigger_error("Plugin $name does not exist");
00148 die;
00149 }
00150 }
00151
00152 $this->plugins =& Registry::get('pronto:plugins');
00153 if($class) return $class;
00154 }
00155
00162 function validate_for_insert($data)
00163 {
00164 return false;
00165 }
00166
00173 function validate_for_update($data)
00174 {
00175 return false;
00176 }
00177
00185 function validate($data, $is_update=false)
00186 {
00187 if($is_update) {
00188 return $this->validate_for_update($data);
00189 }
00190 return $this->validate_for_insert($data);
00191 }
00192
00199 function purify($data)
00200 {
00201 require_once(DIR_FS_PRONTO.DS.'extlib'.DS.'safehtml'.DS.'safehtml.php');
00202 foreach($data as $k=>$v) {
00203 if(is_array($v)) {
00204
00205 $data[$k] = Model::purify($v);
00206 } else if(class_exists('safehtml')) {
00207 $purifier = new safehtml();
00208 $data[$k] = $purifier->parse($v);
00209 }
00210 }
00211 return $data;
00212 }
00213
00221 function sanitize($data)
00222 {
00223 $data = $this->purify($data);
00224 return $data;
00225 }
00226
00234 function create()
00235 {
00236 return array();
00237 }
00238
00247 function get($id)
00248 {
00249 if($this->enable_cache && $this->cache) {
00250 $key = 'model_'.get_class($this)."_{$this->pk}_$id";
00251 $item =& $this->cache->get($key);
00252 if(!$item) {
00253 $item = $this->get_record($id);
00254 $this->cache->set($key, $item);
00255 }
00256 return $item;
00257 }
00258 return $this->get_record($id);
00259 }
00260
00267 function invalidate($id)
00268 {
00269 if(!is_array($id)) $id = array($id);
00270 foreach($id as $i) {
00271 if($this->enable_cache && $this->cache) {
00272 $key = 'model_'.get_class($this)."_{$this->pk}_$i";
00273 $this->cache->delete($key);
00274 }
00275 }
00276 }
00277
00286 function get_record($id)
00287 {
00288 return $this->get_bare($id);
00289 }
00290
00300 function get_bare($id)
00301 {
00302 return $this->db->get_item("SELECT * FROM {$this->table} WHERE \"{$this->pk}\"=%i LIMIT 1", array($id));
00303 }
00304
00312 function get_multi($ids, $ignore_empty=true)
00313 {
00314 $data = array();
00315 foreach($ids as $id) {
00316 $rec = $this->get($id);
00317 if($rec || !$ignore_empty) $data[] = $rec;
00318 }
00319 return $data;
00320 }
00321
00329 function get_value($id, $column)
00330 {
00331 return $this->db->get_value("SELECT \"$column\" FROM {$this->table} WHERE \"{$this->pk}\"=%i LIMIT 1", array($id));
00332 }
00333
00342 function get_by($column, $value)
00343 {
00344 if(!is_array($column)) $column = array($column);
00345 if(!is_array($value)) $value = array($value);
00346
00347 $where = array();
00348 foreach($column as $c) $where[] = "\"{$c}\"='%s'";
00349 $where = implode(' AND ', $where);
00350
00351 $id = $this->db->get_value("SELECT \"{$this->pk}\" FROM {$this->table} WHERE $where LIMIT 1", $value);
00352 return $this->get($id);
00353 }
00354
00363 function get_all_by($column, $value)
00364 {
00365 if(!is_array($column)) $column = array($column);
00366 if(!is_array($value)) $value = array($value);
00367
00368 $where = array();
00369 foreach($column as $c) $where[] = "\"{$c}\"='%s'";
00370 $where = implode(' AND ', $where);
00371
00372 $ids = $this->db->get_values("SELECT \"{$this->pk}\" FROM {$this->table} WHERE $where ORDER BY {$this->default_sort}", $value);
00373 return $this->get_multi($ids);
00374 }
00375
00381 function get_all()
00382 {
00383 $ids = $this->db->get_values("SELECT \"{$this->pk}\" FROM {$this->table} ORDER BY {$this->default_sort}");
00384 return $this->get_multi($ids);
00385 }
00386
00393 function get_or_404($id)
00394 {
00395 $data = $this->get($id);
00396 if($data === false) {
00397 $this->web->notfound();
00398 }
00399 return $data;
00400 }
00401
00407 function delete($id)
00408 {
00409 $this->invalidate($id);
00410 return $this->db->execute("DELETE FROM {$this->table} WHERE \"{$this->pk}\"=%i", array($id));
00411 }
00412
00419 function insert($data)
00420 {
00421 $data = $this->sanitize($data);
00422 return $this->db->insert_row($this->table, $data);
00423 }
00424
00430 function update($data)
00431 {
00432 $this->invalidate($data['id']);
00433 $data = $this->sanitize($data);
00434 return $this->db->update_row($this->table, $data, $this->pk."='%s'", array($data[$this->pk]));
00435 }
00436
00442 function list_params()
00443 {
00444 return array(
00445 'from' => $this->table,
00446 'exprs' => array(),
00447 'gexprs' => array(),
00448 'select' => '*',
00449 'where' => '',
00450 'group_by' => '',
00451 'having' => '',
00452 'order' => $this->default_sort,
00453 'limit' => $this->per_page,
00454 );
00455 }
00456
00463 function __call($name, $args)
00464 {
00465 switch(true) {
00466 case substr($name, 0, 4) == 'get_':
00467 $field = substr($name, 4);
00468 return $this->db->get_value("SELECT \"$field\" FROM {$this->table} WHERE \"{$this->pk}\"='%s'", array($args[0]));
00469 case substr($name, 0, 4) == 'set_':
00470 $field = substr($name, 4);
00471 $this->db->execute("UPDATE {$this->table} SET \"$field\"='%s' WHERE \"{$this->pk}\"='%s'", array($args[1],$args[0]));
00472 $this->invalidate($args[0]);
00473 return true;
00474 }
00475 trigger_error("Method does not exist: $name");
00476 }
00477
00484 function save($data)
00485 {
00486 if($data['id']) {
00487 return $this->update($data);
00488 } else {
00489 return $this->insert($data);
00490 }
00491 }
00492
00493 function load($id)
00494 {
00495 return $this->get($id);
00496 }
00497
00498 function create_record()
00499 {
00500 return $this->create();
00501 }
00502
00503 function enum_schema()
00504 {
00505 return $this->list_params();
00506 }
00507 }
00508
00509 ?>