f711b7b2331355beab5fd5ad6eca0e749bdf2781
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_listing = NULL; // The listing 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");
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 = explode("_",$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 }
122 // Prepare lists
123 $this->targetList = new sortableListing();
124 $this->targetList->setDeleteable(true);
125 $this->targetList->setInstantDelete(false);
126 $this->targetList->setEditable(false);
127 $this->targetList->setWidth("100%");
128 $this->targetList->setHeight("200px");
129 $this->targetList->setColspecs(array('*'));
130 $this->targetList->setHeader(array(_("Mac")));
131 $this->targetList->setDefaultSortColumn(0);
132 $this->targetList->setAcl('rwcdm');
133 }
136 /*! \brief Create the HTML output for the plugin.
137 @return String The generated HTML output.
138 */
139 public function execute()
140 {
141 $this->time_select_used = FALSE;
142 $this->target_list_used = FALSE;
144 $str = "<h3>"._("This job has no template!")."</h3>";
145 $str.= "<hr>";
146 $str.= "<div class='plugin-actions'>
147 <button type='submit' name='abort_event_dialog'>".msgPool::cancelButton()."</button> </div>";
148 return($str);
149 }
151 /*! \brief Returns the plugin header, displayed in the template.
152 @return String HTML header part.
153 */
154 public function get_header()
155 {
156 if($this->target_add_list_used){
157 return("");
158 }
159 $str = "<h3>".sprintf(_("Create '%s' job"),$this->s_Event_Name)."</h3>";
160 return($str);
161 }
164 /*! \brief Returns the plugin footer (save cancel), displayed in the template.
165 @return String HTML footer part.
166 */
167 public function get_footer()
168 {
169 if($this->target_add_list_used){
170 return("");
171 }
172 $str = "<hr>";
173 $str.= "<div class='plugin-actions'>
174 <button type='submit' name='save_event_dialog'>".msgPool::saveButton()."</button>
175 <button type='submit' name='abort_event_dialog'>".msgPool::cancelButton()."</button>
176 </div>";
177 return($str);
178 }
181 /*! \brief Returns HTML representation of a timestamp using <select> boxes.
182 @return Returns HTML content.
183 */
184 public function get_time_select()
185 {
186 $this->time_select_used = TRUE;
188 $smarty = get_smarty();
190 $year = date("Y",$this->timestamp);
191 $month = date("m",$this->timestamp);
192 $day = date("d",$this->timestamp);
194 $hour = date("H",$this->timestamp);
195 $minute = date("i",$this->timestamp);
196 $second = date("s",$this->timestamp);
198 $years = array();
199 for($i = date("Y",time()); $i <= 2037 ;$i ++){
200 $years[$i] = $i;
201 }
202 $months = array();
203 for($i = 1; $i <= 12; $i ++){
204 $e = str_pad($i,2,"0",STR_PAD_LEFT);
205 $months[$e] = $e;
206 }
207 $days = array();
208 for($i = 1; $i <= cal_days_in_month(CAL_GREGORIAN,$month,$year); $i ++){
209 $e = str_pad($i,2,"0",STR_PAD_LEFT);
210 $days[$e] = $e;
211 }
212 $hours = array();
213 for($i = 0; $i < 24; $i ++){
214 $e = str_pad($i,2,"0",STR_PAD_LEFT);
215 $hours[$e] = $e;
216 }
217 $minutes = array();
218 for($i = 0; $i < 60; $i ++){
219 $e = str_pad($i,2,"0",STR_PAD_LEFT);
220 $minutes[$e] = $e;
221 }
222 $seconds = array();
223 for($i = 0; $i < 60; $i ++){
224 $e = str_pad($i,2,"0",STR_PAD_LEFT);
225 $seconds[$e] = $e;
226 }
228 $periodTypes = array(
229 "minutes" => _("Minutes"),
230 "hours" => _("Hours"),
231 "days" => _("Days"),
232 "weeks" => _("Weeks"),
233 "months" => _("Months"));
236 $smarty->assign("periodTypes", set_post($periodTypes));
237 $smarty->assign("periodType", set_post($this->periodType));
238 $smarty->assign("periodValue", set_post($this->periodValue));
239 $smarty->assign("activate_periodical_job", set_post($this-> activate_periodical_job));
241 $smarty->assign("years", set_post($years));
242 $smarty->assign("months", set_post($months));
243 $smarty->assign("days", set_post($days));
244 $smarty->assign("hours", set_post($hours));
245 $smarty->assign("minutes", set_post($minutes));
246 $smarty->assign("seconds", set_post($seconds));
247 $smarty->assign("time_year", set_post($year));
248 $smarty->assign("time_month", set_post($month));
249 $smarty->assign("time_day", set_post($day));
250 $smarty->assign("time_hour", set_post($hour));
251 $smarty->assign("time_minute", set_post($minute));
252 $smarty->assign("time_second", set_post($second));
253 return($smarty->fetch(get_template_path('timestamp_select.tpl', TRUE, dirname(__FILE__))));
254 }
257 /*! \brief HTML representation of all currently assigned targets.
258 @return String Returns a listbox with all used targets.
259 */
260 public function get_target_list()
261 {
262 $data = $lData = array();
264 $this->target_list_used = TRUE;
265 foreach($this->a_targets as $key => $target){
266 $data[$key] = $target;
267 $lData[$key] = array('data' => array($target));
268 }
269 $this->targetList->setListData($data, $lData);
270 $this->targetList->update();
271 $list_footer = "<button type='submit' name='open_target_list'>"._("Add")."</button>";
272 return($this->targetList->render().$list_footer);
273 }
276 /*! \brief Returns HTML content, displaying a dialog which allows to add new targets.
277 @return String HTML content. (EventAddSystemDialog)
278 */
279 public function get_target_add_list()
280 {
281 $this->target_add_list_used = TRUE;
283 if($this->target_listing == NULL){
284 $this->target_listing = new EventAddSystemDialog($this->config,$this);
285 }
287 $smarty = get_smarty();
288 $smarty->assign("listing",$this->target_listing->execute());
289 return($smarty->fetch(get_template_path('target_list.tpl', TRUE, dirname(__FILE__))));
290 }
293 /*! \brief Handles all posted HTML data, including target add,remove...
294 */
295 public function save_object()
296 {
297 if(isset($_POST['open_target_list'])){
298 $this->target_add_list_used =TRUE;
299 }
300 if($this->target_listing != NULL){
301 $this->target_listing->save_object();
302 }
303 if($this->target_add_list_used){
304 if(isset($_POST['abort_target_dialog'])){
305 $this->target_add_list_used =FALSE;
306 $this->target_listing = NULL;
307 }
308 if(isset($_POST['save_target_dialog'])){
309 $this->target_add_list_used =FALSE;
310 $this->add_targets($this->target_listing->get_selected_targets());
311 $this->target_listing = NULL;
312 }
313 }
315 if($this->time_select_used){
316 $time_stamp_values_found = TRUE;
317 foreach(array("time_year","time_month","time_day","time_hour","time_minute","time_second") as $attr){
318 $time_stamp_values_found &= isset($_POST[$attr]);
319 }
320 if($time_stamp_values_found){
321 $this->timestamp = mktime(
322 get_post('time_hour'),
323 get_post('time_minute'),
324 get_post('time_second'),
325 get_post('time_month'),
326 get_post('time_day'),
327 get_post('time_year'));
329 if(isset($_POST['periodValue'])) $this->periodValue = get_post('periodValue');
330 if(isset($_POST['periodType'])) $this->periodType = get_post('periodType');
332 if(isset($_POST['activate_periodical_job'])){
333 $this->activate_periodical_job = TRUE;
334 }else{
335 $this->activate_periodical_job = FALSE;
336 }
337 }
338 }
340 $this->targetList->save_object();
341 $action = $this->targetList->getAction();
342 if($action['action'] == 'delete'){
343 $id = $this->targetList->getKey($action['targets'][0]);
344 unset($this->a_targets[$id]);
345 }
346 }
349 /*! \brief Converts a daemon timestamp into an unix timestamp. \
350 e.g. 20080101125959 -> 1199188799
351 @param A daemon timestamp YYYYddmmHHiiss
352 @return Integer A unix timestamp.
353 */
354 public function _event_to_timestamp($str)
355 {
356 return(strtotime($str));
357 }
360 /*! \brief Converts a unix timestamp in to a gosa-si timestamp. \
361 e.g. 1199188799 -> 20080101125959
362 @param A unix timestamp (e.g. 1199188799)
363 @return Integer A daemon timestamp (e.g. 20080101125959).
364 */
365 public function _timestamp_to_event($stamp)
366 {
367 return(date("YmdHis",$stamp));
368 }
371 /*! \brief Returns event information, like menu strings, images ...
372 @return Array Event informations.
373 */
374 public function get_event_info()
375 {
376 $data =array();
377 $data['CLASS_NAME'] = get_class($this);
378 $data['s_Menu_Name'] = $this->s_Menu_Name;
379 $data['s_Event_Name'] = $this->s_Event_Name;
380 foreach(array("s_Queued_Action","s_Schedule_Action","s_Trigger_Action") as $attr){
381 if(!empty($this->$attr)){
382 $data[$attr] = $this->$attr;
383 }
384 }
385 $data['MenuImage'] = "<img src='".$this->s_Menu_Image."' alt='".$this->s_Menu_Name."' border='0' class='center'>";
386 $data['ListImage'] = "<img src='".$this->s_List_Image."' title='".$this->s_Event_Name."'
387 alt='".$this->s_Event_Name."' border='0' class='center'>";
388 return($data);
389 }
392 /*! \brief Check if we have opened the target add dialog.
393 @return Boolean TRUE if we have opened the target add dialog else FALSE.
394 */
395 protected function is_target_list_open()
396 {
397 return($this->target_add_list_used);
398 }
401 /*! \brief Returns a complete list of all available events.
402 @return Array Containing $this->get_event_info() for all available events.
403 */
404 static function get_event_types_by_category($categories)
405 {
406 $types= array();
408 foreach ($categories as $category) {
409 if (preg_match('/^users$/', $category)) {
410 $types= array_merge($types, DaemonEvent::get_event_types(USER_EVENT));
411 }
412 if (preg_match('/^systems$/', $category)) {
413 $types= array_merge($types, DaemonEvent::get_event_types(SYSTEM_EVENT));
414 }
415 }
417 return $types;
418 }
421 /*! \brief Returns a complete list of all available events.
422 @return Array Containing $this->get_event_info() for all available events.
423 */
424 static function get_event_types($type)
425 {
426 global $class_mapping,$config;
427 $list = array();
428 $list['BY_CLASS'] = array();
429 $list['TRIGGERED'] = array();
430 $list['SCHEDULED'] = array();
431 $list['QUEUED'] = array();
433 foreach($class_mapping as $name => $path){
434 if(preg_match("/^DaemonEvent_/",$name)){
435 $tmp = new $name($config);
436 if($tmp->visible_for & $type){
437 $evt = $tmp->get_event_info();
438 $list['BY_CLASS'][$name] = $evt;
439 if(isset($evt['s_Trigger_Action'])){
440 $list['TRIGGERED'][$name] = $evt;
441 $list['QUEUED'][$evt['s_Trigger_Action']] = $name;
442 }
443 if(isset($evt['s_Schedule_Action'])){
444 $list['SCHEDULED'][$name] = $evt;
445 $list['QUEUED'][$evt['s_Schedule_Action']] = $name;
446 }
447 if(isset($evt['s_Queued_Action'])){
448 $list['QUEUED'][$evt['s_Queued_Action']] = $name;
449 }
450 }
451 }
452 }
453 return($list);
454 }
457 /*! \brief Returns TRUE if this event is new. (Not edited)
458 @return Boolean TRUE if new, else FALSE.
459 */
460 public function is_new()
461 {
462 return($this->is_new);
463 }
466 /*! \brief Returns the event tag to schedule a new action
467 @param Returns the event e.g. 'job_trigger_action_wake'
468 */
469 public function get_schedule_action()
470 {
471 return($this->s_Schedule_Action);
472 }
475 /*! \brief Returns the event tag to schedule a new action
476 @param Returns the event e.g. 'trigger_action_wake'
477 */
478 public function get_trigger_action()
479 {
480 return($this->s_Trigger_Action);
481 }
484 /*! brief Returns an array containig all attributes \
485 That should be written.
486 @return Array e.g. 'status' => 'bla blub'
487 */
488 public function save()
489 {
490 $ret = array();
491 foreach($this->attributes as $attr){
492 $ret[$attr] = $this->$attr;
493 }
494 if($this->mode == SCHEDULED_EVENT){
495 $ret['timestamp'] = $this->_timestamp_to_event($this->timestamp);
496 }elseif(isset($ret['timestamp'])){
497 unset($ret['timestamp']);
498 }
500 if(isset($ret['periodic'])){
501 unset($ret['periodic']);
502 }
503 if($this->activate_periodical_job){
504 $ret['periodic']= $this->periodValue."_".$this->periodType;
505 }
507 return($ret);
508 }
511 /*! \brief Returns the event targets
512 @return Array All selected targets.
513 */
514 public function get_targets()
515 {
516 return($this->a_targets);
517 }
520 /*! \brief Returns the event timestamp in GOsa daemon format.
521 @return Returns the event timestamp (20081231120000)
522 */
523 public function get_timestamp($si_type = TRUE)
524 {
525 if($si_type){
526 return($this->_timestamp_to_event($this->timestamp));
527 }else{
528 return($this->timestamp);
529 }
530 }
533 /*! \brief Returns the event ID
534 @return Returns the event ID
535 */
536 public function get_id()
537 {
538 if($this->is_new){
539 return(-1);
540 }else{
541 return($this->data['ID']);
542 }
543 }
546 /*! \brief Add a target MAC address
547 @param Array A List of all target that should be added.
548 */
549 public function set_timestamp($stamp)
550 {
551 $this->timestamp = $stamp;
552 }
555 /*! \brief Add a target MAC address
556 @param Array A List of all target that should be added.
557 */
558 public function add_targets($targets)
559 {
560 foreach($targets as $target){
561 $this->a_targets[] = $target;
562 }
563 }
565 public function check()
566 {
567 return(array());
568 }
571 /*! \brief Update a class variable from outside
572 */
573 public function set_value($name,$value)
574 {
575 $name = strtolower($name);
576 if(isset($this->$name) && in_array($name,$this->attributes)){
577 $this->$name = $value;
578 }
579 }
580 }
582 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
583 ?>