Code

Bugfix for #4721
[gosa.git] / trunk / gosa-plugins / goto / addons / goto / events / class_DaemonEvent.inc
index 575626165251341721be9b18f95388cc4bf8fef2..13f80c9dcbff83c2aa5ff3717b0206f652e17fd9 100644 (file)
@@ -51,7 +51,9 @@ class DaemonEvent
   protected $s_Schedule_Action = "";       // The deamon command name when newly created. (e.g. job_trigger_action_halt)
   protected $s_Trigger_Action= "";       // The deamon command name when edited. (e.g. trigger_action_halt)
   protected $s_Queued_Action= "";       // The deamon command name when edited. (e.g. trigger_action_halt)
-  protected $timestamp    = 0;        // Event execution time; 
+  public $timestamp    = 0;        // Event execution time; 
+  public $time_offset = 0;    // An offset when an event should start (normally not used)
+  public $concurrent_operations = 1; // Number of concurrent operations when a timestamp offset is used
   protected $id           = -1;       // The Table ID
   protected $status       = "unknown";// The current event status
   protected $is_new       = TRUE;     // Is TRUE if this is a new event
@@ -70,6 +72,12 @@ class DaemonEvent
   protected $visible_for          = HIDDEN_EVENT;
 
   protected $attributes           = array("timestamp");
+
+  protected $time_offset_used = FALSE;
+
+  /* Indicate that multiple events have to be created from this event */
+  public $multiple_events = FALSE;
+
  
   function set_type($type)
   {
@@ -154,11 +162,33 @@ class DaemonEvent
   }
 
 
+  public function get_time_offset_select()
+  {
+    $this->time_offset_used = TRUE;
+    $smarty = get_smarty();
+    
+    $opts_minutes = array();
+    $opts_operations = array();
+    foreach(range(0,120) as $i) {
+      $opts_minutes[$i] = $i;
+    }
+    foreach(range(1,100) as $i) {
+      $opts_operations[$i] = $i;
+    }
+
+    $smarty->assign('offset_minutes', $opts_minutes);
+    $smarty->assign('offset_operations', $opts_operations);
+    $smarty->assign('time_offset', $this->time_offset);
+    $smarty->assign('concurrent_operations', $this->concurrent_operations);
+    return($smarty->fetch(get_template_path('time_offset.tpl', TRUE, dirname(__FILE__))));
+  }
+
   /*! \brief  Returns HTML representation of a timestamp using <select> boxes. 
     @return Returns HTML content.
    */
   public function get_time_select()
   {
+    timezone::get_default_timezone();
     $this->time_select_used = TRUE;
 
     $smarty = get_smarty();
@@ -225,8 +255,22 @@ class DaemonEvent
     $this->target_list_used = TRUE;
     $divlist = new divSelectBox("EventTargets");
     foreach($this->a_targets as $key => $target){
+      $string = "";
+      if (is_array($target) && isset($target['cn'])) {
+          $string = $target['cn'];
+      }
+      if (is_array($target) && isset($target['ogroup'])) {
+        $title = sprintf(_("Added via object group '%s'"), $target['ogroup']);
+        $string .= sprintf("<img src='plugins/ogroups/images/select_ogroup.png' title=\"%s\">", $title);
+      }
+      if (is_array($target) && isset($target['mac'])) {
+        $string .= " - " . $target['mac'];
+      }else {
+        $string = $target;
+      }
+
       $divlist->AddEntry(array(
-            array("string"  => $target),
+            array("string"  => $string),
             array("string"  => "<input type='image' src='images/lists/trash.png' title='"._("Remove")."' name='del_target_".$key."'>",
               "attach"  => "style='width:20px; border-right:0px;'")
             ));
@@ -276,12 +320,32 @@ class DaemonEvent
       }
     }
 
