00001 <?php
00007 namespace Habari;
00012 class Options extends Singleton
00013 {
00014 private $options = null;
00015
00021 protected static function instance()
00022 {
00023 return self::getInstanceOf( __CLASS__ );
00024 }
00025
00039 public static function get( $name, $default_value = null )
00040 {
00041 if ( is_array( $name ) ) {
00042 $results = array();
00043 foreach ( $name as $key ) {
00044 $results[ $key ] = Options::get( $key );
00045 }
00046 return $results;
00047 }
00048
00049 if ( isset( self::instance()->$name ) ) {
00050 return self::instance()->$name;
00051 }
00052 else {
00053 return $default_value;
00054 }
00055 }
00056
00064 public static function get_group( $prefix )
00065 {
00066
00067 if ( substr( $prefix, -2 ) != '__' ) {
00068 $prefix .= '__';
00069 }
00070
00071 $results = array();
00072 foreach ( array_keys( self::instance()->options ) as $key ) {
00073
00074 if ( strpos( $key, $prefix ) === 0 ) {
00075 $results[ substr( $key, strlen( $prefix ) ) ] = Options::get( $key );
00076 }
00077
00078 }
00079
00080 return $results;
00081
00082 }
00083
00095 public static function set_group ( $prefix, $values )
00096 {
00097
00098 if ( substr( $prefix, -2 ) != '__' ) {
00099 $prefix .= '__';
00100 }
00101
00102
00103 foreach ( $values as $k => $v ) {
00104
00105 Options::set( $prefix . $k, $v );
00106
00107 }
00108
00109 }
00110
00121 public static function delete_group ( $prefix )
00122 {
00123
00124 if ( substr( $prefix, -2 ) != '__' ) {
00125 $prefix .= '__';
00126 }
00127
00128 foreach ( array_keys( self::instance()->options ) as $key ) {
00129
00130 if ( strpos( $key, $prefix ) === 0 ) {
00131 Options::delete( $key );
00132 }
00133
00134 }
00135
00136 }
00137
00145 public static function out( $name )
00146 {
00147 echo self::instance()->get( $name );
00148 }
00149
00162 public static function set( $name, $value = '' )
00163 {
00164
00165 if ( is_array( $name ) ) {
00166 foreach ( $name as $k => $v ) {
00167 Options::set( $k, $v );
00168 }
00169 }
00170 else {
00171 self::instance()->$name = $value;
00172 }
00173
00174 }
00175
00176
00182 public static function delete( $name )
00183 {
00184
00185 if ( is_array( $name ) ) {
00186 foreach ( $name as $key ) {
00187 Options::delete( $key );
00188 }
00189 }
00190 else {
00191 unset( self::instance()->$name );
00192 }
00193
00194 }
00195
00201 public function __get( $name )
00202 {
00203 if ( ! isset( $this->options ) ) {
00204 $this->get_all_options();
00205 }
00206 $option_value = isset( $this->options[$name] ) ? $this->options[$name] : null;
00207 $option_value = Plugins::filter( 'option_get_value', $option_value, $name );
00208 return $option_value;
00209 }
00210
00211 public function __isset ( $name ) {
00212
00213 if ( !isset( $this->options ) ) {
00214 $this->get_all_options();
00215 }
00216
00217 return isset( $this->options[ $name ] );
00218
00219 }
00220
00224 public function get_all_options()
00225 {
00226
00227 $this->options = array(
00228 'pagination' => 10,
00229 'comments_require_id' => false,
00230 );
00231 if(Config::exists('default_options')) {
00232 $this->options = array_merge($this->options, Config::get('default_options'));
00233 }
00234 if(DB::is_connected()) {
00235 $results = DB::get_results( 'SELECT name, value, type FROM {options}', array() );
00236 foreach ( $results as $result ) {
00237 if ( $result->type == 1 ) {
00238 $this->options[$result->name] = unserialize( $result->value );
00239 }
00240 else {
00241 $this->options[$result->name] = $result->value;
00242 }
00243 }
00244 }
00245 if(Config::exists('static_options')) {
00246 $this->options = array_merge($this->options, Config::get('static_options'));
00247 }
00248 }
00249
00255 public function __set( $name, $value )
00256 {
00257 if ( ! isset( $this->options ) ) {
00258 $this->get_all_options();
00259 }
00260
00261
00262
00263 if ( isset( $this->options[ $name ] ) && $this->options[ $name ] == $value ) {
00264 return;
00265 }
00266
00267 $value = Plugins::filter( 'option_set_value', $value, $name, isset( $this->options[$name] ) ? $this->options[$name] : null );
00268 $this->options[$name] = $value;
00269
00270
00271 $result = DB::update( DB::table( 'options' ), array( 'name' => $name, 'value' => serialize( $value ), 'type' => 1 ), array( 'name' => $name ) );
00272
00273 if ( Error::is_error( $result ) ) {
00274 $result->out();
00275 die();
00276 }
00277 }
00278
00284 public function __unset( $name )
00285 {
00286 unset( $this->options[$name] );
00287 DB::delete( DB::table( 'options' ), array( 'name' => $name ) );
00288 }
00289
00293 public static function clear_cache()
00294 {
00295 self::instance()->options = null;
00296 }
00297
00304 public static function is_static($name)
00305 {
00306 if($static_options = Config::exists('static_options')) {
00307 if(isset($static_options[$name])) {
00308 return true;
00309 }
00310 }
00311 return false;
00312 }
00313
00314 }
00315
00316 ?>