1 <?php
3 class goto_log_view extends plugin
4 {
6 var $mac;
7 var $logs;
8 var $event;
9 var $parent;
10 var $config;
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 = "file";
21 var $sort_dir = 1; // 1 => up, 0 => down
23 var $ignore_account = TRUE;
25 function __construct(&$config,$dn,$parent)
26 {
27 $this->config = $config;
28 $this->parent = $parent;
30 /* Try to fetch logs for the given event (mac)
31 */
32 $this->o_queue = new gosaSupportDaemon();
34 /* Load ldap object if given
35 and use this macAddress.
36 */
37 if(is_object($parent) && $dn != "" && $dn != "new"){
38 plugin::plugin($config,$dn,$parent);
39 }
41 /* Get correct macAddress.
42 Check if an event is given or a ldap object.
43 */
44 if(is_array($this->parent) && isset($this->parent['MACADDRESS'])){
45 $this->mac = $this->parent['MACADDRESS'];
46 }elseif(isset($parent->attrs['macAddress'][0])){
47 $this->mac = $parent->attrs['macAddress'][0];
48 }
50 /* Query for log files
51 */
52 $res = $this->o_queue->get_log_info_for_mac($this->mac);
53 if($this->o_queue->is_error()){
54 msg_dialog::display(_("Error"), $this->o_queue->get_error(), ERROR_DIALOG);
55 }
57 /* Check if there is at least one log file
58 */
59 if(!isset($res[$this->mac]) || !is_array($res[$this->mac])){
60 $this->logs = array();
61 }else{
62 $this->selected_date = key($res[$this->mac]);
63 $this->logs = $res;
64 }
65 }
68 function execute()
69 {
70 $smarty = get_smarty();
71 $smarty->assign("logs",$this->logs);
72 $smarty->assign("logs_available", isset($this->logs[$this->mac]));
73 $smarty->assign("mac",$this->mac);
74 $smarty->assign("selected_file",$this->selected_file);
75 $smarty->assign("selected_date",$this->selected_date);
76 $smarty->assign("log_file", $this->get_log($this->mac,$this->selected_date,$this->selected_file));
78 $divlist = new divlist("log_view");
80 /* Create sort direction images
81 */
82 if($this->sort_dir){
83 $img = "<img src='images/down-arrow.png' border='0' alt='\\/'>";
84 }else{
85 $img = "<img src='images/up-arrow.png' border='0' alt='/\\'";
86 }
88 /* Create list header
89 */
90 $divlist->SetHeader(array(
91 array("string"=>"<a href='?plug=".$_GET['plug']."&sort_by=file'>"._("Log file")." ".$img."</a>",
92 "attach"=>"width='200px;'"),
93 array("string"=>"<a href='?plug=".$_GET['plug']."&sort_by=time'>"._("Log date")." ".$img."</a>",
94 "attach" => "style='border-right:none;'"),
95 ));
97 /* Create divlist list
98 */
99 $divlist->SetEntriesPerPage(0);
100 $divlist->SetHeight(150);
102 /* Create sortable array
103 */
104 $link = "<a href='?plug=".$_GET['plug']."&time=%time%&file=%file%&mac=%mac%'>%str%</a>";
105 $to_add = array();
106 $sort_by = $this->sort_by;
107 foreach($this->logs as $mac => $times){
108 foreach($times as $time => $data){
109 $rtime = $data['REAL_DATE'];
110 foreach($data['FILES'] as $file){
111 $use_link = preg_replace(array("/%mac%/","/%time%/","/%file%/"),array($mac,$time,$file),$link);
112 $to_add[$$sort_by.$file.$time] = array(
113 array("string" => preg_replace("/%str%/",$file,$use_link),
114 "attach" => "style='width:200px;'"),
115 array("string" => preg_replace("/%str%/",date("d.m.Y H:i:s",$rtime),$use_link),
116 "attach" => "style='border-right:none;'"),
117 );
118 }
119 }
120 }
122 /* Sort entries
123 */
124 if(!$this->sort_dir){
125 uksort($to_add, "strnatcasecmp");
126 }else{
127 uksort($to_add, "strnatcasecmp");
128 $to_add = array_reverse($to_add);
129 }
131 /* Append entries to list
132 */
133 foreach($to_add as $entry){
134 $divlist->AddEntry($entry);
135 }
137 $smarty->assign("divlist",$divlist->DrawList());
138 return($smarty->fetch(get_template_path('log_view.tpl', TRUE,dirname(__FILE__))));
139 }
142 function get_log($mac,$date,$file)
143 {
144 $res = $this->o_queue->get_log_file($mac,$date,$file);
145 if($this->o_queue->is_error()){
146 msg_dialog::display(_("Error"), $this->o_queue->get_error(), ERROR_DIALOG);
147 }
148 $res = nl2br(htmlentities($res));
149 return($res);
150 }
153 function save_object()
154 {
155 foreach(array("time"=>"selected_date","file"=>"selected_file") as $attr => $dest){
156 if(isset($_GET[$attr])){
157 $this->$dest = $_GET[$attr];
158 }
159 }
160 if(isset($_GET['sort_by']) && in_array($_GET['sort_by'],array("file","time"))){
161 if($_GET['sort_by'] == $this->sort_by){
162 $this->sort_dir = !$this->sort_dir;
163 }
164 $this->sort_by = $_GET['sort_by'];
165 }
166 }
167 }
168 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
169 ?>