Code

Some updates
[gosa.git] / plugins / gofon / fonreports / class_fonreport.inc
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();
28   
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);
36   
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     }
72     
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     /* remove unwanted tags */
89     $this->search_for = stripslashes(preg_replace("/[^0-9a-z\*\+ \-]/i","",$this->search_for));
90   
91     foreach($this->attributes_SO as $name){
92       $fonfilter[$name] = $this->$name;
93     }
94     register_global("fonfilter", $fonfilter);
95   }
98   /* Search & display results */
99   function execute()
100   {
101     /* Call parent execute */
102     plugin::execute();
104     /* GVet template engine */
105     $smarty= get_smarty();
108     /*****************
109       Variable Init
110      *****************/
112     $fields_str = "";
114     $months= array();
115     for($i = 1 ; $i <= 12 ; $i ++ ){
116       $months[$i] = _(date("F",gmmktime(0,0,0,$i)));
117     }
119     /* Prepare template */
120     $current= date("Y");
121     $years= array();
122     for ($y= $current - 5; $y<=$current; $y++){
123       $years[$y]= $y;
124     }
126     /*****************
127       Smarty 
128      *****************/
129     $bases      = array();
130     $cat_bases  = $this->ui->get_module_departments("gofon");
131     foreach($this->config->idepartments as $dn => $name){
132       if(in_array_ics($dn,$cat_bases)){
133         $bases[$dn] = $name;
134       }
135     }
137     if(!isset($bases[$this->search_base])){
138       $this->search_base = key($bases);
139     }
141     $smarty->assign("plug",           "?plug=".validate($_GET['plug']));
142     $smarty->assign("launchimage",    get_template_path('images/launch.png'));
143     $smarty->assign("search_image",   get_template_path('images/search.png'));
144     $smarty->assign("search_for",     $this->search_for);
145     $smarty->assign("bases",          $bases);
146     $smarty->assign("base_select",    $this->search_base);
147     $smarty->assign("months",         $months);
148     $smarty->assign("month_select",   $this->month);
149     $smarty->assign("years",          $years);
150     $smarty->assign("year_select",    $this->year);
151     $smarty->assign("search_result",  "");
154     /*****************
155       Check Database , Table , Connection 
156      *****************/
158     /* Connecting, selecting database */
159     if (!isset($this->config->data['SERVERS']['FON'][0])){
160       print_red(_("Can't connect to phone database, no reports can be shown!"));
161       return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
162     }elseif(!is_callable("mysql_connect")){
163       print_red(_("There is no mysql extension available, please check your php setup."));
164       return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
165     }else{
167       $cfg= $this->config->data['SERVERS']['FON'][0];
168       $link = @mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD']);
169       if ($link === FALSE){
170         print_red(_("Can't connect to phone database, no reports can be shown!"));
171         return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
172       }
173       if (! @mysql_select_db("gophone")){
174         print_red(_("Can't select phone database for report generation!"));
175         return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
176       }
177     }
180     /*****************
181       Get Query String && Search
182      *****************/
184     $query  = $this->CreateQuerySyntax();
185     $cfg    = $this->config->data['SERVERS']['FON'][0];
186     $link   = @mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD']);
188     @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__,$query, "Database query");
190     $result = @mysql_query($query);
191     if ($result === false){
192       print_red(_("Query for phone database failed!"));
193       return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
194     }
196     /*****************
197       Fetch results 
198      *****************/
199   
200     $report_list= array();
201     
202     /* Restricted attributes will not be displayed, this will be displayed instead */
203     $no_acl = "<img class='center' src='images/closedlock.png'
204       title='"._("Insufficient permissions to view this attribute")."' alt='"._("Insufficient permissions")."'>";
206     $no_acl = "&nbsp;";
208     while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
210       foreach($line as $attr => $value){
212         if($attr == "duration") continue;
213   
214         $acl = $this->ui->get_permissions($this->search_base,"gofon/fonreport",$attr);
215         if(!preg_match("/r/",$acl)){
216           $line[$attr] = $no_acl;
217         }
218       }
220       if($this->ui->get_permissions($this->search_base,"gofon/fonreport","calldate")){
221         $hour=  substr($line["calldate"], 11, 2);
222         $minute=substr($line["calldate"], 14, 2);
223         $format= _("Y-M-D");
224         $date= preg_replace("/Y/", substr($line["calldate"], 0, 4), $format);
225         $date= preg_replace("/M/", substr($line["calldate"], 5, 2), $date);
226         $date= preg_replace("/D/", substr($line["calldate"], 8, 2), $date);
227         $date_str = $date." ".$hour.":".$minute;
228       }else{
229         $date_str = $no_acl;
230       }
231   
232       $append_str = "";
233       $append_str .=    "<td>".$date_str."</td>";
235       foreach(array("src","dst","channel","lastapp","disposition") as $atr){
236         if(isset($line[$atr])){
237           $append_str .=        "<td>".$line[$atr]."</td>";
238         }
239       }
241       if($this->ui->get_permissions($this->search_base,"gofon/fonreport","duration")){
242         $append_str .=  "<td>".$this->gen_duration($line["duration"])."</td>";
243       }else{
244         $append_str .=  "<td>".$no_acl."</td>";
245       }
246       $report_list[] = $append_str;
247     }
249     $this->report_list= $report_list;
250     @mysql_close($link);
253     /*****************
254       Create list of results  
255      *****************/
257     /* Generate output */
258     $mod      = 0;
259     $output   = "";
260     if(count($this->report_list) < $this->start){
261       $this->start = 0;
262     }
263     foreach($this->report_list as $val){
264       if ($mod < $this->start) {
265         $mod++;
266         continue;
267       }
268       if ($mod >= ($this->start + $this->range)){
269         $mod++;
270         break;
271       }
272       if ( ($mod++) & 1){
273         $col= "background-color: #ECECEC;";
274       } else {
275         $col= "background-color: #F5F5F5;";
276       }
277       $output.= "<tr style=\"height:22px; $col\">$val</tr>";
278     }
280     /*****************
281       Tell smarty what we have found  
282      *****************/
284     if ($output != ""){
285       $smarty->assign("search_result", $output);
286       $smarty->assign("range_selector", range_selector(count($this->report_list), $this->start,$this->range,"EntryPerPage"));
287     } else {
288       $smarty->assign("search_result", "");
289     }
291     /* Show main page */
292     for($i= 0; $i<7; $i++){
293       $smarty->assign("mode$i", "");
294     }
295     $smarty->assign("mode".$this->sort, "<img alt=\"\" src=\"images/sort_".$this->sort_direction.".png\" border=0 align=middle>");
297     return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
298   }
300   
301   /* Create duration string   12'11" */
302   function gen_duration($seconds)
303   {
304     if ($seconds / 60 > 1){
305       $minutes= (int)($seconds / 60);
306       $seconds= $seconds % 60;
307       return ("$minutes&rsquo;$seconds&rdquo;");
308     }
309     return ("$seconds&rdquo;");
310   }
313   /* Create WHERE part for our mysql serach */
314   function GetUidMatchingFilter()
315   {
316     $ldap       = $this->config->get_ldap_link();
317     $ldap->cd     ($this->search_base);
318     $s          = $this->search_for;
320     $s          = preg_replace("/\%/","",$s);  
321     $s2         = preg_replace("/\*/","%",$s);  
323     $filter = "(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))".
324         "(|(uid=$s)(l=$s)(homePhone=$s)(telephoneNumber=$s)(facsimileTelephoneNumber=$s)(mobile=$s)".
325         "(pager=$s)(cn=$s)(givenName=$s)(sn=$s)(personalTitle=$s)(title=$s)))";
327     $attrs  = array("uid");
328     $res    = get_list($filter,"users",$this->search_base,$attrs);
329     
330     $str = " AND (";
331     $fields = array("dstchannel","channel");
332     if(count($res)){
333       foreach($res as $attrs){
334         $uid =  $attrs["uid"][0];
335         foreach($fields as $name){
336           $str .= $name." like '%".$uid."%' OR ";
337         }
338       }
339     }
340     $str .= " channel     like '%".$s."%' OR  
341               dstchannel  like '%".$s."%' OR  
342               dst         like '".$s2."' OR 
343               src         like '".$s2."' OR 
344               lastapp     like '".$s2."')"; 
345     return($str);
346   }
349   /* Create query string */
350   function CreateQuerySyntax()
351   {
352     /* Get extended search filter which contain uids and so on */
353     $uidstring = $this->GetUidMatchingFilter();
355     /* Create string with all fields seperated by ,*/
356     $fields_str ="";
357     foreach($this->fields as $field){
358       if($field == "calldate") {
359         continue;       
360       }
361       $fields_str .= $field.", ";
362     }
363     $fields_str = preg_replace("/, $/","",$fields_str);
365     /* Create Sort tag */
366     if ($this->sort_direction == "down"){
367       $desc= "DESC";
368     } else {
369       $desc= "ASC";
370     }
371     /* Create times */
372     $start= date ("YmdHis", mktime(0,0,0,$this->month,1,$this->year));
373     $end=   date ("YmdHis", mktime(23,59,59,($this->month +1),0,$this->year));
374     $query = "SELECT ".$fields_str.",calldate FROM cdr ".
375       "WHERE   
376       calldate <= $end 
377       AND 
378       calldate >= $start 
379       ". $uidstring." 
380       ORDER BY ".$this->fields[$this->sort]." $desc;";
381     return($query);
382   }
385   /* Return plugin informations for acl handling 
386      #FIXME You can only read attributes within this report plugin */
387   function plInfo()
388   {
389     return (array(
390         "plShortName"   => _("Phone reports"),
391         "plDescription" => _("Phone reports")."&nbsp;<i>"._("All entries are readonly")."</i>",
392         "plSelfModify"  => TRUE,
393         "plDepends"     => array(),
394         "plPriority"    => 0,
395         "plSection"     => array("administration"),
396         "plCategory"    => array("gofon"),
397   
398         "plProvidedAcls" => array(
399           "calldate"      =>_("Date"), 
400           "src"           =>_("Source"), 
401           "dst"           =>_("Destination"), 
402           "channel"       =>_("Channel"), 
403           "lastapp"       =>_("Application called"),
404           "disposition"   =>_("Disposition"), 
405           "duration"      =>_("Duration"))
406         ));
407   }
409 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
410 ?>