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

Revision 4640, 17.0 kB (checked in by p_lindheimer, 5 years ago)

added xml attribute needsenginedb, #1961 enabled to work with extension numbers leading with 0s

  • Property svn:mime-type set to text/html
  • Property svn:eol-style set to native
Line 
1 <?php /* $Id: functions.inc.php 175 2006-10-03 19:12:39Z plindheimer $ */
2
3 // The destinations this module provides
4 // returns a associative arrays with keys 'destination' and 'description'
5 /*
6
7 // Remove destinations, use from-did-direct in core for 2.3 and on
8
9 function findmefollow_destinations() {
10   //get the list of findmefollow
11   $results = findmefollow_full_list();
12  
13   // return an associative array with destination and description
14   if (isset($results)) {
15     foreach($results as $result){
16         $thisgrp = findmefollow_get(ltrim($result['0']));
17         $extens[] = array('destination' => 'ext-findmefollow,FM'.ltrim($result['0']).',1', 'description' => $thisgrp['grppre'].' <'.ltrim($result['0']).'>');
18     }
19   }
20  
21   return isset($extens)?$extens:null;
22 }
23 */
24
25 /*  Generates dialplan for findmefollow
26   We call this with retrieve_conf
27 */
28 function findmefollow_get_config($engine) {
29   global $ext;  // is this the best way to pass this?
30   switch($engine) {
31     case "asterisk":
32       $ext->addInclude('from-internal-additional','ext-findmefollow');
33       $ext->addInclude('from-internal-additional','fmgrps');
34       $contextname = 'ext-findmefollow';
35       $ringlist = findmefollow_full_list();
36       if (is_array($ringlist)) {
37         foreach($ringlist as $item) {
38           $grpnum = ltrim($item['0']);
39           $grp = findmefollow_get($grpnum);
40           
41           $strategy = $grp['strategy'];
42           $grptime = $grp['grptime'];
43           $grplist = $grp['grplist'];
44           $postdest = $grp['postdest'];
45           $grppre = (isset($grp['grppre'])?$grp['grppre']:'');
46           $annmsg = $grp['annmsg'];
47           $dring = $grp['dring'];
48
49           $needsconf = $grp['needsconf'];
50           $remotealert = $grp['remotealert'];
51           $toolate = $grp['toolate'];
52           $ringing = $grp['ringing'];
53           $pre_ring = $grp['pre_ring'];
54
55           if($ringing == 'Ring' || empty($ringing) ) {
56             $dialopts = '${DIAL_OPTIONS}';
57           } else {
58             // We need the DIAL_OPTIONS variable
59             $sops = sql("SELECT value from globals where variable='DIAL_OPTIONS'", "getRow");
60             $dialopts = "m(${ringing})".str_replace('r', '', $sops[0]);
61           }
62
63
64           // Direct target to Follow-Me come here bypassing the followme/ddial conditional check
65           //
66           $ext->add($contextname, 'FM'.$grpnum, '', new ext_goto("$grpnum,FM$grpnum"));
67
68           //
69           // If the followme is configured for extension dialing to go to the the extension and not followme then
70           // go there. This is often used in VmX Locater functionality when the user does not want the followme
71           // to automatically be called but only if chosen by the caller as an alternative to going to voicemail
72           //
73           $ext->add($contextname, $grpnum, '', new ext_gotoif('$[ "${DB(AMPUSER/'.$grpnum.'/followme/ddial)}" = "EXTENSION" ]', 'ext-local,'.$grpnum.',1'));
74           $ext->add($contextname, $grpnum, 'FM'.$grpnum, new ext_macro('user-callerid'));
75
76           // block voicemail until phone is answered at which point a macro should be called on the answering
77           // line to clear this flag so that subsequent transfers can occur, if already set by a the caller
78           // then don't change.
79           //
80           $ext->add($contextname, $grpnum, '', new ext_gotoif('$["foo${BLKVM_OVERRIDE}" = "foo"]', 'skipdb'));
81           $ext->add($contextname, $grpnum, '', new ext_gotoif('$["${DB(${BLKVM_OVERRIDE})}" = "TRUE"]', 'skipov'));
82
83           $ext->add($contextname, $grpnum, 'skipdb', new ext_setvar('__NODEST', ''));
84           $ext->add($contextname, $grpnum, '', new ext_setvar('__BLKVM_OVERRIDE', 'BLKVM/${EXTEN}/${CHANNEL}'));
85           $ext->add($contextname, $grpnum, '', new ext_setvar('__BLKVM_BASE', '${EXTEN}'));
86           $ext->add($contextname, $grpnum, '', new ext_setvar('DB(${BLKVM_OVERRIDE})', 'TRUE'));
87
88           // Remember if NODEST was set later, but clear it in case the call is answered so that subsequent
89           // transfers work.
90           //
91           $ext->add($contextname, $grpnum, 'skipov', new ext_setvar('RRNODEST', '${NODEST}'));
92           $ext->add($contextname, $grpnum, 'skipvmblk', new ext_setvar('__NODEST', '${EXTEN}'));
93
94           // deal with group CID prefix
95           // but strip only if you plan on setting a new one
96           if ($grppre != '') {
97             $ext->add($contextname, $grpnum, '', new ext_gotoif('$["foo${RGPREFIX}" = "foo"]', 'REPCID'));
98             $ext->add($contextname, $grpnum, '', new ext_gotoif('$["${RGPREFIX}" != "${CALLERID(name):0:${LEN(${RGPREFIX})}}"]', 'REPCID'));
99             $ext->add($contextname, $grpnum, '', new ext_noop('Current RGPREFIX is ${RGPREFIX}....stripping from Caller ID'));
100             $ext->add($contextname, $grpnum, '', new ext_setvar('CALLERID(name)', '${CALLERID(name):${LEN(${RGPREFIX})}}'));
101             $ext->add($contextname, $grpnum, '', new ext_setvar('_RGPREFIX', ''));
102             $ext->add($contextname, $grpnum, 'REPCID', new ext_noop('CALLERID(name) is ${CALLERID(name)}'));
103             $ext->add($contextname, $grpnum, '', new ext_setvar('_RGPREFIX', $grppre));
104             $ext->add($contextname, $grpnum, '', new ext_setvar('CALLERID(name)','${RGPREFIX}${CALLERID(name)}'));
105           }
106
107           // MODIFIED (PL)
108           // Add Alert Info if set but don't override and already set value (could be from ringgroup, directdid, etc.)
109           //
110           if ((isset($dring) ? $dring : '') != '') {
111             // If ALERTINFO is set, then skip to the next set command. This was modified to two lines because the previous
112             // IF() couldn't handle ':' as part of the string. The jump to PRIORITY+2 allows for now destination label
113             // which is needed in the 2.3 version.
114             $ext->add($contextname, $grpnum, '', new ext_gotoif('$["x${ALERT_INFO}"!="x"]','$[${PRIORITY}+2])}'));
115             $ext->add($contextname, $grpnum, '', new ext_setvar("__ALERT_INFO", str_replace(';', '\;', $dring) ));
116           }
117           // If pre_ring is set, then ring this number of seconds prior to moving on
118           if ((isset($strategy) ? substr($strategy,0,strlen('ringallv2')) : '') != 'ringallv2') {
119             $ext->add($contextname, $grpnum, '', new ext_gotoif('$[$[ "${DB(AMPUSER/'.$grpnum.'/followme/prering)}" = "0" ] | $[ "${DB(AMPUSER/'.$grpnum.'/followme/prering)}" = "" ]] ', 'skipsimple'));
120             $ext->add($contextname, $grpnum, '', new ext_macro('simple-dial',$grpnum.',${DB(AMPUSER/'."$grpnum/followme/prering)}"));
121           }
122
123           // recording stuff
124           $ext->add($contextname, $grpnum, 'skipsimple', new ext_setvar('RecordMethod','Group'));
125           $ext->add($contextname, $grpnum, '', new ext_macro('record-enable','${DB(AMPUSER/'."$grpnum/followme/grplist)}".',${RecordMethod}'));
126
127           // group dial
128           $ext->add($contextname, $grpnum, '', new ext_setvar('RingGroupMethod',$strategy));
129           $ext->add($contextname, $grpnum, '', new ext_setvar('_FMGRP',$grpnum));
130           if ((isset($annmsg) ? $annmsg : '') != '') {
131             // should always answer before playing anything, shouldn't we ?
132             $ext->add($contextname, $grpnum, '', new ext_gotoif('$[$["${DIALSTATUS}" = "ANSWER"] | $["foo${RRNODEST}" != "foo"]]','DIALGRP'));     
133             $ext->add($contextname, $grpnum, '', new ext_answer(''));
134             $ext->add($contextname, $grpnum, '', new ext_wait(1));
135             $ext->add($contextname, $grpnum, '', new ext_playback($annmsg));
136           }
137
138           // Create the confirm target
139           $len=strlen($grpnum)+4;
140           $ext->add("fmgrps", "_RG-${grpnum}-.", '', new ext_macro('dial','${DB(AMPUSER/'."$grpnum/followme/grptime)},".
141             "M(confirm^${remotealert}^${toolate}^${grpnum})$dialopts".',${EXTEN:'.$len.'}'));
142
143           // If grpconf == ENABLED call with confirmation ELSE call normal
144           $ext->add($contextname, $grpnum, 'DIALGRP', new
145               ext_gotoif('$[ "${DB(AMPUSER/'.$grpnum.'/followme/grpconf)}" = "ENABLED" ]', 'doconfirm'));
146
147           // Normal call
148           if ((isset($strategy) ? substr($strategy,0,strlen('ringallv2')) : '') != 'ringallv2') {
149             $ext->add($contextname, $grpnum, '', new ext_macro('dial','${DB(AMPUSER/'."$grpnum/followme/grptime)},$dialopts,".'${DB(AMPUSER/'."$grpnum/followme/grplist)}"));
150           } else {
151             $ext->add($contextname, $grpnum, '', new ext_macro('dial','$[ ${DB(AMPUSER/'.$grpnum.'/followme/grptime)} + ${DB(AMPUSER/'.$grpnum.'/followme/prering)} ],'.$dialopts.',${DB(AMPUSER/'.$grpnum.'/followme/grplist)}'));
152           }
153           $ext->add($contextname, $grpnum, '', new ext_goto('nextstep'));
154
155           // Call Confirm call
156           if ((isset($strategy) ? substr($strategy,0,strlen('ringallv2')) : '') != 'ringallv2') {
157             $ext->add($contextname, $grpnum, 'doconfirm', new ext_macro('dial-confirm','${DB(AMPUSER/'."$grpnum/followme/grptime)},$dialopts,".'${DB(AMPUSER/'."$grpnum/followme/grplist)},".$grpnum));
158           } else {
159             $ext->add($contextname, $grpnum, 'doconfirm', new ext_macro('dial-confirm','$[ ${DB(AMPUSER/'.$grpnum.'/followme/grptime)} + ${DB(AMPUSER/'.$grpnum.'/followme/prering)} ],'.$dialopts.',${DB(AMPUSER/'.$grpnum.'/followme/grplist)},'.$grpnum));
160           }
161
162           $ext->add($contextname, $grpnum, 'nextstep', new ext_setvar('RingGroupMethod',''));
163
164           // Did the call come from a queue or ringgroup, if so, don't go to the destination, just end and let
165           // the queue or ringgroup decide what to do next
166           //
167           $ext->add($contextname, $grpnum, '', new ext_gotoif('$["foo${RRNODEST}" != "foo"]', 'nodest'));
168           $ext->add($contextname, $grpnum, '', new ext_setvar('__NODEST', ''));
169
170           $ext->add($contextname, $grpnum, '', new ext_dbdel('${BLKVM_OVERRIDE}'));
171
172           // where next?
173           if ((isset($postdest) ? $postdest : '') != '') {
174             $ext->add($contextname, $grpnum, '', new ext_goto($postdest));
175           } else {
176             $ext->add($contextname, $grpnum, '', new ext_hangup(''));
177           }
178           $ext->add($contextname, $grpnum, 'nodest', new ext_noop('SKIPPING DEST, CALL CAME FROM Q/RG: ${RRNODEST}'));
179         }
180       }
181     break;
182   }
183 }
184
185 function findmefollow_add($grpnum,$strategy,$grptime,$grplist,$postdest,$grppre='',$annmsg='',$dring,$needsconf,$remotealert,$toolate,$ringing,$pre_ring,$ddial) {
186   global $amp_conf;
187   global $astman;
188
189   $sql = "INSERT INTO findmefollow (grpnum, strategy, grptime, grppre, grplist, annmsg, postdest, dring, needsconf, remotealert, toolate, ringing, pre_ring) VALUES ('".str_replace("'", "''",$grpnum)."', '".str_replace("'", "''", $strategy)."', ".str_replace("'", "''", $grptime).", '".str_replace("'", "''", $grppre)."', '".str_replace("'", "''", $grplist)."', '".str_replace("'", "''", $annmsg)."', '".str_replace("'", "''", $postdest)."', '".str_replace("'", "''", $dring)."', '$needsconf', '$remotealert', '$toolate', '$ringing', '$pre_ring')";
190   $results = sql($sql);
191
192   if ($astman) {
193     $astman->database_put("AMPUSER",$grpnum."/followme/prering",isset($pre_ring)?$pre_ring:'');
194     $astman->database_put("AMPUSER",$grpnum."/followme/grptime",isset($grptime)?$grptime:'');
195     $astman->database_put("AMPUSER",$grpnum."/followme/grplist",isset($grplist)?$grplist:'');
196
197     $needsconf = isset($needsconf)?$needsconf:'';
198     $confvalue = ($needsconf == 'CHECKED')?'ENABLED':'DISABLED';
199     $astman->database_put("AMPUSER",$grpnum."/followme/grpconf",$confvalue);
200
201     $ddial      = isset($ddial)?$ddial:'';
202     $ddialvalue = ($ddial == 'CHECKED')?'EXTENSION':'DIRECT';
203     $astman->database_put("AMPUSER",$grpnum."/followme/ddial",$ddialvalue);
204   } else {
205     fatal("Cannot connect to Asterisk Manager with ".$amp_conf["AMPMGRUSER"]."/".$amp_conf["AMPMGRPASS"]);
206   }
207 }
208
209 function findmefollow_del($grpnum) {
210   global $amp_conf;
211   global $astman;
212
213   $results = sql("DELETE FROM findmefollow WHERE grpnum = '".str_replace("'", "''", $grpnum)."'","query");
214
215   if ($astman) {
216     $astman->database_deltree("AMPUSER/".$grpnum."/followme");
217   } else {
218     fatal("Cannot connect to Asterisk Manager with ".$amp_conf["AMPMGRUSER"]."/".$amp_conf["AMPMGRPASS"]);
219   }
220 }
221
222 function findmefollow_full_list() {
223   $results = sql("SELECT grpnum FROM findmefollow ORDER BY CAST(grpnum as UNSIGNED)","getAll",DB_FETCHMODE_ASSOC);
224   foreach ($results as $result) {
225     if (isset($result['grpnum']) && checkRange($result['grpnum'])) {
226       $grps[] = array($result['grpnum']);
227     }
228   }
229   if (isset($grps))
230     return $grps;
231   else
232     return null;
233 }
234
235 function findmefollow_list() {
236
237         global $db;
238         $sql = "SELECT grpnum FROM findmefollow ORDER BY CAST(grpnum as UNSIGNED)";
239         $results = $db->getCol($sql);
240         if(DB::IsError($results)) {
241                 $results = null;
242         }
243         if (isset($results)) {
244           foreach($results as $result) {
245             if (checkRange($result)){
246               $grps[] = $result;
247             }
248           }
249         }
250         if (isset($grps)) {
251     sort($grps); // hmm, should be sorted already
252           return $grps;
253   }
254   else {
255     return null;
256   }
257 }
258
259 // This gets the list of all active users so that the Find Me Follow display can limit the options to only created users.
260 // the returned arrays contain [0]:extension [1]:name
261 //
262 // This was pulled straight out of previous 1.x version, might need cleanup laster
263 //
264 function findmefollow_allusers() {
265         global $db;
266         $sql = "SELECT extension,name FROM users ORDER BY extension";
267         $results = $db->getAll($sql);
268         if(DB::IsError($results)) {
269                 $results = null;
270         }
271         foreach($results as $result){
272                 if (checkRange($result[0])){
273                         $users[] = array($result[0],$result[1]);
274                 }
275         }
276         if (isset($users)) sort($users);
277         return $users;
278 }
279
280 // Only check astdb if check_astdb is not 0. For some reason, this fails if the asterisk manager code
281 // is included (executed) by all calls to this function. This results in silently not generating the
282 // extensions_additional.conf file. page.findmefollow.php does set it to 1 which means that when running
283 // the GUI, any changes not reflected in SQL will be detected and written back to SQL so that they are
284 // in sync. Ideally, anything that changes the astdb should change SQL. (in some ways, these should both
285 // not be here but ...
286 //
287 // Need to go back and confirm at some point that the $check_astdb error is still there and deal with it.
288 // as variables like $ddial get introduced to only be in astdb, the result array will not include them
289 // if not able to get to astdb. (I suspect in 2.2 and beyond this may all be fixed).
290 //
291 function findmefollow_get($grpnum, $check_astdb=0) {
292   global $amp_conf;
293   global $astman;
294
295   $results = sql("SELECT grpnum, strategy, grptime, grppre, grplist, annmsg, postdest, dring, needsconf, remotealert, toolate, ringing, pre_ring FROM findmefollow WHERE grpnum = '".str_replace("'", "''", $grpnum)."'","getRow",DB_FETCHMODE_ASSOC);
296
297   if ($check_astdb) {
298     if ($astman) {
299       $astdb_prering = $astman->database_get("AMPUSER",$grpnum."/followme/prering");
300       $astdb_grptime = $astman->database_get("AMPUSER",$grpnum."/followme/grptime");
301       $astdb_grplist = $astman->database_get("AMPUSER",$grpnum."/followme/grplist");
302       $astdb_grpconf = $astman->database_get("AMPUSER",$grpnum."/followme/grpconf");
303     } else {
304       fatal("Cannot connect to Asterisk Manager with ".$amp_conf["AMPMGRUSER"]."/".$amp_conf["AMPMGRPASS"]);
305     }
306       $astdb_ddial   = $astman->database_get("AMPUSER",$grpnum."/followme/ddial");                                     
307     // If the values are different then use what is in astdb as it may have been changed.
308     //
309     $changed=0;
310     if (($astdb_prering != $results['pre_ring']) && ($astdb_prering >= 0)) {
311       $results['pre_ring'] = $astdb_prering;
312       $changed=1;
313     }
314     if (($astdb_grptime != $results['grptime']) && ($astdb_grptime > 0)) {
315       $results['grptime'] = $astdb_grptime;
316       $changed=1;
317     }
318     if ((trim($astdb_grplist) != trim($results['grplist'])) && (trim($astdb_grplist) != '')) {
319       $results['grplist'] = $astdb_grplist;
320       $changed=1;
321     }
322
323     if (trim($astdb_grpconf) == 'ENABLED') {
324       $confvalue = 'CHECKED';
325     } elseif (trim($astdb_grpconf) == 'DISABLED') {
326       $confvalue = '';
327     } else {
328       //Bogus value, should not get here but treat as disabled
329       $confvalue = '';
330     }
331     if ($confvalue != trim($results['needsconf'])) {
332       $results['needsconf'] = $confvalue;
333       $changed=1;
334     }
335
336     // Not in sql so no sanity check needed
337     //
338     if (trim($astdb_ddial) == 'EXTENSION') {
339       $ddial = 'CHECKED';
340     } elseif (trim($astdb_ddial) == 'DIRECT') {
341       $ddial = '';
342     } else {
343       //Bogus value, should not get here but treat as disabled
344       $ddial = '';
345     }
346     $results['ddial'] = $ddial;
347
348     if ($changed) {
349       $sql = "UPDATE findmefollow SET grptime = '".$results['grptime']."', grplist = '".
350         str_replace("'", "''", trim($results['grplist']))."', pre_ring = '".$results['pre_ring'].
351         "', needsconf = '".$results['needsconf']."' WHERE grpnum = '".str_replace("'", "''", $grpnum)."' LIMIT 1";
352       $sql_results = sql($sql);
353     }
354   } // if check_astdb
355
356   return $results;
357 }
358
359 function findmefollow_configpageinit($dispnum) {
360   global $currentcomponent;
361
362   if ( ($dispnum == 'users' || $dispnum == 'extensions') ) {
363     $currentcomponent->addguifunc('findmefollow_configpageload');
364   }
365 }
366
367 function findmefollow_configpageload() {
368   global $currentcomponent;
369
370   $viewing_itemid =  isset($_REQUEST['extdisplay'])?$_REQUEST['extdisplay']:null;
371   $action =  isset($_REQUEST['action'])?$_REQUEST['action']:null;
372   if ( $viewing_itemid != '' && $action != 'del') {
373     $set_findmefollow = findmefollow_list();
374     $grpURL = $_SERVER['PHP_SELF'].'?'.'display=findmefollow&extdisplay=GRP-'.$viewing_itemid;
375     if (is_array($set_findmefollow)) {
376       $grpTEXT = (in_array($viewing_itemid,$set_findmefollow) ? "Edit" : "Add")." Follow Me Settings";
377     } else {
378       $grpTEXT = "Add Follow Me Settings";
379     }
380     $currentcomponent->addguielem('_top', new gui_link('findmefollowlink', $grpTEXT, $grpURL));
381   }
382 }
383
384 ?>
Note: See TracBrowser for help on using the browser.