root/freepbx/branches/experimental/amp_conf/htdocs/panel/index.php

Revision 2140, 7.0 kB (checked in by gregmac, 2 years ago)

Initial work on DhtmlOperatorPanel

  • Property svn:mime-type set to text/plain
  • Property svn:eol-style set to native
Line 
1 <?php
2 /** $Id$
3  * FreePBX DHTML Operator Panel
4  *
5  * @package     FreePBX
6  * @author      Greg MacLellan
7  * @copyright   Copyright (c) 2006, Greg MacLellan
8  * @license     GPL
9  * @link        http://www.freepbx.org/trac/wiki/DhtmlOperatorPanel
10 */
11  
12 //header('Content-type: text/plain');
13 ini_set("error_log", "/tmp/phperr.log");
14
15 include('xajax.inc.php');
16
17 $xajax = new Xajax();
18
19 $xajax->registerFunction('check_updates');
20 $xajax->processRequests();
21
22
23 function display_log($text) {
24         global $js;
25         $end = !$js;
26         if ($end) start_js();
27         echo "document.getElementsById('log').appendTextNode('".$text."<br />'); \n";
28         if ($end) done_js();
29 }
30
31 function log_err($errno, $errstr, $errfile, $errline) {
32         display_log($errstr);
33 }
34 set_error_handler('log_err');
35
36 function parse_button_config($filename) {
37         $channels = array();
38        
39         $ini = parse_ini_file($filename, true);
40         foreach ($ini as $section=>$config) {
41                 $channel = array();
42
43                 $channel['name'] = $config['Label'];
44                 if ($config['Extension'] > 0) {
45                         $channel['number'] = $config['Extension'];
46                         $channel['type'] = 'extension';
47                 } else {
48                         $channel['type'] = 'trunk';
49                 }
50                 $channel['matchstart'] = $section.'-';
51
52                 $channels[] = $channel;
53         }
54         return $channels;
55 }
56 $channels = parse_button_config('/var/www/html/admin/panel/op_buttons_additional.cfg');
57
58 /*
59 echo '<pre>';
60 var_dump($channels);
61 die();
62 /**/
63
64 $active_channels = array();
65
66 $js = false;
67
68 function start_js() {
69         global $js;
70         if (!$js) {
71                 $js = true;
72                 echo '<script type="text/javascript">';
73         }
74 }
75
76 function done_js() {
77         global $js;
78         if ($js) {
79                 echo '</script>';
80                 $js = false;
81         }
82         flush();
83 }
84
85 function find_channel($chantext) {
86         global $channels;
87        
88         foreach ($channels as $channel) {
89                 if (isset($channel['match']) && ($channel['match'] == $chantext)) {
90                         return $channel;
91                 } else if (isset($channel['matchstart']) && (0 === strpos(strtolower($chantext), strtolower($channel['matchstart'])))) {
92                         return $channel;
93                 }
94         }
95         return false;
96 }
97
98 function change_state($chantext, $state) {
99         global $active_channels;
100         error_log("change_state $chantext $state");
101        
102         start_js();
103         if (($chan = find_channel($chantext)) && ($chan['type'] == 'extension')) {
104                 switch ($state) {
105                         case 'Ring':
106                                 echo "$('extension_".$chan['number']."').className = 'extension busy'; \n";
107                         break;
108                         case 'Ringing':
109                                 echo "$('extension_".$chan['number']."').className = 'extension busy'; \n";
110                                 echo "new Effect.Pulsate($('extension_".$chan['number']."')); \n";
111                         break;
112                 }
113         } else {
114                 if (isset($active_channels[$chantext])) {
115                         // existing
116                         js_channel_status($chantext, $state);
117                 } else {
118                         // new
119                         $active_channels[$chantext] = true;
120                         if ($chan) {
121                                 js_channel_add($chantext, $chan['name'], $state);
122                         } else {
123                                 js_channel_add($chantext, $chantext, $state);
124                         }
125                 }
126         }
127         done_js();
128 }
129
130 function hangup($chantext) {
131         global $active_channels;
132        
133         if (($chan = find_channel($chantext)) && ($chan['type'] == 'extension')) {
134                 error_log("Got hangup for extension ".$chan['number']);
135                 start_js();
136                 echo "$('extension_".$chan['number']."').className = 'extension idle'; \n";
137                 done_js();
138         } else if (isset($active_channels[$chantext])) {
139                 error_log("Got hangup for ".$chantext);
140                 unset($active_channels[$chantext]);
141                 // destroy it
142                 start_js();
143                 js_channel_delete($chantext);
144                 done_js();
145         } else {
146                 error_log("Got hangup for unknown channel ".$chantext);
147         }
148 }
149
150
151 function extensionSetStatus($number,$status,$effect=false) {
152     if ($status == "Idle") {
153         echo "$('extension_".$number."').className = 'extension idle'; \n";
154     } else {
155         echo "$('extension_".$number."').className = 'extension busy'; \n";
156     }
157     if ($effect) {
158         echo "new Effect.".$effect."($('extension_".$number."')); \n";
159     }
160 }
161
162 function js_channel_add($id, $desc, $status) {
163         echo "var div = document.createElement('div'); \n";
164         echo "div.setAttribute('id', 'channel_".$id."'); \n";
165         echo "div.setAttribute('class', 'channel trunk'); \n";
166
167         echo "var divname = document.createElement('div'); \n";
168         echo "divname.setAttribute('class', 'name'); \n";
169         echo "divname.innerHTML = '".$desc."'; \n";
170        
171         echo "div.appendChild(divname); \n";
172        
173         echo "var divstatus = document.createElement('div'); \n";
174         echo "divstatus.setAttribute('class', 'status'); \n";
175         echo "divstatus.setAttribute('id', 'channel_".$id."_status'); \n";
176         echo "divstatus.innerHTML = '".$status."'; \n";
177        
178         echo "div.appendChild(divstatus); \n";
179        
180         echo "$('channels').appendChild(div); \n";
181        
182         echo "new Effect.BlindDown($('channel_".$id."')); \n";
183
184
185 function js_channel_status($id,$status) {
186     echo "$('channel_".$id."_status').innerHTML = '".$status."'; \n";
187 }
188
189 function js_channel_delete($id) {
190 //    echo "new Effect.BlindUp($('trunk_".$id."')); \n";
191     echo "setTimeout(function () { \n";
192     echo "  $('channels').removeChild( document.getElementById('channel_".$id."') ); \n";
193     echo "}, 1000); \n";
194 }
195
196 ?>
197 <html>
198 <head>
199 <title>Panel test</title>
200 <style>
201 body {
202     font-family: Ariel, Sans-Serif;
203 }
204 #extensions {
205     border:1px dashed black;
206     float:left;
207 }
208 #channels {
209     float:right;
210     border:1px dashed gray;
211 }
212 .extension {
213     background-color:#99ff99;
214     border:1px solid black;
215     width:200px;
216     height:20px;
217     margin:5px;
218     padding:5px;
219 }
220 .busy {
221     background-color:#ff9999;
222 }
223 .channel {
224     background-color:#9999ff;
225     border:1px solid black;
226     width:180px;
227     height:55px;
228     margin:5px;
229     padding:5px;
230 }
231
232 .name {
233     float:left;
234 }
235 .number {
236     float:right;
237     font-weight:bold;
238 }
239 .status {
240     clear:both;
241     float:left;
242     font-size:60%;
243 }
244 </style>
245 <script type="text/javascript" src="prototype.js"></script>
246 <script type="text/javascript" src="scriptaculous.js"></script>
247 </head>
248 <body>
249
250 <div id="panel">
251     <div id="extensions">
252     <?php
253     foreach ($channels as $ext) {
254         if ($ext['type'] == 'extension') {
255             echo '<div class="extension" id="extension_'.$ext['number'].'">';
256             echo '  <div class="name">'.$ext['name'].'</div>';
257             echo '  <div class="number">'.$ext['number'].'</div>';
258             echo '</div>';
259         }
260     }
261     ?>
262     </div>
263     <div id="channels">
264     </div>
265 </div>
266
267 <div id="log">
268 </div>
269
270 </body>
271 </html>
272 <?php
273 set_time_limit(0);
274 flush();
275
276 function event_handler($event, $param) {
277         switch ($event) {
278                 case 'Newchannel':
279                         change_state($param['Channel'],$param['State']);
280                 break;
281                 case 'Newcallerid':
282                 break;
283                 case 'Newstate':
284                         change_state($param['Channel'],$param['State']);
285                 break;
286                 case 'Hangup':
287                         hangup($param['Channel']);
288                 break;
289                 case 'Newexten':
290                         // check for auto_attendant
291                         if (substr($param['Context'],0,3) == 'aa_') {
292                                 change_state($param['Channel'], 'Auto Attendant '.substr($param['Context'],4));
293                         }
294                 break;
295                 case 'Link':
296                         change_state($param['Channel'],'Linked to '.$param['Linking']);
297                 //break;
298                 case 'Unlink':
299                         change_state($param['Channel'],'');
300                 //break;
301                 default:
302                         display_log($event.": ".implode(",",$param));
303                 break;
304                        
305
306         }
307 }
308
309 include('managerproxy.php');
310
311 $man = new ManagerProxy('localhost', 5039);
312 $man->set_handler('event_handler');
313 $man->connect();
314
315 while (1) {
316         $man->wait_read(5);
317         flush();
318 }
319 $man->disconnect();
320
321 ?>
Note: See TracBrowser for help on using the browser.
Donate



Support
Download
Develop
Forums
News
Documentation
Paid Support
About

Paid Ads