00001 <?php
00007 namespace Habari;
00008
00019 class Block extends QueryRecord implements IsContent, FormStorage
00020 {
00021 public $_first = false;
00022 public $_last = false;
00023 public $_area_index = 0;
00024 public $_area = '';
00025 private $data_values = array( '_show_title' => true );
00026
00031 public function __construct( $paramarray = array() )
00032 {
00033
00034 $this->fields = array_merge(
00035 self::default_fields(),
00036 $this->fields,
00037 $this->newfields
00038 );
00039 parent::__construct( $paramarray );
00040
00041 $this->exclude_fields( 'id' );
00042 $this->unserialize_data();
00043 }
00044
00051 public function __get( $name )
00052 {
00053 switch($name) {
00054 case 'css_classes':
00055 $classes = array();
00056 if(array_key_exists( $name, $this->data_values )) {
00057 $classes = $this->data_values[$name];
00058 }
00059 if(is_string($classes)) {
00060 $classes = explode(' ', $classes);
00061 }
00062 if($this->_first) {
00063 $classes[] = 'first';
00064 }
00065 if($this->_last) {
00066 $classes[] = 'last';
00067 }
00068 if($this->_area_index) {
00069 $classes[] = 'index_' . $this->_area_index;
00070 }
00071 $classes[] = 'block-type-' . Utils::slugify($this->type);
00072 $classes[] = 'block-title-' . Utils::slugify($this->title);
00073 $classes[] = 'block';
00074 $classes = Plugins::filter('block_classes', $classes, $this);
00075 return implode(' ', $classes);
00076 break;
00077 default:
00078 if ( array_key_exists( $name, $this->data_values ) ) {
00079 return $this->data_values[$name];
00080 }
00081 else {
00082 return parent::__get( $name );
00083 }
00084 }
00085 }
00086
00094 public function __set( $name, $value )
00095 {
00096 switch ( $name ) {
00097 case 'id':
00098 case 'title':
00099 case 'data':
00100 parent::__set( $name, $value );
00101 $this->unserialize_data();
00102 return parent::__get( $name );
00103 break;
00104 case 'type':
00105 return parent::__set( $name, $value );
00106 break;
00107 default:
00108 $this->data_values[ $name ] = $value;
00109 return $this->data_values[ $name ];
00110 break;
00111 }
00112 }
00113
00121 public function __isset( $name )
00122 {
00123 return ( isset( $this->data_values[$name] ) || parent::__isset( $name ) );
00124 }
00125
00130 public static function default_fields()
00131 {
00132 return array(
00133 'id' => 0,
00134 'title' => '',
00135 'data' => '',
00136 'type' => '',
00137 );
00138 }
00139
00146 public function fetch( $theme )
00147 {
00148 Plugins::act( 'block_content_' . $this->type, $this, $theme );
00149 Plugins::act( 'block_content', $this, $theme );
00150 $output = implode( '', $theme->content_return( $this ) );
00151 return $output;
00152 }
00153
00160 public function content_type()
00161 {
00162 $types = array(
00163 'block.' . $this->type,
00164 'block',
00165 );
00166 if ( isset( $this->title ) ) {
00167 array_unshift( $types, 'block.' . $this->type . '.' . Utils::slugify( $this->title ) );
00168 }
00169 if ( isset( $this->_area ) ) {
00170 $areas = array();
00171 foreach ( $types as $type ) {
00172 $areas[] = $this->_area . '.' . $type;
00173 }
00174 $types = array_merge( $areas, $types );
00175 }
00176 $types = Plugins::filter( 'block_content_type_' . $this->type, $types, $this );
00177 $types = Plugins::filter( 'block_content_type', $types, $this );
00178 return $types;
00179 }
00180
00184 public function unserialize_data()
00185 {
00186 if ( trim( $this->data ) != '' ) {
00187 $this->data_values = unserialize( $this->data );
00188 }
00189 }
00190
00197 public function field_save( $key, $value )
00198 {
00199 $this->$key = $value;
00200 Session::queue($this);
00201 }
00202
00209 public function field_load( $key )
00210 {
00211 return $this->$key;
00212 }
00213
00219 public function insert()
00220 {
00221
00222 $allow = true;
00223 $allow = Plugins::filter( 'block_insert_allow', $allow, $this );
00224 if ( ! $allow ) {
00225 return null;
00226 }
00227 Plugins::act( 'block_insert_before', $this );
00228
00229 $this->data = serialize( $this->data_values );
00230 $result = parent::insertRecord( DB::table( 'blocks' ) );
00231
00232
00233 $this->newfields['id'] = DB::last_insert_id();
00234
00235
00236 $this->fields = array_merge( $this->fields, $this->newfields );
00237
00238
00239 $this->newfields = array();
00240
00241 EventLog::log( _t( 'New block %1$s: %2$s', array( $this->id, $this->title ) ), 'info', 'content', 'habari' );
00242
00243
00244 Plugins::act( 'block_insert_after', $this );
00245 return $result;
00246 }
00247
00253 public function update()
00254 {
00255 $allow = true;
00256 $allow = Plugins::filter( 'block_update_allow', $allow, $this );
00257 if ( ! $allow ) {
00258 return null;
00259 }
00260 Plugins::act( 'block_update_before', $this );
00261
00262 $this->data = serialize( $this->data_values );
00263 $result = parent::updateRecord( DB::table( 'blocks' ), array( 'id' => $this->id ) );
00264
00265 $this->fields = array_merge( $this->fields, $this->newfields );
00266 $this->newfields = array();
00267
00268
00269 Plugins::act( 'block_update_after', $this );
00270 return $result;
00271 }
00272
00277 public function delete()
00278 {
00279
00280 $allow = true;
00281 $allow = Plugins::filter( 'block_delete_allow', $allow, $this );
00282 if ( !$allow ) {
00283 return false;
00284 }
00285 Plugins::act( 'block_delete_before', $this );
00286
00287 $result = parent::deleteRecord( '{blocks_areas}', array( 'block_id'=>$this->id ) );
00288 $result = $result && parent::deleteRecord( '{blocks}', array( 'id'=>$this->id ) );
00289
00290 EventLog::log( _t( 'Block %1$s (%2$s) deleted.', array( $this->id, $this->title ) ), 'info', 'content', 'habari' );
00291
00292
00293 Plugins::act( 'block_delete_after', $this );
00294 return $result;
00295 }
00296
00302 public function get_form()
00303 {
00304 $form = new FormUI( 'block-' . $this->id, 'block' );
00305 $form->on_success( array( $this, 'save_block' ) );
00306
00307 Plugins::act( 'block_form_' . $this->type, $form, $this );
00308 Plugins::act( 'block_form', $form, $this );
00309 return $form;
00310 }
00311
00318 public function save_block( FormUI $form )
00319 {
00320 $form->save();
00321 return false;
00322 }
00323
00331 public function add_to_area( $area, $order = null, $scope = null )
00332 {
00333 if( is_null( $scope ) ) {
00334 $scope = 0;
00335 }
00336 if( is_null( $order ) || ! is_int( $order ) ) {
00337 $order = DB::get_value( 'SELECT max(display_order) + 1 FROM {blocks_areas} WHERE area = :area AND scope_id = :scope', array( 'area' => $area, 'scope' => $scope ) );
00338 if( is_null( $order ) ) {
00339 $order = 1;
00340 }
00341 }
00342 else {
00343 DB::query( 'UPDATE {blocks_areas} SET display_order = display_order + 1 WHERE area = :area AND scope_id = :scope AND display_order >= :order', array( 'area' => $area, 'scope' => $scope, 'order' => $order ) );
00344 }
00345
00346
00347 if( !$this->id ) {
00348 $this->insert();
00349 }
00350
00351 $result = DB::query( 'INSERT INTO {blocks_areas} (block_id, area, scope_id, display_order) VALUES (:block_id, :area, :scope_id, :display_order)', array( 'block_id' => $this->id, 'area' => $area, 'scope_id' => $scope, 'display_order' => $order ) );
00352 }
00353
00358 public function __toString()
00359 {
00360 return $this->title;
00361 }
00362 }
00363
00364
00365 ?>