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

Revision 5845, 7.2 kB (checked in by p_lindheimer, 4 years ago)

Merged revisions 5842-5843 via svnmerge from
http://svn.freepbx.org/modules/branches/2.4

........

r5842 | p_lindheimer | 2008-06-29 15:24:35 -0700 (Sun, 29 Jun 2008) | 1 line


#2734 fixed issue creating index with no description made it disapear

........

r5843 | p_lindheimer | 2008-06-29 15:26:26 -0700 (Sun, 29 Jun 2008) | 1 line


Module Publish Script: daynight 2.4.0.3

........

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_get_config($engine) {
112   global $ext;
113
114   switch($engine) {
115     case "asterisk":
116
117       $id = "app-daynight"; // The context to be included
118       $ext->addInclude('from-internal-additional', $id); // Add the include from from-internal
119
120       $list = daynight_list();
121
122       foreach ($list as $item) {
123         $dests = daynight_get_obj($item['ext']);
124         $ext->add($id, $item['ext'], '', new ext_gotoif('$["${DB(DAYNIGHT/C${EXTEN})}" = "NIGHT"]',$dests['night'],$dests['day']));
125       }
126
127       daynight_toggle();
128
129       break;
130   }
131 }
132
133 function daynight_toggle() {
134   global $ext;
135
136   $fcc = new featurecode('daynight', 'toggle-mode');
137   $c = $fcc->getCodeActive();
138   unset($fcc);
139
140   if (!empty($c)) {
141     $id = "app-daynight-toggle"; // The context to be included
142
143     $ext->addInclude('from-internal-additional', $id); // Add the include from from-internal
144
145     $list = daynight_list();
146     $passwords = daynight_passwords();
147     foreach ($list as $item) {
148       $index = $item['ext'];
149       $ext->add($id, $c.$index, '', new ext_answer(''));
150       $ext->add($id, $c.$index, '', new ext_wait('1'));
151
152       if (isset($passwords[$index]) && trim($passwords[$index]) != "" && ctype_digit(trim($passwords[$index]))) {
153         $ext->add($id, $c.$index, '', new ext_authenticate($passwords[$index]));
154       }
155       $ext->add($id, $c.$index, '', new ext_setvar('INDEX', $index));
156       $ext->add($id, $c.$index, '', new ext_goto($id.',s,1'));
157     }
158
159     $c='s';
160     $ext->add($id, $c, '', new ext_setvar('DAYNIGHTMODE', '${DB(DAYNIGHT/C${INDEX})}')); 
161     $ext->add($id, $c, '', new ext_gotoif('$["${DAYNIGHTMODE}" = "NIGHT"]', 'day', 'night'));
162
163     $ext->add($id, $c, 'day', new ext_setvar('DB(DAYNIGHT/C${INDEX})', 'DAY'));
164     $ext->add($id, $c, '', new ext_playback('beep&silence/1&day&reception&digits/${INDEX}&enabled'));
165     $ext->add($id, $c, '', new ext_hangup(''));
166
167     $ext->add($id, $c, 'night', new ext_setvar('DB(DAYNIGHT/C${INDEX})', 'NIGHT'));
168     $ext->add($id, $c, '', new ext_playback('beep&silence/1&beep&silence/1&day&reception&digits/${INDEX}&disabled'));
169     $ext->add($id, $c, '', new ext_hangup(''));
170   }
171 }
172
173 function daynight_get_avail() {
174   global $db;
175
176   $sql = "SELECT ext FROM daynight ORDER BY ext";
177   $results = $db->getCol($sql);
178   if(DB::IsError($results)) {
179     $results = array();
180   }
181
182   for ($i=0;$i<=9;$i++) {
183     if (!in_array($i,$results)) {
184       $list[]=$i;
185     }
186   }
187   return $list;
188 }
189
190 //get the existing daynight codes
191 function daynight_list() {
192   $results = sql("SELECT ext, dest FROM daynight WHERE dmode = 'fc_description' ORDER BY ext","getAll",DB_FETCHMODE_ASSOC);
193   if(is_array($results)){
194     foreach($results as $result){
195       $list[] = $result;
196     }
197   }
198   if (isset($list)) {
199     return $list;
200   } else {
201     return array();
202   }
203 }
204
205 //get the existing password codes
206 function daynight_passwords() {
207   $results = sql("SELECT ext, dest FROM daynight WHERE dmode = 'password'","getAll",DB_FETCHMODE_ASSOC);
208   if(is_array($results)){
209     foreach($results as $result){
210       $list[$result['ext']] = $result['dest'];
211     }
212   }
213   if (isset($list)) {
214     return $list;
215   } else {
216     return array();
217   }
218 }
219
220 function daynight_edit($post, $id=0) {
221
222   // TODO: Probably have separate add and edit (and change in page.daynight.php also)
223   //       Need to set the day/night mode in the system if new
224
225   // Delete all the old dests
226   sql("DELETE FROM daynight WHERE dmode IN ('day', 'night', 'password', 'fc_description') AND ext = '$id'");
227
228   $day   = isset($post[$post['goto0'].'0'])?$post[$post['goto0'].'0']:'';
229   $night = isset($post[$post['goto1'].'1'])?$post[$post['goto1'].'1']:'';
230
231   sql("INSERT INTO daynight (ext, dmode, dest) VALUES ('$id', 'day', '$day')");
232   sql("INSERT INTO daynight (ext, dmode, dest) VALUES ('$id', 'night', '$night')");
233
234   if (isset($post['password']) && trim($post['password'] != "")) {
235     $password = trim($post['password']);
236     sql("INSERT INTO daynight (ext, dmode, dest) VALUES ('$id', 'password', '$password')");
237   }
238   $fc_description = isset($post['fc_description']) ? trim($post['fc_description']) : "";
239   sql("INSERT INTO daynight (ext, dmode, dest) VALUES ('$id', 'fc_description', '$fc_description')");
240
241   $dn = new dayNightObject($id);
242   $dn->del();
243   $dn->create($post['state']);
244
245   needreload();
246 }
247
248 function daynight_del($id){
249
250   // TODO: delete ASTDB entry when deleting the mode
251   //
252   $results = sql("DELETE FROM daynight WHERE ext = \"$id\"","query");
253 }
254
255 function daynight_get_obj($id=0) {
256   global $db;
257
258   $sql = "SELECT dmode, dest FROM daynight WHERE dmode IN ('day', 'night', 'password', 'fc_description') AND ext = '$id' ORDER BY dmode";
259   $res = $db->getAll($sql, DB_FETCHMODE_ASSOC);
260   if(DB::IsError($res)) {
261     return null;
262   }
263     foreach($res as $pair) {
264       $dmodes[$pair['dmode']] = $pair['dest'];
265     }
266     $dn = new dayNightObject($id);
267     $dmodes['state'] = $dn->getState();
268
269     return $dmodes;
270 }
271 ?>
Note: See TracBrowser for help on using the browser.