Code

Updated translation
[gosa.git] / 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;
71  
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='"._("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>".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:100%;padding:5px;'>
148       <input type='submit' name='save_event_dialog' value='"._("Save")."'>&nbsp;
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     $data['s_Menu_Name']  = $this->s_Menu_Name;
352     $data['s_Event_Name'] = $this->s_Event_Name;
353     foreach(array("s_Queued_Action","s_Schedule_Action","s_Trigger_Action") as $attr){
354       if(!empty($this->$attr)){
355         $data[$attr]  = $this->$attr;
356       }
357     }
358     $data['MenuImage']    = "<img src='".$this->s_Menu_Image."' alt='".$this->s_Menu_Name."' border='0' class='center'>";
359     $data['ListImage']    = "<img src='".$this->s_List_Image."' title='".$this->s_Event_Name."' 
360       alt='".$this->s_Event_Name."' border='0' class='center'>";
361     return($data);
362   }
365   /*! \brief  Check if we have opened the target add dialog. 
366     @return   Boolean TRUE if we have opened the target add dialog else FALSE.
367    */
368   protected function is_target_list_open()
369   {
370     return($this->target_add_list_used);
371   }
374   /*! \brief  Returns a complete list of all available events.
375     @return   Array   Containing $this->get_event_info() for all available events.
376    */
377   static function get_event_types($type)
378   {
379     global $class_mapping,$config;
380     $list = array();
381     $list['BY_CLASS']  = array();
382     $list['TRIGGERED'] = array();
383     $list['SCHEDULED'] = array();
384     $list['QUEUED']    = array();
386     foreach($class_mapping as $name => $path){
387       if(preg_match("/^DaemonEvent_/",$name)){
388         $tmp  = new $name($config);
389         if($tmp->visible_for & $type){
390           $evt  = $tmp->get_event_info();
391           $list['BY_CLASS'][$name]                      = $evt;
392           if(isset($evt['s_Trigger_Action'])){
393             $list['TRIGGERED'][$name] = $evt;
394             $list['QUEUED'][$evt['s_Trigger_Action']] = $name;
395           }
396           if(isset($evt['s_Schedule_Action'])){
397             $list['SCHEDULED'][$name] = $evt;
398             $list['QUEUED'][$evt['s_Schedule_Action']] = $name;
399           }
400           if(isset($evt['s_Queued_Action'])){
401             $list['QUEUED'][$evt['s_Queued_Action']] = $name;
402           }
403         }
404       }
405     }
406     return($list);
407   }
410   /*! \brief  Returns TRUE if this event is new. (Not edited)
411     @return Boolean TRUE if new, else FALSE.
412    */
413   public function is_new()
414   {
415     return($this->is_new);
416   }
419   /*! \brief  Returns the event tag to schedule a new action 
420     @param    Returns the event e.g. 'job_trigger_action_wake'
421    */
422   public function get_schedule_action()
423   {
424     return($this->s_Schedule_Action);
425   }
427   //
428   /*! \brief  Returns the event tag to schedule a new action 
429     @param    Returns the event e.g. 'trigger_action_wake'
430    */
431   public function get_trigger_action()
432   {
433     return($this->s_Trigger_Action);
434   }
436   /*! brief  Returns an array containig all attributes \
437     That should be written.
438     @return Array e.g. 'status' => 'bla blub'  
439    */ 
440   public function save()
441   {
442     return(array("timestamp" => $this->_timestamp_to_event($this->timestamp)));
443   }
446   /*! \brief  Returns the event targets
447     @return Array  All selected targets.
448    */ 
449   public function get_targets()
450   {
451     return($this->a_targets);
452   }
455   /*! \brief  Returns the event timestamp in GOsa daemon format. 
456     @return Returns the event timestamp (20081231120000)
457    */
458   public function get_timestamp()
459   {
460     return($this->_timestamp_to_event($this->timestamp));
461   }
464   /*! \brief  Returns the event ID
465     @return Returns the event ID
466    */
467   public function get_id()
468   {
469     if($this->is_new){
470       return(-1);
471     }else{
472       return($this->data['ID']);
473     }
474   }
477   /*! \brief  Returns a set of elements selected in a MultiSelectWindow
478     @return Array[integer]=integer
479    */
480   protected  function list_get_selected_items()
481   {
482     $ids = array();
483     foreach($_POST as $name => $value){
484       if(preg_match("/^item_selected_[0-9]*$/",$name)){
485         $id   = preg_replace("/^item_selected_/","",$name);
486         $ids[$id] = $id;
487       }
488     }
489     return($ids);
490   }
493   /*! \brief Add a target MAC address 
494       @param Array A List of all target that should be added.
495    */
496   public function set_timestamp($stamp) 
497   {
498     $this->timestamp = $stamp;
499   }
502   /*! \brief Add a target MAC address 
503       @param Array A List of all target that should be added.
504    */
505   public function add_targets($targets) 
506   {
507     foreach($targets as $target){
508       $this->a_targets[] = $target;
509     }
510   }
513 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
514 ?>