47e051b98049b0f17dcb7d7db3b655d26e21bbe0
1 <?php
3 class fonreport extends plugin
4 {
5 /* Definitions */
6 var $plHeadline= "Phone Reports";
7 var $plDescription= "This does something";
9 /* For internal use */
10 var $start = 0;
11 var $search_for = "*";
12 var $search_base = "";
13 var $fields = array("calldate", "src", "dst", "channel", "lastapp", "disposition", "duration");
14 var $year = "";
15 var $month = "";
16 var $sort = 0;
17 var $sort_direction = "down";
18 var $report_list = array();
19 var $userfilter = "";
20 var $ui = NULL;
21 var $range = 20;
22 var $EntryPerPage = 20;
24 /* attribute list for save action */
25 var $attributes_SO = array("start","search_for","search_base","range","month","sort_direction","sort","year");
26 var $objectclasses = array();
29 /* Construct class */
30 function fonreport ($config, $ui)
31 {
32 /* Include config object */
33 $this->config = $config;
34 $this->ui = $ui;
35 $this->search_base= get_base_from_people($ui->dn);
37 $this->month = date("m");
38 $this->year = date("Y");
40 /* Use filter settings if we have already searched */
41 if (!is_global("fonfilter")){
42 $fonfilter = array();
43 foreach($this->attributes_SO as $name){
44 $fonfilter[$name]=$this->$name;
45 }
46 register_global("fonfilter", $fonfilter);
47 }else{
48 $fonfilter = get_global("fonfilter");
49 foreach($this->attributes_SO as $name){
50 $this->$name = $fonfilter[$name];
51 }
52 }
53 }
56 /* Save ui interactions and store results in session,
57 to remember settings */
58 function save_object()
59 {
60 $fonfilter= get_global("fonfilter");
61 if(isset($_POST['EntryPerPage'])){
62 $this->range = $_POST['EntryPerPage'];
63 }
64 if (isset($_GET['start'])){
65 $this->start= (int)$_GET['start'];
66 }
67 foreach( array("year", "month", "search_for", "search_base") as $type){
68 if (isset($_POST[$type])){
69 $this->$type= $_POST[$type];
70 }
71 }
73 /* Adapt sorting */
74 if (isset($_GET['sort'])){
75 if ($this->sort == (int)$_GET['sort']){
76 if ($this->sort_direction == "down"){
77 $this->sort_direction= "up";
78 } else {
79 $this->sort_direction= "down";
80 }
81 }
82 $this->sort= (int)$_GET['sort'];
83 if ($this->sort < 0 || $this->sort > 6){
84 $this->sort= 0;
85 }
86 }
88 foreach($this->attributes_SO as $name){
89 $fonfilter[$name] = $this->$name;
90 }
91 register_global("fonfilter", $fonfilter);
92 }
95 /* Search & display results */
96 function execute()
97 {
98 /* Call parent execute */
99 plugin::execute();
101 /* GVet template engine */
102 $smarty= get_smarty();
105 /*****************
106 Variable Init
107 *****************/
109 $fields_str = "";
111 $months= array();
112 for($i = 1 ; $i <= 12 ; $i ++ ){
113 $months[$i] = _(date("F",gmmktime(0,0,0,$i)));
114 }
116 /* Prepare template */
117 $current= date("Y");
118 $years= array();
119 for ($y= $current - 5; $y<=$current; $y++){
120 $years[$y]= $y;
121 }
123 /*****************
124 Smarty
125 *****************/
127 foreach($this->fields as $field){
128 if(chkacl($this->acl,$field)==""){
129 $smarty->assign($field."ACL","");
130 }else{
131 $smarty->assign($field."ACL"," disabled ");
132 }
133 }
135 $smarty->assign("plug", "?plug=".validate($_GET['plug']));
136 $smarty->assign("launchimage", get_template_path('images/launch.png'));
137 $smarty->assign("search_image", get_template_path('images/search.png'));
138 $smarty->assign("search_for", $this->search_for);
139 $smarty->assign("bases", $this->config->idepartments);
140 $smarty->assign("base_select", $this->search_base);
141 $smarty->assign("months", $months);
142 $smarty->assign("month_select", $this->month);
143 $smarty->assign("years", $years);
144 $smarty->assign("year_select", $this->year);
145 $smarty->assign("search_result", "");
148 /*****************
149 Check Database , Table , Connection
150 *****************/
152 /* Connecting, selecting database */
153 if (!isset($this->config->data['SERVERS']['FON'])){
154 print_red(_("Can't connect to phone database, no reports can be shown!"));
155 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
156 }elseif(!is_callable("mysql_connect")){
157 print_red(_("There is no mysql extension available, please check your php setup."));
158 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
159 }else{
161 $cfg= $this->config->data['SERVERS']['FON'];
162 $link = @mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD']);
163 if ($link === FALSE){
164 print_red(_("Can't connect to phone database, no reports can be shown!"));
165 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
166 }
167 if (! @mysql_select_db("gophone")){
168 print_red(_("Can't select phone database for report generation!"));
169 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
170 }
171 }
174 /*****************
175 Get Query String && Search
176 *****************/
178 $query = $this->CreateQuerySyntax();
179 $cfg = $this->config->data['SERVERS']['FON'];
180 $link = @mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD']);
182 @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__,$query, "Database query");
184 $result = @mysql_query($query);
185 if ($result === false){
186 print_red(_("Query for phone database failed!"));
187 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
188 }
190 /*****************
191 Fetch results
192 *****************/
194 $report_list= array();
195 while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
196 $append_str = "";
197 $hour= substr($line["calldate"], 11, 2);
198 $minute=substr($line["calldate"], 14, 2);
199 $format= _("Y-M-D");
200 $date= preg_replace("/Y/", substr($line["calldate"], 0, 4), $format);
201 $date= preg_replace("/M/", substr($line["calldate"], 5, 2), $date);
202 $date= preg_replace("/D/", substr($line["calldate"], 8, 2), $date);
203 $append_str .= "<td>$date $hour:$minute</td>";
204 foreach(array("src","dst","channel","lastapp","disposition") as $atr){
205 if(isset($line[$atr])){
206 $append_str .= "<td>".$line[$atr]."</td>";
207 }
208 }
209 if(isset($line['duration'])){
210 $append_str .= "<td>".$this->gen_duration($line["duration"])."</td>";
211 }
212 $report_list[] = $append_str;
213 }
215 $this->report_list= $report_list;
216 @mysql_close($link);
218 /* Generate output */
219 $mod= 0;
222 /*****************
223 Create list of results
224 *****************/
226 $output= "";
227 foreach ($this->report_list as $val){
228 if ($mod < $this->start) {
229 $mod++;
230 continue;
231 }
232 if ($mod >= ($this->start + $this->range)){
233 $mod++;
234 break;
235 }
236 if ( ($mod++) & 1){
237 $col= "background-color: #ECECEC;";
238 } else {
239 $col= "background-color: #F5F5F5;";
240 }
241 $output.= "<tr style=\"height:22px; $col\">$val</tr>";
242 }
245 /*****************
246 Tell smarty what we have found
247 *****************/
249 if ($output != ""){
250 $smarty->assign("search_result", $output);
251 $smarty->assign("range_selector", range_selector(count($this->report_list), $this->start,$this->range,"EntryPerPage"));
252 } else {
253 $smarty->assign("search_result", "");
254 }
256 /* Show main page */
257 for($i= 0; $i<7; $i++){
258 $smarty->assign("mode$i", "");
259 }
260 $smarty->assign("mode".$this->sort, "<img alt=\"\" src=\"images/sort_".$this->sort_direction.
261 ".png\" border=0 align=middle>");
262 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
263 }
266 /* Create duration string 12'11" */
267 function gen_duration($seconds)
268 {
269 if ($seconds / 60 > 1){
270 $minutes= (int)($seconds / 60);
271 $seconds= $seconds % 60;
272 return ("$minutes’$seconds”");
273 }
274 return ("$seconds”");
275 }
278 /* Create WHERE part for our mysql serach */
279 function GetUidMatchingFilter()
280 {
281 $ldap = $this->config->get_ldap_link();
282 $ldap->cd ($this->search_base);
283 $s = $this->search_for;
284 $s = preg_replace("/\%/","",$s);
285 $s2 = preg_replace("/\*/","%",$s);
287 /* Perform ldap search for potential users */
288 $ldap->search ("(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))(objectClass=gofonAccount)".
289 "(|(uid=$s)(l=$s)(homePhone=$s)(telephoneNumber=$s)(facsimileTelephoneNumber=$s)(mobile=$s)".
290 "(pager=$s)(cn=$s)(givenName=$s)(sn=$s)(personalTitle=$s)(title=$s)))" ,array("uid"));
291 $str = " AND (";
292 $fields = array("dstchannel","channel");
293 if($ldap->count()){
294 while ($attrs= $ldap->fetch()){
295 $uid = $attrs["uid"][0];
296 foreach($fields as $name){
297 $str .= $name." like '%".$uid."%' OR ";
298 }
299 }
300 }
301 $str .= " dst like '".$s2."' OR src like '".$s2."' OR lastapp like '".$s2."')";
302 return($str);
303 }
306 /* Create query string */
307 function CreateQuerySyntax()
308 {
309 /* Get extended search filter which contain uids and so on */
310 $uidstring = $this->GetUidMatchingFilter();
312 /* Create string with all fields seperated by ,*/
313 $fields_str ="";
314 foreach($this->fields as $field){
315 if($field == "calldate") continue;
316 if(chkacl($this->acl,$field)==""){
317 $fields_str .= $field.", ";
318 }
319 }
320 $fields_str = preg_replace("/, $/","",$fields_str);
322 /* Create Sort tag */
323 if ($this->sort_direction == "down"){
324 $desc= "DESC";
325 } else {
326 $desc= "ASC";
327 }
328 /* Create times */
329 $start= date ("YmdHis", mktime(0,0,0,$this->month,1,$this->year));
330 $end= date ("YmdHis", mktime(23,59,59,($this->month +1),0,$this->year));
331 $query = "SELECT ".$fields_str.",calldate FROM cdr ".
332 "WHERE
333 calldate <= $end
334 AND
335 calldate >= $start
336 ". $uidstring."
337 ORDER BY ".$this->fields[$this->sort]." $desc;";
338 return($query);
339 }
340 }
341 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
342 ?>