Code

32e98614fff84868b79546bbbdacbbc572488403
[gosa.git] / plugins / gofax / faxreports / 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();
65         
66         $months= array();
67         $months[1]= _("January"); $months[2]= _("February"); $months[3]= _("March"); $months[4]= _("April");
68         $months[5]= _("May"); $months[6]= _("June"); $months[7]= _("July"); $months[8]= _("August");
69         $months[9]= _("September"); $months[10]= _("October"); $months[11]= _("November"); $months[12]= _("December");
71         $current= date("Y");
72         $years= array();
73         for ($y= $current - 5; $y<=$current; $y++){
74                 $years[]= $y;
75         }
77         $smarty= get_smarty();
78         $smarty->assign("launchimage"           , get_template_path('images/launch.png'));
79         $smarty->assign("search_image"          , get_template_path('images/search.png'));
80         $smarty->assign("search_for"            , $this->search_for);
81     $smarty->assign("bases"                             , $this->config->idepartments);
82     $smarty->assign("base_select"               , $this->search_base);
83     $smarty->assign("months"                    , $months);
84         $smarty->assign("month_select"          , $this->month);
85     $smarty->assign("years"                             , $years);
86         $smarty->assign("year_select"           , $this->year);
87         $smarty->assign("search_result"         , "");
89         /* Some checks */
90         if(!isset($this->config->data['SERVERS']['FAX'])){
91                 print_red(_("Can't connect to fax database, no reports can be shown!"));
92                 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
93         }elseif(!is_callable("mysql_connect")){
94                 print_red(_("There is no mysql extension available, please check your php setup."));
95                 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
96         }else{
97                 /* Connecting, selecting database */
98                 $cfg    = $this->config->data['SERVERS']['FAX'];
99                 $link   = @mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD']);
100                 if ($link === FALSE){
101                         print_red(_("Can't connect to fax database, no reports can be shown!"));
102                         return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
103                 }
104                 if (! @mysql_select_db("gofax")){
105                         print_red(_("Can't select fax database for report generation!"));
106                         return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
107                 }
108         }               
110         if(isset($_POST['EntriesPerPage'])){
111                 $this->range = $_POST['EntriesPerPage'];
112         }
114         /* Get template engine */
115         $faxfilter= get_global("faxreportfilter");
116         foreach( array("year", "month", "search_for", "search_base") as $type){
117                 if (isset($_POST[$type])){
118                         $faxfilter[$type]= $_POST[$type];
119                 }
120                 $this->$type= $faxfilter[$type];
121         }
122         register_global("faxreportfilter", $faxfilter);
123   
124         /* Adapt sorting */
125         if (isset($_GET['sort'])){
126                 if ($this->sort == (int)$_GET['sort']){
127                         if ($this->sort_direction == "down"){
128                                 $this->sort_direction= "up";
129                         } else {
130                                 $this->sort_direction= "down";
131                         }
132                 }
133                 $this->sort= (int)$_GET['sort'];
134                 if ($this->sort < 0 || $this->sort > 5){
135                         $this->sort= 0;
136                 }
137         }
139         /* Do detail view? */
140         if (isset($_GET['detail'])){
141                 $query = "SELECT id,uid,date_format(queuing_time, '%Y%m%d%H%i%s') as queuing_time,status,sender_id,sender_msn,receiver_id,".
142                         "receiver_msn,pages,status_message,transfer_time FROM faxlog ".
143                         "WHERE id=".$_GET['detail'].";";
145                 /* Connecting, selecting database */
146                 $cfg= $this->config->data['SERVERS']['FAX'];
149                 $link = @mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD']);
150                 if ($link === FALSE){
151                         print_red(_("Can't connect to fax database, no reports can be shown!"));
152                         return;
153                 }
154                 if (! @mysql_select_db("gofax")){
155                         print_red(_("Can't select fax database for report generation!"));
156                         return;
157                 }
159                 @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__,
160                         $query, "Database query");
161                 $result = @mysql_query($query);
162                 if ($result === false){
163                         print_red(_("Query for fax database failed!"));
164                         return;
165                 }
167                 $line = mysql_fetch_array($result, MYSQL_ASSOC);
168                 mysql_close($link);
170                 if (!preg_match ("/'".$line["uid"]."'/", $this->userfilter)){
171                         print_red (_("You have no permission to retrieve informations about this fax id!"));
172                         return;
173                 }
175                 $parts= array( "id", "uid", "queuing_time", "status", "sender_id", "sender_msn",
176                         "receiver_id", "receiver_msn", "pages", "status_message", "transfer_time" );
177                 foreach ($parts as $vname) {
178                         $final="fax_$vname";
179                         if ($line[$vname] != ""){
180                                 $smarty->assign("$final", $line[$vname]);
181                         } else {
182                                 $smarty->assign("$final", "-");
183                         }
184                 }
185                 $queuing_time= $line['queuing_time'];
187                 $_SESSION['fuserfilter']= $this->userfilter;
188             $smarty->assign("plug", "?plug=".validate($_GET['plug']));
189             $smarty->assign("detail", validate($_GET['detail']));
191                 $format= _("Y-M-D");
192                 $date= preg_replace("/Y/", substr($queuing_time,0,4), $format);
193                 $date= preg_replace("/M/", substr($queuing_time,4,2), $date);
194                 $date= preg_replace("/D/", substr($queuing_time,6,2), $date);
195                 $smarty->assign("date", $date);
196                 $smarty->assign("time", substr($queuing_time,8,2).":".
197                                         substr($queuing_time,10,2).":".
198                                         substr($queuing_time,12,2));
199                 return($smarty->fetch(get_template_path('detail.tpl', TRUE)));
200         }
202         /* Search button has been pressed */
203         if ($this->search_for != ""){
204                 $this->start= 0;
206                 if (is_integer (strpos($this->search_for, "*"))){
207                         $s= $this->search_for;
208                 } else {
209                         $s= "*".$this->search_for."*";
210                 }
211                 $ldap= $this->config->get_ldap_link();
212                 $ldap->cd ($this->search_base);
214                 /* Perform ldap search for potential users */
215                 $ldap->search ("(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))".
216                         "(objectClass=goFaxAccount)".
217                         "(|(uid=$s)(l=$s)(homePhone=$s)".
218                         "(telephoneNumber=$s)(facsimileTelephoneNumber=$s)(mobile=$s)".
219                         "(pager=$s)(cn=$s)(givenName=$s)(sn=$s)(personalTitle=$s)".
220                         "(title=$s)))");
222                 $fax_users= array();
223                 while ($attrs= $ldap->fetch()){
224                         $acl= get_permissions ($ldap->getDN(), $this->ui->subtreeACL);
225                         $acl= get_module_permission($acl, "fax", $ldap->getDN());
227                         if (chkacl ($acl, "faxReport") == ""){
228                                 $fax_users[]= $attrs["uid"][0];
229                         }
230                 }
232                 /* Prepare SQL query */
233                 $this->userfilter= "";
234                 foreach ($fax_users as $user){
235                         $this->userfilter.= "uid = '$user' OR ";
236                 }
237                 $this->userfilter= preg_replace("/OR $/", "", $this->userfilter);
238         }
241         /* Perform SQL query */
242         if ($this->userfilter){
243                 if ($this->sort_direction == "down"){
244                         $desc= "DESC";
245                 } else {
246                         $desc= "";
247                 }
248                 $start= date ("YmdHis", mktime(0,0,0,$this->month,1,$this->year));
249                 $end=   date ("YmdHis", mktime(23,59,59,$this->month+1,0,$this->year));
250                 $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 ".
251                         "WHERE ( ".$this->userfilter." ) AND queuing_time <= $end AND ".
252                         "queuing_time >= $start ORDER BY ".$this->fields[$this->sort]." $desc;";
254                 if(!is_callable("mysql_connect")){
255                         print_red("There is no mysql extension configured in your php setup.");
256                         return;
257                 }
259                 /* Connecting, selecting database */
260                 $cfg= $this->config->data['SERVERS']['FAX'];
261                 $link = @mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD']);
262                 if ($link === FALSE){
263                         print_red(_("Can't connect to fax database, no reports can be shown!"));
264                         return;
265                 }
266                 if (! @mysql_select_db("gofax")){
267                         print_red(_("Can't select fax database for report generation!"));
268                         return;
269                 }
271                 @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__,
272                         $query, "Database query");
273                 $result = @mysql_query($query);
274                 if ($result === false){
275                         print_red(_("Query for fax database failed!"));
276                         return;
277                 }
279                 $report_list= array();
280                 while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
281                         $hour=  substr($line["queuing_time"], 8, 2);
282                         $minute=substr($line["queuing_time"], 10, 2);
283                         $format= _("Y-M-D");
284                         $date= preg_replace("/Y/", substr($line["queuing_time"], 0, 4), $format);
285                         $date= preg_replace("/M/", substr($line["queuing_time"], 4, 2), $date);
286                         $date= preg_replace("/D/", substr($line["queuing_time"], 6, 2), $date);
289                         $report_list[]= "<td class=\"phonelist\"><a href=\"main.php?plug=".validate($_GET['plug'])."&amp;detail=".
290                                 $line["id"]."\"><img alt=\"\" align=\"middle\" border=0 src=\"".get_template_path('images/info_small.png')."\">&nbsp;".$line["uid"]."</a></td>".
291                                 "<td>$date $hour:$minute</td>".
292                                 "<td>".$this->status[$line["status"]]."</td>".
293                                 "<td>".$line["sender_id"]."</td>".
294                                 "<td>".$line["receiver_id"]."</td>".
295                                 "<td>".$line["pages"]."</td>";
296                 }
298                 $this->report_list= $report_list;
299                 mysql_close($link);
300         }
302         /* Generate output */
303         $mod= 0;
304         if (isset($_GET['start'])){
305                 $this->start= (int)$_GET['start'];
306         }
308         $output= "";
309         foreach ($this->report_list as $val){
310                 if ($mod < $this->start) {
311                         $mod++;
312                         continue;
313                 }
314                 if ($mod >= ($this->start + $this->range)){
315                         $mod++;
316                         break;
317                 }
318                 if ( ($mod++) & 1){
319                         $col= "background-color: #ECECEC;";
320                 } else {
321                         $col= "background-color: #F5F5F5;";
322                 }
323                 $output.= "<tr style=\"height:22px; $col\">$val</tr>";
324         }
327         if (isset($fax_users) && count($fax_users)){
328                 $smarty->assign("search_result", $output);
329                 $smarty->assign("range_selector", range_selector(count($this->report_list), $this->start, $this->range,"EntriesPerPage"));
330         }else{
331                 $smarty->assign("search_result", "");
332         }
334         
336         /* Show main page */
337     $smarty->assign("plug", "?plug=".validate($_GET['plug']));
338         for($i= 0; $i<7; $i++){
339                 $smarty->assign("mode$i", "");
340         }
341         $smarty->assign("mode".$this->sort, "<img alt=\"\" src=\"images/sort_".$this->sort_direction.
342                         ".png\" border=0 align=middle>");
343     return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
344   }
348 ?>