phpgroupware-cvs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Phpgroupware-cvs] CVS: phpgwapi/inc class.preferences.inc.php,1.43.2.2


From: Ralf Becker <address@hidden>
Subject: [Phpgroupware-cvs] CVS: phpgwapi/inc class.preferences.inc.php,1.43.2.2,1.43.2.2.2.1
Date: Tue, 29 Apr 2003 19:49:47 -0400

Update of /cvsroot/phpgroupware/phpgwapi/inc
In directory subversions:/tmp/cvs-serv811

Modified Files:
      Tag: Version-0_9_16-branch
        class.preferences.inc.php 
Log Message:
reworked preferences:
- not set user-prefs use the default value, default values have been used only 
for new accounts before
- preference-table has new column preference_app
- preferences got automaticaly quoted now, eg. its save to use single and 
double quotes as well as backslashs


Index: class.preferences.inc.php
===================================================================
RCS file: /cvsroot/phpgroupware/phpgwapi/inc/class.preferences.inc.php,v
retrieving revision 1.43.2.2
retrieving revision 1.43.2.2.2.1
diff -C2 -r1.43.2.2 -r1.43.2.2.2.1
*** class.preferences.inc.php   26 Dec 2002 20:41:14 -0000      1.43.2.2
--- class.preferences.inc.php   29 Apr 2003 23:49:44 -0000      1.43.2.2.2.1
***************
*** 28,32 ****
        @class preferences 
        @abstract preferences class used for setting application preferences
!       @discussion Author: none yet
        */
        class preferences
--- 28,36 ----
        @class preferences 
        @abstract preferences class used for setting application preferences
!       @discussion the prefs are read into 4 arrays: \
!               $data the effective prefs used everywhere in phpgw, they are 
merged from the other 3 arrays \
!               $user the stored user prefs, only used for manipulating and 
storeing the user prefs \
!               $default the default preferences, always used when the user has 
no own preference set \
!               $forced forced preferences set by the admin, they take 
precedence over user or default prefs
        */
        class preferences
***************
*** 36,41 ****
                /*! @var account_type */
                var $account_type;
!               /*! @var data */
                var $data = array();
                /*! @var db */
                var $db;
--- 40,51 ----
                /*! @var account_type */
                var $account_type;
!               /*! @var data effectiv user prefs, used by all apps */
                var $data = array();
+               /*! @var user set user prefs for saveing (no defaults/forced 
prefs merged) */
+               var $user = array();
+               /*! @var default default prefs */
+               var $default = array();
+               /*! @var forced forced prefs */
+               var $forced = array();
                /*! @var db */
                var $db;
***************
*** 59,96 ****
                
