Code

Updated fon plugin.
[gosa.git] / gosa-plugins / gofon / 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";
8   var $plIcon       = "plugins/gofon/images/phonereport.png";
11   /* For internal use */
12   var $start                              = 0;
13   var $search_for                 = "*";
14   var $search_base        = "";
15   var $fields                             = array("calldate", "src", "dst", "channel", "lastapp", "disposition", "duration");
16   var $year                                       = "";
17   var $month                              = "";
18   var $sort                                       = 0;
19   var $sort_direction   = "down";
20   var $report_list              = array();
21   var $userfilter                       = "";
22   var $ui                                           = NULL;
23   var $range                              = 20;
24   var $EntryPerPage   = 20;
26   /* attribute list for save action */
27   var $attributes_SO  = array("start","search_for","search_base","range","month","sort_direction","sort","year");
28   var $objectclasses  = array();
29   var $view_logged = FALSE;
30   
31   /* Construct class */
32   function fonreport (&$config, $ui)
33   {
34     /* Include config object */
35     $this->config                       = $config;
36     $this->ui                             = $ui;
37     $this->search_base= get_base_from_people($ui->dn);
38   
39     $this->month      = date("m");
40     $this->year       = date("Y");
42     /* Use filter settings if we have already searched */
43     if (!session::is_set("fonfilter")){
44       $fonfilter = array();
45       foreach($this->attributes_SO as $name){
46         $fonfilter[$name]=$this->$name;
47       }
48       session::set("fonfilter", $fonfilter);
49     }else{
50       $fonfilter = session::get("fonfilter");
51       foreach($this->attributes_SO as $name){
52         $this->$name = $fonfilter[$name];
53       }
54     }
55   }
58   /* Save ui interactions and store results in session, 
59       to remember settings */
60   function save_object()
61   {
62     $fonfilter= session::get("fonfilter");
63     if(isset($_POST['EntryPerPage'])){
64       $this->range = $_POST['EntryPerPage'];
65     }
66     if (isset($_GET['start'])){
67       $this->start= (int)$_GET['start'];
68     }
69     foreach( array("year", "month", "search_for", "search_base") as $type){
70       if (isset($_POST[$type])){
71         $this->$type= $_POST[$type];
72       }
73     }
74     
75     /* Adapt sorting */
76     if (isset($_GET['sort'])){
77       if ($this->sort == (int)$_GET['sort']){
78         if ($this->sort_direction == "down"){
79           $this->sort_direction= "up";
80         } else {
81           $this->sort_direction= "down";
82         }
83       }
84       $this->sort= (int)$_GET['sort'];
85       if ($this->sort < 0 || $this->sort > 6){
86         $this->sort= 0;
87       }
88     }
90     /* remove unwanted tags */
91     $this->search_for = stripslashes(preg_replace("/[^0-9a-z\*\+ \-]/i","",$this->search_for));
92   
93     foreach($this->attributes_SO as $name){
94       $fonfilter[$name] = $this->$name;
95     }
96     session::set("fonfilter", $fonfilter);
97   }
100   /* Search & display results */
101   function execute()
102   {
103     /* Call parent execute */
104     plugin::execute();
106     /* GVet template engine */
107     $smarty= get_smarty();
109     /* Log view */
110     if(!$this->view_logged){
111       $this->view_logged = TRUE;
112       new log("view","gofon/".get_class($this),$this->dn);
113     }
115     /*****************
116       Variable Init
117      *****************/
119     $fields_str = "";
121     $months= array();
122     for($i = 1 ; $i <= 12 ; $i ++ ){
123       $months[$i] = _(date("F",gmmktime(0,0,0,$i)));
124     }
126     /* Prepare template */
127     $current= date("Y");
128     $years= array();
129     for ($y= $current - 5; $y<=$current; $y++){
130       $years[$y]= $y;
131     }
133     /*****************
134       Smarty 
135      *****************/
136     $bases      = array();
137     $cat_bases  = $this->ui->get_module_departments("gofon");
138     foreach($this->config->idepartments as $dn => $name){
139       if(in_array_ics($dn,$cat_bases)){
140         $bases[$dn] = $name;
141       }
142     }
144     if(!isset($bases[$this->search_base])){
145       $this->search_base = key($bases);
146     }
148     $smarty->assign("plug",           "?plug=".validate($_GET['plug']));
149     $smarty->assign("launchimage",    get_template_path('images/launch.png'));
150     $smarty->assign("search_image",   get_template_path('images/lists/search.png'));
151     $smarty->assign("search_for",     $this->search_for);
152     $smarty->assign("bases",          $bases);
153     $smarty->assign("base_select",    $this->search_base);
154     $smarty->assign("months",         $months);
155     $smarty->assign("month_select",   $this->month);
156     $smarty->assign("years",          $years);
157     $smarty->assign("year_select",    $this->year);
158     $smarty->assign("search_result",  "");
161     /*****************
162       Check Database , Table , Connection 
163      *****************/
165     /* Connecting, selecting database */
166     if (!isset($this->config->data['SERVERS']['FON'][0])){
167       msg_dialog::display(_("Configuration error"), msgPool::noserver(_("GOfon")), WARNING_DIALOG);
168       return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
169     }elseif(!is_callable("mysql_connect")){
170       msg_dialog::display(_("Configuration error"), msgPool::missingext("php-mysql"), WARNING_DIALOG);
171       return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
172     }else{
174       $cfg= $this->config->data['SERVERS']['FON'][0];
175       $link = @mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD']);
176       if ($link === FALSE){
177         msg_dialog::display(_("Error"), msgPool::dbconnect(_("GOfon"),@mysql_error(),$cfg['SERVER']),ERROR_DIALOG);
178         return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
179       }
180       if (! @mysql_select_db("gophone")){
181         msg_dialog::display(_("Error"), msgPool::dbselect(_("GOfon"),@mysql_error(),$cfg['DB']),ERROR_DIALOG);
182         return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
183       }
184     }
187     /*****************
188       Get Query String && Search
189      *****************/
191     $query  = $this->CreateQuerySyntax();
192     $cfg    = $this->config->data['SERVERS']['FON'][0];
193     $link   = @mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD']);
195     @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__,$query, "Database query");
197     $result = @mysql_query($query);
198     if ($result === false){
199       msg_dialog::display(_("Error"), msgPool::dbquery(_("GOfon"),@mysql_error(),$cfg['SERVER']),ERROR_DIALOG);
200       return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
201     }
203     /*****************
204       Fetch results 
205      *****************/
206   
207     $report_list= array();
208     
209     /* Restricted attributes will not be displayed, this will be displayed instead */
210     $no_acl = "<img class='center' src='images/lists/locked.png'
211       title='".msgPool::permView()."' alt='"._("Insufficient permissions")."'>";
213     $no_acl = "&nbsp;";
215     while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
217       foreach($line as $attr => $value){
219         if($attr == "duration") continue;
220   
221         $acl = $this->ui->get_permissions($this->search_base,"gofon/fonreport",$attr);
222         if(!preg_match("/r/",$acl)){
223           $line[$attr] = $no_acl;
224         }
225       }
227       if($this->ui->get_permissions($this->search_base,"gofon/fonreport","calldate")){
228         $hour=  substr($line["calldate"], 11, 2);
229         $minute=substr($line["calldate"], 14, 2);
230         $format= _("Y-M-D");
231         $date= preg_replace("/Y/", substr($line["calldate"], 0, 4), $format);
232         $date= preg_replace("/M/", substr($line["calldate"], 5, 2), $date);
233         $date= preg_replace("/D/", substr($line["calldate"], 8, 2), $date);
234         $date_str = $date." ".$hour.":".$minute;
235       }else{
236         $date_str = $no_acl;
237       }
238   
239       $append_str = "";
240       $append_str .=    "<td>".$date_str."</td>";
242       foreach(array("src","dst","channel","lastapp","disposition") as $atr){
243         if(isset($line[$atr])){
244           $append_str .=        "<td>".$line[$atr]."</td>";
245         }
246       }
248       if($this->ui->get_permissions($this->search_base,"gofon/fonreport","duration")){
249         $append_str .=  "<td>".$this->gen_duration($line["duration"])."</td>";
250       }else{
251         $append_str .=  "<td>".$no_acl."</td>";
252       }
253       $report_list[] = $append_str;
254     }
256     $this->report_list= $report_list;
257     @mysql_close($link);
260     /*****************
261       Create list of results  
262      *****************/
264     /* Generate output */
265     $mod      = 0;
266     $output   = "";
267     if(count($this->report_list) < $this->start){
268       $this->start = 0;
269     }
270     foreach($this->report_list as $val){
271       if ($mod < $this->start) {
272         $mod++;
273         continue;
274       }
275       if ($mod >= ($this->start + $this->range)){
276         $mod++;
277         break;
278       }
279       if ( ($mod++) & 1){
280         $col= "background-color: #ECECEC;";
281       } else {
282         $col= "background-color: #F5F5F5;";
283       }
284       $output.= "<tr style=\"height:22px; $col\">$val</tr>";
285     }
287     /*****************
288       Tell smarty what we have found  
289      *****************/
291     if ($output != ""){
292       $smarty->assign("search_result", $output);
293       $smarty->assign("range_selector", range_selector(count($this->report_list), $this->start,$this->range,"EntryPerPage"));
294     } else {
295       $smarty->assign("search_result", "");
296     }
298     /* Show main page */
299     for($i= 0; $i<7; $i++){
300       $smarty->assign("mode$i", "");
301     }
302     $smarty->assign("mode".$this->sort, "<img alt=\"\" src=\"images/sort_".$this->sort_direction.".png\" border=0 align=middle>");
304     return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
305   }
307   
308   /* Create duration string   12'11" */
309   function gen_duration($seconds)
310   {
311     if ($seconds / 60 > 1){
312       $minutes= (int)($seconds / 60);
313       $seconds= $seconds % 60;
314       return ("$minutes&rsquo;$seconds&rdquo;");
315     }
316     return ("$seconds&rdquo;");
317   }
320   /* Create WHERE part for our mysql serach */
321   function GetUidMatchingFilter()
322   {
323     $ldap       = $this->config->get_ldap_link();
324     $ldap->cd     ($this->search_base);
325     $s          = $this->search_for;
327     $s          = preg_replace("/\%/","",$s);  
328     $s2         = preg_replace("/\*/","%",$s);  
330     $filter = "(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))".
331         "(|(uid=$s)(l=$s)(homePhone=$s)(telephoneNumber=$s)(facsimileTelephoneNumber=$s)(mobile=$s)".
332         "(pager=$s)(cn=$s)(givenName=$s)(sn=$s)(personalTitle=$s)(title=$s)))";
334     $attrs  = array("uid");
335     $res    = get_sub_list($filter,"users",get_people_ou(),$this->search_base,$attrs);
336     
337     $str = " AND (";
338     $fields = array("dstchannel","channel");
339     if(count($res)){
340       foreach($res as $attrs){
341         $uid =  $attrs["uid"][0];
342         foreach($fields as $name){
343           $str .= $name." like '%".$uid."%' OR ";
344         }
345       }
346     }
347     $str .= " channel     like '%".$s."%' OR  
348               dstchannel  like '%".$s."%' OR  
349               dst         like '".$s2."' OR 
350               src         like '".$s2."' OR 
351               lastapp     like '".$s2."')"; 
352     return($str);
353   }
356   /* Create query string */
357   function CreateQuerySyntax()
358   {
359     /* Get extended search filter which contain uids and so on */
360     $uidstring = $this->GetUidMatchingFilter();
362     /* Create string with all fields seperated by ,*/
363     $fields_str ="";
364     foreach($this->fields as $field){
365       if($field == "calldate") {
366         continue;       
367       }
368       $fields_str .= $field.", ";
369     }
370     $fields_str = preg_replace("/, $/","",$fields_str);
372     /* Create Sort tag */
373     if ($this->sort_direction == "down"){
374       $desc= "DESC";
375     } else {
376       $desc= "ASC";
377     }
378     /* Create times */
379     $start= date ("YmdHis", mktime(0,0,0,$this->month,1,$this->year));
380     $end=   date ("YmdHis", mktime(23,59,59,($this->month +1),0,$this->year));
381     $query = "SELECT ".$fields_str.",calldate FROM cdr ".
382       "WHERE   
383       calldate <= $end 
384       AND 
385       calldate >= $start 
386       ". $uidstring." 
387       ORDER BY ".$this->fields[$this->sort]." $desc;";
388     return($query);
389   }
392   /* Return plugin informations for acl handling 
393      #FIXME You can only read attributes within this report plugin */
394   static function plInfo()
395   {
396     return (array(
397         "plShortName"   => _("Phone reports"),
398         "plDescription" => _("Phone reports")."&nbsp;<i>"._("All entries are readonly")."</i>",
399         "plSelfModify"  => TRUE,
400         "plDepends"     => array(),
401         "plPriority"    => 0,
402         "plSection"     => array("administration"),
403         "plCategory"    => array("fonreport"),
404   
405         "plProvidedAcls" => array(
406           "calldate"      =>_("Date"), 
407           "src"           =>_("Source"), 
408           "dst"           =>_("Destination"), 
409           "channel"       =>_("Channel"), 
410           "lastapp"       =>_("Application called"),
411           "disposition"   =>_("Disposition"), 
412           "duration"      =>_("Duration"))
413         ));
414   }
416 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
417 ?>