Code

dbae32e3c7c782f8122725ab23aeb3f7fdf29816
[gosa.git] / plugins / gofax / reports / class_faxreport.inc
1 <?php
3 class faxreport extends plugin
4 {
5   /* Definitions */
6   var $plHeadline= "FAX 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= 1;
16   var $sort_direction= "down";
17   var $report_list= array();
18   var $userfilter= "";
19   var $ui= NULL;
21   var $range = 20;
23   /* Constant stuff */
24   var $status= array( "SENT", "MAILED", "SERROR", "RERROR", "SBLOCK", "RBLOCK",
25                         "DELETED", "REQUEUED", "DISABLED", "PRINTED", "DIVERTED",
26                         "UNDEFINED", "UNDEFINED", "UNDEFINED", "UNDEFINED",
27                         "UNDEFINED");
28   var $fields= array("uid", "queuing_time", "status", "sender_id", "receiver_id", "pages");
30   /* attribute list for save action */
31   var $attributes= array();
32   var $objectclasses= array();
34   function faxreport ($config, $ui)
35   {
36         /* Include config object */
37         $this->config= $config;
38         $this->ui= $ui;
40         /* Try to get matching search base for user provided
41            by 'dn' */
42         $tmp= preg_replace ("/^[^,]+,[^,]+,/", "", $ui->dn);
43         $sb= array_search($tmp, $this->config->departments);
44         if ($sb === FALSE){
45                 $sb= "/";
46         }
47         $this->search_base= $sb;
49         /* Get global filter config */
50         if (!is_global("faxreportfilter")){
51                 $ui= get_userinfo();
52                 $base= get_base_from_people($ui->dn);
53                 $faxfilter= array("year" => date("Y"),
54                                 "month" => date("m"),
55                                 "search_base" => $base,
56                                 "search_for" => "*");
57                 register_global("faxreportfilter", $faxfilter);
58         }
59   }
61   function execute()
62   {
63         /* Call parent execute */
64         plugin::execute();
66         if(isset($_POST['EntriesPerPage'])){
67                 $this->range = $_POST['EntriesPerPage'];
68         }
70         /* Get template engine */
71         $smarty= get_smarty();
72         $faxfilter= get_global("faxreportfilter");
73         foreach( array("year", "month", "search_for", "search_base") as $type){
74                 if (isset($_POST[$type])){
75                         $faxfilter[$type]= $_POST[$type];
76                 }
77                 $this->$type= $faxfilter[$type];
78         }
79         register_global("faxreportfilter", $faxfilter);
80   
81         /* Adapt sorting */
82         if (isset($_GET['sort'])){
83                 if ($this->sort == (int)$_GET['sort']){
84                         if ($this->sort_direction == "down"){
85                                 $this->sort_direction= "up";
86                         } else {
87                                 $this->sort_direction= "down";
88                         }
89                 }
90                 $this->sort= (int)$_GET['sort'];
91                 if ($this->sort < 0 || $this->sort > 5){
92                         $this->sort= 0;
93                 }
94         }
96         /* Do detail view? */
97         if (isset($_GET['detail'])){
98                 $query = "SELECT id,uid,date_format(queuing_time, '%Y%m%d%H%i%s') as queuing_time,status,sender_id,sender_msn,receiver_id,".
99                         "receiver_msn,pages,status_message,transfer_time FROM faxlog ".
100                         "WHERE id=".$_GET['detail'].";";
102                 /* Connecting, selecting database */
103                 $cfg= $this->config->data['SERVERS']['FAX'];
105                 if(!is_callable("mysql_connect")){
106                         print_red(_("There is no mysql extension available, please check your php setup."));    
107                         return; 
108                 }               
110                 $link = @mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD']);
111                 if ($link === FALSE){
112                         print_red(_("Can't connect to fax database, no reports can be shown!"));
113                         return;
114                 }
115                 if (! @mysql_select_db("gofax")){
116                         print_red(_("Can't select fax database for report generation!"));
117                         return;
118                 }
120                 @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__,
121                         $query, "Database query");
122                 $result = @mysql_query($query);
123                 if ($result === false){
124                         print_red(_("Query for fax database failed!"));
125                         return;
126                 }
128                 $line = mysql_fetch_array($result, MYSQL_ASSOC);
129                 mysql_close($link);
131                 if (!preg_match ("/'".$line["uid"]."'/", $this->userfilter)){
132                         print_red (_("You have no permission to retrieve informations about this fax id!"));
133                         return;
134                 }
136                 $parts= array( "id", "uid", "queuing_time", "status", "sender_id", "sender_msn",
137                         "receiver_id", "receiver_msn", "pages", "status_message", "transfer_time" );
138                 foreach ($parts as $vname) {
139                         $final="fax_$vname";
140                         if ($line[$vname] != ""){
141                                 $smarty->assign("$final", $line[$vname]);
142                         } else {
143                                 $smarty->assign("$final", "-");
144                         }
145                 }
146                 $queuing_time= $line['queuing_time'];
148                 $_SESSION['fuserfilter']= $this->userfilter;
149             $smarty->assign("plug", "?plug=".validate($_GET['plug']));
150             $smarty->assign("detail", validate($_GET['detail']));
152                 $format= _("Y-M-D");
153                 $date= preg_replace("/Y/", substr($queuing_time,0,4), $format);
154                 $date= preg_replace("/M/", substr($queuing_time,4,2), $date);
155                 $date= preg_replace("/D/", substr($queuing_time,6,2), $date);
156                 $smarty->assign("date", $date);
157                 $smarty->assign("time", substr($queuing_time,8,2).":".
158                                         substr($queuing_time,10,2).":".
159                                         substr($queuing_time,12,2));
160                 return($smarty->fetch(get_template_path('detail.tpl', TRUE)));
161         }
163         /* Search button has been pressed */
164         if ($this->search_for != ""){
165                 $this->start= 0;
167                 if (is_integer (strpos($this->search_for, "*"))){
168                         $s= $this->search_for;
169                 } else {
170                         $s= "*".$this->search_for."*";
171                 }
172                 $ldap= $this->config->get_ldap_link();
173                 $ldap->cd ($this->search_base);
175                 /* Perform ldap search for potential users */
176                 $ldap->search ("(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))".
177                         "(objectClass=goFaxAccount)".
178                         "(|(uid=$s)(l=$s)(homePhone=$s)".
179                         "(telephoneNumber=$s)(facsimileTelephoneNumber=$s)(mobile=$s)".
180                         "(pager=$s)(cn=$s)(givenName=$s)(sn=$s)(personalTitle=$s)".
181                         "(title=$s)))");
183                 $fax_users= array();
184                 while ($attrs= $ldap->fetch()){
185                         $acl= get_permissions ($ldap->getDN(), $this->ui->subtreeACL);
186                         $acl= get_module_permission($acl, "fax", $ldap->getDN());
188                         if (chkacl ($acl, "faxReport") == ""){
189                                 $fax_users[]= $attrs["uid"][0];
190                         }
191                 }
193                 /* Prepare SQL query */
194                 $this->userfilter= "";
195                 foreach ($fax_users as $user){
196                         $this->userfilter.= "uid = '$user' OR ";
197                 }
198                 $this->userfilter= preg_replace("/OR $/", "", $this->userfilter);
199         }
202         /* Perform SQL query */
203         if ($this->userfilter){
204                 if ($this->sort_direction == "down"){
205                         $desc= "DESC";
206                 } else {
207                         $desc= "";
208                 }
209                 $start= date ("YmdHis", mktime(0,0,0,$this->month,1,$this->year));
210                 $end=   date ("YmdHis", mktime(23,59,59,$this->month+1,0,$this->year));
211                 $query = "SELECT id,uid,date_format(queuing_time, '%Y%m%d%H%i%s') as queuing_time,status,sender_id,receiver_id,pages FROM faxlog ".
212                         "WHERE ( ".$this->userfilter." ) AND queuing_time <= $end AND ".
213                         "queuing_time >= $start ORDER BY ".$this->fields[$this->sort]." $desc;";
215                 if(!is_callable("mysql_connect")){
216                         print_red("There is no mysql extension configured in your php setup.");
217                         return;
218                 }
220                 /* Connecting, selecting database */
221                 $cfg= $this->config->data['SERVERS']['FAX'];
222                 $link = @mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD']);
223                 if ($link === FALSE){
224                         print_red(_("Can't connect to fax database, no reports can be shown!"));
225                         return;
226                 }
227                 if (! @mysql_select_db("gofax")){
228                         print_red(_("Can't select fax database for report generation!"));
229                         return;
230                 }
232                 @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__,
233                         $query, "Database query");
234                 $result = @mysql_query($query);
235                 if ($result === false){
236                         print_red(_("Query for fax database failed!"));
237                         return;
238                 }
240                 $report_list= array();
241                 while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
242                         $hour=  substr($line["queuing_time"], 8, 2);
243                         $minute=substr($line["queuing_time"], 10, 2);
244                         $format= _("Y-M-D");
245                         $date= preg_replace("/Y/", substr($line["queuing_time"], 0, 4), $format);
246                         $date= preg_replace("/M/", substr($line["queuing_time"], 4, 2), $date);
247                         $date= preg_replace("/D/", substr($line["queuing_time"], 6, 2), $date);
250                         $report_list[]= "<td class=\"phonelist\"><a href=\"main.php?plug=".validate($_GET['plug'])."&amp;detail=".
251                                 $line["id"]."\"><img alt=\"\" align=\"middle\" border=0 src=\"".get_template_path('images/info_small.png')."\">&nbsp;".$line["uid"]."</a></td>".
252                                 "<td>$date $hour:$minute</td>".
253                                 "<td>".$this->status[$line["status"]]."</td>".
254                                 "<td>".$line["sender_id"]."</td>".
255                                 "<td>".$line["receiver_id"]."</td>".
256                                 "<td>".$line["pages"]."</td>";
257                 }
259                 $this->report_list= $report_list;
260                 mysql_close($link);
261         }
263         /* Generate output */
264         $mod= 0;
265         if (isset($_GET['start'])){
266                 $this->start= (int)$_GET['start'];
267         }
269         $output= "";
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         /* Prepare template */
288         $smarty->assign("search_for", $this->search_for);
289         $smarty->assign("bases", $this->config->idepartments);
290         $smarty->assign("base_select", $this->search_base);
291         $months= array();
292         $months[1]= _("January"); $months[2]= _("February"); $months[3]= _("March"); $months[4]= _("April");
293         $months[5]= _("May"); $months[6]= _("June"); $months[7]= _("July"); $months[8]= _("August");
294         $months[9]= _("September"); $months[10]= _("October"); $months[11]= _("November"); $months[12]= _("December");
295         $smarty->assign("months", $months);
296         $smarty->assign("month_select", $this->month);
297         $current= date("Y");
298         $years= array();
299         for ($y= $current - 5; $y<=$current; $y++){
300                 $years[]= $y;
301         }
302         $smarty->assign("years", $years);
303         $smarty->assign("year_select", $this->year);
305         if (isset($fax_users) && count($fax_users)){
306                 $smarty->assign("search_result", $output);
307                 $smarty->assign("range_selector", range_selector(count($this->report_list), $this->start, $this->range,"EntriesPerPage"));
308         }else{
309                 $smarty->assign("search_result", "");
310         }
312         
314         /* Show main page */
315     $smarty->assign("plug", "?plug=".validate($_GET['plug']));
316         $smarty->assign("launchimage", get_template_path('images/launch.png'));
317         $smarty->assign("search_image", get_template_path('images/search.png'));
318         for($i= 0; $i<7; $i++){
319                 $smarty->assign("mode$i", "");
320         }
321         $smarty->assign("mode".$this->sort, "<img alt=\"\" src=\"images/sort_".$this->sort_direction.
322                         ".png\" border=0 align=middle>");
323         return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
324   }
328 ?>