Code

adcea31410e9fd9ffd0ed381fe3d03c502e74547
[gosa.git] / trunk / gosa-plugins / goto / addons / goto / events / class_DaemonEvent.inc
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");
73  
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()."'>&nbsp;
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($type)
365   {
366     global $class_mapping,$config;
367     $list = array();
368     $list['BY_CLASS']  = array();
369     $list['TRIGGERED'] = array();
370     $list['SCHEDULED'] = array();
371     $list['QUEUED']    = array();
373     foreach($class_mapping as $name => $path){
374       if(preg_match("/^DaemonEvent_/",$name)){
375         $tmp  = new $name($config);
376         if($tmp->visible_for & $type){
377           $evt  = $tmp->get_event_info();
378           $list['BY_CLASS'][$name]                      = $evt;
379           if(isset($evt['s_Trigger_Action'])){
380             $list['TRIGGERED'][$name] = $evt;
381             $list['QUEUED'][$evt['s_Trigger_Action']] = $name;
382           }
383           if(isset($evt['s_Schedule_Action'])){
384             $list['SCHEDULED'][$name] = $evt;
385             $list['QUEUED'][$evt['s_Schedule_Action']] = $name;
386           }
387           if(isset($evt['s_Queued_Action'])){
388             $list['QUEUED'][$evt['s_Queued_Action']] = $name;
389           }
390         }
391       }
392     }
393     return($list);
394   }
397   /*! \brief  Returns TRUE if this event is new. (Not edited)
398     @return Boolean TRUE if new, else FALSE.
399    */
400   public function is_new()
401   {
402     return($this->is_new);
403   }
406   /*! \brief  Returns the event tag to schedule a new action 
407     @param    Returns the event e.g. 'job_trigger_action_wake'
408    */
409   public function get_schedule_action()
410   {
411     return($this->s_Schedule_Action);
412   }
415   /*! \brief  Returns the event tag to schedule a new action 
416     @param    Returns the event e.g. 'trigger_action_wake'
417    */
418   public function get_trigger_action()
419   {
420     return($this->s_Trigger_Action);
421   }
424   /*! brief  Returns an array containig all attributes \
425     That should be written.
426     @return Array e.g. 'status' => 'bla blub'  
427    */ 
428   public function save()
429   {
430     $ret = array();
431     foreach($this->attributes as $attr){
432       $ret[$attr] = $this->$attr;
433     }
435     if(!isset($ret['timestamp'])){
436       $ret['timestamp'] = time();
437     }
439     # Check if timestamp is in gosa-si-time-format 
440     if(!tests::is_gosa_si_time($ret['timestamp'])){ 
441       $ret['timestamp'] = $this->_timestamp_to_event($this->timestamp); 
442     } 
443     return($ret);
444   }
447   /*! \brief  Returns the event targets
448     @return Array  All selected targets.
449    */ 
450   public function get_targets()
451   {
452     return($this->a_targets);
453   }
456   /*! \brief  Returns the event timestamp in GOsa daemon format. 
457     @return Returns the event timestamp (20081231120000)
458    */
459   public function get_timestamp($si_type = TRUE)
460   {
461     if($si_type){
462       return($this->_timestamp_to_event($this->timestamp));
463     }else{
464       return($this->timestamp);
465     }
466   }
469   /*! \brief  Returns the event ID
470     @return Returns the event ID
471    */
472   public function get_id()
473   {
474     if($this->is_new){
475       return(-1);
476     }else{
477       return($this->data['ID']);
478     }
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   }
507   /*! \brief Update a class variable from outside 
508    */
509   public function set_value($name,$value)
510   {
511     $name = strtolower($name);
512     if(isset($this->$name) && in_array($name,$this->attributes)){
513       $this->$name = $value;
514     }
515   }
518 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
519 ?>