Code

Removed test entries
[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 $year= "";
14   var $month= "";
15   var $sort= 0;
16   var $sort_direction= "down";
17   var $report_list= array();
18   var $userfilter= "";
19   var $ui= NULL;
20   var $fields= array("calldate", "src", "dst", "channel", "lastapp", "disposition", "duration");
21   var $range = 20;
23   /* attribute list for save action */
24   var $attributes= array();
25   var $objectclasses= array();
27   function fonreport ($config, $ui)
28   {
29           /* Include config object */
30           $this->config= $config;
31           $this->ui= $ui;
33           /* Try to get matching search base for user provided
34                  by 'dn' */
35           $tmp= preg_replace ("/^[^,]+,[^,]+,/", "", $ui->dn);
36           $sb= array_search($tmp, $this->config->departments);
37           if ($sb === FALSE){
38                   $sb= "/";
39           }
40           $this->search_base= $sb;
42           /* Get global filter config */
43           if (!is_global("fonfilter")){
44                   $ui= get_userinfo();
45                   $base= get_base_from_people($ui->dn);
46                   $fonfilter= array("year" => date("Y"),
47                                   "month" => date("m"),
48                                   "search_base" => $base,
49                                   "search_for" => "*");
50                   register_global("fonfilter", $fonfilter);
51           }
52   }
54   function execute()
55   {
56         /* Call parent execute */
57         plugin::execute();
59         /* GVet template engine */
60         $smarty= get_smarty();
62         foreach($this->fields as $field){
63                 if(chkacl($this->acl,$field)==""){
64                         $smarty->assign($field."ACL","");       
65                 }else{
66                         $smarty->assign($field."ACL"," disabled ");     
67                 }
68         }
70         $months= array();
71         $months[1]= _("January"); $months[2]= _("February"); $months[3]= _("March"); $months[4]= _("April");
72         $months[5]= _("May"); $months[6]= _("June"); $months[7]= _("July"); $months[8]= _("August");
73         $months[9]= _("September"); $months[10]= _("October"); $months[11]= _("November"); $months[12]= _("December");
75         /* Prepare template */
76         $current= date("Y");
77         $years= array();
78         for ($y= $current - 5; $y<=$current; $y++){
79                 $years[]= $y;
80         }
82         $smarty->assign("plug"                  , "?plug=".validate($_GET['plug']));
83         $smarty->assign("launchimage"   , get_template_path('images/launch.png'));
84         $smarty->assign("search_image"  , get_template_path('images/search.png'));
85         $smarty->assign("search_for"    , $this->search_for);
86         $smarty->assign("bases"                 , $this->config->idepartments);
87         $smarty->assign("base_select"   , $this->search_base);
88         $smarty->assign("months"                , $months);
89         $smarty->assign("month_select"  , $this->month);
90         $smarty->assign("years"                 , $years);
91         $smarty->assign("year_select"   , $this->year);
92         $smarty->assign("search_result" , "");
93         
94         /* Connecting, selecting database */
95         if (!isset($this->config->data['SERVERS']['FON'])){
96                 print_red(_("Can't connect to phone database, no reports can be shown!"));
97                 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
98         }elseif(!is_callable("mysql_connect")){
99                 print_red(_("There is no mysql extension available, please check your php setup."));
100                 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
101         }else{
103                 $cfg= $this->config->data['SERVERS']['FON'];
104                 $link = @mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD']);
105                 if ($link === FALSE){
106                         print_red(_("Can't connect to phone database, no reports can be shown!"));
107                         return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
108                 }
109                 if (! @mysql_select_db("gophone")){
110                         print_red(_("Can't select phone database for report generation!"));
111                         return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
112                 }
113         }
116         if(isset($_POST['EntryPerPage'])){
117                 $this->range = $_POST['EntryPerPage'];
118         }
120           $fonfilter= get_global("fonfilter");
121           foreach( array("year", "month", "search_for", "search_base") as $type){
122                   if (isset($_POST[$type])){
123                           $fonfilter[$type]= $_POST[$type];
124                   }
125                   $this->$type= $fonfilter[$type];
126           }
127           register_global("fonfilter", $fonfilter);
129           /* Adapt sorting */
130           if (isset($_GET['sort'])){
131                   if ($this->sort == (int)$_GET['sort']){
132                           if ($this->sort_direction == "down"){
133                                   $this->sort_direction= "up";
134                           } else {
135                                   $this->sort_direction= "down";
136                           }
137                   }
138                   $this->sort= (int)$_GET['sort'];
139                   if ($this->sort < 0 || $this->sort > 6){
140                           $this->sort= 0;
141                   }
142           }
144           /* Search button has been pressed */
145           if ($this->search_for != ""){
146                   $this->start= 0;
148                   if (is_integer (strpos($this->search_for, "*"))){
149                           $s= $this->search_for;
150                   } else {
151                           $s= "*".$this->search_for."*";
152                   }
153                   $ldap= $this->config->get_ldap_link();
154                   $ldap->cd ($this->search_base);
156                   /* Perform ldap search for potential users */
157                   $ldap->search ("(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))".
158                                   "(objectClass=gofonAccount)".
159                                   "(|(uid=$s)(l=$s)(homePhone=$s)".
160                                   "(telephoneNumber=$s)(facsimileTelephoneNumber=$s)(mobile=$s)".
161                                   "(pager=$s)(cn=$s)(givenName=$s)(sn=$s)(personalTitle=$s)".
162                                   "(title=$s)))");
164                   $fon_users= array();
165                   while ($attrs= $ldap->fetch()){
166                           $acl= get_permissions ($ldap->getDN(), $this->ui->subtreeACL);
167                           $acl= get_module_permission($acl, "fax", $ldap->getDN());
169                           if (chkacl ($acl, "faxReport") == ""){
170                                   $fax_users[]= $attrs["uid"][0];
171                           }
172                   }
174                   /* Prepare SQL query */
175                   $this->userfilter= "";
176                   foreach ($fon_users as $user){
177                           $this->userfilter.= "uid = '$user' OR ";
178                   }
179                   $this->userfilter= preg_replace("/OR $/", "", $this->userfilter);
180           }
182         $fields_str = "calldate, ";
184         foreach($this->fields as $field){
185                 if($field == "calldate") continue;      
186                 if(chkacl($this->acl,$field)==""){
187                         $fields_str .= $field.", ";
188                 }
189         }
191         $fields_str = preg_replace("/, $/","",$fields_str);
193         if ($this->sort_direction == "down"){
194                 $desc= "DESC";
195         } else {
196                 $desc= "";
197         }
198         $start= date ("YmdHis", mktime(0,0,0,$this->month,1,$this->year));
199         $end=   date ("YmdHis", mktime(23,59,59,$this->month+1,0,$this->year));
200         $query = "SELECT ".$fields_str." FROM cdr ".
201                 "WHERE calldate <= $end AND ".
202                 "calldate >= $start ORDER BY ".$this->fields[$this->sort]." $desc;";
204         $cfg= $this->config->data['SERVERS']['FON'];
205         $link = @mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD']);
207         @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__,
208                         $query, "Database query");
210         $result = @mysql_query($query);
211         if ($result === false){
212                 print_red(_("Query for phone database failed!"));
213                 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
214         }
215         $report_list= array();
216         while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
217                 $append_str = "";
218                 $hour=  substr($line["calldate"], 11, 2);
219                 $minute=substr($line["calldate"], 14, 2);
220                 $format= _("Y-M-D");
221                 $date= preg_replace("/Y/", substr($line["calldate"], 0, 4), $format);
222                 $date= preg_replace("/M/", substr($line["calldate"], 5, 2), $date);
223                 $date= preg_replace("/D/", substr($line["calldate"], 8, 2), $date);
224                 $append_str .=  "<td>$date $hour:$minute</td>";
225                 foreach(array("src","dst","channel","lastapp","disposition") as $atr){
226                         if(isset($line[$atr])){
227                                 $append_str .=  "<td>".$line[$atr]."</td>";
228                         }
229                 }
230                 if(isset($line['duration'])){
231                         $append_str .=  "<td>".$this->gen_duration($line["duration"])."</td>";
232                 }
233                 $report_list[] = $append_str;
234         }
236         $this->report_list= $report_list;
237         @mysql_close($link);
238 #   }
240         /* Generate output */
241         $mod= 0;
242         if (isset($_GET['start'])){
243                 $this->start= (int)$_GET['start'];
244         }
246         $output= "";
247         foreach ($this->report_list as $val){
248                 if ($mod < $this->start) {
249                         $mod++;
250                         continue;
251                 }
252                 if ($mod >= ($this->start + $this->range)){
253                         $mod++;
254                         break;
255                 }
256                 if ( ($mod++) & 1){
257                         $col= "background-color: #ECECEC;";
258                 } else {
259                         $col= "background-color: #F5F5F5;";
260                 }
261                 $output.= "<tr style=\"height:22px; $col\">$val</tr>";
262         }
265         if ($output != ""){
266                 $smarty->assign("search_result", $output);
267                 $smarty->assign("range_selector", range_selector(count($this->report_list), $this->start,$this->range,"EntryPerPage"));
268         } else {
269                 $smarty->assign("search_result", "");
270         }
272         /* Show main page */
273         for($i= 0; $i<7; $i++){
274                 $smarty->assign("mode$i", "");
275         }
276         $smarty->assign("mode".$this->sort, "<img alt=\"\" src=\"images/sort_".$this->sort_direction.
277                         ".png\" border=0 align=middle>");
278         return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
279   }
281   function gen_duration($seconds)
282   {
283         if ($seconds / 60 > 1){
284                 $minutes= (int)($seconds / 60);
285                 $seconds= $seconds % 60;
286                 return ("$minutes&rsquo;$seconds&rdquo;");
287         }
288         
289         return ("$seconds&rdquo;");
290   }
294 ?>