Code

Fixed check for delivery flag, use binary operator instead of boolean.
[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 $ui             = NULL;
19   var $range          = 20;
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   /* these vars will be stored in session to be able to remember last search config */
29   var $attributes_SO= array("search_for","search_base","month","year","start","year","month","sort","sort_direction","range");
30   var $objectclasses= array();
31   var $fax_users    = array();
33   /* Create class */
34   function faxreport ($config, $ui)
35   {
36     /* Include config object */
37     $this->config       = $config;
38     $this->ui           = $ui;
39     $this->search_base  = get_base_from_people($ui->dn);
40     $this->year         = date("Y");
41     $this->month        = date("m");
43     /* Get global filter config and set class vars , 
44        or create a filter */
45     if (!is_global("faxreportfilter")){
46       $faxreportfilter = array();
47       foreach($this->attributes_SO as $name){
48         $faxreportfilter[$name] = $this->$name;
49       } 
50       register_global("faxreportfilter",$faxreportfilter);
51     }else{
52       $faxreportfilter = get_global("faxreportfilter");
53       foreach($this->attributes_SO as $name){
54         $this->$name = $faxreportfilter[$name];
55       }
56     }
57   }
59   
60   /* Create Filter & Search & Display results */
61   function execute()
62   {
63     /* Call parent execute */
64     plugin::execute();
66     /************ 
67       Variable initialisation 
68      ************/
69  
70     /* Create months */ 
71     $months= array();
72     for($i = 1 ; $i <= 12 ; $i ++ ){
73       $months[$i] = _(date("F",gmmktime(0,0,0,$i)));
74     }
76     /* Create years */
77     $current= date("Y");
78     $years= array();
79     for ($y= $current - 5; $y<=$current; $y++){
80       $years[]= $y;
81     }
83     
84     /************ 
85       Set smarty defaults  
86      ************/
88     $smarty= get_smarty();
89     $smarty->assign("launchimage"               , get_template_path('images/launch.png'));
90     $smarty->assign("search_image"  , get_template_path('images/search.png'));
91     $smarty->assign("search_for"                , $this->search_for);
92     $smarty->assign("bases"                               , $this->config->idepartments);
93     $smarty->assign("base_select"               , $this->search_base);
94     $smarty->assign("months"                      , $months);
95     $smarty->assign("month_select"      , $this->month);
96     $smarty->assign("years"                               , $years);
97     $smarty->assign("year_select"               , $this->year);
98     $smarty->assign("search_result"     , "");
100     
101     /************ 
102       Check database accessibility 
103      ************/
105     /* Some checks */
106     if(!isset($this->config->data['SERVERS']['FAX'])){
107       print_red(_("No fax extension defined in your server configuration, no reports can be shown!"));
108       return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
109     }elseif(!is_callable("mysql_connect")){
110       print_red(_("There is no mysql extension available, please check your php setup."));
111       return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
112     }else{
113       /* Connecting, selecting database */
114       $cfg      = $this->config->data['SERVERS']['FAX'];
115       $link     = @mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD']);
116       if ($link === FALSE){
117         print_red(_("Can't connect to fax database, no reports can be shown!"));
118         return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
119       }
120       if (! @mysql_select_db("gofax")){
121         print_red(_("Can't select fax database for report generation!"));
122         return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
123       }
125       if (! mysql_query("SELECT * FROM faxlog;")){
126         print_red(_("Can't query fax table 'faxlog' for report generation!"));
127         return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
128       }
129     }           
131     
132     /************ 
133       Perform a deatil view 
134      ************/
136     /* Do detail view? */
137     if (isset($_GET['detail'])){
139       /* Create query */
140       $query = "SELECT id,uid,date_format(queuing_time, '%Y%m%d%H%i%s') as queuing_time,status,sender_id,sender_msn,receiver_id,".
141         "receiver_msn,pages,status_message,transfer_time FROM faxlog WHERE id=".$_GET['detail'].";";
143       /* Connecting, selecting database */
144       $cfg= $this->config->data['SERVERS']['FAX'];
146       /* Check if everything went ok*/
147       $result = @mysql_query($query);
148       if ($result === false){
149         print_red(_("Query for fax database failed!"));
150         @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__,$query, "Database query failed");
151         return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
152       }
154       $line = mysql_fetch_array($result, MYSQL_ASSOC);
155       mysql_close($link);
157       if (!preg_match ("/'".$line["uid"]."'/", $this->userfilter)){
158         print_red (_("You have no permission to retrieve informations about this fax id!"));
159         return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
160       }
162       /* Check acls */
163       $fax_uids = array_flip($this->fax_users);
164       $uid = $line['uid'];
165       $dn = $fax_uids[$uid];
166       $acls = $this->ui->get_permissions($dn,"gofax/faxreport");
167       if(!preg_match("/r/",$acls)){
168         print_red (_("You have no permission to retrieve informations about this fax id!"));
169         return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
170       }
172       $parts= array( "id", "uid", "queuing_time", "status", "sender_id", "sender_msn",
173           "receiver_id", "receiver_msn", "pages", "status_message", "transfer_time" );
175       foreach ($parts as $vname) {
177         $final="fax_$vname";
178         if($vname != "uid"){
179           $v_acl = $this->ui->get_permissions($dn,"gofax/faxreport",preg_replace("/_/","",$vname));
180         }else{
181           $v_acl = "r";
182         }
184         if ($line[$vname] != "" && preg_match("/r/",$v_acl)){
185           $smarty->assign("$final", $line[$vname]);
186         } else {
187           $smarty->assign("$final", "-");
188         }
189       }
190       $queuing_time= $line['queuing_time'];
192       $_SESSION['fuserfilter']= $this->userfilter;
193       $smarty->assign("plug", "?plug=".validate($_GET['plug']));
194       $smarty->assign("detail", validate($_GET['detail']));
196       $format= _("Y-M-D");
197       $date= preg_replace("/Y/", substr($queuing_time,0,4), $format);
198       $date= preg_replace("/M/", substr($queuing_time,4,2), $date);
199       $date= preg_replace("/D/", substr($queuing_time,6,2), $date);
200       $smarty->assign("date", $date);
201       $smarty->assign("time", substr($queuing_time,8,2).":".
202           substr($queuing_time,10,2).":".
203           substr($queuing_time,12,2));
204       return($smarty->fetch(get_template_path('detail.tpl', TRUE)));
205     }
207     
208     /************ 
209       Search for uids matching the filter  
210      ************/
212     /* Search button has been pressed */
213     if ($this->search_for != ""){
215       if (is_integer (strpos($this->search_for, "*"))){
216         $s= $this->search_for;
217       } else {
218         $s= "*".$this->search_for."*";
219       }
220       $ldap= $this->config->get_ldap_link();
221       $ldap->cd ($this->search_base);
223       /* Perform ldap search for potential users */
224       $filter= "(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))".
225           "(objectClass=goFaxAccount)".
226           "(|(uid=$s)(l=$s)(homePhone=$s)".
227           "(telephoneNumber=$s)(facsimileTelephoneNumber=$s)(mobile=$s)".
228           "(pager=$s)(cn=$s)(givenName=$s)(sn=$s)(personalTitle=$s)".
229           "(title=$s)))";
230   
231       $res = get_list($filter, "users", $this->search_base, array("uid"), GL_SUBSEARCH );
233       /* Check if we are allowed to collect fax data */
234       $fax_users= array();
235       foreach($res as $attrs){
236         $acl = $this->ui->get_permissions($attrs['dn'],"users/user","uid");
237         if(preg_match("/r/",$acl)){
238           $fax_users[ $attrs['dn']]= $attrs["uid"][0];
239         }
240       }
242       $this->fax_users = $fax_users;
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     /************ 
253       Create filter  
254      ************/
256     /* Perform SQL query */
257     if ($this->userfilter){
258       if ($this->sort_direction == "down"){
259         $desc= "DESC";
260       } else {
261         $desc= "";
262       }
263       $start= date ("YmdHis", mktime(0,0,0,$this->month,1,$this->year));
264       $end=   date ("YmdHis", mktime(23,59,59,$this->month+1,0,$this->year));
265       $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 ".
266         "WHERE ( ".$this->userfilter." ) AND queuing_time <= $end AND ".
267         "queuing_time >= $start ORDER BY ".$this->fields[$this->sort]." $desc;";
269       if(!is_callable("mysql_connect")){
270         print_red("There is no mysql extension configured in your php setup.");
271         return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
272       }
274     
275     /************ 
276       Create results  
277      ************/
279       /* Connecting, selecting database */
280       $cfg= $this->config->data['SERVERS']['FAX'];
281       $link = @mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD']);
282       
283       @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__,$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       $this->report_list= array();
291       $user_ids = array_flip($fax_users);
292       while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
294         /* Check permissions for each field */
295         $dn = $user_ids[$line['uid']];
297         /* Hide restricted attributes */ 
298         foreach(array("pages","receiverid","senderid","status","queuingtime","detailedView") as $attr){
299           $var = $attr."ACL";
300           $$var = $this->ui->get_permissions($dn,"gofax/faxreport",$attr);
301         }
303         /* Restricted attributes will not be displayed, this will be displayed instead */
304         $no_acl = "<img class='center' src='images/closedlock.png' 
305                     title='"._("Insufficient permissions to view this attribute")."' alt='"._("Insufficient permissions")."'>";
307         /* Create date */ 
308         if((!empty($line["queuing_time"])) && preg_match("/r/",$queuingtimeACL)){
309           $hour=  substr($line["queuing_time"], 8, 2);
310           $minute=substr($line["queuing_time"], 10, 2);
311           $format= _("Y-M-D");
312           $date= preg_replace("/Y/", substr($line["queuing_time"], 0, 4), $format);
313           $date= preg_replace("/M/", substr($line["queuing_time"], 4, 2), $date);
314           $date= preg_replace("/D/", substr($line["queuing_time"], 6, 2), $date);
315           $str_date = $date." ".$hour.":".$minute;
316         }else{
317           $str_date = $no_acl;
318         }
319   
320         /* Create entry html str */
322         if(preg_match("/r/",$detailedViewACL)){
323           $str = "<td class=\"phonelist\">
324             <a href=\"main.php?plug=".validate($_GET['plug'])."&amp;detail=".$line["id"]."\">
325             <img class='center' alt=\"\" align=\"middle\" border=0 src=\"".get_template_path('images/info_small.png')."\">
326             &nbsp;".$line["uid"].
327             "</a>
328             </td>
329             <td>$str_date</td>";
330         }else{
331           $str = "<td>
332             <img class='center' alt=\"\" align=\"middle\" border=0 src=\"".get_template_path('images/info_small.png')."\">
333             &nbsp;".$line["uid"]."</td>
334             <td>$str_date</td>";
335         }
336         
337         /* Add Status td */
338         if(preg_match("/r/",$statusACL)){
339           $str.="<td>".$this->status[$line["status"]]."</td>";
340         }else{
341           $str.="<td>".$no_acl."</td>";
342         }
344         /* Add sender_id td */
345         if(preg_match("/r/",$senderidACL)){
346           $str.="<td>".$line["sender_id"]."</td>";
347         }else{
348           $str.="<td>".$no_acl."</td>";
349         }
351         /* Add receiver_id td */
352         if(preg_match("/r/",$receiveridACL)){
353           $str.="<td>".$line["receiver_id"]."</td>";
354         }else{
355           $str.="<td>".$no_acl."</td>";
356         }
358         /* Add receiver_id td */
359         if(preg_match("/r/",$pagesACL)){
360           $str.="<td>".$line["pages"]."</td>";
361         }else{
362           $str.="<td>".$no_acl."</td>";
363         }
364       $this->report_list[] =  $str;
365       }
367       mysql_close($link);
368     }
370     /************ 
371       Create output out of results  
372      ************/
374     /* Generate output */
375     $mod= 0;
376     $output= "";
377     foreach ($this->report_list as $val){
378       if ($mod < $this->start) {
379         $mod++;
380         continue;
381       }
382       if ($mod >= ($this->start + $this->range)){
383         $mod++;
384         break;
385       }
386       if ( ($mod++) & 1){
387         $col= "background-color: #ECECEC;";
388       } else {
389         $col= "background-color: #F5F5F5;";
390       }
391       $output.= "<tr style=\"height:22px; $col\">$val</tr>";
392     }
394     
395     /************ 
396       Display results  
397      ************/
399     if (isset($fax_users) && count($fax_users)){
400       $smarty->assign("search_result", $output);
401       $smarty->assign("range_selector", range_selector(count($this->report_list), $this->start, $this->range,"EntriesPerPage"));
402     }else{
403       $smarty->assign("search_result", "");
404     }
406     /* Show main page */
407     $smarty->assign("plug", "?plug=".validate($_GET['plug']));
408     for($i= 0; $i<7; $i++){
409       $smarty->assign("mode$i", "");
410     }
411     $smarty->assign("mode".$this->sort, "<img alt=\"\" src=\"images/sort_".$this->sort_direction.
412         ".png\" border=0 align=middle>");
413     return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
414   }
417   /* Save ui input, and store it in $_SESSION 
418      to remember last search next time*/
419   function save_object()
420   {
421     $faxreportfilter = get_global("faxreportfilter");
422     if(isset($_POST['EntriesPerPage'])){
423       $this->range = $_POST['EntriesPerPage'];
424     }
426     if (isset($_GET['start'])){
427       $this->start= (int)$_GET['start'];
428     }
430     /* Adapt sorting */
431     if (isset($_GET['sort'])){
432       if ($this->sort == (int)$_GET['sort']){
433         if ($this->sort_direction == "down"){
434           $this->sort_direction= "up";
435         } else {
436           $this->sort_direction= "down";
437         }
438       }
439       $this->sort= (int)$_GET['sort'];
440       if ($this->sort < 0 || $this->sort > 5){
441         $this->sort= 0;
442       }
443     }
444     foreach( array("year", "month", "search_for", "search_base") as $type){
445       if (isset($_POST[$type])){
446         $faxreportfilter[$type]= $_POST[$type];
448         /* reset start page, if filter has changed */ 
449         if(!isset($_GET['start'])){
450           $this->start = 0;
451         }
452       }
453       $this->$type= $faxreportfilter[$type];
454    
455     }
456     foreach($this->attributes_SO as $name){
457       $faxreportfilter[$name] = $this->$name;
458     }  
459     register_global("faxreportfilter",$faxreportfilter);
460   }
463   /* Return plugin informations for acl handling
464     #FIXME You can only read attributes within this report plugin */
465   function plInfo()
466   {
467   
468     return (array(
469           "plShortName"     => _("Fax report"),
470           "plDescription"   => _("Fax report")."&nbsp;<i>"._("All entries are readonly")."</i>",
471           "plSelfModify"    => TRUE,
472           "plDepends"       => array(),
473           "plPriority"      => 1,                                 // Position in tabs
474           "plSection"       => array("administration"),                     // This belongs to personal
475           "plCategory"      => array("gofax"),
476           "plOptions"       => array(),
478           "plProvidedAcls" => array(
479             "detailedView"   => _("Detailed view"),
480             "id"             => _("Fax ID"),
481             "queuingtime"    => _("Date")."&nbsp;/&nbsp;"._("Time"),
482             "status"         => _("Status"),
483             "senderid"       => _("Sender ID"),
484             "sendermsn"      => _("Sender MSN"),
485             "receiverid"     => _("Receiver ID"),
486             "receivermsn"    => _("Receiver MSN"),
487             "pages"          => _("Number of pages"),
488             "statusmessage"  => _("Status Message"),
489             "transfertime"   => _("Transfer time"))
490         ));
491   }
494 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
495 ?>