1 <?php
2 /*
3 * This code is part of GOsa (http://www.gosa-project.org)
4 * Copyright (C) 2003-2008 GONICUS GmbH
5 *
6 * ID: $$Id$$
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
23 define("SCHEDULED_EVENT",1);
24 define("TRIGGERED_EVENT",2);
26 define("HIDDEN_EVENT",0);
27 define("SYSTEM_EVENT",1);
28 define("USER_EVENT" ,2);
31 /*! \brief This is the event base class
32 \author Fabian Hickert <hickert@gonicus.de>
33 \version 1.00
34 \date 26.02.2008
36 This is the base class for all new daemon events.
37 It implements most of the required functionality.
38 */
39 class DaemonEvent
40 {
41 /* Menu Strings */
42 protected $s_Menu_Name = "s_Menu_Name not set"; // Diplayed in the ActionsMenu->CreateNew
43 protected $s_Event_Name = "s_Event_Name not set"; // Will be displayed in the Management list.
44 protected $s_Menu_Image = "images/empty.png"; // Image displayed in Actions->New
45 protected $s_List_Image = ""; // Image displayed in event listing
47 public $config; // GOsa configuration object
48 protected $data; // The event data, when edited
50 protected $a_targets = array(); // The list of assigned Targets (When newly created)
51 protected $s_Schedule_Action = ""; // The deamon command name when newly created. (e.g. job_trigger_action_halt)
52 protected $s_Trigger_Action= ""; // The deamon command name when edited. (e.g. trigger_action_halt)
53 protected $s_Queued_Action= ""; // The deamon command name when edited. (e.g. trigger_action_halt)
54 protected $timestamp = 0; // Event execution time;
55 protected $id = -1; // The Table ID
56 protected $status = "unknown";// The current event status
57 protected $is_new = TRUE; // Is TRUE if this is a new event
59 protected $mode = SCHEDULED_EVENT; // Default action is sheduled.
61 /* Sub dialog hanlding */
62 protected $target_divlist = NULL; // The divlist used by the target add dialog
63 protected $target_add_list_used = FALSE; // Indicates that the target add list was used.
64 protected $time_select_used = FALSE; // Indicates that we have used the timestamp select boxes.
65 protected $target_list_used = FALSE; // Target list was diaplayed?
66 protected $_target_list = array(); // Object Cache of those objects displayed in the target add dialog
67 protected $workstation_list = array(); // Used as cache in the target list.
68 protected $server_list = array(); // Used as cache in the target list.
70 protected $visible_for = HIDDEN_EVENT;
72 function set_type($type)
73 {
74 $this->mode = $type;
75 }
78 function get_type()
79 {
80 return($this->mode);
81 }
83 /*! \brief Class contructor.
84 @param Array GOsa configuration object.
85 @param Array Event data, only given when edited.
86 */
87 public function __construct($config,$data = array())
88 {
89 $this->data = $data;
90 $this->config = $config;
91 timezone::get_default_timezone();
92 $this->timestamp = time();
94 /* Load values from given data */
95 if(count($data)){
96 $this->is_new = FALSE;
98 $attrs = array("id" => "ID");
99 foreach($attrs as $to => $from){
100 $this->$to = $data[$from];
101 }
102 if(isset($data['TIMESTAMP'])){
103 $this->timestamp = $this->_event_to_timestamp($data['TIMESTAMP']);
104 }
105 }
106 }
109 /*! \brief Create the HTML output for the plugin.
110 @return String The generated HTML output.
111 */
112 public function execute()
113 {
114 $this->time_select_used = FALSE;
115 $this->target_list_used = FALSE;
117 $str = "<h2>"._("This event has no template.")."</h2>";
118 $str.= "<p class='seperator'></p>";
119 $str.= "<div style='text-align:right;width:100%;padding:3px;'>
120 <input type='submit' name='abort_event_dialog' value='"._("Cancel")."'>
121 </div>";
122 return($str);
123 }
125 /*! \brief Returns the plugin header, displayed in the template.
126 @return String HTML header part.
127 */
128 public function get_header()
129 {
130 if($this->target_add_list_used){
131 return("");
132 }
133 $str = "<h2>"._("Daemon event")." - ".$this->s_Event_Name."</h2>";
134 return($str);
135 }
138 /*! \brief Returns the plugin footer (save cancel), displayed in the template.
139 @return String HTML footer part.
140 */
141 public function get_footer()
142 {
143 if($this->target_add_list_used){
144 return("");
145 }
146 $str = "<p class='seperator'></p>";
147 $str.= "<div style='text-align:right;width:100%;padding:5px;'>
148 <input type='submit' name='save_event_dialog' value='"._("Save")."'>
149 <input type='submit' name='abort_event_dialog' value='"._("Cancel")."'>
150 </div>";
151 return($str);
152 }
155 /*! \brief Returns HTML representation of a timestamp using <select> boxes.
156 @return Returns HTML content.
157 */
158 public function get_time_select()
159 {
160 $this->time_select_used = TRUE;
162 $smarty = get_smarty();
164 $year = date("Y",$this->timestamp);
165 $month = date("m",$this->timestamp);
166 $day = date("d",$this->timestamp);
168 $hour = date("H",$this->timestamp);
169 $minute = date("i",$this->timestamp);
170 $second = date("s",$this->timestamp);
172 $years = array();
173 for($i = date("Y",time()); $i <= 2037 ;$i ++){
174 $years[$i] = $i;
175 }
176 $months = array();
177 for($i = 1; $i <= 12; $i ++){
178 $e = str_pad($i,2,"0",STR_PAD_LEFT);
179 $months[$e] = $e;
180 }
181 $days = array();
182 for($i = 1; $i <= cal_days_in_month(CAL_GREGORIAN,$month,$year); $i ++){
183 $e = str_pad($i,2,"0",STR_PAD_LEFT);
184 $days[$e] = $e;
185 }
186 $hours = array();
187 for($i = 0; $i < 24; $i ++){
188 $e = str_pad($i,2,"0",STR_PAD_LEFT);
189 $hours[$e] = $e;
190 }
191 $minutes = array();
192 for($i = 0; $i < 60; $i ++){
193 $e = str_pad($i,2,"0",STR_PAD_LEFT);
194 $minutes[$e] = $e;
195 }
196 $seconds = array();
197 for($i = 0; $i < 60; $i ++){
198 $e = str_pad($i,2,"0",STR_PAD_LEFT);
199 $seconds[$e] = $e;
200 }
202 $smarty->assign("years", $years);
203 $smarty->assign("months", $months);
204 $smarty->assign("days", $days);
205 $smarty->assign("hours", $hours);
206 $smarty->assign("minutes", $minutes);
207 $smarty->assign("seconds", $seconds);
208 $smarty->assign("time_year",$year);
209 $smarty->assign("time_month",$month);
210 $smarty->assign("time_day",$day);
211 $smarty->assign("time_hour",$hour);
212 $smarty->assign("time_minute",$minute);
213 $smarty->assign("time_second",$second);
214 return($smarty->fetch(get_template_path('timestamp_select.tpl', TRUE, dirname(__FILE__))));
215 }
218 /*! \brief HTML representation of all currently assigned targets using (divSelectBox).
219 @return String Returns a listbox with all used targets.
220 */
221 public function get_target_list()
222 {
223 $this->target_list_used = TRUE;
224 $divlist = new divSelectBox("EventTargets");
225 foreach($this->a_targets as $key => $target){
226 $divlist->AddEntry(array(
227 array("string" => $target),
228 array("string" => "<input type='image' src='images/edittrash.png' title='"._("Remove")."' name='del_target_".$key."'>",
229 "attach" => "style='width:20px; border-right:0px;'")
230 ));
231 }
232 $list_footer = "<input type='submit' name='open_target_list' value='"._("Add target")."'>";
233 return($divlist->DrawList().$list_footer);
234 }
237 /*! \brief Returns HTML content, displaying a dialog which allows to add new targets.
238 @return String HTML content. (EventTargetAddList)
239 */
240 public function get_target_add_list()
241 {
242 $this->target_add_list_used = TRUE;
244 if($this->target_divlist == NULL){
245 $this->target_divlist = new EventTargetAddList($this->config,$this);
246 }
247 $this->target_divlist->execute();
249 $smarty = get_smarty();
250 $smarty->assign("divlist",$this->target_divlist->Draw());
251 return($smarty->fetch(get_template_path('target_list.tpl', TRUE, dirname(__FILE__))));
252 }
255 /*! \brief Handles all posted HTML data, including target add,remove...
256 */
257 public function save_object()
258 {
259 if(isset($_POST['open_target_list'])){
260 $this->target_add_list_used =TRUE;
261 }
262 if($this->target_divlist != NULL){
263 $this->target_divlist->save_object();
264 }
265 if($this->target_add_list_used){
266 if(isset($_POST['abort_target_dialog'])){
267 $this->target_add_list_used =FALSE;
268 $this->target_divlist = NULL;
269 }
270 if(isset($_POST['save_target_dialog'])){
271 $this->target_add_list_used =FALSE;
272 foreach($this->list_get_selected_items() as $id){
273 if(in_array("gosaGroupOfNames",$this->target_divlist->_target_list[$id]['objectClass'])){
274 foreach($this->target_divlist->_target_list[$id]['member'] as $mem_dn){
275 if(isset($this->target_divlist->workstation_list[$mem_dn])){
276 $this->a_targets[] = $this->target_divlist->workstation_list[$mem_dn]['macAddress'][0];
277 }
278 if(isset($this->target_divlist->server_list[$mem_dn])){
279 $this->a_targets[] = $this->target_divlist->server_list[$mem_dn]['macAddress'][0];
280 }
281 }
282 }else{
283 if(isset($this->target_divlist->_target_list[$id]['macAddress'][0])){
284 $this->a_targets[] = $this->target_divlist->_target_list[$id]['macAddress'][0];
285 }
286 }
287 }
288 $this->target_divlist = NULL;
289 }
290 }
292 if($this->time_select_used){
293 $time_stamp_values_found = TRUE;
294 foreach(array("time_year","time_month","time_day","time_hour","time_minute","time_second") as $attr){
295 $time_stamp_values_found &= isset($_POST[$attr]);
296 }
297 if($time_stamp_values_found){
298 $this->timestamp = mktime(
299 $_POST['time_hour'],
300 $_POST['time_minute'],
301 $_POST['time_second'],
302 $_POST['time_month'],
303 $_POST['time_day'],
304 $_POST['time_year']);
305 }
306 }
308 if($this->target_list_used){
309 foreach($_POST as $name => $value){
310 if(preg_match("/^del_target_/",$name)){
311 $id = preg_replace("/^del_target_([0-9]*)_.*/","\\1",$name);
312 if(isset($this->a_targets[$id])){
313 unset($this->a_targets[$id]);
314 }
315 break;
316 }
317 }
318 }
319 }
322 /*! \brief Converts a daemon timestamp into an unix timestamp. \
323 e.g. 20080101125959 -> 1199188799
324 @param A daemon timestamp YYYYddmmHHiiss
325 @return Integer A unix timestamp.
326 */
327 public function _event_to_timestamp($str)
328 {
329 return(strtotime($str));
330 }
333 /*! \brief Converts a unix timestamp in to a gosa-si timestamp. \
334 e.g. 1199188799 -> 20080101125959
335 @param A unix timestamp (e.g. 1199188799)
336 @return Integer A daemon timestamp (e.g. 20080101125959).
337 */
338 public function _timestamp_to_event($stamp)
339 {
340 return(date("YmdHis",$stamp));
341 }
344 /*! \brief Returns event information, like menu strings, images ...
345 @return Array Event informations.
346 */
347 public function get_event_info()
348 {
349 $data =array();
350 $data['CLASS_NAME'] = get_class($this);
351 foreach(array("s_Menu_Name","s_Event_Name","s_Queued_Action","s_Schedule_Action","s_Trigger_Action") as $attr){
352 $data[$attr] = $this->$attr;
353 }
354 $data['MenuImage'] = "<img src='".$this->s_Menu_Image."' alt='".$this->s_Menu_Name."' border='0' class='center'>";
355 $data['ListImage'] = "<img src='".$this->s_List_Image."' title='".$this->s_Event_Name."'
356 alt='".$this->s_Event_Name."' border='0' class='center'>";
357 return($data);
358 }
361 /*! \brief Check if we have opened the target add dialog.
362 @return Boolean TRUE if we have opened the target add dialog else FALSE.
363 */
364 protected function is_target_list_open()
365 {
366 return($this->target_add_list_used);
367 }
370 /*! \brief Returns a complete list of all available events.
371 @return Array Containing $this->get_event_info() for all available events.
372 */
373 static function get_event_types($type)
374 {
375 global $class_mapping,$config;
376 $list = array();
377 $list['BY_CLASS'] = array();
378 $list['BY_TRIGGERED_ACTION'] = array();
379 $list['BY_SCHEDULED_ACTION'] = array();
380 $list['BY_QUEUED_ACTION'] = array();
382 foreach($class_mapping as $name => $path){
383 if(preg_match("/^DaemonEvent_/",$name)){
384 $tmp = new $name($config);
386 if($tmp->visible_for & $type){
387 $evt = $tmp->get_event_info();
389 $list['BY_CLASS'][$name] = $evt;
390 $list['BY_TRIGGERED_ACTION'][$evt['s_Trigger_Action']] = $evt;
391 $list['BY_SCHEDULED_ACTION'][$evt['s_Schedule_Action']] = $evt;
392 $list['BY_QUEUED_ACTION'] [$evt['s_Queued_Action']] = $evt;
393 }
394 }
395 }
396 return($list);
397 }
400 /*! \brief Returns TRUE if this event is new. (Not edited)
401 @return Boolean TRUE if new, else FALSE.
402 */
403 public function is_new()
404 {
405 return($this->is_new);
406 }
409 /*! \brief Returns the event tag to schedule a new action
410 @param Returns the event e.g. 'job_trigger_action_wake'
411 */
412 public function get_schedule_action()
413 {
414 return($this->s_Schedule_Action);
415 }
417 //
418 /*! \brief Returns the event tag to schedule a new action
419 @param Returns the event e.g. 'trigger_action_wake'
420 */
421 public function get_trigger_action()
422 {
423 return($this->s_Trigger_Action);
424 }
426 /*! brief Returns an array containig all attributes \
427 That should be written.
428 @return Array e.g. 'status' => 'bla blub'
429 */
430 public function save()
431 {
432 return(array("timestamp" => $this->_timestamp_to_event($this->timestamp)));
433 }
436 /*! \brief Returns the event targets
437 @return Array All selected targets.
438 */
439 public function get_targets()
440 {
441 return($this->a_targets);
442 }
445 /*! \brief Returns the event timestamp in GOsa daemon format.
446 @return Returns the event timestamp (20081231120000)
447 */
448 public function get_timestamp()
449 {
450 return($this->_timestamp_to_event($this->timestamp));
451 }
454 /*! \brief Returns the event ID
455 @return Returns the event ID
456 */
457 public function get_id()
458 {
459 if($this->is_new){
460 return(-1);
461 }else{
462 return($this->data['ID']);
463 }
464 }
467 /*! \brief Returns a set of elements selected in a MultiSelectWindow
468 @return Array[integer]=integer
469 */
470 protected function list_get_selected_items()
471 {
472 $ids = array();
473 foreach($_POST as $name => $value){
474 if(preg_match("/^item_selected_[0-9]*$/",$name)){
475 $id = preg_replace("/^item_selected_/","",$name);
476 $ids[$id] = $id;
477 }
478 }
479 return($ids);
480 }
483 /*! \brief Add a target MAC address
484 @param Array A List of all target that should be added.
485 */
486 public function set_timestamp($stamp)
487 {
488 $this->timestamp = $stamp;
489 }
492 /*! \brief Add a target MAC address
493 @param Array A List of all target that should be added.
494 */
495 public function add_targets($targets)
496 {
497 foreach($targets as $target){
498 $this->a_targets[] = $target;
499 }
500 }
501 }
503 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
504 ?>