00001 <?php
00013 class RecordModel_Base
00014 {
00015 var $db = null;
00016 var $web = null;
00017 var $cache = null;
00018 var $depends = null;
00019 var $plugins = null;
00020
00034 var $context = null;
00035
00036
00037
00038
00039
00043 var $table = null;
00047 var $pk = 'id';
00055 var $enable_cache = false;
00056
00070 var $submodels = null;
00071
00103 var $files = null;
00104
00109 function RecordModel_Base()
00110 {
00111 $this->db =& Registry::get('pronto:db:main');
00112 $this->cache =& Registry::get('pronto:cache');
00113 $this->web =& Registry::get('pronto:web');
00114 $this->validator =& Registry::get('pronto:validator');
00115
00116
00117
00118 if(is_null($this->table) || empty($this->table)) {
00119 $this->table = strtolower(substr(get_class($this),1)).'s';
00120 }
00121
00122
00123 $this->plugins =& Registry::get('pronto:plugins');
00124
00125 if(method_exists($this, '__init__')) {
00126 $this->__init__();
00127 }
00128 }
00129
00135 function depend($model)
00136 {
00137 foreach(func_get_args() as $name) {
00138 if(isset($this->depends->$name)) continue;
00139 $this->depends->$name =& Factory::model($name);
00140 }
00141 }
00142
00147 function &import_plugin($name)
00148 {
00149 foreach(func_get_args() as $name) {
00150 $class =& Factory::plugin($name, 'page');
00151 if($class === false) {
00152 trigger_error("Plugin $name does not exist");
00153 die;
00154 }
00155 }
00156
00157 $this->plugins =& Registry::get('pronto:plugins');
00158 if($class) return $class;
00159 }
00160
00167 function purify($data)
00168 {
00169 require_once(DIR_FS_PRONTO.DS.'extlib'.DS.'safehtml'.DS.'safehtml.php');
00170 foreach($data as $k=>$v) {
00171 if(is_array($v)) {
00172
00173 $data[$k] = RecordModel::purify($v);
00174 } else if(class_exists('safehtml')) {
00175 $purifier = new safehtml();
00176 $data[$k] = $purifier->parse($v);
00177 }
00178 }
00179 return $data;
00180 }
00181
00188 function invalidate($id)
00189 {
00190 if(!is_array($id)) $id = array($id);
00191 foreach($id as $i) {
00192 if($this->enable_cache && $this->cache) {
00193 $key = 'model_'.get_class($this)."_{$this->pk}_$i";
00194 $this->cache->delete($key);
00195 }
00196 }
00197 }
00198
00209 function find()
00210 {
00211 $args = func_get_args();
00212 $q = array_shift($args);
00213 return new RecordSelector($q, $args, $this);
00214 }
00215
00224 function find_arr($q, $args=array())
00225 {
00226 return new RecordSelector($q, $args, $this);
00227 }
00228
00229
00230
00231
00232
00233
00234
00235
00236
00244 function fetch($id)
00245 {
00246 $q = $this->db->query("SELECT * FROM {$this->table} WHERE \"{$this->pk}\"='%s' LIMIT 1", array($id));
00247 return $this->db->get_item($q);
00248 }
00249
00256 function store($data)
00257 {
00258 $data = $this->sanitize($data);
00259 if(isset($data[$this->pk])) {
00260 $this->db->update_row($this->table, $data, $this->pk."='%s'", array($data[$this->pk]));
00261 return $data[$this->pk];
00262 } else {
00263 return $this->db->insert_row($this->table, $data);
00264 }
00265 }
00266
00272 function erase($id)
00273 {
00274 $q = $this->db->query("DELETE FROM {$this->table} WHERE \"{$this->pk}\"='%s' LIMIT 1", array($id));
00275 return $this->db->execute($q);
00276 }
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00296 function create()
00297 {
00298 return $this->create_record();
00299 }
00300
00309 function load($id)
00310 {
00311 if($this->enable_cache && $this->cache) {
00312 $key = 'model_'.get_class($this)."_{$this->pk}_$id";
00313 $rec =& $this->cache->get($key);
00314 if(!$rec) {
00315 $rec = $this->load_record($id);
00316 $this->cache->set($key, $rec);
00317 }
00318 return $rec;
00319 }
00320 return $this->load_record($id);
00321 }
00322
00330 function load_all($ids, $ignore_empty=true)
00331 {
00332 $data = array();
00333 foreach($ids as $id) {
00334 $rec = $this->load($id);
00335 if($rec || !$ignore_empty) $data[] = $rec;
00336 }
00337 return $data;
00338 }
00339
00345 function save($data)
00346 {
00347 if($data[$this->pk]) $this->invalidate($data[$this->pk]);
00348 return $this->save_record($data);
00349 }
00350
00356 function delete($id)
00357 {
00358 $this->invalidate($id);
00359 return $this->delete_record($id);
00360 }
00361
00362
00363
00364
00365
00366
00376 function create_record()
00377 {
00378 return array();
00379 }
00380
00385 function load_record($id)
00386 {
00387 $data = $this->fetch($id);
00388 if(!$data) return false;
00389
00390
00391
00392
00393
00394 if(is_array($this->submodels)) foreach($this->submodels as $dep) {
00395 if(is_array($dep)) {
00396
00397 $mod = $dep[0];
00398 $col = $dep[1];
00399 } else {
00400
00401 $col = "{$dep}_id";
00402 $mod = $dep;
00403 }
00404 $this->depend($mod);
00405 $data[$dep] = $this->depends->$mod->load($data[$col]);
00406 }
00407
00408
00409
00410 if(is_array($this->files)) foreach($this->files as $k=>$f) {
00411 $fn = $f['filename'];
00412 foreach($data as $dk=>$dv) {
00413 if(is_array($dv)) continue;
00414 $fn = str_replace("<$dk>", $dv, $fn);
00415 }
00416 if(!file_exists($f['fileroot'].DS.$fn)) continue;
00417 $data[$k] = $f['webroot'].'/'.$fn;
00418 };
00419
00420 return $data;
00421 }
00422
00430 function save_record($data)
00431 {
00432 return $this->store($data);
00433 }
00434
00441 function delete_record($id)
00442 {
00443 return $this->erase($id);
00444 }
00445
00452 function validate($data)
00453 {
00454 return array();
00455 }
00456
00466 function sanitize($data)
00467 {
00468 $data = $this->purify($data);
00469 return $data;
00470 }
00471
00472
00479 function enum_schema()
00480 {
00481 return array(
00482 'from' => $this->table,
00483 'exprs' => array(),
00484 'gexprs' => array(),
00485 'select' => '*',
00486 'where' => '',
00487 'group_by' => '',
00488 'having' => '',
00489 'order' => "{$this->pk} ASC",
00490 'limit' => 50
00491 );
00492 }
00493 }
00494
00495 ?>