Dotclear

Changeset 2505:62775923ddff


Ignore:
Timestamp:
10/30/13 10:12:11 (12 years ago)
Author:
Dsls
Branch:
default
Message:

Added getGlobal and getLocal methods to dcNamespace, closes #1828

File:
1 edited

Legend:

Unmodified
Added
Removed
  • inc/core/class.dc.namespace.php

    r1179 r2505  
    2222     protected $table;        ///< <b>string</b> Settings table name 
    2323     protected $blog_id;      ///< <b>string</b> Blog ID 
    24       
     24 
    2525     protected $global_settings = array();   ///< <b>array</b> Global settings array 
    2626     protected $local_settings = array();    ///< <b>array</b> Local settings array 
    2727     protected $settings = array();          ///< <b>array</b> Associative settings array 
    2828     protected $ns;           ///< <b>string</b> Current namespace 
    29       
     29 
    3030     /** 
    3131     Object constructor. Retrieves blog settings and puts them in $settings 
    3232     array. Local (blog) settings have a highest priority than global settings. 
    33       
     33 
    3434     @param    name      <b>string</b>       ID for this namespace 
    3535     */ 
     
    4141               throw new Exception(sprintf(__('Invalid setting dcNamespace: %s'),$name)); 
    4242          } 
    43            
     43 
    4444          $this->con =& $core->con; 
    4545          $this->table = $core->prefix.'setting'; 
    4646          $this->blog_id =& $blog_id; 
    47            
     47 
    4848          $this->getSettings($rs); 
    4949     } 
    50       
     50 
    5151     private function getSettings($rs=null) 
    52      {     
     52     { 
    5353          if ($rs == null) { 
    5454               $strReq = 'SELECT blog_id, setting_id, setting_value, '. 
     
    5959                         "AND setting_ns = '".$this->con->escape($this->ns)."' ". 
    6060                         'ORDER BY setting_id DESC '; 
    61            
     61 
    6262               try { 
    6363                    $rs = $this->con->select($strReq); 
     
    7474               $value = $rs->f('setting_value'); 
    7575               $type = $rs->f('setting_type'); 
    76                 
     76 
    7777               if ($type == 'float' || $type == 'double') { 
    7878                    $type = 'float'; 
     
    8080                    $type = 'string'; 
    8181               } 
    82                 
     82 
    8383               settype($value,$type); 
    84                 
     84 
    8585               $array = $rs->blog_id ? 'local' : 'global'; 
    86                 
     86 
    8787               $this->{$array.'_settings'}[$id] = array( 
    8888                    'ns' => $this->ns, 
     
    9393               ); 
    9494          } 
    95            
     95 
    9696          $this->settings = $this->global_settings; 
    97            
     97 
    9898          foreach ($this->local_settings as $id => $v) { 
    9999               $this->settings[$id] = $v; 
    100100          } 
    101            
     101 
    102102          return true; 
    103103     } 
    104       
     104 
    105105     private function settingExists($id,$global=false) 
    106106     { 
     
    108108          return isset($this->{$array.'_settings'}[$id]); 
    109109     } 
    110       
     110 
    111111     /** 
    112112     Returns setting value if exists. 
    113       
     113 
    114114     @param    n         <b>string</b>       Setting name 
    115115     @return   <b>mixed</b> 
     
    120120               return $this->settings[$n]['value']; 
    121121          } 
    122            
     122 
    123123          return null; 
    124124     } 
    125       
     125 
     126     /** 
     127     Returns global setting value if exists. 
     128 
     129     @param    n         <b>string</b>       setting name 
     130     @return   <b>mixed</b> 
     131     */ 
     132     public function getGlobal($n) 
     133     { 
     134          if (isset($this->global_settings[$n]['value'])) { 
     135               return $this->global_settings[$n]['value']; 
     136          } 
     137 
     138          return null; 
     139     } 
     140 
     141     /** 
     142     Returns local setting value if exists. 
     143 
     144     @param    n         <b>string</b>       setting name 
     145     @return   <b>mixed</b> 
     146     */ 
     147     public function getLocal($n) 
     148     { 
     149          if (isset($this->local_settings[$n]['value'])) { 
     150               return $this->local_settings[$n]['value']; 
     151          } 
     152 
     153          return null; 
     154     } 
     155 
    126156     /** 
    127157     Magic __get method. 
     
    132162          return $this->get($n); 
    133163     } 
    134       
     164 
    135165     /** 
    136166     Sets a setting in $settings property. This sets the setting for script 
    137167     execution time only and if setting exists. 
    138       
     168 
    139169     @param    n         <b>string</b>       Setting name 
    140170     @param    v         <b>mixed</b>        Setting value 
     
    146176          } 
    147177     } 
    148       
     178 
    149179     /** 
    150180     Magic __set method. 
     
    155185          $this->set($n,$v); 
    156186     } 
    157       
     187 
    158188     /** 
    159189     Creates or updates a setting. 
    160       
     190 
    161191     $type could be 'string', 'integer', 'float', 'boolean' or null. If $type is 
    162192     null and setting exists, it will keep current setting type. 
    163       
     193 
    164194     $value_change allow you to not change setting. Useful if you need to change 
    165195     a setting label or type and don't want to change its value. 
    166       
     196 
    167197     @param    id             <b>string</b>       Setting ID 
    168198     @param    value          <b>mixed</b>        Setting value 
     
    177207               throw new Exception(sprintf(__('%s is not a valid setting id'),$id)); 
    178208          } 
    179            
     209 
    180210          # We don't want to change setting value 
    181211          if (!$value_change) 
     
    187217               } 
    188218          } 
    189            
     219 
    190220          # Setting type 
    191221          if ($type == 'double') 
     
    207237               $type = 'string'; 
    208238          } 
    209            
     239 
    210240          # We don't change label 
    211241          if ($label == null) 
     
    217247               } 
    218248          } 
    219            
     249 
    220250          settype($value,$type); 
    221            
     251 
    222252          $cur = $this->con->openCursor($this->table); 
    223253          $cur->setting_value = ($type == 'boolean') ? (string) (integer) $value : (string) $value; 
    224254          $cur->setting_type = $type; 
    225255          $cur->setting_label = $label; 
    226            
     256 
    227257          #If we are local, compare to global value 
    228258          if (!$global && $this->settingExists($id,true)) 
     
    231261               $same_setting = $g['ns'] == $this->ns && $g['value'] == $value 
    232262               && $g['type'] == $type && $g['label'] == $label; 
    233                 
     263 
    234264               # Drop setting if same value as global 
    235265               if ($same_setting && $this->settingExists($id,false)) { 
     
    239269               } 
    240270          } 
    241            
     271 
    242272          if ($this->settingExists($id,$global) && $this->ns == $this->settings[$id]['ns']) 
    243273          { 
     
    247277                    $where = "WHERE blog_id = '".$this->con->escape($this->blog_id)."' "; 
    248278               } 
    249                 
     279 
    250280               $cur->update($where."AND setting_id = '".$this->con->escape($id)."' AND setting_ns = '".$this->con->escape($this->ns)."' "); 
    251281          } 
     
    255285               $cur->blog_id = $global ? null : $this->blog_id; 
    256286               $cur->setting_ns = $this->ns; 
    257                 
     287 
    258288               $cur->insert(); 
    259289          } 
     
    272302               throw new Exception(__('No namespace specified')); 
    273303          } 
    274            
     304 
    275305          if (!array_key_exists($oldId,$this->settings) || array_key_exists($newId,$this->settings)) { 
    276306               return false; 
     
    291321 
    292322     /** 
    293      Removes an existing setting in a Namespace  
    294       
     323     Removes an existing setting in a Namespace 
     324 
    295325     @param    id        <b>string</b>       Setting ID 
    296326     */ 
     
    300330               throw new Exception(__('No namespace specified')); 
    301331          } 
    302            
     332 
    303333          $strReq = 'DELETE FROM '.$this->table.' '; 
    304            
     334 
    305335          if ($this->blog_id === null) { 
    306336               $strReq .= 'WHERE blog_id IS NULL '; 
     
    308338               $strReq .= "WHERE blog_id = '".$this->con->escape($this->blog_id)."' "; 
    309339          } 
    310            
     340 
    311341          $strReq .= "AND setting_id = '".$this->con->escape($id)."' "; 
    312342          $strReq .= "AND setting_ns = '".$this->con->escape($this->ns)."' "; 
    313            
     343 
    314344          $this->con->execute($strReq); 
    315345     } 
    316       
    317      /** 
    318      Removes all existing settings in a Namespace  
    319       
     346 
     347     /** 
     348     Removes all existing settings in a Namespace 
     349 
    320350     @param    force_global   <b>boolean</b> Force global pref drop 
    321351     */ 
     
    325355               throw new Exception(__('No namespace specified')); 
    326356          } 
    327            
     357 
    328358          $strReq = 'DELETE FROM '.$this->table.' '; 
    329            
     359 
    330360          if (($force_global) || ($this->blog_id === null)) { 
    331361               $strReq .= 'WHERE blog_id IS NULL '; 
     
    335365               $global = false; 
    336366          } 
    337            
     367 
    338368          $strReq .= "AND setting_ns = '".$this->con->escape($this->ns)."' "; 
    339            
     369 
    340370          $this->con->execute($strReq); 
    341            
     371 
    342372          $array = $global ? 'global' : 'local'; 
    343373          unset($this->{$array.'_settings'}); 
    344374          $this->{$array.'_settings'} = array(); 
    345            
     375 
    346376          $array = $global ? 'local' : 'global'; 
    347377          $this->settings = $this->{$array.'_settings'}; 
    348378     } 
    349       
     379 
    350380     /** 
    351381     Returns $settings property content. 
    352       
     382 
    353383     @return   <b>array</b> 
    354384     */ 
     
    357387          return $this->settings; 
    358388     } 
    359       
     389 
    360390     /** 
    361391     Returns $global_settings property content. 
    362       
     392 
    363393     @return   <b>array</b> 
    364394     */ 
Note: See TracChangeset for help on using the changeset viewer.

Sites map