Code

Fixed post handling.
[gosa.git] / gosa-plugins / goto / addons / goto / events / class_DaemonEvent.inc
index 3bfd30e8fd4becf5f4b250236c0d57787d9c6023..6e56949ad80c86255a9477c0bdfd687876dc0c27 100644 (file)
@@ -59,7 +59,7 @@ class DaemonEvent
   protected $mode         = SCHEDULED_EVENT; // Default action is sheduled.
 
   /* Sub dialog hanlding */
-  protected $target_divlist       = NULL;     // The divlist used by the target add dialog
+  protected $target_listing       = NULL;     // The listing used by the target add dialog
   protected $target_add_list_used = FALSE;    // Indicates that the target add list was used.
   protected $time_select_used     = FALSE;    // Indicates that we have used the timestamp select boxes.
   protected $target_list_used     = FALSE;    // Target list was diaplayed?
@@ -68,6 +68,12 @@ class DaemonEvent
   protected $server_list          = array();  // Used as cache in the target list.
 
   protected $visible_for          = HIDDEN_EVENT;
+
+  protected $periodType           = "days";
+  protected $periodValue          = 7;
+  protected $activate_periodical_job = FALSE;
+
+  protected $attributes           = array("timestamp");
  
   function set_type($type)
   {
@@ -103,6 +109,27 @@ class DaemonEvent
         $this->timestamp = $this->_event_to_timestamp($data['TIMESTAMP']);
       }
     }
+
+    if(isset($data['PERIODIC']) && !preg_match("/none/i",$data['PERIODIC'])){
+      $tmp = explode("_",$data['PERIODIC']);
+      if(count($tmp) == 2){
+        $this->activate_periodical_job = TRUE;
+        $this->periodValue = $tmp[0];
+        $this->periodType = $tmp[1];
+      }
+    }
+
+    // Prepare lists
+    $this->targetList = new sortableListing();
+    $this->targetList->setDeleteable(true);
+    $this->targetList->setInstantDelete(false);
+    $this->targetList->setEditable(false);
+    $this->targetList->setWidth("100%");
+    $this->targetList->setHeight("200px");
+    $this->targetList->setColspecs(array('*'));
+    $this->targetList->setHeader(array(_("Mac")));
+    $this->targetList->setDefaultSortColumn(0);
+    $this->targetList->setAcl('rwcdm');
   }
 
 
@@ -114,11 +141,10 @@ class DaemonEvent
     $this->time_select_used = FALSE;
     $this->target_list_used = FALSE;
 
-    $str = "<h2>"._("This job has no template.")."</h2>";
-    $str.= "<p class='seperator'></p>";
-    $str.= "<div style='text-align:right;width:100%;padding:3px;'>
-      <input type='submit' name='abort_event_dialog' value='".msgPool::cancelButton()."'>
-      </div>";
+    $str = "<h3>"._("This job has no template!")."</h3>";
+    $str.= "<hr>";
+    $str.= "<div class='plugin-actions'>
+      <button type='submit' name='abort_event_dialog'>".msgPool::cancelButton()."</button> </div>";
     return($str);
   }
 
@@ -130,7 +156,7 @@ class DaemonEvent
     if($this->target_add_list_used){
       return("");
     }
-    $str = "<h2>".sprintf(_("Create '%s' job"),$this->s_Event_Name)."</h2>";
+    $str = "<h3>".sprintf(_("Create '%s' job"),$this->s_Event_Name)."</h3>";
     return($str);
   }
 
@@ -143,10 +169,10 @@ class DaemonEvent
     if($this->target_add_list_used){
       return("");
     }
-    $str = "<p class='seperator'></p>";
-    $str.= "<div style='text-align:right;width:99%;padding:5px;'>
-      <input type='submit' name='save_event_dialog' value='".msgPool::saveButton()."'>&nbsp;
-    <input type='submit' name='abort_event_dialog' value='".msgPool::cancelButton()."'>
+    $str = "<hr>";
+    $str.= "<div class='plugin-actions'>
+        <button type='submit' name='save_event_dialog'>".msgPool::saveButton()."</button>&nbsp;
+        <button type='submit' name='abort_event_dialog'>".msgPool::cancelButton()."</button>
       </div>";
     return($str);
   }
@@ -199,6 +225,19 @@ class DaemonEvent
       $seconds[$e] = $e;
     }
 
+    $periodTypes = array(
+        "minutes" => _("Minutes"),
+        "hours"   => _("Hours"),
+        "days"    => _("Days"),
+        "weeks"   => _("Weeks"),
+        "months"  => _("Months"));
+    
+
+    $smarty->assign("periodTypes", $periodTypes);
+    $smarty->assign("periodType", $this->periodType);
+    $smarty->assign("periodValue", $this->periodValue);
+    $smarty->assign("activate_periodical_job", $this-> activate_periodical_job);
+
     $smarty->assign("years", $years);
     $smarty->assign("months", $months);
     $smarty->assign("days", $days);
@@ -215,39 +254,38 @@ class DaemonEvent
   } 
 
 