\**************************************************************************/
  
                /*! 
                @function read_repository
                @abstract private - read preferences from the repository
                @discussion private function should only be called from within 
this class
                */
                function read_repository()
                {
!                       $this->db->query("SELECT * FROM phpgw_preferences WHERE 
"
!                               . "preference_owner='" . $this->account_id . "' 
or "
!                               . "preference_owner='-1' order by 
preference_owner desc",__LINE__,__FILE__);
!                       $this->db->next_record();
  
!                       $pref_info  = $this->db->f('preference_value');
!                       $this->data = unserialize($pref_info);
! 
!                       if ($this->db->next_record())
                        {
!                               $global_defaults = 
unserialize($this->db->f('preference_value'));
! 
!                               while (is_array($global_defaults) && 
list($appname,$values) = each($global_defaults))
                                {
!                                       while (is_array($values) && 
list($var,$value) = each($values))
                                        {
!                                               $this->data[$appname][$var] = 
$value;
                                        }
                                }
                        }
! 
!                       /* This is to supress warnings during login */
!                       if (is_array($this->data))
                        {
!                                reset ($this->data);
                        }
- 
                        // This is to supress warnings durring login
                        if (is_array($this->data))
--- 69,175 ----
                
\**************************************************************************/
  
+               /*!
+               @function unquote
+               @abstract unquote (stripslashes) recursivly the whole array
+               @param $arr array to unquote (var-param!)
+               */
+               function unquote(&$arr)
+               {
+                       if (!is_array($arr))
+                       {
+                               $arr = stripslashes($arr);
+                               return;
+                       }
+                       foreach($arr as $key => $value)
+                       {
+                               if (is_array($value))
+                               {
+                                       $this->unquote($arr[$key]);
+                               }
+                               else
+                               {
+                                       $arr[$key] = stripslashes($value);      
+                               }
+                       }
+               }
+ 
                /*! 
                @function read_repository
                @abstract private - read preferences from the repository
+               @note the function ready all 3 prefs user/default/forced and 
merges them to the effective ones
                @discussion private function should only be called from within 
this class
                */
                function read_repository()
                {
!                       $this->db->query("SELECT * FROM phpgw_preferences".
!                               " WHERE preference_owner IN 
(-1,-2,".intval($this->account_id).")",__LINE__,__FILE__);
  
!                       $this->forced = $this->default = $this->user = array();
!                       while($this->db->next_record())
                        {
!                               $app = $this->db->f('preference_app');
!                               $value = 
unserialize($this->db->f('preference_value'));
!                               $this->unquote($value);
!                               if (!is_array($value))
!                               {
!                                       $value = array();
!                               }
!                               switch($this->db->f('preference_owner'))
!                               {
!                                       case -1:        // forced
!                                               if (empty($app))        // db 
not updated
!                                               {
!                                                       $this->forced = $value;
!                                               }
!                                               else
!                                               {
!                                                       $this->forced[$app] = 
$value;
!                                               }
!                                               break;
!                                       case -2:        // default
!                                               if (empty($app))        // db 
not updated
!                                               {
!                                                       $this->default = $value;
!                                               }
!                                               else
!                                               {
!                                                       $this->default[$app] = 
$value;
!                                               }
!                                               break;
!                                       default:        // user
!                                               if (empty($app))        // db 
not updated
!                                               {
!                                                       $this->user = $value;
!                                               }
!                                               else
!                                               {
!                                                       $this->user[$app] = 
$value;
!                                               }
!                                               break;
!                               }
!                       }
!                       $this->data = $this->user;
!                       
!                       // now use defaults if needed (user-value unset or 
empty)
!                       //
!                       foreach($this->default as $app => $values)
!                       {
!                               foreach($values as $var => $value)
                                {
!                                       if (!isset($this->data[$app][$var]) || 
empty($this->data[$app][$var]))
                                        {
!                                               $this->data[$app][$var] = 
$value;
                                        }
                                }
                        }
!                       // now set/force forced values
!                       //
!                       foreach($this->forced as $app => $values)
                        {
!                               foreach($values as $var => $value)
!                               {
!                                       $this->data[$app][$var] = $value;
!                               }
                        }
                        // This is to supress warnings durring login
                        if (is_array($this->data))
***************
*** 98,101 ****
--- 177,186 ----
                                reset($this->data);
                        }
+                       if ($this->debug && 
substr($GLOBALS['phpgw_info']['flags']['currentapp'],0,3) != 'log') {
+                               echo "user<pre>";    print_r($this->user); echo 
"</pre>\n";
+                               echo "forced<pre>";  print_r($this->forced); 
echo "</pre>\n";
+                               echo "default<pre>"; print_r($this->default); 
echo "</pre>\n";
+                               echo "effectiv<pre>";print_r($this->data); echo 
"</pre>\n"; 
+                       }
                        return $this->data;
                }
***************
*** 125,131 ****
--- 210,218 ----
                @param $var name of preference to be stored
                @param $value value of the preference
+               @note the function works on user and data, to be able to save 
the pref and to have imediate effect
                */
                function add($app_name,$var,$value = '')
                {
+                       //echo "<p>add('$app_name','$var','$value')</p>\n";
                        if ($value == '')
                        {
***************
*** 134,138 ****
                        }
   
!                       $this->data[$app_name][$var] = $value;
                        reset($this->data);
                        return $this->data;
--- 221,225 ----
                        }
   
!                       $this->user[$app_name][$var] = 
$this->data[$app_name][$var] = $value;
                        reset($this->data);
                        return $this->data;
***************
*** 145,159 ****
--- 232,250 ----
                @param $app_name name of app
                @param $var variable to be deleted
+               @note the function works on user and data, to be able to save 
the pref and to have imediate effect
                */
                function delete($app_name, $var = '')
                {
+                       //echo "<p>delete('$app_name','$var')</p>\n";
                        if (is_string($var) && $var == '')
                        {
  //                            $this->data[$app_name] = array();
                                unset($this->data[$app_name]);
+                               unset($this->user[$app_name]);
                        }
                        else
                        {
                                unset($this->data[$app_name][$var]);
+                               unset($this->user[$app_name][$var]);
                        }
                        reset ($this->data);
***************
*** 166,177 ****
                @discussion Use for sublevels of prefs, such as email app's 
extra accounts preferences
                @param $app_name name of the app
!               @param $var String to be evaled's as an ARRAY structure, name 
of preference to be stored
                @param $value value of the preference
                */
                function add_struct($app_name,$var,$value = '')
                {
                        $code = '$this->data[$app_name]'.$var.' = $value;';
                        print_debug('class.preferences: add_struct: $code: ', 
$code,'api');
                        eval($code);
                        print_debug('class.preferences: add_struct: 
$this->data[$app_name] dump:', $this->data[$app_name],'api');
                        reset($this->data);
--- 257,280 ----
                @discussion Use for sublevels of prefs, such as email app's 
extra accounts preferences
                @param $app_name name of the app
!               @param $var array keys separated by '/', eg. 'ex_accounts/1'
                @param $value value of the preference
+               @note the function works on user and data, to be able to save 
the pref and to have imediate effect
                */
                function add_struct($app_name,$var,$value = '')
                {
+                       /* eval is slow and dangerous
                        $code = '$this->data[$app_name]'.$var.' = $value;';
                        print_debug('class.preferences: add_struct: $code: ', 
$code,'api');
                        eval($code);
+                       */
+                       $parts = 
explode('/',str_replace(array('][','[',']','"',"'"),array('/','','','',''),$var));
+                       $data = &$this->data[$app_name];
+                       $user = &$this->user[$app_name];
+                       foreach($parts as $name)
+                       {
+                               $data = &$data[$name];
+                               $user = &$user[$name];
+                       }
+                       $data = $user = $value;
                        print_debug('class.preferences: add_struct: 
$this->data[$app_name] dump:', $this->data[$app_name],'api');
                        reset($this->data);
***************
*** 184,191 ****
                @discussion Use for sublevels of prefs, such as email app's 
extra accounts preferences
                @param $app_name name of app
!               @param $var String to be evaled's as an ARRAY structure, name 
of preference to be deleted
                */
                function delete_struct($app_name, $var = '')
                {
                        $code_1 = '$this->data[$app_name]'.$var.' = "";';
                        print_debug('class.preferences: delete_struct: 
$code_1:', $code_1,'api');
--- 287,296 ----
                @discussion Use for sublevels of prefs, such as email app's 
extra accounts preferences
                @param $app_name name of app
!               @param $var array keys separated by '/', eg. 'ex_accounts/1'
!               @note the function works on user and data, to be able to save 
the pref and to have imediate effect
                */
                function delete_struct($app_name, $var = '')
                {
+                       /* eval is slow and dangerous
                        $code_1 = '$this->data[$app_name]'.$var.' = "";';
                        print_debug('class.preferences: delete_struct: 
$code_1:', $code_1,'api');
***************
*** 194,197 ****
--- 299,314 ----
                        print_debug('class.preferences: delete_struct:  
$code_2: ', $code_2,'api');
                        eval($code_2);
+                       */
+                       $parts = 
explode('/',str_replace(array('][','[',']','"',"'"),array('/','','','',''),$var));
+                       $last = array_pop($parts);
+                       $data = &$this->data[$app_name];
+                       $user = &$this->user[$app_name];
+                       foreach($parts as $name)
+                       {
+                               $data = &$data[$name];
+                               $user = &$user[$name];
+                       }
+                       unset($data[$last]);
+                       unset($user[$last]);
                        print_debug('* $this->data[$app_name] dump:', 
$this->data[$app_name],'api');
                        reset ($this->data);
***************
*** 199,228 ****
                }
  
! 
                /*!
                @function save_repository
                @abstract save the the preferences to the repository
!               @discussion
                */
!               function save_repository($update_session_info = False)
                {
!                       $temp_data = $this->data;
                        if (! 
$GLOBALS['phpgw']->acl->check('session_only_preferences',1,'preferences'))
                        {
                                $this->db->transaction_begin();
!                               $this->db->query("delete from phpgw_preferences 
where preference_owner='" . $this->account_id
!                                       . "'",__LINE__,__FILE__);
  
!                               if (floor(phpversion()) < 4)
                                {
!                                       $pref_info = 
addslashes(serialize($this->data));
                                }
-                               else
-                               {
-                                       $pref_info = serialize($this->data);
-                               }
-                               $this->db->query("insert into phpgw_preferences 
(preference_owner,preference_value) values ('"
-                                       . $this->account_id . "','" . 
$pref_info . "')",__LINE__,__FILE__);
- 
                                $this->db->transaction_commit();
                        }
--- 316,388 ----
                }
  
!               /*!
!               @function quote
!               @abstract quote (addslashes) recursivly the whole array
!               @param $arr array to unquote (var-param!)
!               */
!               function quote(&$arr)
!               {
!                       if (!is_array($arr))
!                       {
!                               $arr = addslashes($arr);
!                               return;
!                       }
!                       foreach($arr as $key => $value)
!                       {
!                               if (is_array($value))
!                               {
!                                       $this->quote($arr[$key]);
!                               }
!                               else
!                               {
!                                       $arr[$key] = addslashes($value);
!                               }
!                       }
!               }
!               
                /*!
                @function save_repository
                @abstract save the the preferences to the repository
!               @syntax save_repository($update_session_info = False,$type='')
!               @param $update_session_info old param, seems not to be used
!               @param $type which prefs to update: user/default/forced 
!               @note the user prefs for saveing are in $this->user not in 
$this->data, which are the effectiv prefs only
                */
!               function save_repository($update_session_info = 
False,$type='user')
                {
!                       switch($type)
!                       {
!                               case 'forced':
!                                       $account_id = -1;
!                                       $prefs = &$this->forced;
!                                       break;
!                               case 'default':
!                                       $account_id = -2;
!                                       $prefs = &$this->default;
!                                       break;
!                               default:
!                                       $account_id = intval($this->account_id);
!                                       $prefs = &$this->user;  // we use the 
user-array as data contains default values too
!                                       break;
!                       }
!                       //echo "<p>preferences::save_repository(,$type): 
account_id=$account_id, prefs="; print_r($prefs); echo "</p>\n";
! 
                        if (! 
$GLOBALS['phpgw']->acl->check('session_only_preferences',1,'preferences'))
                        {
                                $this->db->transaction_begin();
!                               $this->db->query("delete from phpgw_preferences 
where preference_owner=$account_id",
!                                       __LINE__,__FILE__);
  
!                               foreach($prefs as $app => $value)
                                {
!                                       if (!is_array($value)) continue;
!                                       $this->quote($value);
!                                       $value = addslashes(serialize($value)); 
// this addslashes is for the database
!                                       $app = $this->db->db_addslashes($app);
!                                       
!                                       $this->db->query($sql = "INSERT INTO 
phpgw_preferences".
!                                               " 
(preference_owner,preference_app,preference_value)".
!                                               " VALUES 
($account_id,'$app','$value')",__LINE__,__FILE__);
                                }
                                $this->db->transaction_commit();
                        }
***************
*** 233,237 ****
                        }
  
!                       if 
($GLOBALS['phpgw_info']['server']['cache_phpgw_info'] && $this->account_id == 
$GLOBALS['phpgw_info']['user']['account_id'])
                        {
                                
$GLOBALS['phpgw']->session->delete_cache($this->account_id);
--- 393,397 ----
                        }
  
!                       if (($type == 'user' || !$type) && 
$GLOBALS['phpgw_info']['server']['cache_phpgw_info'] && $this->account_id == 
$GLOBALS['phpgw_info']['user']['account_id'])
                        {
                                
$GLOBALS['phpgw']->session->delete_cache($this->account_id);
***************
*** 239,243 ****
                        }
                        
!                       return $temp_data;
                }
  
--- 399,403 ----
                        }
                        
!                       return $this->data;
                }
  
***************
*** 250,253 ****
--- 410,414 ----
                function create_defaults($account_id)
                {
+                       return; // not longer needed, as the defaults are 
merged in on runtime
                        $this->db->query("select * from phpgw_preferences where 
preference_owner='-2'",__LINE__,__FILE__);
                        $this->db->next_record();





reply via email to

[Prev in Thread] Current Thread [Next in Thread]