root/freepbx/trunk/amp_conf/htdocs/admin/featurecodes.class.php

Revision 5872, 8.7 kB (checked in by p_lindheimer, 6 months ago)

added support to featurescodes class to use freepbx_featurecodes.conf in place of hardcoded module settings for default values when supplied

  • Property svn:mime-type set to text/html
  • Property svn:eol-style set to native
Line 
1 <?php
2 class featurecode
3 {
4         var $_modulename;       // Module name
5         var $_featurename;      // Feature name
6         var $_description;      // Description (i.e. what the user will see)
7         var $_defaultcode;      // Default code if user doesn't pick one
8         var $_customcode;       // Custom code
9         var $_enabled;          // Enabled/Disabled (0=disabled; 1=enabled; -1=unknown)
10         var $_loaded;           // If this feature code was succesfully loaded from the DB
11         var $_overridecodes;            // Overide defaults from featurecodes.conf
12
13         // CONSTRUCTOR
14         function featurecode($modulename, $featurename) {
15                 global $amp_conf;
16
17                 if ($modulename == '' || $featurename == '')
18                         die_freepbx('feature code class must be called with ModuleName and FeatureName');
19
20                 $fd = $amp_conf['ASTETCDIR'].'/freepbx_featurecodes.conf';
21                 $this->_overridecodes = array();
22                 if (file_exists($fd)) {
23                         $this->_overridecodes = parse_ini_file($fd,true);
24                 }
25
26                 $this->_modulename = $modulename;
27                 $this->_featurename = $featurename;
28                 $this->_enabled = -1;  // -1 means not initialised
29                 $this->_loaded = false;
30         }
31
32         // HAS BEEN INIT'D ????
33         function isReady() {
34                 return (!($this->_enabled == -1));
35         }
36         
37         // INIT FUNCTION -- READS FROM DATABASE IF THERE BASICALLY
38         // $opt = 0 -- called by user code (i.e. outside this class)
39         // $opt = 1 -- called automatically by this class
40         // $opt = 2 -- called by user code, run even if called once already
41         function init($opt = 0) {
42                 if ($this->isReady()) {
43                         if ($opt < 2)
44                                 die_freepbx('FeatureCode: init already called!');
45                 }
46                         
47                 $s = "SELECT description, defaultcode, customcode, enabled ";
48                 $s .= "FROM featurecodes ";
49                 $s .= "WHERE modulename = ".sql_formattext($this->_modulename)." AND featurename = ".sql_formattext($this->_featurename)." ";
50                 
51                 $res = sql($s, "getRow");
52                 if (is_array($res)) { // found something, read it
53                         $this->_description = $res[0];
54                         if (isset($this->_overridecodes[$this->_modulename][$this->_featurename]) && trim($this->_overridecodes[$this->_modulename][$this->_featurename]) != '') {
55                                 $this->_defaultcode = $this->_overridecodes[$this->_modulename][$this->_featurename];
56                                 if ($this->_defaultcode != $res[1]) {
57                                         $sql = 'UPDATE featurecodes SET defaultcode = '.sql_formattext($this->_defaultcode).
58                                                 'WHERE modulename = '.sql_formattext($this->_modulename). ' AND featurename = '.sql_formattext($this->_featurename);
59                                         sql($sql, 'query');
60                                 }
61                         } else {
62                                 $this->_defaultcode = $res[1];
63                         }
64                         $this->_customcode = $res[2];
65                         $this->_enabled = $res[3];
66                         
67                         $this->_loaded = true;
68
69                         return true;
70                 } else {
71                         
72                         return false;
73                 }
74         }
75         
76         // UPDATE FUNCTION -- WRITES CURRENT STUFF BACK TO DATABASE
77         function update() {
78                 global $amp_conf;
79                 if ($this->_enabled == -1) {
80                         // not explicitly set, old default was to enable by default, we will preserve that behaviour
81                         $this->_enabled = 1;
82                 }
83
84                 if (!$this->isReady())
85                         die_freepbx('FeatureCode: class function init never called...will not update');
86
87                 
88                 if ($this->_loaded) {
89                         $sql = 'UPDATE featurecodes SET '.
90                                'description = '.sql_formattext($this->_description).', '.
91                                'defaultcode = '.sql_formattext($this->_defaultcode).', '.
92                                'customcode = '.sql_formattext($this->_customcode).', '.
93                                'enabled = '.sql_formattext($this->_enabled).' '.
94                                'WHERE modulename = '.sql_formattext($this->_modulename).
95                                ' AND featurename = '.sql_formattext($this->_featurename);
96                 } else {
97                         $sql = 'INSERT INTO featurecodes (modulename, featurename, description, defaultcode, customcode, enabled) '.
98                                'VALUES ('.sql_formattext($this->_modulename).', '.sql_formattext($this->_featurename).', '.sql_formattext($this->_description).', '.sql_formattext($this->_defaultcode).', '.sql_formattext($this->_customcode).', '.sql_formattext($this->_enabled).') ';
99                 }
100
101                 sql($sql, 'query');
102                 
103                 return true;
104         }
105         
106         // SET DESCRIPTION
107         function setDescription($description) {
108                 if (!$this->isReady())
109                         $this->init(1);
110
111                 if ($description == '') {
112                         unset($this->_description);
113                 } else {
114                         $this->_description = $description;
115                 }
116         }
117         
118         // GET DESCRIPTION
119         function getDescription() {
120                 if (!$this->isReady())
121                         $this->init(1);
122                 
123                 $desc = (isset($this->_description) ? $this->_description : '');
124                 
125                 return ($desc != '' ? $desc : $this->_featurename);
126         }
127         
128         // SET DEFAULT CODE
129         function setDefault($defaultcode, $defaultenabled = true) {
130                 if (!$this->isReady())
131                         $this->init(1);
132                         
133                 if (isset($this->_overridecodes[$this->_modulename][$this->_featurename])) {
134                         $defaultcode = $this->_overridecodes[$this->_modulename][$this->_featurename];
135                 }
136
137                 if (trim($defaultcode) == '') {
138                         unset($this->_defaultcode);
139                 } else {
140                         $this->_defaultcode = $defaultcode;                     
141                 }
142
143                 if ($this->_enabled == -1) {
144                         $this->_enabled = ($defaultenabled) ? 1 : 0;
145                 }
146
147         }
148         
149         // GET DEFAULT CODE
150         function getDefault() {
151                 if (!$this->isReady())
152                         $this->init(1);
153                 
154                 $def = (isset($this->_defaultcode) ? $this->_defaultcode : '');
155                 
156                 return $def;
157         }
158         
159         // SET CUSTOM CODE
160         function setCode($customcode) {
161                 if (!$this->isReady())
162                         $this->init(1);
163
164                 if ($customcode == '') {
165                         unset($this->_customcode);
166                 } else {
167                         $this->_customcode = $customcode;
168                 }
169         }
170         
171         // GET FEATURE CODE -- DEFAULT OR CUSTOM IF SET
172         //                     RETURN '' IF NOT AVAILABLE
173         function getCode() {
174                 if (!$this->isReady())
175                         $this->init(1);
176
177                 $curcode = (isset($this->_customcode) ? $this->_customcode : '');
178                 $defcode = (isset($this->_defaultcode) ? $this->_defaultcode : '');
179                 
180                 return ($curcode == '' ? $defcode : $curcode);
181         }
182         
183         // GET FEATURE CODE ONLY IF ENABLED
184         function getCodeActive() {
185                 if ($this->isEnabled()) {
186                         return $this->getCode();
187                 } else {
188                         return '';
189                 }
190         }
191         
192         // SET ENABLED
193         function setEnabled($b = true) {
194                 if (!$this->isReady())
195                         $this->init(1);
196
197                 $this->_enabled = ($b ? 1 : 0);
198         }
199         
200         // GET ENABLED
201         function isEnabled() {
202                 if (!$this->isReady())
203                         $this->init(1);
204
205                 return ($this->_enabled == 1);
206         }
207
208         function delete() {
209                 $s = "DELETE ";
210                 $s .= "FROM featurecodes ";
211                 $s .= "WHERE modulename = ".sql_formattext($this->_modulename)." ";
212                 $s .= "AND featurename = ".sql_formattext($this->_featurename);
213                 sql($s, 'query');
214                 
215                 $this->_enabled = -1; // = not ready
216                 
217                 return true;
218         }
219 }
220
221 // Helpers for eleswhere
222
223 // Return Array() of 'enabled' features for a specific module
224 function featurecodes_getModuleFeatures($modulename) {
225         $s = "SELECT featurename, description ";
226         $s .= "FROM featurecodes ";
227         $s .= "WHERE modulename = ".sql_formattext($modulename)." AND enabled = 1 ";
228
229         $results = sql($s, "getAll", DB_FETCHMODE_ASSOC);
230
231         if (is_array($results)) {
232                 return $results;
233         } else {
234                 return null;
235                 
236         }
237 }
238
239 function featurecodes_getAllFeaturesDetailed() {
240         global $amp_conf;
241
242         $fd = $amp_conf['ASTETCDIR'].'/freepbx_featurecodes.conf';
243         $overridecodes = array();
244         if (file_exists($fd)) {
245                 $overridecodes = parse_ini_file($fd,true);
246         }
247         $s = "SELECT featurecodes.modulename, featurecodes.featurename, featurecodes.description AS featuredescription, featurecodes.enabled AS featureenabled, featurecodes.defaultcode, featurecodes.customcode, ";
248         $s .= "modules.enabled AS moduleenabled ";
249         $s .= "FROM featurecodes ";
250         $s .= "INNER JOIN modules ON modules.modulename = featurecodes.modulename ";
251         $s .= "ORDER BY featurecodes.modulename, featurecodes.description ";
252         
253         $results = sql($s, "getAll", DB_FETCHMODE_ASSOC);
254         if (is_array($results)) {
255                 $modules = module_getinfo(false, MODULE_STATUS_ENABLED);
256                 foreach ($results as $key => $item) {
257
258                         // get the module display name
259                         $results[$key]['moduledescription'] = (!empty($modules[ $item['modulename'] ]['name']) ? $modules[ $item['modulename'] ]['name'] : ucfirst($item['modulename']));
260                         if (isset($overridecodes[$item['modulename']][$item['featurename']]) && trim($overridecodes[$item['modulename']][$item['featurename']]) != '') {
261                                 $results[$key]['defaultcode'] = $overridecodes[$item['modulename']][$item['featurename']];
262                         }
263                 }
264                 
265                 return $results;
266         } else {
267                 return null;
268         }
269 }
270
271 // removes all features for a specific module
272 function featurecodes_delModuleFeatures($modulename) {
273        $s = "DELETE ";
274        $s .= "FROM featurecodes ";
275        $s .= "WHERE modulename = ".sql_formattext($modulename);
276
277        sql($s, 'query');
278
279        return true;
280 }
281
282 function featurecodes_getFeatureCode($modulename, $featurename) {
283         $fc_code = '';
284         
285         $fcc = new featurecode($modulename, $featurename);
286         $fc_code = $fcc->getCodeActive();
287         unset($fcc);
288
289         return $fc_code != '' ? $fc_code : _('** MISSING FEATURE CODE **');
290 }
291
292 function featurecodes_delFeatureCode($modulename, $featurename) {
293        $s = "DELETE ";
294        $s .= "FROM featurecodes ";
295        $s .= "WHERE modulename = ".sql_formattext($modulename)." ";
296        $s .= "AND featurename = ".sql_formattext($featurename);
297
298        sql($s, 'query');
299
300        return true;
301 }
302
303 ?>
Note: See TracBrowser for help on using the browser.