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

Revision 7623, 17.6 kB (checked in by p_lindheimer, 4 years ago)

reverse logic fix on FCBEEPONLY

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     var $DEVSTATE;
10
11     // contstructor
12     function dayNightObject($item) {
13         global $amp_conf;
14
15         $this->id = $item;
16
17         if ($amp_conf['USEDEVSTATE']) {
18             $engine_info = engine_getinfo();
19             $version = $engine_info['version'];
20             $this->DEVSTATE = version_compare($version, "1.6", "ge") ? "DEVICE_STATE" : "DEVSTATE";
21         } else {
22             $this->DEVSTATE = false;
23         }
24     }
25         
26     function getState() {
27         global $astman;
28
29         if ($astman != null) {
30             $mode = $astman->database_get("DAYNIGHT","C".$this->id);
31             if ($mode != "DAY" && $mode != "NIGHT") {
32                 // TODO: should this return an error?
33                 return false;
34             } else {
35                 return $mode;
36             }
37         } else {
38             die_freepbx("No open connection to asterisk manager, can not access object.");
39         }
40     }
41
42     function setState($state) {
43         global $astman;
44
45         if ($this->getState() === false) {
46             die_freepbx("You must create the object before setting the state.");
47             return false;
48         } else {
49             switch ($state) {
50                 case "DAY":
51                 case "NIGHT":
52                     if ($astman != null) {
53                         $astman->database_put("DAYNIGHT","C".$this->id,$state);
54                         if ($this->DEVSTATE) {
55                             $value_opt = ($state  == 'DAY') ? 'NOT_INUSE' : 'INUSE';
56                             $astman->send_request('Command',array('Command'=>"core set global ".$this->DEVSTATE."(Custom:DAYNIGHT".$this->id.") $value_opt"));
57                         }
58                     } else {
59                         die_freepbx("No open connection to asterisk manager, can not access object.");
60                     }
61                     break;
62                 default:
63                     die_freepbx("Invalid state: $state");
64                     break;
65             }
66         }
67     }
68
69     function create($state="DAY") {
70         global $astman;
71
72         $current_state = $this->getState();
73         if ($current_state !== false) {
74             die_freepbx("Object already exists and is in state: $current_state, you must delete it first");
75             return false;
76         } else {
77             switch ($state) {
78                 case "DAY":
79                 case "NIGHT":
80                     if ($astman != null) {
81                         $astman->database_put("DAYNIGHT","C".$this->id,$state);
82                         if ($this->DEVSTATE) {
83                             $value_opt = ($state  == 'DAY') ? 'NOT_INUSE' : 'INUSE';
84                             $astman->send_request('Command',array('Command'=>"core set global ".$this->DEVSTATE."(Custom:DAYNIGHT".$this->id.") $value_opt"));
85                         }
86                     } else {
87                         die_freepbx("No open connection to asterisk manager, can not access object.");
88                     }
89                     break;
90                 default:
91                     die_freepbx("Invalid state: $state");
92                     break;
93             }
94         }
95     }
96
97     function del() {
98         global $astman;
99
100         if ($astman != null) {
101             $astman->database_del("DAYNIGHT","C".$this->id);
102         } else {
103             die_freepbx("No open connection to asterisk manager, can not access object.");
104         }
105     }
106 }
107
108 // The destinations this module provides
109 // returns a associative arrays with keys 'destination' and 'description'
110 function daynight_destinations() {
111
112     $list = daynight_list();
113     foreach ($list as $item) {
114         $dests = daynight_get_obj($item['ext']);
115         if (!isset($dests['day']) || !isset($dests['night'])) {
116             continue;
117         }
118         $description = $item['dest'] != ""?$item['dest']:"Day/Night Switch";
119         $description = "(".$item['ext'].") ".$description;
120         $extens[] = array('destination' => 'app-daynight,'.$item['ext'].',1', 'description' => $description);
121     }
122
123     // return an associative array with destination and description
124     if (isset($extens))
125         return $extens;
126     else
127         return null;
128 }
129
130 function daynight_getdest($exten) {
131     return array('app-daynight,'.$exten.',1');
132 }
133
134 function daynight_getdestinfo($dest) {
135     global $active_modules;
136
137     if (substr(trim($dest),0,13) == 'app-daynight,') {
138         $exten = explode(',',$dest);
139         $exten = $exten[1];
140
141         $thisexten = array();
142         $thislist = daynight_list($exten);
143         foreach ($thislist as $item) {
144             if ($item['ext'] == $exten) {
145                 $thisexten = $item;
146                 break;
147             }
148         }
149         if (empty($thisexten)) {
150             return array();
151         } else {
152             //$type = isset($active_modules['announcement']['type'])?$active_modules['announcement']['type']:'setup';
153             return array('description' => sprintf(_("Day/Night (%s) : %s"),$exten,$thisexten['dest']),
154                          'edit_url' => 'config.php?display=daynight&itemid='.urlencode($exten).'&action=edit',
155                                   );
156         }
157     } else {
158         return false;
159     }
160 }
161
162 function daynight_get_config($engine) {
163     global $ext;
164
165     switch($engine) {
166         case "asterisk":
167
168             $id = "app-daynight"; // The context to be included
169
170             $list = daynight_list();
171
172             foreach ($list as $item) {
173                 $dests = daynight_get_obj($item['ext']);
174                 $ext->add($id, $item['ext'], '', new ext_gotoif('$["${DB(DAYNIGHT/C${EXTEN})}" = "NIGHT"]',$dests['night'],$dests['day']));
175             }
176
177             daynight_toggle();
178
179             break;
180     }
181 }
182
183 function daynight_toggle() {
184     global $ext;
185     global $amp_conf;
186     global $version;
187     global $DEVSTATE;
188
189     $DEVSTATE = version_compare($version, "1.6", "ge") ? "DEVICE_STATE" : "DEVSTATE";
190
191     $list = daynight_list();
192     $passwords = daynight_passwords();
193     $got_code = false;
194     
195     $id = "app-daynight-toggle"; // The context to be included
196     foreach ($list as $item) {
197         $index = $item['ext'];
198         $fcc = new featurecode('daynight', 'toggle-mode-'.$index);
199         $c = $fcc->getCodeActive();
200         unset($fcc);
201         if (!$c) {
202             continue;
203         }
204         $got_code = true;
205         if ($amp_conf['USEDEVSTATE']) {
206             $ext->addHint($id, $c, 'Custom:DAYNIGHT'.$index);
207         }
208         $ext->add($id, $c, '', new ext_answer(''));
209         $ext->add($id, $c, '', new ext_wait('1'));
210         if (isset($passwords[$index]) && trim($passwords[$index]) != "" && ctype_digit(trim($passwords[$index]))) {
211             $ext->add($id, $c, '', new ext_authenticate($passwords[$index]));
212         }
213         $ext->add($id, $c, '', new ext_setvar('INDEX', $index));   
214         $ext->add($id, $c, '', new ext_goto($id.',s,1'));
215     }
216
217     if ($got_code) {
218         $ext->addInclude('from-internal-additional', $id); // Add the include from from-internal
219
220         $c='s';
221         $ext->add($id, $c, '', new ext_setvar('DAYNIGHTMODE', '${DB(DAYNIGHT/C${INDEX})}'));   
222         $ext->add($id, $c, '', new ext_gotoif('$["${DAYNIGHTMODE}" = "NIGHT"]', 'day', 'night'));
223
224         $ext->add($id, $c, 'day', new ext_setvar('DB(DAYNIGHT/C${INDEX})', 'DAY'));   
225         if ($amp_conf['USEDEVSTATE']) {
226             $ext->add($id, $c, '', new ext_setvar($DEVSTATE.'(Custom:DAYNIGHT${INDEX})', 'NOT_INUSE'));
227         }
228         if ($amp_conf['FCBEEPONLY']) {
229             $ext->add($id, $c, 'hook_day', new ext_playback('beep')); // $cmd,n,Playback(...)
230         } else {
231             $ext->add($id, $c, 'hook_day', new ext_playback('beep&silence/1&day&reception&digits/${INDEX}&enabled'));
232         }
233         $ext->add($id, $c, '', new ext_hangup(''));
234
235         $ext->add($id, $c, 'night', new ext_setvar('DB(DAYNIGHT/C${INDEX})', 'NIGHT'));   
236         if ($amp_conf['USEDEVSTATE']) {
237             $ext->add($id, $c, '', new ext_setvar($DEVSTATE.'(Custom:DAYNIGHT${INDEX})', 'INUSE'));
238         }
239         if ($amp_conf['FCBEEPONLY']) {
240             $ext->add($id, $c, 'hook_night', new ext_playback('beep')); // $cmd,n,Playback(...)
241         } else {
242             $ext->add($id, $c, 'hook_night', new ext_playback('beep&silence/1&beep&silence/1&day&reception&digits/${INDEX}&disabled'));
243         }
244         $ext->add($id, $c, '', new ext_hangup(''));
245     }
246 }
247
248 function daynight_get_avail() {
249     global $db;
250
251     $sql = "SELECT ext FROM daynight ORDER BY ext";
252     $results = $db->getCol($sql);
253     if(DB::IsError($results)) {
254         $results = array();
255     }
256
257     for ($i=0;$i<=9;$i++) {
258         if (!in_array($i,$results)) {
259             $list[]=$i;
260         }
261     }
262     return $list;
263 }
264
265 //get the existing daynight codes
266 function daynight_list() {
267     $results = sql("SELECT ext, dest FROM daynight WHERE dmode = 'fc_description' ORDER BY ext","getAll",DB_FETCHMODE_ASSOC);
268     if(is_array($results)){
269         foreach($results as $result){
270             $list[] = $result;
271         }
272     }
273     if (isset($list)) {
274         return $list;
275     } else {
276         return array();
277     }
278 }
279
280 //get the existing password codes
281 function daynight_passwords() {
282     $results = sql("SELECT ext, dest FROM daynight WHERE dmode = 'password'","getAll",DB_FETCHMODE_ASSOC);
283     if(is_array($results)){
284         foreach($results as $result){
285             $list[$result['ext']] = $result['dest'];
286         }
287     }
288     if (isset($list)) {
289         return $list;
290     } else {
291         return array();
292     }
293 }
294
295 function daynight_edit($post, $id=0) {
296     global $db;
297
298     // TODO: Probably have separate add and edit (and change in page.daynight.php also)
299     //       Need to set the day/night mode in the system if new
300
301     // Delete all the old dests
302     sql("DELETE FROM daynight WHERE dmode IN ('day', 'night', 'password', 'fc_description') AND ext = '$id'");
303
304     $day   = isset($post[$post['goto0'].'0'])?$post[$post['goto0'].'0']:'';
305     $night = isset($post[$post['goto1'].'1'])?$post[$post['goto1'].'1']:'';
306
307     sql("INSERT INTO daynight (ext, dmode, dest) VALUES ('$id', 'day', '$day')");
308     sql("INSERT INTO daynight (ext, dmode, dest) VALUES ('$id', 'night', '$night')");
309
310     if (isset($post['password']) && trim($post['password'] != "")) {
311         $password = trim($post['password']);
312         sql("INSERT INTO daynight (ext, dmode, dest) VALUES ('$id', 'password', '$password')");
313     }
314     $fc_description = isset($post['fc_description']) ? trim($post['fc_description']) : "";
315     sql("INSERT INTO daynight (ext, dmode, dest) VALUES ('$id', 'fc_description', '".$db->escapeSimple($fc_description)."')");
316
317     $dn = new dayNightObject($id);
318     $dn->del();
319     $dn->create($post['state']);
320
321     $fcc = new featurecode('daynight', 'toggle-mode-'.$id);
322     if ($fc_description) {
323         $fcc->setDescription("$id: $fc_description");
324     } else {
325         $fcc->setDescription("$id: Day Night Control");
326     }
327     $fcc->setDefault('*28'.$id);
328     $fcc->update();
329     unset($fcc);   
330
331     needreload();
332 }
333
334 function daynight_del($id){
335
336     // TODO: delete ASTDB entry when deleting the mode
337     //
338     $results = sql("DELETE FROM daynight WHERE ext = \"$id\"","query");
339
340     $fcc = new featurecode('daynight', 'toggle-mode-'.$id);
341     $fcc->delete();
342     unset($fcc);   
343
344     $dn = new dayNightObject($id);
345     $dn->del();
346     unset($dn);   
347 }
348
349 function daynight_get_obj($id=0) {
350     global $db;
351
352     $sql = "SELECT dmode, dest FROM daynight WHERE dmode IN ('day', 'night', 'password', 'fc_description') AND ext = '$id' ORDER BY dmode";
353     $res = $db->getAll($sql, DB_FETCHMODE_ASSOC);
354     if(DB::IsError($res)) {
355         return null;
356     }
357         foreach($res as $pair) {
358             $dmodes[$pair['dmode']] = $pair['dest'];
359         }
360         $dn = new dayNightObject($id);
361         $dmodes['state'] = $dn->getState();
362
363         return $dmodes;
364 }
365
366 /*
367 SELECT s1.ext ext, dest, dmode, s2.description descirption FROM daynight s1
368 INNER JOIN
369     (
370                   SELECT ext, dest description FROM daynight WHERE dmode = 'fc_description') s2
371                         ON s1.ext = s2.ext WHERE dmode in ('day','night')
372                         AND dest = '$dest'
373
374 Provides: ext, dest, dmode, description
375 */
376 function daynight_check_destinations($dest=true) {
377     global $active_modules;
378
379     $destlist = array();
380     if (is_array($dest) && empty($dest)) {
381         return $destlist;
382     }
383     $sql = "
384         SELECT s1.ext ext, dest, dmode, s2.description description FROM daynight s1
385         INNER JOIN
386             (
387                     SELECT ext, dest description FROM daynight WHERE dmode = 'fc_description') s2
388                     ON s1.ext = s2.ext WHERE dmode in ('day','night')
389         ";
390     if ($dest !== true) {
391         $sql .= "AND dest in ('".implode("','",$dest)."')";
392     }
393     $results = sql($sql,"getAll",DB_FETCHMODE_ASSOC);
394
395     //$type = isset($active_modules['announcement']['type'])?$active_modules['announcement']['type']:'setup';
396
397     foreach ($results as $result) {
398         $thisdest = $result['dest'];
399         $thisid   = $result['ext'];
400         $destlist[] = array(
401             'dest' => $thisdest,
402             'description' => sprintf(_("Daynight: %s (%s)"),$result['description'],$result['dmode']),
403             'edit_url' => 'config.php?display=daynight&itemid='.urlencode($thisid).'&action=edit',
404         );
405     }
406     return $destlist;
407 }
408
409 //-----------------------------------------------------------------------------------------------------
410 //-----------------------------------------------------------------------------------------------------
411 // TIMECONDITIONS HOOK:
412 //
413 // Helper Functions
414 //
415
416 // Note only one of these should be set, a feature code can't be associated with both the day and night mode
417 function daynight_get_timecondition($id=0) {
418     global $db;
419
420     $sql = "SELECT ext, dmode FROM daynight WHERE dmode IN ('timeday', 'timenight') AND dest = '$id' ORDER BY dmode";
421     $res = $db->getAll($sql, DB_FETCHMODE_ASSOC);
422     if(DB::IsError($res)) {
423         return null;
424     }
425     // we will start the loop but only return the first occurence since there should only be one
426     if (empty($res)) {
427         return array('ext' => '', 'dmode' => '');
428     } else {
429         foreach($res as $pair) {
430             return $pair;
431         }
432     }
433 }
434
435 function daynight_list_timecondition($daynight_id='all') {
436     global $db;
437
438     if ($daynight_id == 'all') {
439         $results = sql("SELECT ext, dmode, dest FROM daynight WHERE dmode IN ('timeday', 'timenight') ORDER BY dest","getAll",DB_FETCHMODE_ASSOC);
440     } else {
441         $results = sql("SELECT ext, dmode, dest FROM daynight WHERE dmode IN ('timeday', 'timenight') AND `ext` = '$daynight_id' ORDER BY CAST(dest AS UNSIGNED)","getAll",DB_FETCHMODE_ASSOC);
442     }
443     return $results;
444 }
445
446 function daynight_edit_timecondition($viewing_itemid, $daynight_ref) {
447     global $db;
448
449     $sql = "DELETE FROM `daynight` WHERE `dmode` IN ('timeday', 'timenight') AND dest = '$viewing_itemid'";
450     $res = $db->getAll($sql, DB_FETCHMODE_ASSOC);
451
452     if ($daynight_ref != '') {
453         $daynight_vals = explode(',',$daynight_ref,2);
454         $sql = "INSERT INTO `daynight` (`ext`, `dmode`, `dest`) VALUES ('".$daynight_vals[0]."', '".$daynight_vals[1]."', '$viewing_itemid')";
455         sql($sql);
456     }
457 }
458
459 function daynight_add_timecondition($daynight_ref) {
460     global $db;
461
462     // We don't know what the new timecondition id is yet so we will put a place holder and check it when the page reloads
463     //
464     daynight_edit_timecondition('add', $daynight_ref);
465 }
466
467 function daynight_checkadd_timecondition() {
468     global $db;
469
470     $sql = "SELECT ext FROM daynight WHERE dmode IN ('timeday', 'timenight') AND dest = 'add'";
471     $res = $db->getAll($sql, DB_FETCHMODE_ASSOC);
472     if(DB::IsError($res)) {
473         return null;
474     }
475
476     // If we find anything, then we get the highest timeconditions_id which should be the last one inserted
477     //
478     if (! empty($res)) {
479
480         $timeconditions_arr = timeconditions_list();
481
482         foreach ($timeconditions_arr as $item) {
483             $timeconditions_ids[] = $item['timeconditions_id'];
484         }
485         rsort($timeconditions_ids);
486         $viewing_itemid = $timeconditions_ids[0];
487
488         $sql = "UPDATE `daynight` SET `dest` = '$viewing_itemid' WHERE `dest` = 'add'";
489         sql($sql);
490     }
491 }
492
493 function daynight_del_timecondition($viewing_itemid) {
494     global $db;
495
496     $sql = "DELETE FROM `daynight` WHERE `dmode` IN ('timeday', 'timenight') AND dest = '$viewing_itemid'";
497     $res = $db->getAll($sql, DB_FETCHMODE_ASSOC);
498 }
499
500 // -----------------------------------------------------------------
501 // Hooks to associate a daynight featurecode with a timecondition
502 //
503 function daynight_hook_timeconditions($viewing_itemid, $target_menuid) {
504     global $tabindex;
505     switch ($target_menuid) {
506         // only provide display for timeconditions
507         case 'timeconditions':
508             $html = '';
509             $html = '<tr><td colspan="2"><h5>';
510             $html .= _("Day/Night Mode Association");
511             $html .= '<hr></h5></td></tr>';
512             $html .= '<tr>';
513             $html .= '<td><a href="#" class="info">';
514             $html .= _("Associate with").'<span>'._("If a selection is made, this timecondition will be associated with that featurecode and will allow this timecondition to be direct overriden by that daynight mode featurecode").'.</span></a>:</td>';
515             $html .= '<td><select tabindex="'.++$tabindex.'" name="daynight_ref">';
516             $daynightcodes = daynight_list();
517             $current = daynight_get_timecondition($viewing_itemid);
518             $html .= "\n";
519             $html .= sprintf('<option value="" %s>%s</option>',$current['ext'] == '' ?'selected':'', _("No Association"));
520             $html .= "\n";
521             foreach ($daynightcodes as $dn_item) {
522                 $html .= sprintf('<option value="%d,timeday" %s>%s</option>', $dn_item['ext'], ($current['ext'].','.$current['dmode'] == $dn_item['ext'].',timeday'?'selected':''), $dn_item['dest']._(" - Force Day"));
523                 $html .= "\n";
524                 $html .= sprintf('<option value="%d,timenight" %s>%s</option>', $dn_item['ext'], ($current['ext'].','.$current['dmode'] == $dn_item['ext'].',timenight'?'selected':''), $dn_item['dest']._(" - Force Night"));
525                 $html .= "\n";
526             }
527             $html .= '</select></td></tr>';
528
529             return $html;
530             break;
531         default:
532             return false;
533             break;
534     }
535 }
536
537 function daynight_hookProcess_timeconditions($viewing_itemid, $request) {
538
539     $daynight_ref = isset($request['daynight_ref']) ? $request['daynight_ref'] : '';
540
541     // Do the un-natural act of checking to see if the last call was an add
542     // in which case we left a place holder for the timeconditions_id and we
543     // need to go up-date it
544     //
545     // This is necessary because this process hook is called prior to the
546     // creation of the timecondition from the timeconditions module
547     //
548     if(!isset($request['action']) ) {
549         daynight_checkadd_timecondition($daynight_ref);
550     } else {
551         switch ($request['action'])    {
552             case 'add':
553                 // we don't have an viewing_itemid at this point
554                 daynight_add_timecondition($daynight_ref);
555                 break;
556             case 'delete':
557                 daynight_del_timecondition($viewing_itemid);
558                 break;
559             case 'edit':
560                 daynight_edit_timecondition($viewing_itemid, $daynight_ref);
561                 break;
562         }
563     }
564 }
565
566 // Splice into the timecondition dialplan and put an override if associated with a daynight mode code
567 //
568 function daynight_hookGet_config($engine) {
569     global $ext// is this the best way to pass this?
570     switch($engine) {
571         case "asterisk":
572             if (! function_exists('timeconditions_get')) {
573                 return true;
574             }
575             $overrides = daynight_list_timecondition();
576             $context = "timeconditions";
577
578             if(is_array($overrides)) {
579                 foreach($overrides as $item) {
580                     $daynight_id     = $item['ext'];
581                     $mode            = ($item['dmode'] == 'timeday') ? 'DAY' : 'NIGHT';
582                     $timecondition_id = $item['dest'];
583                     $timeconditions_arr = timeconditions_get($timecondition_id);
584                     if (is_array($timeconditions_arr)) {
585                         $dest = ($mode == 'DAY') ? $timeconditions_arr['truegoto'] : $timeconditions_arr['falsegoto'];
586                         $ext->splice($context, $timecondition_id, 0, new ext_gotoif('$["${DB(DAYNIGHT/C'.$daynight_id.')}" = "'.$mode.'"]',$dest));
587                     }
588                 }
589             }
590         break;
591     }
592 }
593
594 ?>
595
Note: See TracBrowser for help on using the browser.