Code

Added periodical jobs
[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'])){
114       $tmp = split("_",$data['PERIODIC']);
115       if(count($tmp)){
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. (EventTargetAddList)
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 EventTargetAddList($this->config,$this);
274     }
275     $this->target_divlist->execute();
277     $smarty = get_smarty();
278     $smarty->assign("divlist",$this->target_divlist->Draw());
279     return($smarty->fetch(get_template_path('target_list.tpl', TRUE, dirname(__FILE__))));
280   }
283   /*! \brief  Handles all posted HTML data, including target add,remove...
284    */
285   public function save_object()
286   {
287     if(isset($_POST['open_target_list'])){
288       $this->target_add_list_used =TRUE;
289     }
290     if($this->target_divlist != NULL){
291       $this->target_divlist->save_object();
292     }
293     if($this->target_add_list_used){
294       if(isset($_POST['abort_target_dialog'])){
295         $this->target_add_list_used =FALSE;
296         $this->target_divlist = NULL;
297       }
298       if(isset($_POST['save_target_dialog'])){
299         $this->target_add_list_used =FALSE;
300         $this->add_targets($this->target_divlist->get_selected_targets());
301         $this->target_divlist = NULL;
302       }
303     }
305     if($this->time_select_used){
306       $time_stamp_values_found = TRUE;
307       foreach(array("time_year","time_month","time_day","time_hour","time_minute","time_second") as $attr){
308         $time_stamp_values_found &= isset($_POST[$attr]);
309       }
310       if($time_stamp_values_found){
311         $this->timestamp = mktime(
312             $_POST['time_hour'],
313             $_POST['time_minute'],        
314             $_POST['time_second'],        
315             $_POST['time_month'],        
316             $_POST['time_day'],        
317             $_POST['time_year']);
319         if(isset($_POST['periodValue'])) $this->periodValue = get_post('periodValue');
320         if(isset($_POST['periodType'])) $this->periodType = get_post('periodType');
322         if(isset($_POST['activate_periodical_job'])){
323           $this->activate_periodical_job = TRUE;
324         }else{
325           $this->activate_periodical_job = FALSE;
326         }
327       }
328     }
330     if($this->target_list_used){
331       foreach($_POST as $name => $value){
332         if(preg_match("/^del_target_/",$name)){
333           $id = preg_replace("/^del_target_([0-9]*)_.*/","\\1",$name);
334           if(isset($this->a_targets[$id])){
335             unset($this->a_targets[$id]);
336           }
337           break; 
338         }
339       }
340     } 
341   }
344   /*! \brief  Converts a daemon timestamp into an unix timestamp. \
345     e.g.  20080101125959 -> 1199188799 
346     @param  A daemon timestamp  YYYYddmmHHiiss
347     @return Integer  A unix timestamp.
348    */
349   public function _event_to_timestamp($str)
350   {
351     return(strtotime($str));
352   }
355   /*! \brief  Converts a unix timestamp in to a gosa-si timestamp. \
356     e.g.  1199188799 -> 20080101125959
357     @param  A unix timestamp (e.g. 1199188799)
358     @return Integer  A daemon timestamp (e.g. 20080101125959).
359    */
360   public function _timestamp_to_event($stamp)
361   {
362     return(date("YmdHis",$stamp));
363   }
366   /*! \brief  Returns event information, like menu strings, images ... 
367     @return   Array Event informations.
368    */
369   public function get_event_info()
370   {
371     $data =array();
372     $data['CLASS_NAME']   = get_class($this);
373     $data['s_Menu_Name']  = $this->s_Menu_Name;
374     $data['s_Event_Name'] = $this->s_Event_Name;
375     foreach(array("s_Queued_Action","s_Schedule_Action","s_Trigger_Action") as $attr){
376       if(!empty($this->$attr)){
377         $data[$attr]  = $this->$attr;
378       }
379     }
380     $data['MenuImage']    = "<img src='".$this->s_Menu_Image."' alt='".$this->s_Menu_Name."' border='0' class='center'>";
381     $data['ListImage']    = "<img src='".$this->s_List_Image."' title='".$this->s_Event_Name."' 
382       alt='".$this->s_Event_Name."' border='0' class='center'>";
383     return($data);
384   }
387   /*! \brief  Check if we have opened the target add dialog. 
388     @return   Boolean TRUE if we have opened the target add dialog else FALSE.
389    */
390   protected function is_target_list_open()
391   {
392     return($this->target_add_list_used);
393   }
396   /*! \brief  Returns a complete list of all available events.
397     @return   Array   Containing $this->get_event_info() for all available events.
398    */
399   static function get_event_types_by_category($categories)
400   {
401     $types= array();
403     foreach ($categories as $category) {
404       if (preg_match('/^users$/', $category)) {
405         $types= array_merge($types, DaemonEvent::get_event_types(USER_EVENT));
406       }
407       if (preg_match('/^systems$/', $category)) {
408         $types= array_merge($types, DaemonEvent::get_event_types(SYSTEM_EVENT));
409       }
410     }
412     return $types;
413   }
416   /*! \brief  Returns a complete list of all available events.
417     @return   Array   Containing $this->get_event_info() for all available events.
418    */
419   static function get_event_types($type)
420   {
421     global $class_mapping,$config;
422     $list = array();
423     $list['BY_CLASS']  = array();
424     $list['TRIGGERED'] = array();
425     $list['SCHEDULED'] = array();
426     $list['QUEUED']    = array();
428     foreach($class_mapping as $name => $path){
429       if(preg_match("/^DaemonEvent_/",$name)){
430         $tmp  = new $name($config);
431         if($tmp->visible_for & $type){
432           $evt  = $tmp->get_event_info();
433           $list['BY_CLASS'][$name]                      = $evt;
434           if(isset($evt['s_Trigger_Action'])){
435             $list['TRIGGERED'][$name] = $evt;
436             $list['QUEUED'][$evt['s_Trigger_Action']] = $name;
437           }
438           if(isset($evt['s_Schedule_Action'])){
439             $list['SCHEDULED'][$name] = $evt;
440             $list['QUEUED'][$evt['s_Schedule_Action']] = $name;
441           }
442           if(isset($evt['s_Queued_Action'])){
443             $list['QUEUED'][$evt['s_Queued_Action']] = $name;
444           }
445         }
446       }
447     }
448     return($list);
449   }
452   /*! \brief  Returns TRUE if this event is new. (Not edited)
453     @return Boolean TRUE if new, else FALSE.
454    */
455   public function is_new()
456   {
457     return($this->is_new);
458   }
461   /*! \brief  Returns the event tag to schedule a new action 
462     @param    Returns the event e.g. 'job_trigger_action_wake'
463    */
464   public function get_schedule_action()
465   {
466     return($this->s_Schedule_Action);
467   }
470   /*! \brief  Returns the event tag to schedule a new action 
471     @param    Returns the event e.g. 'trigger_action_wake'
472    */
473   public function get_trigger_action()
474   {
475     return($this->s_Trigger_Action);
476   }
479   /*! brief  Returns an array containig all attributes \
480     That should be written.
481     @return Array e.g. 'status' => 'bla blub'  
482    */ 
483   public function save()
484   {
485     $ret = array();
486     foreach($this->attributes as $attr){
487       $ret[$attr] = $this->$attr;
488     }
489     if($this->mode == SCHEDULED_EVENT){
490       $ret['timestamp'] = $this->_timestamp_to_event($this->timestamp);
491     }elseif(isset($ret['timestamp'])){
492       unset($ret['timestamp']);
493     }
495     if($this-> activate_periodical_job){
496       $ret['periodic'][$this->periodType] = $this->periodValue; 
497     }
498     return($ret);
499   }
502   /*! \brief  Returns the event targets
503     @return Array  All selected targets.
504    */ 
505   public function get_targets()
506   {
507     return($this->a_targets);
508   }
511   /*! \brief  Returns the event timestamp in GOsa daemon format. 
512     @return Returns the event timestamp (20081231120000)
513    */
514   public function get_timestamp($si_type = TRUE)
515   {
516     if($si_type){
517       return($this->_timestamp_to_event($this->timestamp));
518     }else{
519       return($this->timestamp);
520     }
521   }
524   /*! \brief  Returns the event ID
525     @return Returns the event ID
526    */
527   public function get_id()
528   {
529     if($this->is_new){
530       return(-1);
531     }else{
532       return($this->data['ID']);
533     }
534   }
537   /*! \brief Add a target MAC address 
538       @param Array A List of all target that should be added.
539    */
540   public function set_timestamp($stamp) 
541   {
542     $this->timestamp = $stamp;
543   }
546   /*! \brief Add a target MAC address 
547       @param Array A List of all target that should be added.
548    */
549   public function add_targets($targets) 
550   {
551     foreach($targets as $target){
552       $this->a_targets[] = $target;
553     }
554   }
556   public function check()
557   {
558     return(array());
559   }
562   /*! \brief Update a class variable from outside 
563    */
564   public function set_value($name,$value)
565   {
566     $name = strtolower($name);
567     if(isset($this->$name) && in_array($name,$this->attributes)){
568       $this->$name = $value;
569     }
570   }
573 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
574 ?>