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",1);
27 define("SYSTEM_EVENT",2);
28 define("USER_EVENT" ,4);
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 protected $attributes = array("timestamp");
74 function set_type($type)
75 {
76 $this->mode = $type;
77 }
80 function get_type()
81 {
82 return($this->mode);
83 }
85 /*! \brief Class contructor.
86 @param Array GOsa configuration object.
87 @param Array Event data, only given when edited.
88 */
89 public function __construct($config,$data = array())
90 {
91 $this->data = $data;
92 $this->config = $config;
93 timezone::get_default_timezone();
94 $this->timestamp = time();
96 /* Load values from given data */
97 if(count($data)){
98 $this->is_new = FALSE;
100 $attrs = array("id" => "ID");
101 foreach($attrs as $to => $from){
102 $this->$to = $data[$from];
103 }
104 if(isset($data['TIMESTAMP'])){
105 $this->timestamp = $this->_event_to_timestamp($data['TIMESTAMP']);
106 }
107 }
108 }
111 /*! \brief Create the HTML output for the plugin.
112 @return String The generated HTML output.
113 */
114 public function execute()
115 {
116 $this->time_select_used = FALSE;
117 $this->target_list_used = FALSE;
119 $str = "<h2>"._("This job has no template!")."</h2>";
120 $str.= "<p class='seperator'></p>";
121 $str.= "<div style='text-align:right;width:100%;padding:3px;'>
122 <input type='submit' name='abort_event_dialog' value='".msgPool::cancelButton()."'>
123 </div>";
124 return($str);
125 }
127 /*! \brief Returns the plugin header, displayed in the template.
128 @return String HTML header part.
129 */
130 public function get_header()
131 {
132 if($this->target_add_list_used){
133 return("");
134 }
135 $str = "<h2>".sprintf(_("Create '%s' job"),$this->s_Event_Name)."</h2>";
136 return($str);
137 }
140 /*! \brief Returns the plugin footer (save cancel), displayed in the template.
141 @return String HTML footer part.
142 */
143 public function get_footer()
144 {
145 if($this->target_add_list_used){
146 return("");
147 }
148 $str = "<p class='seperator'></p>";
149 $str.= "<div style='text-align:right;width:99%;padding:5px;'>
150 <input type='submit' name='save_event_dialog' value='".msgPool::saveButton()."'>
151 <input type='submit' name='abort_event_dialog' value='".msgPool::cancelButton()."'>
152 </div>";
153 return($str);
154 }
157 /*! \brief Returns HTML representation of a timestamp using <select> boxes.
158 @return Returns HTML content.
159 */
160 public function get_time_select()
161 {
162 $this->time_select_used = TRUE;
164 $smarty = get_smarty();
166 $year = date("Y",$this->timestamp);
167 $month = date("m",$this->timestamp);
168 $day = date("d",$this->timestamp);
170 $hour = date("H",$this->timestamp);
171 $minute = date("i",$this->timestamp);
172 $second = date("s",$this->timestamp);
174 $years = array();
175 for($i = date("Y",time()); $i <= 2037 ;$i ++){
176 $years[$i] = $i;
177 }
178 $months = array();
179 for($i = 1; $i <= 12; $i ++){
180 $e = str_pad($i,2,"0",STR_PAD_LEFT);
181 $months[$e] = $e;
182 }
183 $days = array();
184 for($i = 1; $i <= cal_days_in_month(CAL_GREGORIAN,$month,$year); $i ++){
185 $e = str_pad($i,2,"0",STR_PAD_LEFT);
186 $days[$e] = $e;
187 }
188 $hours = array();
189 for($i = 0; $i < 24; $i ++){
190 $e = str_pad($i,2,"0",STR_PAD_LEFT);
191 $hours[$e] = $e;
192 }
193 $minutes = array();
194 for($i = 0; $i < 60; $i ++){
195 $e = str_pad($i,2,"0",STR_PAD_LEFT);
196 $minutes[$e] = $e;
197 }
198 $seconds = array();
199 for($i = 0; $i < 60; $i ++){
200 $e = str_pad($i,2,"0",STR_PAD_LEFT);
201 $seconds[$e] = $e;
202 }
204 $smarty->assign("years", $years);
205 $smarty->assign("months", $months);
206 $smarty->assign("days", $days);
207 $smarty->assign("hours", $hours);
208 $smarty->assign("minutes", $minutes);
209 $smarty->assign("seconds", $seconds);
210 $smarty->assign("time_year",$year);
211 $smarty->assign("time_month",$month);
212 $smarty->assign("time_day",$day);
213 $smarty->assign("time_hour",$hour);
214 $smarty->assign("time_minute",$minute);
215 $smarty->assign("time_second",$second);
216 return($smarty->fetch(get_template_path('timestamp_select.tpl', TRUE, dirname(__FILE__))));
217 }
220 /*! \brief HTML representation of all currently assigned targets using (divSelectBox).
221 @return String Returns a listbox with all used targets.
222 */
223 public function get_target_list()
224 {
225 $this->target_list_used = TRUE;
226 $divlist = new divSelectBox("EventTargets");
227 foreach($this->a_targets as $key => $target){
228 $divlist->AddEntry(array(
229 array("string" => $target),
230 array("string" => "<input type='image' src='images/lists/trash.png' title='"._("Remove")."' name='del_target_".$key."'>",
231 "attach" => "style='width:20px; border-right:0px;'")
232 ));
233 }
234 $list_footer = "<input type='submit' name='open_target_list' value='"._("Add")."'>";
235 return($divlist->DrawList().$list_footer);
236 }
239 /*! \brief Returns HTML content, displaying a dialog which allows to add new targets.
240 @return String HTML content. (EventTargetAddList)
241 */
242 public function get_target_add_list()
243 {
244 $this->target_add_list_used = TRUE;
246 if($this->target_divlist == NULL){
247 $this->target_divlist = new EventTargetAddList($this->config,$this);
248 }
249 $this->target_divlist->execute();
251 $smarty = get_smarty();
252 $smarty->assign("divlist",$this->target_divlist->Draw());
253 return($smarty->fetch(get_template_path('target_list.tpl', TRUE, dirname(__FILE__))));
254 }
257 /*! \brief Handles all posted HTML data, including target add,remove...
258 */
259 public function save_object()
260 {
261 if(isset($_POST['open_target_list'])){
262 $this->target_add_list_used =TRUE;
263 }
264 if($this->target_divlist != NULL){
265 $this->target_divlist->save_object();
266 }
267 if($this->target_add_list_used){
268 if(isset($_POST['abort_target_dialog'])){
269 $this->target_add_list_used =FALSE;
270 $this->target_divlist = NULL;
271 }
272 if(isset($_POST['save_target_dialog'])){
273 $this->target_add_list_used =FALSE;
274 $this->add_targets($this->target_divlist->get_selected_targets());
275 $this->target_divlist = NULL;
276 }
277 }
279 if($this->time_select_used){
280 $time_stamp_values_found = TRUE;
281 foreach(array("time_year","time_month","time_day","time_hour","time_minute","time_second") as $attr){
282 $time_stamp_values_found &= isset($_POST[$attr]);
283 }
284 if($time_stamp_values_found){
285 $this->timestamp = mktime(
286 $_POST['time_hour'],
287 $_POST['time_minute'],
288 $_POST['time_second'],
289 $_POST['time_month'],
290 $_POST['time_day'],
291 $_POST['time_year']);
292 }
293 }
295 if($this->target_list_used){
296 foreach($_POST as $name => $value){
297 if(preg_match("/^del_target_/",$name)){
298 $id = preg_replace("/^del_target_([0-9]*)_.*/","\\1",$name);
299 if(isset($this->a_targets[$id])){
300 unset($this->a_targets[$id]);
301 }
302 break;
303 }
304 }
305 }
306 }
309 /*! \brief Converts a daemon timestamp into an unix timestamp. \
310 e.g. 20080101125959 -> 1199188799
311 @param A daemon timestamp YYYYddmmHHiiss
312 @return Integer A unix timestamp.
313 */
314 public function _event_to_timestamp($str)
315 {
316 return(strtotime($str));
317 }
320 /*! \brief Converts a unix timestamp in to a gosa-si timestamp. \
321 e.g. 1199188799 -> 20080101125959
322 @param A unix timestamp (e.g. 1199188799)
323 @return Integer A daemon timestamp (e.g. 20080101125959).
324 */
325 public function _timestamp_to_event($stamp)
326 {
327 return(date("YmdHis",$stamp));
328 }
331 /*! \brief Returns event information, like menu strings, images ...
332 @return Array Event informations.
333 */
334 public function get_event_info()
335 {
336 $data =array();
337 $data['CLASS_NAME'] = get_class($this);
338 $data['s_Menu_Name'] = $this->s_Menu_Name;
339 $data['s_Event_Name'] = $this->s_Event_Name;
340 foreach(array("s_Queued_Action","s_Schedule_Action","s_Trigger_Action") as $attr){
341 if(!empty($this->$attr)){
342 $data[$attr] = $this->$attr;
343 }
344 }
345 $data['MenuImage'] = "<img src='".$this->s_Menu_Image."' alt='".$this->s_Menu_Name."' border='0' class='center'>";
346 $data['ListImage'] = "<img src='".$this->s_List_Image."' title='".$this->s_Event_Name."'
347 alt='".$this->s_Event_Name."' border='0' class='center'>";
348 return($data);
349 }
352 /*! \brief Check if we have opened the target add dialog.
353 @return Boolean TRUE if we have opened the target add dialog else FALSE.
354 */
355 protected function is_target_list_open()
356 {
357 return($this->target_add_list_used);
358 }
361 /*! \brief Returns a complete list of all available events.
362 @return Array Containing $this->get_event_info() for all available events.
363 */
364 static function get_event_types_by_category($categories)
365 {
366 $types= array();
368 foreach ($categories as $category) {
369 if (preg_match('/^users$/', $category)) {
370 $types= array_merge($types, DaemonEvent::get_event_types(USER_EVENT));
371 }
372 if (preg_match('/^systems$/', $category)) {
373 $types= array_merge($types, DaemonEvent::get_event_types(SYSTEM_EVENT));
374 }
375 }
377 return $types;
378 }
381 /*! \brief Returns a complete list of all available events.
382 @return Array Containing $this->get_event_info() for all available events.
383 */
384 static function get_event_types($type)
385 {
386 global $class_mapping,$config;
387 $list = array();
388 $list['BY_CLASS'] = array();
389 $list['TRIGGERED'] = array();
390 $list['SCHEDULED'] = array();
391 $list['QUEUED'] = array();
393 foreach($class_mapping as $name => $path){
394 if(preg_match("/^DaemonEvent_/",$name)){
395 $tmp = new $name($config);
396 if($tmp->visible_for & $type){
397 $evt = $tmp->get_event_info();
398 $list['BY_CLASS'][$name] = $evt;
399 if(isset($evt['s_Trigger_Action'])){
400 $list['TRIGGERED'][$name] = $evt;
401 $list['QUEUED'][$evt['s_Trigger_Action']] = $name;
402 }
403 if(isset($evt['s_Schedule_Action'])){
404 $list['SCHEDULED'][$name] = $evt;
405 $list['QUEUED'][$evt['s_Schedule_Action']] = $name;
406 }
407 if(isset($evt['s_Queued_Action'])){
408 $list['QUEUED'][$evt['s_Queued_Action']] = $name;
409 }
410 }
411 }
412 }
413 return($list);
414 }
417 /*! \brief Returns TRUE if this event is new. (Not edited)
418 @return Boolean TRUE if new, else FALSE.
419 */
420 public function is_new()
421 {
422 return($this->is_new);
423 }
426 /*! \brief Returns the event tag to schedule a new action
427 @param Returns the event e.g. 'job_trigger_action_wake'
428 */
429 public function get_schedule_action()
430 {
431 return($this->s_Schedule_Action);
432 }
435 /*! \brief Returns the event tag to schedule a new action
436 @param Returns the event e.g. 'trigger_action_wake'
437 */
438 public function get_trigger_action()
439 {
440 return($this->s_Trigger_Action);
441 }
444 /*! brief Returns an array containig all attributes \
445 That should be written.
446 @return Array e.g. 'status' => 'bla blub'
447 */
448 public function save()
449 {
450 $ret = array();
451 foreach($this->attributes as $attr){
452 $ret[$attr] = $this->$attr;
453 }
454 if($this->mode == SCHEDULED_EVENT){
455 $ret['timestamp'] = $this->_timestamp_to_event($this->timestamp);
456 }elseif(isset($ret['timestamp'])){
457 unset($ret['timestamp']);
458 }
459 return($ret);
460 }
463 /*! \brief Returns the event targets
464 @return Array All selected targets.
465 */
466 public function get_targets()
467 {
468 return($this->a_targets);
469 }
472 /*! \brief Returns the event timestamp in GOsa daemon format.
473 @return Returns the event timestamp (20081231120000)
474 */
475 public function get_timestamp($si_type = TRUE)
476 {
477 if($si_type){
478 return($this->_timestamp_to_event($this->timestamp));
479 }else{
480 return($this->timestamp);
481 }
482 }
485 /*! \brief Returns the event ID
486 @return Returns the event ID
487 */
488 public function get_id()
489 {
490 if($this->is_new){
491 return(-1);
492 }else{
493 return($this->data['ID']);
494 }
495 }
498 /*! \brief Add a target MAC address
499 @param Array A List of all target that should be added.
500 */
501 public function set_timestamp($stamp)
502 {
503 $this->timestamp = $stamp;
504 }
507 /*! \brief Add a target MAC address
508 @param Array A List of all target that should be added.
509 */
510 public function add_targets($targets)
511 {
512 foreach($targets as $target){
513 $this->a_targets[] = $target;
514 }
515 }
517 public function check()
518 {
519 return(array());
520 }
523 /*! \brief Update a class variable from outside
524 */
525 public function set_value($name,$value)
526 {
527 $name = strtolower($name);
528 if(isset($this->$name) && in_array($name,$this->attributes)){
529 $this->$name = $value;
530 }
531 }
532 }
534 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
535 ?>