Code

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