Code

Code indent
[gosa.git] / gosa-plugins / gofax / 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";
8     var $plIcon         = "plugins/gofax/images/reports.png";
10     /* For internal use */
11     var $start          = 0;
12     var $search_for     = "*";
13     var $search_base    = "";
14     var $year           = "";
15     var $month          = "";
16     var $sort           = 1;
17     var $sort_direction = "down";
18     var $report_list    = array();
19     var $ui             = NULL;
20     var $range          = 20;
21     var $view_logged = FALSE;
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     /* these vars will be stored in session to be able to remember last search config */
31     var $attributes_SO= array("search_for","search_base","month","year","start","year","month","sort","sort_direction","range");
32     var $objectclasses= array();
33     var $fax_users    = array();
36     /* Create class */
37     function faxreport (&$config, &$ui)
38     {
39         /* Include config object */
40         $this->config       = $config;
41         $this->ui           = &$ui;
42         $this->search_base  = $this->config->current['BASE'];
43         $this->year         = date("Y");
44         $this->month        = date("m");
46     /* Get global filter config and set class vars , 
47     or create a filter */
48         if (!session::is_set("faxreportfilter")){
49             $faxreportfilter = array();
50             foreach($this->attributes_SO as $name){
51                 $faxreportfilter[$name] = $this->$name;
52             } 
53             session::set("faxreportfilter",$faxreportfilter);
54         }else{
55             $faxreportfilter = session::get("faxreportfilter");
56             foreach($this->attributes_SO as $name){
57                 $this->$name = $faxreportfilter[$name];
58             }
59         }
60     }
63     /* Create Filter & Search & Display results */
64     function execute()
65     {
66         /* Call parent execute */
67         plugin::execute();
69         /* Log view */
70         if(!$this->view_logged){
71             $this->view_logged = TRUE;
72             new log("view","users/".get_class($this),$this->dn);
73         }
75     /************ 
76       Variable initialisation 
77     ************/
79         /* Create months */ 
80         $months= array();
81         for($i = 1 ; $i <= 12 ; $i ++ ){
82             $months[$i] = _(date("F",gmmktime(0,0,0,$i,1)));
83         }
85         /* Create years */
86         $current= date("Y");
87         $years= array();
88         for ($y= $current - 5; $y<=$current; $y++){
89             $years[]= $y;
90         }
93     /************ 
94       Set smarty defaults  
95     ************/
97         $smarty= get_smarty();
98         $smarty->assign("search_for"            , $this->search_for);
99         $smarty->assign("months"                          , $months);
100         $smarty->assign("month_select"  , $this->month);
101         $smarty->assign("years"                           , $years);
102         $smarty->assign("year_select"           , $this->year);
103         $smarty->assign("search_result" , "");
105     /************ 
106       Check database accessibility 
107     ************/
109         /* Some checks */
110         if(!isset($this->config->data['SERVERS']['FAX'])){
111             msg_dialog::display(_("Error"), _("No fax server found!"), ERROR_DIALOG);
112             return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
113         }elseif(!is_callable("mysql_connect")){
114             msg_dialog::display(_("Configuration error"), sprintf(_("Missing %s PHP extension!"), "mysql"), WARNING_DIALOG);
115             return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
116         }else{
117             /* Connecting, selecting database */
118             $cfg        = $this->config->data['SERVERS']['FAX'];
119             $link       = @mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD']);
120             if ($link === FALSE){
121                 msg_dialog::display(_("Error"), sprintf(_("Cannot connect to %s database!"), "GOfax"), ERROR_DIALOG);
122                 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
123             }
124             if (! @mysql_select_db("gofax")){
125                 msg_dialog::display(_("Error"), sprintf(_("Cannot select %s database!"), "GOfax"), ERROR_DIALOG);
126                 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
127             }
129             if (! mysql_query("SELECT * FROM faxlog;")){
130                 msg_dialog::display(_("Error"), sprintf(_("Cannot query %s database!"), "GOfax"), ERROR_DIALOG);
131                 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
132             }
133         }               
136     /************ 
137       Perform a deatil view 
138     ************/
140         $detail = -1;
141         foreach($_POST as $name => $desc){
142             if(preg_match("/^detail_/", $name)){
143                 $detail = postDecode(preg_replace("/^detail_/","",$name));
144             }
145         }
147         /* Do detail view? */
148         if ($detail != -1){
150             /* Create query */
151             $query = "SELECT id,uid,date_format(queuing_time, '%Y%m%d%H%i%s') ".
152                 "as queuing_time,status,sender_id,sender_msn,receiver_id,".
153                 "receiver_msn,pages,status_message,transfer_time FROM faxlog WHERE id=".$detail.";";
155             /* Connecting, selecting database */
156             $cfg= $this->config->data['SERVERS']['FAX'];
158             /* Check if everything went ok*/
159             $result = @mysql_query($query);
160             if ($result === false){
161                 msg_dialog::display(_("Error"), sprintf(_("Cannot query %s database!"), "GOfax"), ERROR_DIALOG);
162                 @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__,$query, "Database query failed");
163                 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
164             }
166             $line = mysql_fetch_array($result, MYSQL_ASSOC);
167             mysql_close($link);
169             if (!preg_match ("/'".$line["uid"]."'/", $this->userfilter)){
170                 msg_dialog::display(_("Permission error"), _("You have no permission to view this fax id!"), ERROR_DIALOG);
171                 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
172             }
174             /* Check acls */
175             $fax_uids = array_flip($this->fax_users);
176             $uid = $line['uid'];
177             $dn = $fax_uids[$uid];
178             $acls = $this->ui->get_permissions($dn,"faxreport/faxreport");
179             if(!preg_match("/r/",$acls)){
180                 msg_dialog::display(_("Permission error"), _("You have no permission to view this fax id!"), ERROR_DIALOG);
181                 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
182             }
184             $parts= array( "id", "uid", "queuing_time", "status", "sender_id", "sender_msn",
185                 "receiver_id", "receiver_msn", "pages", "status_message", "transfer_time" );
187             foreach ($parts as $vname) {
189                 $final="fax_$vname";
190                 if($vname != "uid"){
191                     $v_acl = $this->ui->get_permissions($dn,"faxreport/faxreport",preg_replace("/_/","",$vname));
192                 }else{
193                     $v_acl = "r";
194                 }
196                 if ($line[$vname] != "" && preg_match("/r/",$v_acl)){
197                     $smarty->assign("$final", $line[$vname]);
198                 } else {
199                     $smarty->assign("$final", "-");
200                 }
201             }
202             $queuing_time= $line['queuing_time'];
204       /* The user is allowed to download all fax images from those users 
205           that are listed in session::get('fuserfilter') 
206       Don't forget to check getfax.php if you change somthing here */
207             session::set('fuserfilter',$this->userfilter);
208             $smarty->assign("plug", "?plug=".validate($_GET['plug']));
209             $smarty->assign("detail", validate($detail));
211             $format= _("Y-M-D");
212             $date= preg_replace("/Y/", substr($queuing_time,0,4), $format);
213             $date= preg_replace("/M/", substr($queuing_time,4,2), $date);
214             $date= preg_replace("/D/", substr($queuing_time,6,2), $date);
215             $smarty->assign("date", $date);
216             $smarty->assign("time", substr($queuing_time,8,2).":".
217                 substr($queuing_time,10,2).":".
218                 substr($queuing_time,12,2));
219             return($smarty->fetch(get_template_path('detail.tpl', TRUE)));
220         }
223     /************ 
224       Search for uids matching the filter  
225     ************/
227         /* Search button has been pressed */
228         $fax_users= array();
229         if ($this->search_for != ""){
231             if (is_integer (strpos($this->search_for, "*"))){
232                 $s= $this->search_for;
233             } else {
234                 $s= "*".$this->search_for."*";
235             }
236             $ldap= $this->config->get_ldap_link();
237             $ldap->cd ($this->search_base);
239             /* Perform ldap search for potential users */
240             $filter= "(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))".
241                 "(|(uid=$s)(l=$s)(homePhone=$s)".
242                 "(telephoneNumber=$s)(facsimileTelephoneNumber=$s)(mobile=$s)".
243                 "(pager=$s)(cn=$s)(givenName=$s)(sn=$s)(personalTitle=$s)".
244                 "(title=$s)))";
246             $res = get_list($filter, "users/user", $this->search_base, array("uid"), GL_SUBSEARCH );
248             /* Check if we are allowed to collect fax data */
249             foreach($res as $attrs){
250                 $acl = $this->ui->get_permissions($attrs['dn'],"users/user","uid");
251                 if(preg_match("/r/",$acl)){
252                     $fax_users[ $attrs['dn']]= $attrs["uid"][0];
253                 }
254             }
256             /* Add self uid, if we have self acl
257              */   
258             if(preg_match("/(r|w)/",$this->ui->get_permissions($this->ui->dn,"faxreport/faxreport",""))){
259                 $fax_users[$this->ui->dn] = $this->ui->uid;
260             }
262             $this->fax_users = $fax_users;
264             /* Prepare SQL query */
265             $this->userfilter= "";
266             foreach ($fax_users as $user){
267                 $this->userfilter.= "uid = '$user' OR ";
268             }
269             $this->userfilter= preg_replace("/OR $/", "", $this->userfilter);
270         }
272     /************ 
273       Create filter  
274     ************/
276         /* Perform SQL query */
277         if ($this->userfilter){
278             if ($this->sort_direction == "down"){
279                 $desc= "DESC";
280             } else {
281                 $desc= "";
282             }
283             $start= date ("YmdHis", mktime(0,0,0,$this->month,1,$this->year));
284             $end=   date ("YmdHis", mktime(23,59,59,$this->month+1,0,$this->year));
285             $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 ".
286                 "WHERE ( ".$this->userfilter." ) AND queuing_time <= $end AND ".
287                 "queuing_time >= $start ORDER BY ".$this->fields[$this->sort]." $desc;";
289             if(!is_callable("mysql_connect")){
290                 msg_dialog::display(_("Configuration error"), sprintf(_("Missing %s PHP extension!"), "mysql"), WARNING_DIALOG);
291                 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
292             }
295     /************ 
296       Create results  
297     ************/
299             /* Connecting, selecting database */
300             $cfg= $this->config->data['SERVERS']['FAX'];
301             $link = @mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD']);
303             @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__,$query, "Database query");
304             $result = @mysql_query($query);
305             if ($result === false){
306                 msg_dialog::display(_("Error"), sprintf(_("Cannot query %s database!"), "GOfax"), ERROR_DIALOG);
307                 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
308             }
310             $this->report_list= array();
311             $user_ids = array_flip($fax_users);
312             while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
314                 /* Check permissions for each field */
315                 $dn = $user_ids[$line['uid']];
317                 /* Hide restricted attributes */ 
318                 foreach(array("pages","receiverid","senderid","status","queuingtime","detailedView") as $attr){
319                     $var = $attr."ACL";
320                     $$var = $this->ui->get_permissions($dn,"faxreport/faxreport",$attr);
321                 }
323                 /* Restricted attributes will not be displayed, this will be displayed instead */
324                 $no_acl = image('images/lists/locked.png','',_("Insufficient permissions to view this attribute"));
326                 /* Create date */ 
327                 if((!empty($line["queuing_time"])) && preg_match("/r/",$queuingtimeACL)){
328                     $hour=  substr($line["queuing_time"], 8, 2);
329                     $minute=substr($line["queuing_time"], 10, 2);
330                     $format= _("Y-M-D");
331                     $date= preg_replace("/Y/", substr($line["queuing_time"], 0, 4), $format);
332                     $date= preg_replace("/M/", substr($line["queuing_time"], 4, 2), $date);
333                     $date= preg_replace("/D/", substr($line["queuing_time"], 6, 2), $date);
334                     $str_date = $date." ".$hour.":".$minute;
335                 }else{
336                     $str_date = $no_acl;
337                 }
339                 /* Create entry html str */
340                 $str = "<td class='list0'>".$line["uid"]."</td>";
341                 $str.= "<td class='list0'>".$str_date."</td>";
343                 /* Add Status td */
344                 if(preg_match("/r/",$statusACL)){
345                     $str.="<td class='list0'>".$this->status[$line["status"]]."</td>";
346                 }else{
347                     $str.="<td class='list0'>".$no_acl."</td>";
348                 }
350                 /* Add sender_id td */
351                 if(preg_match("/r/",$senderidACL)){
352                     $str.="<td class='list0'>".$line["sender_id"]."</td>";
353                 }else{
354                     $str.="<td class='list0'>".$no_acl."</td>";
355                 }
357                 /* Add receiver_id td */
358                 if(preg_match("/r/",$receiveridACL)){
359                     $str.="<td class='list0'>".$line["receiver_id"]."</td>";
360                 }else{
361                     $str.="<td class='list0'>".$no_acl."</td>";
362                 }
364                 /* Add receiver_id td */
365                 if(preg_match("/r/",$pagesACL)){
366                     $str.="<td class='list0'>".$line["pages"]."</td>";
367                 }else{
368                     $str.="<td class='list0'>".$no_acl."</td>";
369                 }
370                 /* Create entry html str */
371                 if(preg_match("/r/",$detailedViewACL)){
372                     $str.= "<td class='list0'>".image('images/info_small.png', 'detail_'.postEncode($line["id"]))."</td>";
373                 }else{
374                     $str.= "<td class='list0'>&nbsp;</td>";
375                 }
376                 $this->report_list[] =  $str;
377             }
379             mysql_close($link);
380         }
382     /************ 
383       Create output out of results  
384     ************/
386         /* Generate output */
387         $mod= 0;
388         $output= "";
389         foreach ($this->report_list as $val){
390             if ($mod < $this->start) {
391                 $mod++;
392                 continue;
393             }
394             if ($mod >= ($this->start + $this->range)){
395                 $mod++;
396                 break;
397             }
398             if ( ($mod++) & 1){
399                 $col= "background-color: #ECECEC;";
400             } else {
401                 $col= "background-color: #F5F5F5;";
402             }
403             $output.= "<tr style=\"height:22px; $col\">$val</tr>";
404         }
407     /************ 
408       Display results  
409     ************/
411         if (isset($fax_users) && count($fax_users)){
412             $smarty->assign("search_result", $output);
413             $smarty->assign("range_selector", range_selector(count($this->report_list), $this->start, $this->range,"EntriesPerPage"));
414         }else{
415             $smarty->assign("search_result", "");
416         }
418         /* Show main page */
419         $smarty->assign("plug", "?plug=".validate($_GET['plug']));
420         for($i= 0; $i<7; $i++){
421             $smarty->assign("mode$i", "");
422         }
423         $smarty->assign("mode".$this->sort, image("images/lists/sort-".$this->sort_direction.".png"));
424         return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
425     }
428   /* Save ui input, and store it in session::get() 
429   to remember last search next time*/
430     function save_object()
431     {
432         $faxreportfilter = session::get("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];
466         }
467         foreach($this->attributes_SO as $name){
468             $faxreportfilter[$name] = $this->$name;
469         }  
470         session::set("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       {
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 ?>