+    if (isset($_POST['concurrent_operations'])) {
+        $this->concurrent_operations = $_POST['concurrent_operations'];
+    }
+    if (isset($_POST['time_offset'])) {
+        $this->time_offset = $_POST['time_offset'];
+    }
+    
+    if ($this->time_offset_used){
+        /* Check that multiple events makes sense at all (e.g. there are more targets
+         * then allowed concurrent operations */
+        if ($this->concurrent_operations != 0 && count($this->a_targets) > $this->concurrent_operations) {
+            if ($this->time_offset > 0) {
+                $this->multiple_events = TRUE;
+            }
+        }
+    }
+
     if($this->time_select_used){
       $time_stamp_values_found = TRUE;
       foreach(array("time_year","time_month","time_day","time_hour","time_minute","time_second") as $attr){
         $time_stamp_values_found &= isset($_POST[$attr]);
       }
       if($time_stamp_values_found){
+        /* Make sure the following conversion happens with the right timezone */
+        timezone::get_default_timezone();
+
         $this->timestamp = mktime(
             $_POST['time_hour'],
             $_POST['time_minute'],        
@@ -290,6 +354,7 @@ class DaemonEvent
             $_POST['time_day'],        
             $_POST['time_year']);
       }
+
     }
 
     if($this->target_list_used){
@@ -358,6 +423,65 @@ class DaemonEvent
   }
 
 
+  /*! \brief Save an event dialog and check if multiple events needs to be created */
+  static function save_event_dialog($dialog, $current, $o_queue, $config)
+  {
+    if(is_object($dialog)){
+      $dialog->save_object();
+      if($dialog->multiple_events) {
+        $event_type = get_class($dialog);
+        $targets = $dialog->get_targets();
+        $tmp_array[0] = $targets[0];
+        foreach ($targets as $current) {
+          $add_flag = 1;
+          foreach ($tmp_array as $tmp) {
+            if ($current["mac"]==$tmp["mac"]) {
+              $add_flag = 0; break;
+            }
+          }
+          if ($add_flag) $tmp_array[] = $current;
+        }
+        $targets = $tmp_array;
+        $timestamp = $dialog->timestamp;
+
+        $i = 1;
+        $count = count($targets);
+        while($i <= $count) {
+          $operations = $dialog->concurrent_operations;
+          $event = new $event_type($config);
+          $event->set_timestamp($timestamp);
+          $event->set_type(SCHEDULED_EVENT);
+
+          while($operations > 0) {
+            $i++;
+            $target = array_shift($targets);
+            $event->add_targets(array($target));
+            $operations--;
+          }
+
+          $event->save_object();
+          $event->get_targets();
+          if(!$o_queue->append($event)){
+            msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
+          }
+
+          # Calculate start time for the next run
+          $timestamp = $timestamp + ($dialog->time_offset*60);
+       }
+
+       return(array(FALSE, -1));
+     } else {
+       $dialog->save_object();
+       if(!$o_queue->append($dialog)){
+        msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
+        return(array($dialog, $current));
+       }else{
+        return(array(FALSE, -1));
+       } 
+     }
+   }
+}
+
   /*! \brief  Returns a complete list of all available events.
     @return   Array   Containing $this->get_event_info() for all available events.
    */
@@ -431,11 +555,16 @@ class DaemonEvent
     foreach($this->attributes as $attr){
       $ret[$attr] = $this->$attr;
     }
-    if($this->mode == SCHEDULED_EVENT){
-      $ret['timestamp'] = $this->_timestamp_to_event($this->timestamp);
-    }elseif(isset($ret['timestamp'])){
-      unset($ret['timestamp']);
+
+    if(!isset($ret['timestamp'])){
+      $ret['timestamp'] = time();
+    }
+
+    # Check if timestamp is in gosa-si-time-format 
+    if(!tests::is_gosa_si_time($ret['timestamp'])){ 
+      $ret['timestamp'] = $this->_timestamp_to_event($this->timestamp); 
     }
+
     return($ret);
   }