Code

Updated lists
[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;
72   protected $periodType           = "days";
73   protected $periodValue          = 7;
74   protected $activate_periodical_job = FALSE;
76   protected $attributes           = array("timestamp");
77  
78   function set_type($type)
79   {
80     $this->mode = $type;
81   }
84   function get_type()
85   {
86     return($this->mode);
87   }
89   /*! \brief  Class contructor.
90     @param  Array   GOsa configuration object.
91     @param  Array   Event data, only given when edited.
92    */
93   public function __construct($config,$data = array())
94   {
95     $this->data   = $data;
96     $this->config = $config;
97     timezone::get_default_timezone();
98     $this->timestamp = time();
100     /* Load values from given data */
101     if(count($data)){
102       $this->is_new = FALSE;
104       $attrs = array("id" => "ID");
105       foreach($attrs as $to => $from){
106         $this->$to = $data[$from];
107       }
108       if(isset($data['TIMESTAMP'])){
109         $this->timestamp = $this->_event_to_timestamp($data['TIMESTAMP']);
110       }
111     }
113     if(isset($data['PERIODIC']) && !preg_match("/none/i",$data['PERIODIC'])){
114       $tmp = split("_",$data['PERIODIC']);
115       if(count($tmp) == 2){
116         $this->activate_periodical_job = TRUE;
117         $this->periodValue = $tmp[0];
118         $this->periodType = $tmp[1];
119       }
120     }
121   }
124   /*! \brief  Create the HTML output for the plugin. 
125     @return String  The generated HTML output.
126    */
127   public function execute()
128   {
129     $this->time_select_used = FALSE;
130     $this->target_list_used = FALSE;
132     $str = "<h2>"._("This job has no template!")."</h2>";
133     $str.= "<p class='seperator'></p>";
134     $str.= "<div style='text-align:right;width:100%;padding:3px;'>
135       <input type='submit' name='abort_event_dialog' value='".msgPool::cancelButton()."'>
136       </div>";
137     return($str);
138   }
140   /*! \brief  Returns the plugin header, displayed in the template.
141     @return String  HTML header part.
142    */
143   public function get_header()
144   {
145     if($this->target_add_list_used){
146       return("");
147     }
148     $str = "<h2>".sprintf(_("Create '%s' job"),$this->s_Event_Name)."</h2>";
149     return($str);
150   }
153   /*! \brief  Returns the plugin footer (save cancel), displayed in the template.
154     @return String  HTML footer part.
155    */
156   public function get_footer()
157   {
158     if($this->target_add_list_used){
159       return("");
160     }
161     $str = "<p class='seperator'></p>";
162     $str.= "<div style='text-align:right;width:99%;padding:5px;'>
163       <input type='submit' name='save_event_dialog' value='".msgPool::saveButton()."'>&nbsp;
164     <input type='submit' name='abort_event_dialog' value='".msgPool::cancelButton()."'>
165       </div>";
166     return($str);
167   }
170   /*! \brief  Returns HTML representation of a timestamp using <select> boxes. 
171     @return Returns HTML content.
172    */
173   public function get_time_select()
174   {
175     $this->time_select_used = TRUE;
177     $smarty = get_smarty();
179     $year   = date("Y",$this->timestamp);
180     $month  = date("m",$this->timestamp);
181     $day    = date("d",$this->timestamp);
183     $hour   = date("H",$this->timestamp);
184     $minute = date("i",$this->timestamp);
185     $second = date("s",$this->timestamp);
187     $years = array();
188     for($i = date("Y",time()); $i <= 2037 ;$i ++){
189       $years[$i] = $i;
190     }
191     $months = array();
192     for($i = 1; $i <= 12; $i ++){
193       $e = str_pad($i,2,"0",STR_PAD_LEFT);
194       $months[$e] = $e;
195     }
196     $days = array();
197     for($i = 1; $i <= cal_days_in_month(CAL_GREGORIAN,$month,$year); $i ++){
198       $e = str_pad($i,2,"0",STR_PAD_LEFT);
199       $days[$e] = $e;
200     }
201     $hours = array();
202     for($i = 0; $i < 24; $i ++){
203       $e = str_pad($i,2,"0",STR_PAD_LEFT);
204       $hours[$e] = $e;
205     }
206     $minutes = array();
207     for($i = 0; $i < 60; $i ++){
208       $e = str_pad($i,2,"0",STR_PAD_LEFT);
209       $minutes[$e] = $e;
210     }
211     $seconds = array();
212     for($i = 0; $i < 60; $i ++){
213       $e = str_pad($i,2,"0",STR_PAD_LEFT);
214       $seconds[$e] = $e;
215     }
217     $periodTypes = array(
218         "minutes" => _("Minutes"),
219         "hours"   => _("Hours"),
220         "days"    => _("Days"),
221         "weeks"   => _("Weeks"),
222         "months"  => _("Months"));
223     
225     $smarty->assign("periodTypes", $periodTypes);
226     $smarty->assign("periodType", $this->periodType);
227     $smarty->assign("periodValue", $this->periodValue);
228     $smarty->assign("activate_periodical_job", $this-> activate_periodical_job);
230     $smarty->assign("years", $years);
231     $smarty->assign("months", $months);
232     $smarty->assign("days", $days);
233     $smarty->assign("hours", $hours);
234     $smarty->assign("minutes", $minutes);
235     $smarty->assign("seconds", $seconds);
236     $smarty->assign("time_year",$year);
237     $smarty->assign("time_month",$month);
238     $smarty->assign("time_day",$day);
239     $smarty->assign("time_hour",$hour);
240     $smarty->assign("time_minute",$minute);
241     $smarty->assign("time_second",$second);
242     return($smarty->fetch(get_template_path('timestamp_select.tpl', TRUE, dirname(__FILE__))));
243   } 
246   /*! \brief  HTML representation of all currently assigned targets using (divSelectBox).
247     @return String Returns a listbox with all used targets.
248    */
249   public function get_target_list()
250   {
251     $this->target_list_used = TRUE;
252     $divlist = new divSelectBox("EventTargets");
253     foreach($this->a_targets as $key => $target){
254       $divlist->AddEntry(array(
255             array("string"  => $target),
256             array("string"  => "<input type='image' src='images/lists/trash.png' title='"._("Remove")."' name='del_target_".$key."'>",
257               "attach"  => "style='width:20px; border-right:0px;'")
258             ));
259     }
260     $list_footer = "<input type='submit' name='open_target_list' value='"._("Add")."'>";
261     return($divlist->DrawList().$list_footer);
262   }
265   /*! \brief  Returns HTML content, displaying a dialog which allows to add new targets.
266     @return String HTML content. (EventAddSystemDialog)
267    */
268   public function get_target_add_list()
269   {
270     $this->target_add_list_used = TRUE;
272     if($this->target_divlist == NULL){ 
273       $this->target_divlist = new EventAddSystemDialog($this->config,$this);
274     }
276     $smarty = get_smarty();
277     $smarty->assign("divlist",$this->target_divlist->execute());
278     return($smarty->fetch(get_template_path('target_list.tpl', TRUE, dirname(__FILE__))));
279   }
282   /*! \brief  Handles all posted HTML data, including target add,remove...
283    */
284   public function save_object()
285   {
286     if(isset($_POST['open_target_list'])){
287       $this->target_add_list_used =TRUE;
288     }
289     if($this->target_divlist != NULL){
290       $this->target_divlist->save_object();
291     }
292     if($this->target_add_list_used){
293       if(isset($_POST['abort_target_dialog'])){
294         $this->target_add_list_used =FALSE;
295         $this->target_divlist = NULL;
296       }
297       if(isset($_POST['save_target_dialog'])){
298         $this->target_add_list_used =FALSE;
299         $this->add_targets($this->target_divlist->get_selected_targets());
300         $this->target_divlist = NULL;
301       }
302     }
304     if($this->time_select_used){
305       $time_stamp_values_found = TRUE;
306       foreach(array("time_year","time_month","time_day","time_hour","time_minute","time_second") as $attr){
307         $time_stamp_values_found &= isset($_POST[$attr]);
308       }
309       if($time_stamp_values_found){
310         $this->timestamp = mktime(
311             $_POST['time_hour'],
312             $_POST['time_minute'],        
313             $_POST['time_second'],        
314             $_POST['time_month'],        
315             $_POST['time_day'],        
316             $_POST['time_year']);
318         if(isset($_POST['periodValue'])) $this->periodValue = get_post('periodValue');
319         if(isset($_POST['periodType'])) $this->periodType = get_post('periodType');
321         if(isset($_POST['activate_periodical_job'])){
322           $this->activate_periodical_job = TRUE;
323         }else{
324           $this->activate_periodical_job = FALSE;
325         }
326       }
327     }
329     if($this->target_list_used){
330       foreach($_POST as $name => $value){
331         if(preg_match("/^del_target_/",$name)){
332           $id = preg_replace("/^del_target_([0-9]*)_.*/","\\1",$name);
333           if(isset($this->a_targets[$id])){
334             unset($this->a_targets[$id]);
335           }
336           break; 
337         }
338       }
339     } 
340   }
343   /*! \brief  Converts a daemon timestamp into an unix timestamp. \
344     e.g.  20080101125959 -> 1199188799 
345     @param  A daemon timestamp  YYYYddmmHHiiss
346     @return Integer  A unix timestamp.
347    */
348   public function _event_to_timestamp($str)
349   {
350     return(strtotime($str));
351   }
354   /*! \brief  Converts a unix timestamp in to a gosa-si timestamp. \
355     e.g.  1199188799 -> 20080101125959
356     @param  A unix timestamp (e.g. 1199188799)
357     @return Integer  A daemon timestamp (e.g. 20080101125959).
358    */
359   public function _timestamp_to_event($stamp)
360   {
361     return(date("YmdHis",$stamp));
362   }
365   /*! \brief  Returns event information, like menu strings, images ... 
366     @return   Array Event informations.
367    */
368   public function get_event_info()
369   {
370     $data =array();
371     $data['CLASS_NAME']   = get_class($this);
372     $data['s_Menu_Name']  = $this->s_Menu_Name;
373     $data['s_Event_Name'] = $this->s_Event_Name;
374     foreach(array("s_Queued_Action","s_Schedule_Action","s_Trigger_Action") as $attr){
375       if(!empty($this->$attr)){
376         $data[$attr]  = $this->$attr;
377       }
378     }
379     $data['MenuImage']    = "<img src='".$this->s_Menu_Image."' alt='".$this->s_Menu_Name."' border='0' class='center'>";
380     $data['ListImage']    = "<img src='".$this->s_List_Image."' title='".$this->s_Event_Name."' 
381       alt='".$this->s_Event_Name."' border='0' class='center'>";
382     return($data);
383   }
386   /*! \brief  Check if we have opened the target add dialog. 
387     @return   Boolean TRUE if we have opened the target add dialog else FALSE.
388    */
389   protected function is_target_list_open()
390   {
391     return($this->target_add_list_used);
392   }
395   /*! \brief  Returns a complete list of all available events.
396     @return   Array   Containing $this->get_event_info() for all available events.
397    */
398   static function get_event_types_by_category($categories)
399   {
400     $types= array();
402     foreach ($categories as $category) {
403       if (preg_match('/^users$/', $category)) {
404         $types= array_merge($types, DaemonEvent::get_event_types(USER_EVENT));
405       }
406       if (preg_match('/^systems$/', $category)) {
407         $types= array_merge($types, DaemonEvent::get_event_types(SYSTEM_EVENT));
408       }
409     }
411     return $types;
412   }
415   /*! \brief  Returns a complete list of all available events.
416     @return   Array   Containing $this->get_event_info() for all available events.
417    */
418   static function get_event_types($type)
419   {
420     global $class_mapping,$config;
421     $list = array();
422     $list['BY_CLASS']  = array();
423     $list['TRIGGERED'] = array();
424     $list['SCHEDULED'] = array();
425     $list['QUEUED']    = array();
427     foreach($class_mapping as $name => $path){
428       if(preg_match("/^DaemonEvent_/",$name)){
429         $tmp  = new $name($config);
430         if($tmp->visible_for & $type){
431           $evt  = $tmp->get_event_info();
432           $list['BY_CLASS'][$name]                      = $evt;
433           if(isset($evt['s_Trigger_Action'])){
434             $list['TRIGGERED'][$name] = $evt;
435             $list['QUEUED'][$evt['s_Trigger_Action']] = $name;
436           }
437           if(isset($evt['s_Schedule_Action'])){
438             $list['SCHEDULED'][$name] = $evt;
439             $list['QUEUED'][$evt['s_Schedule_Action']] = $name;
440           }
441           if(isset($evt['s_Queued_Action'])){
442             $list['QUEUED'][$evt['s_Queued_Action']] = $name;
443           }
444         }
445       }
446     }
447     return($list);
448   }
451   /*! \brief  Returns TRUE if this event is new. (Not edited)
452     @return Boolean TRUE if new, else FALSE.
453    */
454   public function is_new()
455   {
456     return($this->is_new);
457   }
460   /*! \brief  Returns the event tag to schedule a new action 
461     @param    Returns the event e.g. 'job_trigger_action_wake'
462    */
463   public function get_schedule_action()
464   {
465     return($this->s_Schedule_Action);
466   }
469   /*! \brief  Returns the event tag to schedule a new action 
470     @param    Returns the event e.g. 'trigger_action_wake'
471    */
472   public function get_trigger_action()
473   {
474     return($this->s_Trigger_Action);
475   }
478   /*! brief  Returns an array containig all attributes \
479     That should be written.
480     @return Array e.g. 'status' => 'bla blub'  
481    */ 
482   public function save()
483   {
484     $ret = array();
485     foreach($this->attributes as $attr){
486       $ret[$attr] = $this->$attr;
487     }
488     if($this->mode == SCHEDULED_EVENT){
489       $ret['timestamp'] = $this->_timestamp_to_event($this->timestamp);
490     }elseif(isset($ret['timestamp'])){
491       unset($ret['timestamp']);
492     }
494     if(isset($ret['periodic'])){
495       unset($ret['periodic']);
496     }
497     if($this->activate_periodical_job){
498       $ret['periodic']= $this->periodValue."_".$this->periodType;
499     }
501     return($ret);
502   }
505   /*! \brief  Returns the event targets
506     @return Array  All selected targets.
507    */ 
508   public function get_targets()
509   {
510     return($this->a_targets);
511   }
514   /*! \brief  Returns the event timestamp in GOsa daemon format. 
515     @return Returns the event timestamp (20081231120000)
516    */
517   public function get_timestamp($si_type = TRUE)
518   {
519     if($si_type){
520       return($this->_timestamp_to_event($this->timestamp));
521     }else{
522       return($this->timestamp);
523     }
524   }
527   /*! \brief  Returns the event ID
528     @return Returns the event ID
529    */
530   public function get_id()
531   {
532     if($this->is_new){
533       return(-1);
534     }else{
535       return($this->data['ID']);
536     }
537   }
540   /*! \brief Add a target MAC address 
541       @param Array A List of all target that should be added.
542    */
543   public function set_timestamp($stamp) 
544   {
545     $this->timestamp = $stamp;
546   }
549   /*! \brief Add a target MAC address 
550       @param Array A List of all target that should be added.
551    */
552   public function add_targets($targets) 
553   {
554     foreach($targets as $target){
555       $this->a_targets[] = $target;
556     }
557   }
559   public function check()
560   {
561     return(array());
562   }
565   /*! \brief Update a class variable from outside 
566    */
567   public function set_value($name,$value)
568   {
569     $name = strtolower($name);
570     if(isset($this->$name) && in_array($name,$this->attributes)){
571       $this->$name = $value;
572     }
573   }
576 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
577 ?>