Code

f47a552d45d985a34386f2635b2a3d7bf4b1f1be
[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   public $timestamp    = 0;        // Event execution time; 
55   public $time_offset = 0;    // An offset when an event should start (normally not used)
56   public $concurrent_operations = 1; // Number of concurrent operations when a timestamp offset is used
57   protected $id           = -1;       // The Table ID
58   protected $status       = "unknown";// The current event status
59   protected $is_new       = TRUE;     // Is TRUE if this is a new event
61   protected $mode         = SCHEDULED_EVENT; // Default action is sheduled.
63   /* Sub dialog hanlding */
64   protected $target_divlist       = NULL;     // The divlist used by the target add dialog
65   protected $target_add_list_used = FALSE;    // Indicates that the target add list was used.
66   protected $time_select_used     = FALSE;    // Indicates that we have used the timestamp select boxes.
67   protected $target_list_used     = FALSE;    // Target list was diaplayed?
68   protected $_target_list         = array();  // Object Cache of those objects displayed in the target add dialog
69   protected $workstation_list     = array();  // Used as cache in the target list.
70   protected $server_list          = array();  // Used as cache in the target list.
72   protected $visible_for          = HIDDEN_EVENT;
74   protected $attributes           = array("timestamp");
76   protected $time_offset_used = FALSE;
78   /* Indicate that multiple events have to be created from this event */
79   public $multiple_events = FALSE;
81  
82   function set_type($type)
83   {
84     $this->mode = $type;
85   }
88   function get_type()
89   {
90     return($this->mode);
91   }
93   /*! \brief  Class contructor.
94     @param  Array   GOsa configuration object.
95     @param  Array   Event data, only given when edited.
96    */
97   public function __construct($config,$data = array())
98   {
99     $this->data   = $data;
100     $this->config = $config;
101     timezone::get_default_timezone();
102     $this->timestamp = time();
104     /* Load values from given data */
105     if(count($data)){
106       $this->is_new = FALSE;
108       $attrs = array("id" => "ID");
109       foreach($attrs as $to => $from){
110         $this->$to = $data[$from];
111       }
112       if(isset($data['TIMESTAMP'])){
113         $this->timestamp = $this->_event_to_timestamp($data['TIMESTAMP']);
114       }
115     }
116   }
119   /*! \brief  Create the HTML output for the plugin. 
120     @return String  The generated HTML output.
121    */
122   public function execute()
123   {
124     $this->time_select_used = FALSE;
125     $this->target_list_used = FALSE;
127     $str = "<h2>"._("This job has no template!")."</h2>";
128     $str.= "<p class='seperator'></p>";
129     $str.= "<div style='text-align:right;width:100%;padding:3px;'>
130       <input type='submit' name='abort_event_dialog' value='".msgPool::cancelButton()."'>
131       </div>";
132     return($str);
133   }
135   /*! \brief  Returns the plugin header, displayed in the template.
136     @return String  HTML header part.
137    */
138   public function get_header()
139   {
140     if($this->target_add_list_used){
141       return("");
142     }
143     $str = "<h2>".sprintf(_("Create '%s' job"),$this->s_Event_Name)."</h2>";
144     return($str);
145   }
148   /*! \brief  Returns the plugin footer (save cancel), displayed in the template.
149     @return String  HTML footer part.
150    */
151   public function get_footer()
152   {
153     if($this->target_add_list_used){
154       return("");
155     }
156     $str = "<p class='seperator'></p>";
157     $str.= "<div style='text-align:right;width:99%;padding:5px;'>
158       <input type='submit' name='save_event_dialog' value='".msgPool::saveButton()."'>&nbsp;
159     <input type='submit' name='abort_event_dialog' value='".msgPool::cancelButton()."'>
160       </div>";
161     return($str);
162   }
165   public function get_time_offset_select()
166   {
167     $this->time_offset_used = TRUE;
168     $smarty = get_smarty();
169     $smarty->assign('time_offset', $this->time_offset);
170     $smarty->assign('concurrent_operations', $this->concurrent_operations);
171     return($smarty->fetch(get_template_path('time_offset.tpl', TRUE, dirname(__FILE__))));
172   }
174   /*! \brief  Returns HTML representation of a timestamp using <select> boxes. 
175     @return Returns HTML content.
176    */
177   public function get_time_select()
178   {
179     timezone::get_default_timezone();
180     $this->time_select_used = TRUE;
182     $smarty = get_smarty();
184     $year   = date("Y",$this->timestamp);
185     $month  = date("m",$this->timestamp);
186     $day    = date("d",$this->timestamp);
188     $hour   = date("H",$this->timestamp);
189     $minute = date("i",$this->timestamp);
190     $second = date("s",$this->timestamp);
192     $years = array();
193     for($i = date("Y",time()); $i <= 2037 ;$i ++){
194       $years[$i] = $i;
195     }
196     $months = array();
197     for($i = 1; $i <= 12; $i ++){
198       $e = str_pad($i,2,"0",STR_PAD_LEFT);
199       $months[$e] = $e;
200     }
201     $days = array();
202     for($i = 1; $i <= cal_days_in_month(CAL_GREGORIAN,$month,$year); $i ++){
203       $e = str_pad($i,2,"0",STR_PAD_LEFT);
204       $days[$e] = $e;
205     }
206     $hours = array();
207     for($i = 0; $i < 24; $i ++){
208       $e = str_pad($i,2,"0",STR_PAD_LEFT);
209       $hours[$e] = $e;
210     }
211     $minutes = array();
212     for($i = 0; $i < 60; $i ++){
213       $e = str_pad($i,2,"0",STR_PAD_LEFT);
214       $minutes[$e] = $e;
215     }
216     $seconds = array();
217     for($i = 0; $i < 60; $i ++){
218       $e = str_pad($i,2,"0",STR_PAD_LEFT);
219       $seconds[$e] = $e;
220     }
222     $smarty->assign("years", $years);
223     $smarty->assign("months", $months);
224     $smarty->assign("days", $days);
225     $smarty->assign("hours", $hours);
226     $smarty->assign("minutes", $minutes);
227     $smarty->assign("seconds", $seconds);
228     $smarty->assign("time_year",$year);
229     $smarty->assign("time_month",$month);
230     $smarty->assign("time_day",$day);
231     $smarty->assign("time_hour",$hour);
232     $smarty->assign("time_minute",$minute);
233     $smarty->assign("time_second",$second);
234     return($smarty->fetch(get_template_path('timestamp_select.tpl', TRUE, dirname(__FILE__))));
235   } 
238   /*! \brief  HTML representation of all currently assigned targets using (divSelectBox).
239     @return String Returns a listbox with all used targets.
240    */
241   public function get_target_list()
242   {
243     $this->target_list_used = TRUE;
244     $divlist = new divSelectBox("EventTargets");
245     foreach($this->a_targets as $key => $target){
246       $divlist->AddEntry(array(
247             array("string"  => $target),
248             array("string"  => "<input type='image' src='images/lists/trash.png' title='"._("Remove")."' name='del_target_".$key."'>",
249               "attach"  => "style='width:20px; border-right:0px;'")
250             ));
251     }
252     $list_footer = "<input type='submit' name='open_target_list' value='"._("Add")."'>";
253     return($divlist->DrawList().$list_footer);
254   }
257   /*! \brief  Returns HTML content, displaying a dialog which allows to add new targets.
258     @return String HTML content. (EventTargetAddList)
259    */
260   public function get_target_add_list()
261   {
262     $this->target_add_list_used = TRUE;
264     if($this->target_divlist == NULL){ 
265       $this->target_divlist = new EventTargetAddList($this->config,$this);
266     }
267     $this->target_divlist->execute();
269     $smarty = get_smarty();
270     $smarty->assign("divlist",$this->target_divlist->Draw());
271     return($smarty->fetch(get_template_path('target_list.tpl', TRUE, dirname(__FILE__))));
272   }
275   /*! \brief  Handles all posted HTML data, including target add,remove...
276    */
277   public function save_object()
278   {
279     if(isset($_POST['open_target_list'])){
280       $this->target_add_list_used =TRUE;
281     }
282     if($this->target_divlist != NULL){
283       $this->target_divlist->save_object();
284     }
285     if($this->target_add_list_used){
286       if(isset($_POST['abort_target_dialog'])){
287         $this->target_add_list_used =FALSE;
288         $this->target_divlist = NULL;
289       }
290       if(isset($_POST['save_target_dialog'])){
291         $this->target_add_list_used =FALSE;
292         $this->add_targets($this->target_divlist->get_selected_targets());
293         $this->target_divlist = NULL;
294       }
295     }
297     if (isset($_POST['concurrent_operations'])) {
298         $this->concurrent_operations = $_POST['concurrent_operations'];
299     }
300     if (isset($_POST['time_offset'])) {
301         $this->time_offset = $_POST['time_offset'];
302     }
303     
304     if ($this->time_offset_used){
305         /* Check that multiple events makes sense at all (e.g. there are more targets
306          * then allowed concurrent operations */
307         if (count($this->a_targets) > $this->concurrent_operations) {
308             if ($this->time_offset > 0) {
309                 $this->multiple_events = TRUE;
310             }
311         }
312     }
314     if($this->time_select_used){
315       $time_stamp_values_found = TRUE;
316       foreach(array("time_year","time_month","time_day","time_hour","time_minute","time_second") as $attr){
317         $time_stamp_values_found &= isset($_POST[$attr]);
318       }
319       if($time_stamp_values_found){
320         /* Make sure the following conversion happens with the right timezone */
321         timezone::get_default_timezone();
323         $this->timestamp = mktime(
324             $_POST['time_hour'],
325             $_POST['time_minute'],        
326             $_POST['time_second'],        
327             $_POST['time_month'],        
328             $_POST['time_day'],        
329             $_POST['time_year']);
330       }
332     }
334     if($this->target_list_used){
335       foreach($_POST as $name => $value){
336         if(preg_match("/^del_target_/",$name)){
337           $id = preg_replace("/^del_target_([0-9]*)_.*/","\\1",$name);
338           if(isset($this->a_targets[$id])){
339             unset($this->a_targets[$id]);
340           }
341           break; 
342         }
343       }
344     } 
345   }
348   /*! \brief  Converts a daemon timestamp into an unix timestamp. \
349     e.g.  20080101125959 -> 1199188799 
350     @param  A daemon timestamp  YYYYddmmHHiiss
351     @return Integer  A unix timestamp.
352    */
353   public function _event_to_timestamp($str)
354   {
355     return(strtotime($str));
356   }
359   /*! \brief  Converts a unix timestamp in to a gosa-si timestamp. \
360     e.g.  1199188799 -> 20080101125959
361     @param  A unix timestamp (e.g. 1199188799)
362     @return Integer  A daemon timestamp (e.g. 20080101125959).
363    */
364   public function _timestamp_to_event($stamp)
365   {
366     return(date("YmdHis",$stamp));
367   }
370   /*! \brief  Returns event information, like menu strings, images ... 
371     @return   Array Event informations.
372    */
373   public function get_event_info()
374   {
375     $data =array();
376     $data['CLASS_NAME']   = get_class($this);
377     $data['s_Menu_Name']  = $this->s_Menu_Name;
378     $data['s_Event_Name'] = $this->s_Event_Name;
379     foreach(array("s_Queued_Action","s_Schedule_Action","s_Trigger_Action") as $attr){
380       if(!empty($this->$attr)){
381         $data[$attr]  = $this->$attr;
382       }
383     }
384     $data['MenuImage']    = "<img src='".$this->s_Menu_Image."' alt='".$this->s_Menu_Name."' border='0' class='center'>";
385     $data['ListImage']    = "<img src='".$this->s_List_Image."' title='".$this->s_Event_Name."' 
386       alt='".$this->s_Event_Name."' border='0' class='center'>";
387     return($data);
388   }
391   /*! \brief  Check if we have opened the target add dialog. 
392     @return   Boolean TRUE if we have opened the target add dialog else FALSE.
393    */
394   protected function is_target_list_open()
395   {
396     return($this->target_add_list_used);
397   }
400   /*! \brief  Returns a complete list of all available events.
401     @return   Array   Containing $this->get_event_info() for all available events.
402    */
403   static function get_event_types($type)
404   {
405     global $class_mapping,$config;
406     $list = array();
407     $list['BY_CLASS']  = array();
408     $list['TRIGGERED'] = array();
409     $list['SCHEDULED'] = array();
410     $list['QUEUED']    = array();
412     foreach($class_mapping as $name => $path){
413       if(preg_match("/^DaemonEvent_/",$name)){
414         $tmp  = new $name($config);
415         if($tmp->visible_for & $type){
416           $evt  = $tmp->get_event_info();
417           $list['BY_CLASS'][$name]                      = $evt;
418           if(isset($evt['s_Trigger_Action'])){
419             $list['TRIGGERED'][$name] = $evt;
420             $list['QUEUED'][$evt['s_Trigger_Action']] = $name;
421           }
422           if(isset($evt['s_Schedule_Action'])){
423             $list['SCHEDULED'][$name] = $evt;
424             $list['QUEUED'][$evt['s_Schedule_Action']] = $name;
425           }
426           if(isset($evt['s_Queued_Action'])){
427             $list['QUEUED'][$evt['s_Queued_Action']] = $name;
428           }
429         }
430       }
431     }
432     return($list);
433   }
436   /*! \brief  Returns TRUE if this event is new. (Not edited)
437     @return Boolean TRUE if new, else FALSE.
438    */
439   public function is_new()
440   {
441     return($this->is_new);
442   }
445   /*! \brief  Returns the event tag to schedule a new action 
446     @param    Returns the event e.g. 'job_trigger_action_wake'
447    */
448   public function get_schedule_action()
449   {
450     return($this->s_Schedule_Action);
451   }
454   /*! \brief  Returns the event tag to schedule a new action 
455     @param    Returns the event e.g. 'trigger_action_wake'
456    */
457   public function get_trigger_action()
458   {
459     return($this->s_Trigger_Action);
460   }
463   /*! brief  Returns an array containig all attributes \
464     That should be written.
465     @return Array e.g. 'status' => 'bla blub'  
466    */ 
467   public function save()
468   {
469     $ret = array();
470     foreach($this->attributes as $attr){
471       $ret[$attr] = $this->$attr;
472     }
474     if(!isset($ret['timestamp'])){
475       $ret['timestamp'] = time();
476     }
478     # Check if timestamp is in gosa-si-time-format 
479     if(!tests::is_gosa_si_time($ret['timestamp'])){ 
480       $ret['timestamp'] = $this->_timestamp_to_event($this->timestamp); 
481     }
483     return($ret);
484   }
487   /*! \brief  Returns the event targets
488     @return Array  All selected targets.
489    */ 
490   public function get_targets()
491   {
492     return($this->a_targets);
493   }
496   /*! \brief  Returns the event timestamp in GOsa daemon format. 
497     @return Returns the event timestamp (20081231120000)
498    */
499   public function get_timestamp($si_type = TRUE)
500   {
501     if($si_type){
502       return($this->_timestamp_to_event($this->timestamp));
503     }else{
504       return($this->timestamp);
505     }
506   }
509   /*! \brief  Returns the event ID
510     @return Returns the event ID
511    */
512   public function get_id()
513   {
514     if($this->is_new){
515       return(-1);
516     }else{
517       return($this->data['ID']);
518     }
519   }
522   /*! \brief Add a target MAC address 
523       @param Array A List of all target that should be added.
524    */
525   public function set_timestamp($stamp) 
526   {
527     $this->timestamp = $stamp;
528   }
531   /*! \brief Add a target MAC address 
532       @param Array A List of all target that should be added.
533    */
534   public function add_targets($targets) 
535   {
536     foreach($targets as $target){
537       $this->a_targets[] = $target;
538     }
539   }
541   public function check()
542   {
543     return(array());
544   }
547   /*! \brief Update a class variable from outside 
548    */
549   public function set_value($name,$value)
550   {
551     $name = strtolower($name);
552     if(isset($this->$name) && in_array($name,$this->attributes)){
553       $this->$name = $value;
554     }
555   }
558 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
559 ?>