source: sandbox/workflow/branches/993/lib/settings/Settings.php @ 2492

Revision 2492, 8.9 KB checked in by pedroerp, 14 years ago (diff)

Ticket #993 - Trocando acessos à GLOBALS por acessos à Settings.

Line 
1<?php
2/**************************************************************************\
3* eGroupWare                                                               *
4* http://www.egroupware.org                                                *
5* --------------------------------------------                             *
6*  This program is free software; you can redistribute it and/or modify it *
7*  under the terms of the GNU General Public License as published by the   *
8*  Free Software Foundation; either version 2 of the License, or (at your  *
9*  option) any later version.                                              *
10\**************************************************************************/
11
12/**
13 * General settings class. This class should be used to centralize all settings
14 * used by the module.
15 *
16 * @package Settings
17 * @license http://www.gnu.org/copyleft/gpl.html GPL
18 * @author Pedro Eugênio Rocha - pedro.eugenio.rocha@gmail.com
19 */
20class Settings {
21
22        /**
23         * @var array $_configs Here we should define which settings we are able
24         *                                              to get. The settings not defined here will throw
25         *                                              an exception when someone asks for it.
26         * @access private
27         * @static
28         */
29        private static $_configs = array(
30                'workflow' => array(
31                        'db_processes' => array(
32                                'name' => '',
33                                'host' => '',
34                                'port' => '',
35                                'admin_user' => '',
36                                'admin_password' => '',
37                                'type' => ''
38                        ),
39                        'db_module' => array(
40                                'name' => '',
41                                'host' => '',
42                                'port' => '',
43                                'user' => '',
44                                'password' => '',
45                                'type' => ''
46                        ),
47                        'ldap' => array(
48                                'host' => '',
49                                'user' => '',
50                                'password' => '',
51                                'follow_referrals' => '',
52                                'user_context' => '',
53                                'group_context' => ''
54                        ),
55                        'preferences' => array(
56                                'startpage' => '',
57                                'ui_items_per_page' => '',
58                                'show_activity_complete_page' => ''
59                        ),
60                        'intranet_subnetworks' => '',
61                        'user_can_clean_instances' => ''
62                ),
63                'expresso' => array(
64                        'db' => array(
65                                'name' => '',
66                                'host' => '',
67                                'port' => '',
68                                'user' => '',
69                                'password' => '',
70                                'type' => ''
71                        ),
72                        'ldap' => array(
73                                'host' => '',
74                                'user_context' => '',
75                                'group_context' => ''
76                        ),
77                        'user' => array(
78                                'account_id' => '',
79                                'account_lid' => '',
80                                'account_dn' => '',
81                                'userid' => '',
82                                'passwd' => '',
83                                'email' => ''
84                        ),
85                        'preferences' => array(
86                                'lang' => ''
87                        ),
88                        'webserver_url' => ''
89                )
90        );
91
92        /**
93         * @var boolean $_isLoaded Stores whether the settings are loaded or not.
94         * @access private
95         * @static
96         */
97        private static $_isLoaded = false;
98
99
100        /**
101         * Constructor. Just disabling direct instantiation.
102         *
103         * @access public
104         * @return void
105         */
106        public function __construct() {
107                throw new Exception("Oops! Static only class.");
108        }
109
110
111        /**
112         * Returns settings. If the hole path has not be setted, return the hole
113         * hierarchy under it.
114         *
115         * @param string $args  A variable number of parameters which specify the scope
116         *                                              and the configuration to be retrieved.
117         * @access public
118         * @return string
119         * @static
120         */
121        public static function get() {
122
123                /* getting the variable number of parameters */
124                $args = func_get_args();
125
126                /* checking if the key exists */
127                self::_keyExists($args);
128
129                /* check if we already load the settings */
130                if (!self::$_isLoaded)
131                        self::_load();
132
133                /* retrieving the setting. More 'for' programming. */
134                for ($i=0, $value=&self::$_configs; $i<count($args); $value=&$value[$args[$i++]]);
135
136                return $value;
137        }
138
139
140        public static function set() {
141
142                /* getting the variable number of parameters */
143                $args = func_get_args();
144
145                /* the last position is always the value that we are trying to set, so pop it */
146                $value = array_pop($args);
147
148                /* checking if the key exists */
149                self::_keyExists($args);
150
151                /* check if we already load the settings */
152                if (!self::$_isLoaded)
153                        self::_load();
154
155                /* retrieving the setting. More 'for' programming. */
156                for ($i=0, $key=&self::$_configs; $i<count($args); $key=&$key[$args[$i++]]);
157
158                /* updating the value. p.s: $key is a pointer to a $_config node */
159                $key = $value;
160                return true;
161        }
162
163        /**
164         * Here we check if the configuration was defined into $_configs array.
165         * If it does not, an exception will be thrown.
166         *
167         * @param string $args  A variable number of parameters which specify the scope
168         *                                              and the configuration to be retrieved.
169         * @access private
170         * @return boolean
171         * @static
172         */
173        private static function _keyExists($path) {
174
175                /**
176                 * p.s: Do not try to understand this piece of code. Just fave faith! It works...
177                 */
178                for ($i=0, $sub_array=&self::$_configs; $i<count($path); $sub_array=&$sub_array[$path[$i++]]) {
179                        if (!array_key_exists($path[$i], $sub_array))
180                                throw new Exception("Sorry! Setting '".implode("->", $path)."' do not exists.");
181                }
182                return true;
183        }
184
185
186        /**
187         * Loads settings into $_configs private array.
188         *
189         * @access private
190         * @return void
191         * @static
192         */
193        private static function _load() {
194
195                /* loading global variables into $_configs */
196                self::$_configs['expresso']['db']['name'] = $GLOBALS['phpgw_info']['server']['db_name'];
197                self::$_configs['expresso']['db']['host'] = $GLOBALS['phpgw_info']['server']['db_host'];
198                self::$_configs['expresso']['db']['port'] = $GLOBALS['phpgw_info']['server']['db_port'];
199                self::$_configs['expresso']['db']['user'] = $GLOBALS['phpgw_info']['server']['db_user'];
200                self::$_configs['expresso']['db']['password'] = $GLOBALS['phpgw_info']['server']['db_pass'];
201                self::$_configs['expresso']['db']['type'] = $GLOBALS['phpgw_info']['server']['db_type'];
202
203                /* current user information */
204                self::$_configs['expresso']['user']['account_id'] = $GLOBALS['phpgw_info']['user']['account_id'];
205                self::$_configs['expresso']['user']['account_lid'] = $GLOBALS['phpgw_info']['user']['account_lid'];
206                self::$_configs['expresso']['user']['account_dn'] = $GLOBALS['phpgw_info']['user']['account_dn'];
207                self::$_configs['expresso']['user']['userid'] = $GLOBALS['phpgw_info']['user']['userid'];
208                self::$_configs['expresso']['user']['passwd'] = $GLOBALS['phpgw_info']['user']['passwd'];
209                self::$_configs['expresso']['user']['email'] = $GLOBALS['phpgw_info']['user']['email'];
210
211                self::$_configs['expresso']['ldap']['host'] = $GLOBALS['phpgw_info']['server']['ldap_host'];
212                self::$_configs['expresso']['ldap']['user_context'] = $GLOBALS['phpgw_info']['server']['ldap_user_context'];
213                self::$_configs['expresso']['ldap']['group_context'] = $GLOBALS['phpgw_info']['server']['ldap_group_context'];
214
215                /* workflow specific preferences */
216                self::$_configs['workflow']['preferences']['startpage'] = $GLOBALS['phpgw_info']['user']['preferences']['workflow']['startpage'];
217                self::$_configs['workflow']['preferences']['ui_items_per_page'] = $GLOBALS['phpgw_info']['user']['preferences']['workflow']['ui_items_per_page'];
218                self::$_configs['workflow']['preferences']['show_activity_complete_page'] = $GLOBALS['phpgw_info']['user']['preferences']['workflow']['show_activity_complete_page'];
219
220                /*expresso preferences */
221                self::$_configs['expresso']['preferences']['lang'] = $GLOBALS['phpgw_info']['user']['preferences']['common']['lang'];
222
223                self::$_configs['expresso']['webserver_url'] = $GLOBALS['phpgw_info']['server']['webserver_url'];
224
225                /* loading database settings */
226                $config = &Factory::newInstance('config', 'workflow');
227                $values = $config->read_repository();
228
229                self::$_configs['workflow']['intranet_subnetworks'] = $values['intranet_subnetworks'];
230
231                self::$_configs['workflow']['db_processes']['name'] = $values['database_name'];
232                self::$_configs['workflow']['db_processes']['host'] = $values['database_host'];
233                self::$_configs['workflow']['db_processes']['port'] = $values['database_port'];
234                self::$_configs['workflow']['db_processes']['admin_user'] = $values['database_admin_user'];
235                self::$_configs['workflow']['db_processes']['admin_password'] = $values['database_admin_password'];
236                self::$_configs['workflow']['db_processes']['type'] = $values['database_type'];
237
238                self::$_configs['workflow']['db_module']['name'] = $values['workflow_database_name'];
239                self::$_configs['workflow']['db_module']['host'] = $values['workflow_database_host'];
240                self::$_configs['workflow']['db_module']['port'] = $values['workflow_database_port'];
241                self::$_configs['workflow']['db_module']['user'] = $values['workflow_database_user'];
242                self::$_configs['workflow']['db_module']['password'] = $values['workflow_database_password'];
243                self::$_configs['workflow']['db_module']['type'] = $values['workflow_database_type'];
244
245                self::$_configs['workflow']['ldap']['host'] = $values['ldap_host'];
246                self::$_configs['workflow']['ldap']['user'] = $values['ldap_user'];
247                self::$_configs['workflow']['ldap']['password'] = $values['ldap_password'];
248                self::$_configs['workflow']['ldap']['follow_referrals'] = $values['ldap_follow_referrals'];
249                self::$_configs['workflow']['ldap']['user_context'] = $values['ldap_user_context'];
250                self::$_configs['workflow']['ldap']['group_context'] = $values['ldap_group_context'];
251
252                /* the settings are already loaded. No need to call this method again. */
253                self::$_isLoaded = true;
254        }
255}
256
257?>
Note: See TracBrowser for help on using the repository browser.