BAOBAB
co_main_db_record.class.php
Go to the documentation of this file.
1 <?php
2 /***************************************************************************************************************************/
26 defined( 'LGV_DBF_CATCHER' ) or die ( 'Cannot Execute Directly' ); // Makes sure that this file is in the correct context.
27 
28 if ( !defined('LGV_ADBTB_CATCHER') ) {
29  define('LGV_ADBTB_CATCHER', 1);
30 }
31 
32 require_once(CO_Config::db_class_dir().'/a_co_db_table_base.class.php');
33 
34 /***************************************************************************************************************************/
39  static $s_table_name = 'co_data_nodes';
40 
41  protected $_owner_id;
42  protected $_tags;
43 
44  private $_raw_payload;
45 
46  /***********************************************************************************************************************/
47  /***********************/
53  protected function _default_setup() {
54  $default_setup = parent::_default_setup();
55  $default_setup['owner_id'] = $this->_owner_id;
56 
57  for ($tag_no = 0; $tag_no < 10; $tag_no++) {
58  $key = "tag$tag_no";
59  $tag_val = (isset($this->_tags) && is_array($this->_tags) && ($tag_no < count($this->_tags))) ? $this->_tags[$tag_no] : '';
60  $default_setup[$key] = $tag_val;
61  }
62 
63  return $default_setup;
64  }
65 
66  /***********************/
72  protected function _build_parameter_array() {
73  $ret = parent::_build_parameter_array(); // Start with the base class.
74 
75  $ret['owner'] = intval($this->_owner_id);
76  for ($tag_no = 0; $tag_no < 10; $tag_no++) {
77  $key = "tag$tag_no";
78  if (isset($this->_tags[$tag_no])) {
79  $ret[$key] = $this->_tags[$tag_no];
80  } else {
81  $ret[$key] = '';
82  }
83  }
84 
85  $ret['payload'] = base64_encode($this->_raw_payload);
86 
87  return $ret;
88  }
89 
90  /***********************************************************************************************************************/
91  /***********************/
95  public function __construct( $in_db_object = NULL,
96  $in_db_result = NULL,
97  $in_owner_id = NULL,
98  $in_tags_array = NULL
99  ) {
100  $this->_owner_id = intval($in_owner_id);
101  $this->_tags = (isset($in_tags_array) && is_array($in_tags_array) && count($in_tags_array)) ? array_map(function($in) { return strval($in); }, $in_tags_array) : Array();
102  parent::__construct($in_db_object, $in_db_result);
103  $this->class_description = 'Base Class for Main Database Records.';
104  $this->name = (isset($this->name) && trim($this->name)) ? trim($this->name) : "Base Class Instance ($this->_id)";
105  }
106 
107  /***********************/
113  public function load_from_db( $in_db_result
114  ) {
115  $ret = parent::load_from_db($in_db_result); // Start by calling the base class version.
116 
117  if ($ret) { // If that went OK, we add our own two cents...
118  $this->class_description = 'Base Class for Main Database Records.';
119  $this->name = (isset($this->name) && trim($this->name)) ? trim($this->name) : "Base Class Instance ($this->_id)";
120 
121  if ($this->_db_object) {
122  $this->_owner_id = NULL;
123  $this->_tags = array();
124  $this->_raw_payload = NULL;
125 
126  if (isset($in_db_result['owner'])) {
127  $this->_owner_id = intval($in_db_result['owner']);
128  }
129 
130  if (isset($in_db_result['payload']) ) {
131  $payload = $in_db_result['payload'];
132  $length = strlen($payload);
133  $counter = 0;
134  $new_payload = [];
135 
136  // We decode in chunks.
137  while($counter < $length) {
138  $this_chunk = substr($payload, $counter, 4096);
139  $new_payload[] = base64_decode($this_chunk);
140  $counter += 4096;
141  }
142 
143  $this->_raw_payload = implode('', $new_payload);
144  }
145 
146  for ($tag_no = 0; $tag_no < 10; $tag_no++) {
147  $key = "tag$tag_no";
148  $tag_val = (isset($in_db_result[$key])) && $in_db_result[$key] ? $in_db_result[$key] : '';
149  $this->_tags[$tag_no] = $tag_val;
150  }
151 
152  for ($i = 0; $i < 10; $i++) {
153  $tagname = 'tag'.$i;
154  $this->_tags[$i] = '';
155  if (isset($in_db_result[$tagname])) {
156  $this->_tags[$i] = $in_db_result[$tagname];
157  }
158  }
159  }
160  }
161 
162  return $ret;
163  }
164 
165  /***********************/
171  public function set_owner_id( $in_new_id
172  ) {
173  $ret = false;
174 
175  if (isset($in_new_id) && $this->user_can_write()) {
176  $this->_owner_id = intval($in_new_id);
177  $ret = $this->update_db();
178  }
179 
180  return $ret;
181  }
182 
183  /***********************/
189  public function set_tags( $in_tags_array
190  ) {
191  $ret = false;
192 
193  if (isset($in_tags_array) && is_array($in_tags_array) && count($in_tags_array) && (11 > count($in_tags_array)) && $this->user_can_write()) {
194  $this->_tags = array_map('strval', $in_tags_array);
195  $ret = $this->update_db();
196  }
197 
198  return $ret;
199  }
200 
201  /***********************/
207  public function set_tag( $in_tag_index,
208  $in_tag_value
209  ) {
210  $ret = false;
211 
212  $in_tag_index = intval($in_tag_index);
213 
214  if ((10 > $in_tag_index) && $this->user_can_write()) {
215  if (!isset($this->_tags) || !$this->_tags) {
216  $this->_tags = Array();
217  }
218 
219  $this->_tags[$in_tag_index] = (NULL != $in_tag_value) ? strval($in_tag_value) : NULL;
220  $ret = $this->update_db();
221  }
222 
223  return $ret;
224  }
225 
226  /***********************/
232  public function get_payload() {
233  return $this->_raw_payload;
234  }
235 
236  /***********************/
242  public function set_payload( $in_payload
243  ) {
244  $ret = false;
245 
246  if ($this->user_can_write()) {
247  $this->_raw_payload = $in_payload;
248  $ret = $this->update_db();
249  }
250 
251  return $ret;
252  }
253 
254  /***********************/
258  public function owner_id() {
259  return $this->_owner_id;
260  }
261 
262  /***********************/
266  public function tags() {
267  return $this->_tags;
268  }
269 };
if(!defined( 'LGV_ADBTB_CATCHER'))
set_tag( $in_tag_index, $in_tag_value)
__construct( $in_db_object=NULL, $in_db_result=NULL, $in_owner_id=NULL, $in_tags_array=NULL)