00001 <?php
00007 namespace Habari;
00008
00015 abstract class InfoRecords implements URLProperties
00016 {
00017
00018 protected $__inforecord_array = array();
00019
00020 protected $_table_name;
00021
00022 protected $_key_name;
00023
00024 protected $_key_value;
00025
00026 protected $_loaded = false;
00027
00028 protected $_dirty = false;
00029
00030 protected $url_args;
00031
00044 public function __construct( $table_name, $key_name, $key_value = null )
00045 {
00046 $this->_table_name = $table_name;
00047 $this->_key_name = $key_name;
00048 $this->_key_value = $key_value;
00049 $this->_loaded = false;
00050 }
00051
00057 public function is_key_set()
00058 {
00059 return isset( $this->_key_value );
00060 }
00061
00069 public function set_key( $metadata_key )
00070 {
00071 $this->_key_value = $metadata_key;
00072 }
00073
00077 protected function _load()
00078 {
00079 if ( $this->_loaded == true ) {
00080 return;
00081 }
00082 if ( empty($this->_key_value) ) {
00083 return;
00084 }
00085
00086
00087 if ( empty($this->_table_name) ) {
00088 return;
00089 }
00090
00091 $result = DB::get_results( '
00092 SELECT name, value, type
00093 FROM ' . $this->_table_name . '
00094 WHERE ' . $this->_key_name . ' = ?',
00095 array( $this->_key_value )
00096 );
00097
00098 foreach ( $result as $result_element ) {
00099
00100 if ( $result_element->type == 1 ) {
00101 $this->__inforecord_array[$result_element->name] = array( 'value' => unserialize( $result_element->value ) );
00102 }
00103 else {
00104 $this->__inforecord_array[$result_element->name] = array( 'value' => $result_element->value );
00105 }
00106 }
00107
00108 $this->_loaded = true;
00109 }
00110
00116 public function __get ( $name )
00117 {
00118 $this->_load();
00119 if ( ! isset( $this->__inforecord_array[$name] ) ) {
00120 return false;
00121 }
00122 return $this->__inforecord_array[$name]['value'];
00123 }
00124
00125 public function getArrayCopy ( ) {
00126 $this->_load();
00127
00128 $result = array();
00129 foreach ( $this->__inforecord_array as $k => $v ) {
00130 $result[ $k ] = $v['value'];
00131 }
00132
00133 return $result;
00134 }
00135
00143 public function __set( $name, $value )
00144 {
00145 $this->_load();
00146 $this->__inforecord_array[$name] = array('changed'=>true, 'value'=>$value);
00147 $this->_dirty = true;
00148 $self = $this;
00149 register_shutdown_function(function() use ($self){
00150 $self->commit();
00151 });
00152 }
00153
00160 public function __isset ( $name )
00161 {
00162 $this->_load();
00163 return isset( $this->__inforecord_array[$name] );
00164 }
00165
00172 public function __unset( $name )
00173 {
00174 $this->_load();
00175 if ( isset( $this->__inforecord_array[$name] ) ) {
00176 DB::delete( $this->_table_name, array ( $this->_key_name => $this->_key_value, "name" => $name ) );
00177 unset( $this->__inforecord_array[$name] );
00178 return true;
00179 }
00180 return false;
00181 }
00182
00187 public function get_url_args()
00188 {
00189 if ( !$this->url_args ) {
00190 $this->_load();
00191 $this->url_args = array_map( function($element) { return $element['value']; }, $this->__inforecord_array );
00192 }
00193 return $this->url_args;
00194 }
00195
00202 public function delete_all()
00203 {
00204
00205 if ( empty($this->_table_name) ) {
00206 return false;
00207 }
00208 $result = DB::query( '
00209 DELETE FROM ' . $this->_table_name . '
00210 WHERE ' . $this->_key_name . ' = ?',
00211 array( $this->_key_value )
00212 );
00213 if ( Error::is_error( $result ) ) {
00214 $result->out();
00215 return false;
00216 }
00217 $this->__inforecord_array = array();
00218 return true;
00219 }
00220
00228 public function commit( $metadata_key = null )
00229 {
00230 if ( isset( $metadata_key ) ) {
00231 $this->_key_value = $metadata_key;
00232 }
00233
00234
00235 if ( !$this->_loaded && empty($this->_key_value) ) {
00236 return false;
00237 }
00238
00239
00240 if ( !$this->_dirty ) {
00241 return true;
00242 }
00243
00244 foreach ( (array)$this->__inforecord_array as $name=>$record ) {
00245 if ( isset( $record['changed'] ) && $record['changed'] ) {
00246 $value = $record['value'];
00247 if ( is_array( $value ) || is_object( $value ) ) {
00248 $result = DB::update(
00249 $this->_table_name,
00250 array(
00251 $this->_key_name=>$this->_key_value,
00252 'name'=>$name,
00253 'value'=>serialize( $value ),
00254 'type'=>1
00255 ),
00256 array( 'name'=>$name, $this->_key_name=>$this->_key_value )
00257 );
00258 }
00259 else {
00260 $result = DB::update(
00261 $this->_table_name,
00262 array(
00263 $this->_key_name=>$this->_key_value,
00264 'name'=>$name,
00265 'value'=>$value,
00266 'type'=>0
00267 ),
00268 array('name'=>$name, $this->_key_name=> $this->_key_value)
00269 );
00270 }
00271
00272 if ( Error::is_error( $result ) ) {
00273 $result->out();
00274 }
00275 $this->__inforecord_array[$name] = array('value'=>$value);
00276 }
00277 }
00278 $this->_dirty = false;
00279 return true;
00280 }
00281
00286 public function count()
00287 {
00288 $this->_load();
00289 return count( $this->__inforecord_array );
00290
00291 }
00292
00293 }
00294
00295 ?>