root/modules/branches/2.5/daynight/functions.inc.php

Revision 5860, 10.0 kB (checked in by p_lindheimer, 5 years ago)

change to create feature code for each index, add func_devstate blf

Line 
1 <?php
2  /* $Id: functions.inc.php 4024 2007-06-09 03:09:16Z p_lindheimer $ */
3
4 // Class To Create, Access and Change DAYNIGHT objects in the dialplan
5 //
6 class dayNightObject {
7
8     var $id;
9
10     // contstructor
11     function dayNightObject($item) {
12         $this->id = $item;
13     }
14         
15     function getState() {
16         global $astman;
17
18         if ($astman != null) {
19             $mode = $astman->database_get("DAYNIGHT","C".$this->id);
20             if ($mode != "DAY" && $mode != "NIGHT") {
21                 // TODO: should this return an error?
22                 return false;
23             } else {
24                 return $mode;
25             }
26         } else {
27             die_freepbx("No open connection to asterisk manager, can not access object.");
28         }
29     }
30
31     function setState($state) {
32         global $astman;
33
34         if ($this->getState() === false) {
35             die_freepbx("You must create the object before setting the state.");
36             return false;
37         } else {
38             switch ($state) {
39                 case "DAY":
40                 case "NIGHT":
41                     if ($astman != null) {
42                         $astman->database_put("DAYNIGHT","C".$this->id,$state);
43                     } else {
44                         die_freepbx("No open connection to asterisk manager, can not access object.");
45                     }
46                     break;
47                 default:
48                     die_freepbx("Invalid state: $state");
49                     break;
50             }
51         }
52     }
53
54     function create($state="DAY") {
55         global $astman;
56
57         $current_state = $this->getState();
58         if ($current_state !== false) {
59             die_freepbx("Object already exists and is in state: $current_state, you must delete it first");
60             return false;
61         } else {
62             switch ($state) {
63                 case "DAY":
64                 case "NIGHT":
65                     if ($astman != null) {
66                         $astman->database_put("DAYNIGHT","C".$this->id,$state);
67                     } else {
68                         die_freepbx("No open connection to asterisk manager, can not access object.");
69                     }
70                     break;
71                 default:
72                     die_freepbx("Invalid state: $state");
73                     break;
74             }
75         }
76     }
77
78     function del() {
79         global $astman;
80
81         if ($astman != null) {
82             $astman->database_del("DAYNIGHT","C".$this->id);
83         } else {
84             die_freepbx("No open connection to asterisk manager, can not access object.");
85         }
86     }
87 }
88
89 // The destinations this module provides
90 // returns a associative arrays with keys 'destination' and 'description'
91 function daynight_destinations() {
92
93     $list = daynight_list();
94     foreach ($list as $item) {
95         $dests = daynight_get_obj($item['ext']);
96         if (!isset($dests['day']) || !isset($dests['night'])) {
97             continue;
98         }
99         $description = $item['dest'] != ""?$item['dest']:"Day/Night Switch";
100         $description = "(".$item['ext'].") ".$description;
101         $extens[] = array('destination' => 'app-daynight,'.$item['ext'].',1', 'description' => $description);
102     }
103
104     // return an associative array with destination and description
105     if (isset($extens))
106         return $extens;
107     else
108         return null;
109 }
110
111 function daynight_getdest($exten) {
112     return array('app-daynight,'.$exten.',1');
113 }
114
115 function daynight_getdestinfo($dest) {
116     global $active_modules;
117
118     if (substr(trim($dest),0,13) == 'app-daynight,') {
119         $exten = explode(',',$dest);
120         $exten = $exten[1];
121
122         $thisexten = array();
123         $thislist = daynight_list($exten);
124         foreach ($thislist as $item) {
125             if ($item['ext'] == $exten) {
126                 $thisexten = $item;
127                 break;
128             }
129         }
130         if (empty($thisexten)) {
131             return array();
132         } else {
133             //$type = isset($active_modules['announcement']['type'])?$active_modules['announcement']['type']:'setup';
134             return array('description' => 'Day/Night ('.$exten.') : '.$thisexten['dest'],
135                          'edit_url' => 'config.php?display=daynight&itemid='.urlencode($exten).'&action=edit',
136                                   );
137         }
138     } else {
139         return false;
140     }
141 }
142
143 function daynight_get_config($engine) {
144     global $ext;
145
146     switch($engine) {
147         case "asterisk":
148
149             $id = "app-daynight"; // The context to be included
150             $ext->addInclude('from-internal-additional', $id); // Add the include from from-internal
151
152             $list = daynight_list();
153
154             foreach ($list as $item) {
155                 $dests = daynight_get_obj($item['ext']);
156                 $ext->add($id, $item['ext'], '', new ext_gotoif('$["${DB(DAYNIGHT/C${EXTEN})}" = "NIGHT"]',$dests['night'],$dests['day']));
157             }
158
159             daynight_toggle();
160
161             break;
162     }
163 }
164
165 function daynight_toggle() {
166     global $ext;
167
168     $list = daynight_list();
169     $passwords = daynight_passwords();
170     $got_code = false;
171     
172     $id = "app-daynight-toggle"; // The context to be included
173     foreach ($list as $item) {
174         $index = $item['ext'];
175         $fcc = new featurecode('daynight', 'toggle-mode-'.$index);
176         $c = $fcc->getCodeActive();
177         unset($fcc);
178         if (!$c) {
179             continue;
180         }
181         $got_code = true;
182         if ($amp_conf['USEDEVSTATE']) {
183             $ext->addHint($id, $c, 'Custom:DAYNIGHT'.$index);
184         }
185         $ext->add($id, $c, '', new ext_answer(''));
186         $ext->add($id, $c, '', new ext_wait('1'));
187         if (isset($passwords[$index]) && trim($passwords[$index]) != "" && ctype_digit(trim($passwords[$index]))) {
188             $ext->add($id, $c, '', new ext_authenticate($passwords[$index]));
189         }
190         $ext->add($id, $c, '', new ext_setvar('INDEX', $index));   
191         $ext->add($id, $c, '', new ext_goto($id.',s,1'));
192     }
193
194     if ($got_code) {
195         $ext->addInclude('from-internal-additional', $id); // Add the include from from-internal
196
197         $c='s';
198         $ext->add($id, $c, '', new ext_setvar('DAYNIGHTMODE', '${DB(DAYNIGHT/C${INDEX})}'));   
199         $ext->add($id, $c, '', new ext_gotoif('$["${DAYNIGHTMODE}" = "NIGHT"]', 'day', 'night'));
200
201         $ext->add($id, $c, 'day', new ext_setvar('DB(DAYNIGHT/C${INDEX})', 'DAY'));   
202         if ($amp_conf['USEDEVSTATE']) {
203             $ext->add($id, $c, '', new ext_setvar('DEVSTATE(Custom:DAYNIGHT${INDEX})', 'NOT_INUSE'));
204         }
205         $ext->add($id, $c, '', new ext_playback('beep&silence/1&day&reception&digits/${INDEX}&enabled'));
206         $ext->add($id, $c, '', new ext_hangup(''));
207
208         $ext->add($id, $c, 'night', new ext_setvar('DB(DAYNIGHT/C${INDEX})', 'NIGHT'));   
209         if ($amp_conf['USEDEVSTATE']) {
210             $ext->add($id, $c, '', new ext_setvar('DEVSTATE(Custom:DAYNIGHT${INDEX})', 'INUSE'));
211         }
212         $ext->add($id, $c, '', new ext_playback('beep&silence/1&beep&silence/1&day&reception&digits/${INDEX}&disabled'));
213         $ext->add($id, $c, '', new ext_hangup(''));
214     }
215 }
216
217 function daynight_get_avail() {
218     global $db;
219
220     $sql = "SELECT ext FROM daynight ORDER BY ext";
221     $results = $db->getCol($sql);
222     if(DB::IsError($results)) {
223         $results = array();
224     }
225
226     for ($i=0;$i<=9;$i++) {
227         if (!in_array($i,$results)) {
228             $list[]=$i;
229         }
230     }
231     return $list;
232 }
233
234 //get the existing daynight codes
235 function daynight_list() {
236     $results = sql("SELECT ext, dest FROM daynight WHERE dmode = 'fc_description' ORDER BY ext","getAll",DB_FETCHMODE_ASSOC);
237     if(is_array($results)){
238         foreach($results as $result){
239             $list[] = $result;
240         }
241     }
242     if (isset($list)) {
243         return $list;
244     } else {
245         return array();
246     }
247 }
248
249 //get the existing password codes
250 function daynight_passwords() {
251     $results = sql("SELECT ext, dest FROM daynight WHERE dmode = 'password'","getAll",DB_FETCHMODE_ASSOC);
252     if(is_array($results)){
253         foreach($results as $result){
254             $list[$result['ext']] = $result['dest'];
255         }
256     }
257     if (isset($list)) {
258         return $list;
259     } else {
260         return array();
261     }
262 }
263
264 function daynight_edit($post, $id=0) {
265
266     // TODO: Probably have separate add and edit (and change in page.daynight.php also)
267     //       Need to set the day/night mode in the system if new
268
269     // Delete all the old dests
270     sql("DELETE FROM daynight WHERE dmode IN ('day', 'night', 'password', 'fc_description') AND ext = '$id'");
271
272     $day   = isset($post[$post['goto0'].'0'])?$post[$post['goto0'].'0']:'';
273     $night = isset($post[$post['goto1'].'1'])?$post[$post['goto1'].'1']:'';
274
275     sql("INSERT INTO daynight (ext, dmode, dest) VALUES ('$id', 'day', '$day')");
276     sql("INSERT INTO daynight (ext, dmode, dest) VALUES ('$id', 'night', '$night')");
277
278     if (isset($post['password']) && trim($post['password'] != "")) {
279         $password = trim($post['password']);
280         sql("INSERT INTO daynight (ext, dmode, dest) VALUES ('$id', 'password', '$password')");
281     }
282     $fc_description = isset($post['fc_description']) ? trim($post['fc_description']) : "";
283     sql("INSERT INTO daynight (ext, dmode, dest) VALUES ('$id', 'fc_description', '".addslashes($fc_description)."')");
284
285     $dn = new dayNightObject($id);
286     $dn->del();
287     $dn->create($post['state']);
288
289     $fcc = new featurecode('daynight', 'toggle-mode-'.$id);
290     if ($fc_description) {
291         $fcc->setDescription("$id: $fc_description");
292     } else {
293         $fcc->setDescription("$id: Day Night Control");
294     }
295     $fcc->setDefault('*28'.$id);
296     $fcc->update();
297     unset($fcc);   
298
299     needreload();
300 }
301
302 function daynight_del($id){
303
304     // TODO: delete ASTDB entry when deleting the mode
305     //
306     $results = sql("DELETE FROM daynight WHERE ext = \"$id\"","query");
307
308     $fcc = new featurecode('daynight', 'toggle-mode-'.$id);
309     $fcc->delete();
310     unset($fcc);   
311 }
312
313 function daynight_get_obj($id=0) {
314     global $db;
315
316     $sql = "SELECT dmode, dest FROM daynight WHERE dmode IN ('day', 'night', 'password', 'fc_description') AND ext = '$id' ORDER BY dmode";
317     $res = $db->getAll($sql, DB_FETCHMODE_ASSOC);
318     if(DB::IsError($res)) {
319         return null;
320     }
321         foreach($res as $pair) {
322             $dmodes[$pair['dmode']] = $pair['dest'];
323         }
324         $dn = new dayNightObject($id);
325         $dmodes['state'] = $dn->getState();
326
327         return $dmodes;
328 }
329
330
331 /*
332 SELECT s1.ext ext, dest, dmode, s2.description descirption FROM daynight s1
333 INNER JOIN
334     (
335                   SELECT ext, dest description FROM daynight WHERE dmode = 'fc_description') s2
336                         ON s1.ext = s2.ext WHERE dmode in ('day','night')
337                         AND dest = '$dest'
338
339 Provides: ext, dest, dmode, description
340 */
341 function daynight_check_destinations($dest=true) {
342     global $active_modules;
343
344     $destlist = array();
345     if (is_array($dest) && empty($dest)) {
346         return $destlist;
347     }
348     $sql = "
349         SELECT s1.ext ext, dest, dmode, s2.description description FROM daynight s1
350         INNER JOIN
351             (
352                     SELECT ext, dest description FROM daynight WHERE dmode = 'fc_description') s2
353                     ON s1.ext = s2.ext WHERE dmode in ('day','night')
354         ";
355     if ($dest !== true) {
356         $sql .= "AND dest in ('".implode("','",$dest)."')";
357     }
358     $results = sql($sql,"getAll",DB_FETCHMODE_ASSOC);
359
360     //$type = isset($active_modules['announcement']['type'])?$active_modules['announcement']['type']:'setup';
361
362     foreach ($results as $result) {
363         $thisdest = $result['dest'];
364         $thisid   = $result['ext'];
365         $destlist[] = array(
366             'dest' => $thisdest,
367             'description' => 'Daynight: '.$result['description'].' ('.$result['dmode'].')',
368             'edit_url' => 'config.php?display=daynight&itemid='.urlencode($thisid).'&action=edit',
369         );
370     }
371     return $destlist;
372 }
373 ?>
374
Note: See TracBrowser for help on using the browser.