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 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 job 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='".msgPool::cancelButton()."'>
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>".sprintf(_("Create '%s' job"),$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:99%;padding:5px;'>
148 <input type='submit' name='save_event_dialog' value='".msgPool::saveButton()."'>
149 <input type='submit' name='abort_event_dialog' value='".msgPool::cancelButton()."'>
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")."'>";
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 $this->add_targets($this->target_divlist->get_selected_targets());
273 $this->target_divlist = NULL;
274 }
275 }
277 if($this->time_select_used){
278 $time_stamp_values_found = TRUE;
279 foreach(array("time_year","time_month","time_day","time_hour","time_minute","time_second") as $attr){
280 $time_stamp_values_found &= isset($_POST[$attr]);
281 }
282 if($time_stamp_values_found){
283 $this->timestamp = mktime(
284 $_POST['time_hour'],
285 $_POST['time_minute'],
286 $_POST['time_second'],
287 $_POST['time_month'],
288 $_POST['time_day'],
289 $_POST['time_year']);
290 }
291 }
293 if($this->target_list_used){
294 foreach($_POST as $name => $value){
295 if(preg_match("/^del_target_/",$name)){
296 $id = preg_replace("/^del_target_([0-9]*)_.*/","\\1",$name);
297 if(isset($this->a_targets[$id])){
298 unset($this->a_targets[$id]);
299 }
300 break;
301 }
302 }
303 }
304 }
307 /*! \brief Converts a daemon timestamp into an unix timestamp. \
308 e.g. 20080101125959 -> 1199188799
309 @param A daemon timestamp YYYYddmmHHiiss
310 @return Integer A unix timestamp.
311 */
312 public function _event_to_timestamp($str)
313 {
314 return(strtotime($str));
315 }
318 /*! \brief Converts a unix timestamp in to a gosa-si timestamp. \
319 e.g. 1199188799 -> 20080101125959
320 @param A unix timestamp (e.g. 1199188799)
321 @return Integer A daemon timestamp (e.g. 20080101125959).
322 */
323 public function _timestamp_to_event($stamp)
324 {
325 return(date("YmdHis",$stamp));
326 }
329 /*! \brief Returns event information, like menu strings, images ...
330 @return Array Event informations.
331 */
332 public function get_event_info()
333 {
334 $data =array();
335 $data['CLASS_NAME'] = get_class($this);
336 $data['s_Menu_Name'] = $this->s_Menu_Name;
337 $data['s_Event_Name'] = $this->s_Event_Name;
338 foreach(array("s_Queued_Action","s_Schedule_Action","s_Trigger_Action") as $attr){
339 if(!empty($this->$attr)){
340 $data[$attr] = $this->$attr;
341 }
342 }
343 $data['MenuImage'] = "<img src='".$this->s_Menu_Image."' alt='".$this->s_Menu_Name."' border='0' class='center'>";
344 $data['ListImage'] = "<img src='".$this->s_List_Image."' title='".$this->s_Event_Name."'
345 alt='".$this->s_Event_Name."' border='0' class='center'>";
346 return($data);
347 }
350 /*! \brief Check if we have opened the target add dialog.
351 @return Boolean TRUE if we have opened the target add dialog else FALSE.
352 */
353 protected function is_target_list_open()
354 {
355 return($this->target_add_list_used);
356 }
359 /*! \brief Returns a complete list of all available events.
360 @return Array Containing $this->get_event_info() for all available events.
361 */
362 static function get_event_types($type)
363 {
364 global $class_mapping,$config;
365 $list = array();
366 $list['BY_CLASS'] = array();
367 $list['TRIGGERED'] = array();
368 $list['SCHEDULED'] = array();
369 $list['QUEUED'] = array();
371 foreach($class_mapping as $name => $path){
372 if(preg_match("/^DaemonEvent_/",$name)){
373 $tmp = new $name($config);
374 if($tmp->visible_for & $type){
375 $evt = $tmp->get_event_info();
376 $list['BY_CLASS'][$name] = $evt;
377 if(isset($evt['s_Trigger_Action'])){
378 $list['TRIGGERED'][$name] = $evt;
379 $list['QUEUED'][$evt['s_Trigger_Action']] = $name;
380 }
381 if(isset($evt['s_Schedule_Action'])){
382 $list['SCHEDULED'][$name] = $evt;
383 $list['QUEUED'][$evt['s_Schedule_Action']] = $name;
384 }
385 if(isset($evt['s_Queued_Action'])){
386 $list['QUEUED'][$evt['s_Queued_Action']] = $name;
387 }
388 }
389 }
390 }
391 return($list);
392 }
395 /*! \brief Returns TRUE if this event is new. (Not edited)
396 @return Boolean TRUE if new, else FALSE.
397 */
398 public function is_new()
399 {
400 return($this->is_new);
401 }
404 /*! \brief Returns the event tag to schedule a new action
405 @param Returns the event e.g. 'job_trigger_action_wake'
406 */
407 public function get_schedule_action()
408 {
409 return($this->s_Schedule_Action);
410 }
412 //
413 /*! \brief Returns the event tag to schedule a new action
414 @param Returns the event e.g. 'trigger_action_wake'
415 */
416 public function get_trigger_action()
417 {
418 return($this->s_Trigger_Action);
419 }
421 /*! brief Returns an array containig all attributes \
422 That should be written.
423 @return Array e.g. 'status' => 'bla blub'
424 */
425 public function save()
426 {
427 return(array("timestamp" => $this->_timestamp_to_event($this->timestamp)));
428 }
431 /*! \brief Returns the event targets
432 @return Array All selected targets.
433 */
434 public function get_targets()
435 {
436 return($this->a_targets);
437 }
440 /*! \brief Returns the event timestamp in GOsa daemon format.
441 @return Returns the event timestamp (20081231120000)
442 */
443 public function get_timestamp($si_type = TRUE)
444 {
445 if($si_type){
446 return($this->_timestamp_to_event($this->timestamp));
447 }else{
448 return($this->timestamp);
449 }
450 }
453 /*! \brief Returns the event ID
454 @return Returns the event ID
455 */
456 public function get_id()
457 {
458 if($this->is_new){
459 return(-1);
460 }else{
461 return($this->data['ID']);
462 }
463 }
466 /*! \brief Returns a set of elements selected in a MultiSelectWindow
467 @return Array[integer]=integer
468 */
469 protected function list_get_selected_items()
470 {
471 $ids = array();
472 foreach($_POST as $name => $value){
473 if(preg_match("/^item_selected_[0-9]*$/",$name)){
474 $id = preg_replace("/^item_selected_/","",$name);
475 $ids[$id] = $id;
476 }
477 }
478 return($ids);
479 }
482 /*! \brief Add a target MAC address
483 @param Array A List of all target that should be added.
484 */
485 public function set_timestamp($stamp)
486 {
487 $this->timestamp = $stamp;
488 }
491 /*! \brief Add a target MAC address
492 @param Array A List of all target that should be added.
493 */
494 public function add_targets($targets)
495 {
496 foreach($targets as $target){
497 $this->a_targets[] = $target;
498 }
499 }
501 public function check()
502 {
503 return(array());
504 }
505 }
507 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
508 ?>