-  /*! \brief  HTML representation of all currently assigned targets using (divSelectBox).
+  /*! \brief  HTML representation of all currently assigned targets.
     @return String Returns a listbox with all used targets.
    */
   public function get_target_list()
   {
+    $data = $lData = array();
+
     $this->target_list_used = TRUE;
-    $divlist = new divSelectBox("EventTargets");
     foreach($this->a_targets as $key => $target){
-      $divlist->AddEntry(array(
-            array("string"  => $target),
-            array("string"  => "<input type='image' src='images/lists/trash.png' title='"._("Remove")."' name='del_target_".$key."'>",
-              "attach"  => "style='width:20px; border-right:0px;'")
-            ));
+      $data[$key] = $target;
+      $lData[$key] = array('data' => array($target));
     }
-    $list_footer = "<input type='submit' name='open_target_list' value='"._("Add")."'>";
-    return($divlist->DrawList().$list_footer);
+    $this->targetList->setListData($data, $lData);
+    $this->targetList->update();
+    $list_footer = "<button type='submit' name='open_target_list'>"._("Add")."</button>";
+    return($this->targetList->render().$list_footer);
   }
 
 
   /*! \brief  Returns HTML content, displaying a dialog which allows to add new targets.
-    @return String HTML content. (EventTargetAddList)
+    @return String HTML content. (EventAddSystemDialog)
    */
   public function get_target_add_list()
   {
     $this->target_add_list_used = TRUE;
 
-    if($this->target_divlist == NULL){ 
-      $this->target_divlist = new EventTargetAddList($this->config,$this);
+    if($this->target_listing == NULL){ 
+      $this->target_listing = new EventAddSystemDialog($this->config,$this);
     }
-    $this->target_divlist->execute();
 
     $smarty = get_smarty();
-    $smarty->assign("divlist",$this->target_divlist->Draw());
+    $smarty->assign("listing",$this->target_listing->execute());
     return($smarty->fetch(get_template_path('target_list.tpl', TRUE, dirname(__FILE__))));
   }
 
@@ -259,18 +297,18 @@ class DaemonEvent
     if(isset($_POST['open_target_list'])){
       $this->target_add_list_used =TRUE;
     }
-    if($this->target_divlist != NULL){
-      $this->target_divlist->save_object();
+    if($this->target_listing != NULL){
+      $this->target_listing->save_object();
     }
     if($this->target_add_list_used){
       if(isset($_POST['abort_target_dialog'])){
         $this->target_add_list_used =FALSE;
-        $this->target_divlist = NULL;
+        $this->target_listing = NULL;
       }
       if(isset($_POST['save_target_dialog'])){
         $this->target_add_list_used =FALSE;
-        $this->add_targets($this->target_divlist->get_selected_targets());
-        $this->target_divlist = NULL;
+        $this->add_targets($this->target_listing->get_selected_targets());
+        $this->target_listing = NULL;
       }
     }
 
@@ -287,19 +325,23 @@ class DaemonEvent
             $_POST['time_month'],        
             $_POST['time_day'],        
             $_POST['time_year']);
-      }
-    }
 
-    if($this->target_list_used){
-      foreach($_POST as $name => $value){
-        if(preg_match("/^del_target_/",$name)){
-          $id = preg_replace("/^del_target_([0-9]*)_.*/","\\1",$name);
-          if(isset($this->a_targets[$id])){
-            unset($this->a_targets[$id]);
-          }
-          break; 
+        if(isset($_POST['periodValue'])) $this->periodValue = get_post('periodValue');
+        if(isset($_POST['periodType'])) $this->periodType = get_post('periodType');
+
+        if(isset($_POST['activate_periodical_job'])){
+          $this->activate_periodical_job = TRUE;
+        }else{
+          $this->activate_periodical_job = FALSE;
         }
       }
+    }
+
+    $this->targetList->save_object();
+    $action = $this->targetList->getAction();
+    if($action['action'] == 'delete'){
+        $id = $this->targetList->getKey($action['targets'][0]);
+        unset($this->a_targets[$id]);
     } 
   }
 
@@ -356,6 +398,26 @@ class DaemonEvent
   }
 
 
+  /*! \brief  Returns a complete list of all available events.
+    @return   Array   Containing $this->get_event_info() for all available events.
+   */
+  static function get_event_types_by_category($categories)
+  {
+    $types= array();
+
+    foreach ($categories as $category) {
+      if (preg_match('/^users$/', $category)) {
+        $types= array_merge($types, DaemonEvent::get_event_types(USER_EVENT));
+      }
+      if (preg_match('/^systems$/', $category)) {
+        $types= array_merge($types, DaemonEvent::get_event_types(SYSTEM_EVENT));
+      }
+    }
+
+    return $types;
+  }
+
+
   /*! \brief  Returns a complete list of all available events.
     @return   Array   Containing $this->get_event_info() for all available events.
    */
@@ -409,7 +471,7 @@ class DaemonEvent
     return($this->s_Schedule_Action);
   }
 
-  //
+
   /*! \brief  Returns the event tag to schedule a new action 
     @param    Returns the event e.g. 'trigger_action_wake'
    */
@@ -418,13 +480,31 @@ class DaemonEvent
     return($this->s_Trigger_Action);
   }
 
+
   /*! brief  Returns an array containig all attributes \
     That should be written.
     @return Array e.g. 'status' => 'bla blub'  
    */ 
   public function save()
   {
-    return(array("timestamp" => $this->_timestamp_to_event($this->timestamp)));
+    $ret = array();
+    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['periodic'])){
+      unset($ret['periodic']);
+    }
+    if($this->activate_periodical_job){
+      $ret['periodic']= $this->periodValue."_".$this->periodType;
+    }
+
+    return($ret);
   }
 
 
@@ -486,6 +566,17 @@ class DaemonEvent
   {
     return(array());
   }
+
+
+  /*! \brief Update a class variable from outside 
+   */
+  public function set_value($name,$value)
+  {
+    $name = strtolower($name);
+    if(isset($this->$name) && in_array($name,$this->attributes)){
+      $this->$name = $value;
+    }
+  }
 }
 
 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler: