Code

4a08c04cac148f37c0856b7bfa892e699c602c23
[gosa.git] / plugins / gofon / reports / 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         if(isset($_POST['EntryPerPage'])){
60                 $this->range = $_POST['EntryPerPage'];
61         }
63           /* Get template engine */
64           $smarty= get_smarty();
65           $fonfilter= get_global("fonfilter");
66           foreach( array("year", "month", "search_for", "search_base") as $type){
67                   if (isset($_POST[$type])){
68                           $fonfilter[$type]= $_POST[$type];
69                   }
70                   $this->$type= $fonfilter[$type];
71           }
72           register_global("fonfilter", $fonfilter);
74           /* Adapt sorting */
75           if (isset($_GET['sort'])){
76                   if ($this->sort == (int)$_GET['sort']){
77                           if ($this->sort_direction == "down"){
78                                   $this->sort_direction= "up";
79                           } else {
80                                   $this->sort_direction= "down";
81                           }
82                   }
83                   $this->sort= (int)$_GET['sort'];
84                   if ($this->sort < 0 || $this->sort > 6){
85                           $this->sort= 0;
86                   }
87           }
89           /* Search button has been pressed */
90           if ($this->search_for != ""){
91                   $this->start= 0;
93                   if (is_integer (strpos($this->search_for, "*"))){
94                           $s= $this->search_for;
95                   } else {
96                           $s= "*".$this->search_for."*";
97                   }
98                   $ldap= $this->config->get_ldap_link();
99                   $ldap->cd ($this->search_base);
101                   /* Perform ldap search for potential users */
102                   $ldap->search ("(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))".
103                                   "(objectClass=gofonAccount)".
104                                   "(|(uid=$s)(l=$s)(homePhone=$s)".
105                                   "(telephoneNumber=$s)(facsimileTelephoneNumber=$s)(mobile=$s)".
106                                   "(pager=$s)(cn=$s)(givenName=$s)(sn=$s)(personalTitle=$s)".
107                                   "(title=$s)))");
109                   $fon_users= array();
110                   while ($attrs= $ldap->fetch()){
111                           $acl= get_permissions ($ldap->getDN(), $this->ui->subtreeACL);
112                           $acl= get_module_permission($acl, "fax", $ldap->getDN());
114                           if (chkacl ($acl, "faxReport") == ""){
115                                   $fax_users[]= $attrs["uid"][0];
116                           }
117                   }
119                   /* Prepare SQL query */
120                   $this->userfilter= "";
121                   foreach ($fon_users as $user){
122                           $this->userfilter.= "uid = '$user' OR ";
123                   }
124                   $this->userfilter= preg_replace("/OR $/", "", $this->userfilter);
125           }
127           /* Perform SQL query */
128 ##### FIXME ACL, FILTER ######
129 #        if ($this->userfilter){
130         if ($this->sort_direction == "down"){
131                 $desc= "DESC";
132         } else {
133                 $desc= "";
134         }
135         $start= date ("YmdHis", mktime(0,0,0,$this->month,1,$this->year));
136         $end=   date ("YmdHis", mktime(23,59,59,$this->month+1,0,$this->year));
137         $query = "SELECT calldate, channel, src, clid, lastapp, lastdata, dst, ".
138                 "disposition, duration FROM cdr ".
139                 "WHERE calldate <= $end AND ".
140                 "calldate >= $start ORDER BY ".$this->fields[$this->sort]." $desc;";
142         /* Connecting, selecting database */
143         if (!isset($this->config->data['SERVERS']['FON'])){
144                 return ("");
145         }
147         if(!is_callable("mysql_connect")){
148                 print_red("There is no mysql extension available.");
149                 return;
150         }
152         $cfg= $this->config->data['SERVERS']['FON'];
153         $link = @mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD']);
154         if ($link === FALSE){
155                 print_red(_("Can't connect to phone database, no reports can be shown!"));
156                 return;
157         }
158         if (! @mysql_select_db("gophone")){
159                 print_red(_("Can't select phone database for report generation!"));
160                 return;
161         }
163         @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__,
164                         $query, "Database query");
166         $result = @mysql_query($query);
167         if ($result === false){
168                 print_red(_("Query for phone database failed!"));
169                 return;
170         }
171         $report_list= array();
172         while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
173                 $hour=  substr($line["calldate"], 11, 2);
174                 $minute=substr($line["calldate"], 14, 2);
175                 $format= _("Y-M-D");
176                 $date= preg_replace("/Y/", substr($line["calldate"], 0, 4), $format);
177                 $date= preg_replace("/M/", substr($line["calldate"], 5, 2), $date);
178                 $date= preg_replace("/D/", substr($line["calldate"], 8, 2), $date);
180                 $report_list[]= "<td>$date $hour:$minute</td>".
181                         "<td>".$line["src"]."</td>".
182                         "<td>".$line["dst"]."</td>".
183                         "<td>".$line["channel"]."</td>".
184                         "<td>".$line["lastapp"]."</td>".
185                         "<td>".$line["disposition"]."</td>".
186                         "<td>".$this->gen_duration($line["duration"])."</td>";
187         }
189         $this->report_list= $report_list;
190         mysql_close($link);
191 #        }
193         /* Generate output */
194         $mod= 0;
195         if (isset($_GET['start'])){
196                 $this->start= (int)$_GET['start'];
197         }
199         $output= "";
200         foreach ($this->report_list as $val){
201                 if ($mod < $this->start) {
202                         $mod++;
203                         continue;
204                 }
205                 if ($mod >= ($this->start + $this->range)){
206                         $mod++;
207                         break;
208                 }
209                 if ( ($mod++) & 1){
210                         $col= "background-color: #ECECEC;";
211                 } else {
212                         $col= "background-color: #F5F5F5;";
213                 }
214                 $output.= "<tr style=\"height:22px; $col\">$val</tr>";
215         }
217         /* Prepare template */
218         $smarty->assign("search_for", $this->search_for);
219         $smarty->assign("bases", $this->config->idepartments);
220         $smarty->assign("base_select", $this->search_base);
221         $months= array();
222         $months[1]= _("January"); $months[2]= _("February"); $months[3]= _("March"); $months[4]= _("April");
223         $months[5]= _("May"); $months[6]= _("June"); $months[7]= _("July"); $months[8]= _("August");
224         $months[9]= _("September"); $months[10]= _("October"); $months[11]= _("November"); $months[12]= _("December");
225         $smarty->assign("months", $months);
226         $smarty->assign("month_select", $this->month);
227         $current= date("Y");
228         $years= array();
229         for ($y= $current - 5; $y<=$current; $y++){
230                 $years[]= $y;
231         }
232         $smarty->assign("years", $years);
233         $smarty->assign("year_select", $this->year);
235         if ($output != ""){
236                 $smarty->assign("search_result", $output);
237                 $smarty->assign("range_selector", range_selector(count($this->report_list), $this->start,$this->range,"EntryPerPage"));
238         } else {
239                 $smarty->assign("search_result", "");
240         }
242         /* Show main page */
243         $smarty->assign("plug", "?plug=".validate($_GET['plug']));
244         $smarty->assign("launchimage", get_template_path('images/launch.png'));
245         $smarty->assign("search_image", get_template_path('images/search.png'));
246         for($i= 0; $i<7; $i++){
247                 $smarty->assign("mode$i", "");
248         }
249         $smarty->assign("mode".$this->sort, "<img alt=\"\" src=\"images/sort_".$this->sort_direction.
250                         ".png\" border=0 align=middle>");
251         return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
252   }
254   function gen_duration($seconds)
255   {
256         if ($seconds / 60 > 1){
257                 $minutes= (int)($seconds / 60);
258                 $seconds= $seconds % 60;
259                 return ("$minutes&rsquo;$seconds&rdquo;");
260         }
261         
262         return ("$seconds&rdquo;");
263   }
267 ?>