BAOBAB
a_co_db_table_base.class.php
Go to the documentation of this file.
1 <?php
2 /***************************************************************************************************************************/
26 defined( 'LGV_ADBTB_CATCHER' ) or die ( 'Cannot Execute Directly' ); // Makes sure that this file is in the correct context.
27 
28 if ( !defined('LGV_DBF_CATCHER') ) {
29  define('LGV_DBF_CATCHER', 1);
30 }
31 
32 require_once(CO_Config::db_classes_class_dir().'/co_security_login.class.php');
33 
34 /***************************************************************************************************************************/
43 abstract class A_CO_DB_Table_Base {
44  protected $_db_object;
45  protected $_id;
46  protected $_batch_mode;
47 
50 
52  var $name;
55  var $context;
56  var $error;
57 
58  /***********************************************************************************************************************/
59  /***********************/
67  protected function _default_setup() {
68  return Array( 'id' => 0,
69  'last_access' => 86400, // Default is first UNIX day.
70  'object_name' => '',
71  'read_security_id' => 0,
72  'write_security_id' => 0,
73  'access_class_context' => NULL
74  );
75  }
76 
77  /***********************/
83  protected function _build_parameter_array() {
84  $ret = Array();
85 
86  if ($this instanceof CO_Security_ID) {
87  $this->write_security_id = -1; // These always have a -1
88  $this->read_security_id = $this->id(); // These always have their own ID set as the read ID
89  }
90 
91  $ret['id'] = $this->id();
92  $ret['access_class'] = strval(get_class($this));
93  $ret['last_access'] = strval(date('Y-m-d H:i:s'));
94  $ret['read_security_id'] = intval($this->read_security_id);
95  $ret['write_security_id'] = intval($this->write_security_id);
96  $name = trim(strval($this->name));
97  $ret['object_name'] = $name ? $name : NULL;
98  $ret['access_class_context'] = $this->context ? $this->_badger_serialize($this->context) : NULL; // If we have a context, then we serialize it for the DB.
99 
100  return $ret;
101  }
102 
103  /***********************/
109  protected function _badger_serialize( $in_data
110  ) {
111  return serialize($in_data);
112  }
113 
114  /***********************/
120  protected function _badger_unserialize( $in_data
121  ) {
122  return unserialize($in_data);
123  }
124 
125  /***********************/
133  protected function _write_to_db() {
134  $ret = false;
135 
136  if (isset($this->_db_object)) {
137  $params = $this->_build_parameter_array();
138 
139  if (isset($params) && is_array($params) && count($params)) {
140  $ret = $this->_db_object->write_record($params);
141  $this->error = $this->get_access_object()->error;
142  if ((1 < intval($ret)) && !$this->error) {
143  $this->_id = intval($ret);
144  if ($this instanceof CO_Security_ID) {
145  $this->read_security_id = $this->id(); // These always have their own ID set as the read ID
146  $this->write_security_id = -1; // These always have a -1
147  }
148  }
149  }
150  }
151 
152  return $ret;
153  }
154 
155  /***********************/
161  protected function _seppuku() {
162  $ret = false;
163 
164  if ($this->id() && isset($this->_db_object)) {
165  $ret = $this->_db_object->delete_record($this->id());
166  if ($ret) {
167  $this->_id = 0; // Make sure we have no more ID. If anyone wants to re-use this instance, it needs to become a new record.
168  }
169  }
170 
171  return $ret;
172  }
173 
174  /***********************************************************************************************************************/
175  /***********************/
179  public function __construct( $in_db_object = NULL,
180  $in_db_result = NULL
181  ) {
182  $this->class_description = '';
183  $this->_id = NULL;
184  $this->last_access = time();
185  $this->read_security_id = 0;
186  $this->write_security_id = 0;
187  $this->name = NULL;
188  $this->context = NULL;
189  $this->instance_description = NULL;
190  $this->_db_object = $in_db_object;
191  $this->error = NULL;
192 
193  if ($in_db_object) {
194  if (!$in_db_result) {
195  $in_db_result = $this->_default_setup();
196  }
197 
198  $this->load_from_db($in_db_result);
199  }
200  }
201 
202  /***********************/
206  public function set_batch_mode() {
207  $this->_batch_mode = true;
208  }
209 
210  /***********************/
216  public function clear_batch_mode() {
217  $ret = false;
218 
219  $call_update = $this->_batch_mode;
220  $this->_batch_mode = false;
221  if ($call_update) {
222  $ret = $this->update_db();
223  if (method_exists($this, '_scrub')) {
224  $this->_scrub();
225  }
226  }
227 
228  return $ret;
229  }
230 
231  /***********************/
239  public function load_from_db( $in_db_result
240  ) {
241  $ret = false;
242  $this->last_access = max(86400, time()); // Just in case of badly-set clocks in the server.
243 
244  if (isset($this->_db_object) && isset($in_db_result) && isset($in_db_result['id']) && intval($in_db_result['id'])) {
245  $ret = true;
246  $this->_id = intval($in_db_result['id']);
247 
248  if (isset($in_db_result['last_access'])) {
249  $date_from_db = date_create_from_format('Y-m-d H:i:s', $in_db_result['last_access']);
250  $timestamp = date_timestamp_get($date_from_db);
251  $this->last_access = max(86400, $timestamp);
252  }
253 
254  if (isset($in_db_result['read_security_id']) && intval($in_db_result['read_security_id'])) {
255  $this->read_security_id = intval($in_db_result['read_security_id']);
256  }
257 
258  if ($this instanceof CO_Security_ID) {
259  $this->write_security_id = -1; // These always have a -1
260  if ($this->id()) {
261  $this->read_security_id = $this->id(); // These always have their own ID set as the read ID
262  }
263  } else {
264  if (isset($in_db_result['write_security_id'])) {
265  $this->write_security_id = intval($in_db_result['write_security_id']);
266  } else {
267  $this->write_security_id = $this->read_security_id ? -1 : intval($this->write_security_id); // Writing is completely blocked if we have read security, but no write security specified.
268  }
269  }
270 
271  if (isset($in_db_result['object_name'])) {
272  $this->name = strval($in_db_result['object_name']);
273  }
274 
275  if (isset($in_db_result['access_class_context'])) {
276  $serialized_context = trim(strval($in_db_result['access_class_context']));
277  if (isset($serialized_context) && $serialized_context) {
278  $serialized_context = stripslashes($serialized_context);
279  $temp_context = $this->_badger_unserialize($serialized_context);
280 
281  if ($temp_context) {
282  $this->context = $temp_context;
283  }
284  }
285  }
286  }
287 
288  $this->class_description = 'Abstract Base Class for Records -Should never be instantiated.';
289 
290  return $ret;
291  }
292 
293  /***********************/
299  public function id() {
300  return $this->_id;
301  }
302 
303  /***********************/
309  public function lock() {
310  return $this->_db_object->lock_record($this->id());
311  }
312 
313  /***********************/
317  public function locked() {
318  return $this->read_security_id == -2;
319  }
320 
321  /***********************/
328  $this->_id = 0;
329  }
330 
331  /***********************/
335  public function user_can_read() {
336  $ret = false;
337 
338  $ids = $this->get_access_object()->get_security_ids();
339 
340  $my_read_item = intval($this->read_security_id);
341  $my_write_item = intval($this->write_security_id);
342 
343  if ((0 == $my_read_item) || $this->get_access_object()->god_mode()) {
344  $ret = true;
345  } else {
346  if (isset($ids) && is_array($ids) && count($ids)) {
347  $ret = in_array($my_read_item, $ids) || in_array($my_write_item, $ids);
348  }
349  }
350 
351  if (!$ret && $this->get_access_object()->get_login_id()) {
352  $ret = (1 == $my_read_item); // Logged-in users can read 1s.
353  }
354 
355  return $ret;
356  }
357 
358  /***********************/
362  public function user_can_write() {
363  $ret = false;
364 
365  $ids = $this->get_access_object()->get_security_ids();
366 
367  $my_write_item = intval($this->write_security_id);
368  // We can never edit unless we are logged in.
369  if (((isset($ids) && is_array($ids) && count($ids)) && (0 == $my_write_item)) || $this->get_access_object()->god_mode()) {
370  $ret = true;
371  } else {
372  if (isset($ids) && is_array($ids) && count($ids)) {
373  $ret = in_array($my_write_item, $ids);
374  }
375  }
376 
377  return $ret;
378  }
379 
380  /***********************/
388  public function set_read_security_id($in_new_id
389  ) {
390  $ret = false;
391  if ($this->user_can_write() && isset($in_new_id)) {
392  $this->read_security_id = intval($in_new_id);
393  $ret = $this->update_db();
394  }
395 
396  return $ret;
397  }
398 
399  /***********************/
407  public function set_write_security_id($in_new_id
408  ) {
409  $ret = false;
410  if ($this->user_can_write() && isset($in_new_id)) {
411  $this->write_security_id = intval($in_new_id);
412  $ret = $this->update_db();
413  }
414 
415  return $ret;
416  }
417 
418  /***********************/
424  public function set_name($in_new_value
425  ) {
426  $ret = false;
427 
428  if (isset($in_new_value)) {
429  $this->name = strval($in_new_value);
430  $ret = $this->update_db();
431  }
432 
433  return $ret;
434  }
435 
436  /***********************/
444  public function delete_from_db() {
445  if ($this->user_can_write()) {
446  return $this->_seppuku();
447  } else {
448  return false;
449  }
450  }
451 
452  /***********************/
460  public function update_db() {
461  if (!$this->id() || $this->user_can_write()) {
462  if (!$this->_batch_mode) {
463  return $this->_write_to_db();
464  } else {
465  return true;
466  }
467  } else {
468  return false;
469  }
470  }
471 
472  /***********************/
479  public function reload_from_db() {
480  $ret = false;
481  $db_result = $this->_db_object->get_single_raw_row_by_id($this->id());
482  $this->error = $this->get_access_object()->error;
483  if (!isset($this->error) || !$this->error) {
484  $ret = $this->load_from_db($db_result);
485  }
486 
487  return $ret;
488  }
489 
490  /***********************/
494  public function get_access_object() {
495  $db_object = $this->_db_object;
496 
497  if (isset($db_object)) {
498  return $db_object->access_object;
499  }
500 
501  return NULL;
502  }
503 
504  /***********************/
508  public function get_lang() {
509  $ret = CO_Config::$lang;
510 
511  // We replace the default only if we have a valid lang value.
512  if (isset($this->context['lang']) && trim($this->context['lang'])) {
513  $ret = strtolower(trim($this->context['lang'])); // Should be unneccessary, but belt and suspenders...
514  }
515 
516  return $ret;
517  }
518 
519  /***********************/
523  public function set_lang( $in_lang_id = NULL
524  ) {
525  $ret = false;
526 
527  if ($this->user_can_write()) {
528  $this->context['lang'] = strtolower(trim(strval($in_lang_id)));
529  $ret = $this->update_db();
530  }
531 
532  return $ret;
533  }
534 };
if(!defined( 'LGV_DBF_CATCHER'))
$name
This is the "object_name" string field.
$last_access
This is a UNIX epoch date that describes the last modification. The default is UNIX Day Two (in case ...
$instance_description
This is a description that describes the instance.
__construct( $in_db_object=NULL, $in_db_result=NULL)
$_db_object
This is the actual database object that "owns" this instance. It should not be exposed beyond this cl...
$class_description
This is a description of the class (not the instance).
$_id
This is the within-table unique ID of this record.
$write_security_id
This is a single integer, defining the required security token to modify the record....
$context
This is a mixed associative array, containing fields for the object.
$error
If there is an error, it is contained here, in a LGV_Error instance.
$_batch_mode
If this is true, then the write_record call will not be made in update_db. It will be done when clear...
$read_security_id
This is a single integer, defining the security ID required to view the record. If it is 0,...