Code

Moved to trunk/branches/tags structure
[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   /* Constant stuff */
22   var $status= array( "SENT", "MAILED", "SERROR", "RERROR", "SBLOCK", "RBLOCK",
23                         "DELETED", "REQUEUED", "DISABLED", "PRINTED", "DIVERTED",
24                         "UNDEFINED", "UNDEFINED", "UNDEFINED", "UNDEFINED",
25                         "UNDEFINED");
26   var $fields= array("uid", "queuing_time", "status", "sender_id", "receiver_id", "pages");
28   /* attribute list for save action */
29   var $attributes= array();
30   var $objectclasses= array();
32   function faxreport ($config, $ui)
33   {
34         /* Include config object */
35         $this->config= $config;
36         $this->ui= $ui;
38         /* Try to get matching search base for user provided
39            by 'dn' */
40         $tmp= preg_replace ("/^[^,]+,[^,]+,/", "", $ui->dn);
41         $sb= array_search($tmp, $this->config->departments);
42         if ($sb === FALSE){
43                 $sb= "/";
44         }
45         $this->search_base= $sb;
47         /* Get global filter config */
48         if (!is_global("faxfilter")){
49                 $ui= get_userinfo();
50                 $base= get_base_from_people($ui->dn);
51                 $faxfilter= array("year" => date("Y"),
52                                 "month" => date("m"),
53                                 "search_base" => $base,
54                                 "search_for" => "*");
55                 register_global("faxfilter", $faxfilter);
56         }
57   }
59   function execute()
60   {
61         /* Get template engine */
62         $smarty= get_smarty();
63         $faxfilter= get_global("faxfilter");
64         foreach( array("year", "month", "search_for", "search_base") as $type){
65                 if (isset($_POST[$type])){
66                         $faxfilter[$type]= $_POST[$type];
67                 }
68                 $this->$type= $faxfilter[$type];
69         }
70         register_global("faxfilter", $faxfilter);
71   
72         /* Adapt sorting */
73         if (isset($_GET['sort'])){
74                 if ($this->sort == (int)$_GET['sort']){
75                         if ($this->sort_direction == "down"){
76                                 $this->sort_direction= "up";
77                         } else {
78                                 $this->sort_direction= "down";
79                         }
80                 }
81                 $this->sort= (int)$_GET['sort'];
82                 if ($this->sort < 0 || $this->sort > 5){
83                         $this->sort= 0;
84                 }
85         }
87         /* Do detail view? */
88         if (isset($_GET['detail'])){
89                 $query = "SELECT id,uid,date_format(queuing_time, '%Y%m%d%H%i%s') as queuing_time,status,sender_id,sender_msn,receiver_id,".
90                         "receiver_msn,pages,status_message,transfer_time FROM faxlog ".
91                         "WHERE id=".$_GET['detail'].";";
93                 /* Connecting, selecting database */
94                 $cfg= $this->config->data['SERVERS']['FAX'];
95                 $link = @mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD']);
96                 if ($link === FALSE){
97                         print_red(_("Can't connect to fax database, no reports can be shown!"));
98                         return;
99                 }
100                 if (! @mysql_select_db("gofax")){
101                         print_red(_("Can't select fax database for report generation!"));
102                         return;
103                 }
105                 @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__,
106                         $query, "Database query");
107                 $result = @mysql_query($query);
108                 if ($result === false){
109                         print_red(_("Query for fax database failed!"));
110                         return;
111                 }
113                 $line = mysql_fetch_array($result, MYSQL_ASSOC);
114                 mysql_close($link);
116                 if (!preg_match ("/'".$line["uid"]."'/", $this->userfilter)){
117                         print_red (_("You have no permission to retrieve informations about this fax id!"));
118                         return;
119                 }
121                 $parts= array( "id", "uid", "queuing_time", "status", "sender_id", "sender_msn",
122                         "receiver_id", "receiver_msn", "pages", "status_message", "transfer_time" );
123                 foreach ($parts as $vname) {
124                         $final="fax_$vname";
125                         if ($line[$vname] != ""){
126                                 $smarty->assign("$final", $line[$vname]);
127                         } else {
128                                 $smarty->assign("$final", "-");
129                         }
130                 }
131                 $queuing_time= $line['queuing_time'];
133                 $_SESSION['userfilter']= $this->userfilter;
134                 $smarty->assign("plug", validate($_GET['plug']));
135                 $smarty->assign("detail", validate($_GET['detail']));
137                 $format= _("Y-M-D");
138                 $date= preg_replace("/Y/", substr($queuing_time,0,4), $format);
139                 $date= preg_replace("/M/", substr($queuing_time,4,2), $date);
140                 $date= preg_replace("/D/", substr($queuing_time,6,2), $date);
141                 $smarty->assign("date", $date);
142                 $smarty->assign("time", substr($queuing_time,8,2).":".
143                                         substr($queuing_time,10,2).":".
144                                         substr($queuing_time,12,2));
145                 return($smarty->fetch(get_template_path('detail.tpl', TRUE)));
146         }
148         /* Search button has been pressed */
149         if ($this->search_for != ""){
150                 $this->start= 0;
152                 if (is_integer (strpos($this->search_for, "*"))){
153                         $s= $this->search_for;
154                 } else {
155                         $s= "*".$this->search_for."*";
156                 }
157                 $ldap= $this->config->get_ldap_link();
158                 $ldap->cd ($this->search_base);
160                 /* Perform ldap search for potential users */
161                 $ldap->search ("(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))".
162                         "(objectClass=goFaxAccount)".
163                         "(|(uid=$s)(l=$s)(homePhone=$s)".
164                         "(telephoneNumber=$s)(facsimileTelephoneNumber=$s)(mobile=$s)".
165                         "(pager=$s)(cn=$s)(givenName=$s)(sn=$s)(personalTitle=$s)".
166                         "(title=$s)))");
168                 $fax_users= array();
169                 while ($attrs= $ldap->fetch()){
170                         $acl= get_permissions ($ldap->getDN(), $this->ui->subtreeACL);
171                         $acl= get_module_permission($acl, "fax", $ldap->getDN());
173                         if (chkacl ($acl, "faxReport") == ""){
174                                 $fax_users[]= $attrs["uid"][0];
175                         }
176                 }
178                 /* Prepare SQL query */
179                 $this->userfilter= "";
180                 foreach ($fax_users as $user){
181                         $this->userfilter.= "uid = '$user' OR ";
182                 }
183                 $this->userfilter= preg_replace("/OR $/", "", $this->userfilter);
184         }
187         /* Perform SQL query */
188         if ($this->userfilter){
189                 if ($this->sort_direction == "down"){
190                         $desc= "DESC";
191                 } else {
192                         $desc= "";
193                 }
194                 $start= date ("YmdHis", mktime(0,0,0,$this->month,1,$this->year));
195                 $end=   date ("YmdHis", mktime(23,59,59,$this->month+1,0,$this->year));
196                 $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 ".
197                         "WHERE ( ".$this->userfilter." ) AND queuing_time <= $end AND ".
198                         "queuing_time >= $start ORDER BY ".$this->fields[$this->sort]." $desc;";
200                 /* Connecting, selecting database */
201                 $cfg= $this->config->data['SERVERS']['FAX'];
202                 $link = @mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD']);
203                 if ($link === FALSE){
204                         print_red(_("Can't connect to fax database, no reports can be shown!"));
205                         return;
206                 }
207                 if (! @mysql_select_db("gofax")){
208                         print_red(_("Can't select fax database for report generation!"));
209                         return;
210                 }
212                 @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__,
213                         $query, "Database query");
214                 $result = @mysql_query($query);
215                 if ($result === false){
216                         print_red(_("Query for fax database failed!"));
217                         return;
218                 }
220                 $report_list= array();
221                 while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
222                         $hour=  substr($line["queuing_time"], 8, 2);
223                         $minute=substr($line["queuing_time"], 10, 2);
224                         $format= _("Y-M-D");
225                         $date= preg_replace("/Y/", substr($line["queuing_time"], 0, 4), $format);
226                         $date= preg_replace("/M/", substr($line["queuing_time"], 4, 2), $date);
227                         $date= preg_replace("/D/", substr($line["queuing_time"], 6, 2), $date);
230                         $report_list[]= "<td class=\"phonelist\"><a href=\"main.php?plug=".validate($_GET['plug'])."&amp;detail=".
231                                 $line["id"]."\"><img align=\"center\" border=0 src=\"".get_template_path('images/info_small.png')."\">&nbsp;".$line["uid"]."</a></td>".
232                                 "<td>$date $hour:$minute</td>".
233                                 "<td>".$this->status[$line["status"]]."</td>".
234                                 "<td>".$line["sender_id"]."</td>".
235                                 "<td>".$line["receiver_id"]."</td>".
236                                 "<td>".$line["pages"]."</td>";
237                 }
239                 $this->report_list= $report_list;
240                 mysql_close($link);
241         }
243         /* Generate output */
244         $mod= 0;
245         if (isset($_GET['start'])){
246                 $this->start= (int)$_GET['start'];
247         }
249         $output= "";
250         foreach ($this->report_list as $val){
251                 if ($mod < $this->start) {
252                         $mod++;
253                         continue;
254                 }
255                 if ($mod >= ($this->start + 20)){
256                         $mod++;
257                         break;
258                 }
259                 if ( ($mod++) & 1){
260                         $col= "background-color: #ECECEC;";
261                 } else {
262                         $col= "background-color: #F5F5F5;";
263                 }
264                 $output.= "<tr style=\"height:22px; $col\">$val</tr>";
265         }
267         /* Prepare template */
268         $smarty->assign("search_for", $this->search_for);
269         $smarty->assign("bases", $this->config->idepartments);
270         $smarty->assign("base_select", $this->search_base);
271         $months= array();
272         $months[1]= _("January"); $months[2]= _("February"); $months[3]= _("March"); $months[4]= _("April");
273         $months[5]= _("May"); $months[6]= _("June"); $months[7]= _("July"); $months[8]= _("August");
274         $months[9]= _("September"); $months[10]= _("October"); $months[11]= _("November"); $months[12]= _("December");
275         $smarty->assign("months", $months);
276         $smarty->assign("month_select", $this->month);
277         $current= date("Y");
278         $years= array();
279         for ($y= $current - 5; $y<=$current; $y++){
280                 $years[]= $y;
281         }
282         $smarty->assign("years", $years);
283         $smarty->assign("year_select", $this->year);
285         if (isset($fax_users) && count($fax_users)){
286                 $smarty->assign("search_result", $output);
287                 $smarty->assign("range_selector", range_selector(count($this->report_list), $this->start, 20));
288         }
290         /* Show main page */
291         $smarty->assign("plug", validate($_GET['plug']));
292         $smarty->assign("launchimage", get_template_path('images/launch.png'));
293         $smarty->assign("search_image", get_template_path('images/search.png'));
294         $smarty->assign("mode".$this->sort, "<img src=\"images/sort_".$this->sort_direction.
295                         ".png\" border=0 align=center>");
296         return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
297   }
301 ?>