Code

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