Code

a5e166ff2dcd61663e69b432a26046c0cd6ffc32
[gosa.git] / trunk / gosa-plugins / goto / addons / goto / class_gotoLogView.inc
1 <?php
3 class gotoLogView extends plugin
4 {
5   
6   var $mac;
7   var $logs;
8   var $event;
9   var $parent;
10   var $config;
11   
12   var $o_queue;  
14   var $selected_date;
15   var $selected_file = 0;
17   var $attributes = array("macAddress");
18   var $macAddress = "";
20   var $sort_by  = "time";
21   var $sort_dir = 1; // 1 => up, 0 => down
22   
23   var $ignore_account = TRUE;
24   var $standalone = FALSE;
25  
26   function __construct(&$config,$dn,$parent)
27   {
28     $this->config = $config;
29     $this->parent = $parent;
31     /* Try to fetch logs for the given event (mac)
32      */
33     $this->o_queue = new gosaSupportDaemon();
35     /* Load ldap object if given 
36         and use this macAddress.
37      */
38     if(is_object($parent) && $dn != "" && $dn != "new"){
39       plugin::plugin($config,$dn,$parent);
40     } 
42     /* Get correct macAddress.
43         Check if an event is given or a ldap object.
44      */      
45     if(is_array($this->parent) && isset($this->parent['MACADDRESS'])){
46       $this->mac = $this->parent['MACADDRESS'];
47       $this->standalone = TRUE;
48     }elseif(isset($parent->attrs['macAddress'][0])){
49       $this->mac = $parent->attrs['macAddress'][0];
50       $this->standalone = FALSE;
51     }
53     /* Query for log files
54      */
55     $res = $this->o_queue->get_log_info_for_mac($this->mac);
56     if($this->o_queue->is_configured() && $this->o_queue->is_error()){
57       msg_dialog::display(_("Error"), $this->o_queue->get_error(), ERROR_DIALOG);
58     }
60     /* Check if there is at least one log file 
61      */
62     if(!isset($res[$this->mac]) || !is_array($res[$this->mac])){
63       $this->logs = array();
64     }else{
65       $this->selected_date = key($res[$this->mac]);
66       $this->logs = $res;
67     }
69   }
72   function execute()
73   {
74     $smarty = get_smarty();
75     $smarty->assign("logs",$this->logs);   
76     $smarty->assign("logs_available", isset($this->logs[$this->mac]));
77     $smarty->assign("mac",$this->mac);
78     $smarty->assign("selected_file",$this->selected_file);
79     $smarty->assign("selected_date",$this->selected_date);
80     $smarty->assign("log_file", $this->get_log($this->mac,$this->selected_date,$this->selected_file));        
81     $smarty->assign("standalone",$this->standalone);
83     if (isset($this->logs[$this->mac])){
84             $date = date("d.m.Y H:i:s",$this->logs[$this->mac][$this->selected_date]['REAL_DATE']);
85     }
86     $file = $this->selected_file;
87     $smarty->assign("selected_log",_("none"));
88     if(!empty($file)){
89       $smarty->assign("selected_log",$file.", ".$date);
90     }
92     $divlist = new divlist("log_view");
94     /* Create sort direction images 
95      */
96     if($this->sort_dir){
97       $img = "<img src='images/lists/sort-down.png' border='0' alt='\\/'>";
98     }else{
99       $img = "<img src='images/lists/sort-up.png' border='0' alt='/\\'";
100     }
101     if($this->sort_by == "file"){
102       $img1 = $img;
103       $img2 = "";
104     }else{
105       $img1 = "";
106       $img2 = $img;
107     }
110     /* Create list header 
111      */
112     $divlist->SetHeader(array(
113           array("string"=>"<a href='?plug=".$_GET['plug']."&sort_by=file'>"._("File")." ".$img1."</a>",
114                 "attach"=>"width='200px;'"),
115           array("string"=>"<a href='?plug=".$_GET['plug']."&sort_by=time'>"._("Date")." ".$img2."</a>",
116                 "attach" => "style='border-right:none;'"),
117           ));
119     /* Create divlist list 
120      */
121     $divlist->SetEntriesPerPage(0);
122     $divlist->SetHeight(150); 
124     /* Create sortable array
125      */ 
126     $link = "<a href='?plug=".$_GET['plug']."&time=%time%&file=%file%&mac=%mac%'>%str%</a>";
127     $to_add = array();
128     $sort_by = $this->sort_by;
129     foreach($this->logs as $mac => $times){
130       foreach($times as $time => $data){
131         $rtime = $data['REAL_DATE'];
132         foreach($data['FILES'] as $file){
134           $highlight = "";
135           if($file == $this->selected_file && $time == $this->selected_date && $mac == $this->mac){
136             $highlight = "background-color:#CCCCCC";
137           }
139           $use_link = preg_replace(array("/%mac%/","/%time%/","/%file%/"),array($mac,$time,$file),$link);
140           $to_add[$$sort_by.$file.$time] = array(
141             array("string" => preg_replace("/%str%/",$file,$use_link),
142                   "attach" => "style='width:200px; $highlight'"),
143             array("string" => preg_replace("/%str%/",date("d.m.Y H:i:s",$rtime),$use_link),
144                   "attach" => "style='border-right:none; $highlight'"),
145             );
146         }
147       }
148     }
150     /* Sort entries 
151      */
152     if(!$this->sort_dir){
153       uksort($to_add, "strnatcasecmp");
154     }else{
155       uksort($to_add, "strnatcasecmp");
156       $to_add = array_reverse($to_add);
157     }
159     /* Append entries to list 
160      */
161     foreach($to_add as $entry){
162       $divlist->AddEntry($entry);
163     }
165     $smarty->assign("ACL",preg_match("/r/",$this->getacl("")));
166     $smarty->assign("divlist",$divlist->DrawList());
167     return($smarty->fetch(get_template_path('log_view.tpl', TRUE,dirname(__FILE__))));
168   }
171   function get_log($mac,$date,$file)
172   {
173     $res = $this->o_queue->get_log_file($mac,$date,$file);
174     if($this->o_queue->is_configured() && $this->o_queue->is_error()){
175       msg_dialog::display(_("Error"), $this->o_queue->get_error(), ERROR_DIALOG);
176     }
177     $res = nl2br(htmlentities($res));
178     return($res);
179   }
181   
182   function save_object()
183   {
184     foreach(array("time"=>"selected_date","file"=>"selected_file") as $attr => $dest){
185       if(isset($_GET[$attr])){
186         $this->$dest = $_GET[$attr];
187       }
188     }
189     if(isset($_GET['sort_by']) && in_array($_GET['sort_by'],array("file","time"))){
190       if($_GET['sort_by'] == $this->sort_by){
191         $this->sort_dir = !$this->sort_dir;
192       }
193       $this->sort_by = $_GET['sort_by'];
194     }
195   }
198   /* Return plugin informations for acl handling */
199   static function plInfo()
200   {
201     return (array(
202           "plShortName"   => _("Log view"),
203           "plDescription" => _("GOto log view"),
204           "plSelfModify"  => FALSE,
205           "plDepends"     => array(),
206           "plPriority"    => 30,
207           "plSection"     => array("administration"),
208           "plCategory"    => array("workstation","server"),
210           "plProvidedAcls"=> array()
211             ));
212   }
214 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
215 ?>