Code

Skip action if nothing is selected.
[gosa.git] / gosa-core / plugins / addons / gotomasses / class_gotomasses.inc
index e727422aa43d8e71e0249544986133cbe3029fea..87753c53fb942758a89211b9becf80f40146c3aa 100644 (file)
@@ -19,12 +19,15 @@ class gotomasses extends plugin
   var $sort_by  = "QueuePosition";
   var $sort_dir = "up";
 
+  var $range    = 25;
+  var $start    = 0;
+
   function gotomasses(&$config, $dn= NULL)
   {
     /* Include config object */
     $this->config= &$config;
     $this->divlist = new divListMasses($this->config,$this);
-    $this->o_queue = new gosaSupportDaemon("10.3.67.111","20081","secret-gosa-password",TRUE,0.2);
+    $this->o_queue = new gosaSupportDaemon("10.3.67.111","20081","secret-gosa-password",TRUE,10);
   }
 
 
@@ -35,18 +38,13 @@ class gotomasses extends plugin
     /************
      * Handle posts 
      ************/
-
+    
     $s_entry = $s_action = "";
     $arr = array( 
-        "/^prio_top_/"      => "prio_top",
-        "/^prio_increase_/" => "prio_increase",
-        "/^prio_decrease_/" => "prio_decrease",
-        "/^prio_bottom_/"   => "prio_bottom",
-
-        "/^multiple_prio_top_/"      => "mprio_top",
-        "/^multiple_prio_increase_/" => "mprio_increase",
-        "/^multiple_prio_decrease_/" => "mprio_decrease",
-        "/^multiple_prio_bottom_/"   => "mprio_bottom",
+        "/^stop_/"      => "stop",
+        "/^stop_all/"  => "stop_all",
+        "/^start_/"      => "start",
+        "/^start_all/"  => "start_all",
 
         "/^edit_task_/"             =>  "edit",
         "/^remove_task_/"           =>  "remove",
@@ -74,29 +72,13 @@ class gotomasses extends plugin
      * Handle Priority modifications  
      ************/
 
-    if(preg_match("/^mprio_/",$s_action) || preg_match("/^prio_/",$s_action)){
+    if(preg_match("/^start/",$s_action) || preg_match("/^stop/",$s_action)){
 
       switch($s_action){
-        case 'mprio_top'       : foreach($this->list_get_selected_items() as $id ){
-                                   $this->o_queue->max_entry_priority($id);
-                                 }
-                                 break;
-        case 'mprio_increase'  : foreach($this->list_get_selected_items() as $id ){
-                                   $this->o_queue->increase_entry_priority($id);
-                                 }
-                                 break;
-        case 'mprio_decrease'  : foreach($this->list_get_selected_items() as $id ){
-                                   $this->o_queue->decrease_entry_priority($id);
-                                 }
-                                 break;
-        case 'mprio_bottom'    : foreach($this->list_get_selected_items() as $id ){
-                                   $this->o_queue->min_entry_priority($id);
-                                 }
-                                 break;
-        case 'prio_top'       : $this->o_queue->max_entry_priority($s_entry);break;
-        case 'prio_increase'  : $this->o_queue->increase_entry_priority($s_entry);break;
-        case 'prio_decrease'  : $this->o_queue->decrease_entry_priority($s_entry);break;
-        case 'prio_bottom'    : $this->o_queue->min_entry_priority($s_entry);break;
+        case 'start_all'   : $this->start_queue_entries($this->list_get_selected_items());break;
+        case 'start'       : $this->start_queue_entries(array($s_entry));break; 
+        case 'stop_all'    : $this->stop_queue_entries ($this->list_get_selected_items());break;
+        case 'stop'        : $this->stop_queue_entries (array($s_entry));break; 
         default : trigger_error("Undefined priority setting used.");
       }
       if($this->o_queue->is_error()){
@@ -120,29 +102,26 @@ class gotomasses extends plugin
         }else{
           $ids = $this->list_get_selected_items();
         }
-
-        $this->ids_to_remove = $ids;
-        $tmp = "";
-        foreach($this->ids_to_remove as $key => $id){
-          if($this->o_queue->id_exists($id)){
-            $task = $this->o_queue->get_entry($id);
-            $tmp.= "\n".$task['5']." ".$task['3'];
-          }else{
-            unset($this->ids_to_remove[$key]);
+        if(count($ids)){
+          $this->ids_to_remove = $ids;
+          $ret = $this->o_queue->ids_exist($this->ids_to_remove);
+          $ret = $this->o_queue->get_entries_by_id($ret);
+
+          $tmp = "";
+          foreach($ret as $task){
+            $tmp.= "\n".$task['ID']." - ".$task['HEADERTAG']." ".$task['MACADDRESS'];
           }
+          $smarty->assign("multiple", TRUE); 
+          $smarty->assign("info",sprintf(_("Your are about to delete the following tasks: %s"),"<pre>".$tmp."</pre>"));
+          $this->current = $s_entry;
+          return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
         }
-        $smarty->assign("multiple", TRUE); 
-        $smarty->assign("info",sprintf(_("Your are about to delete the following tasks: %s"),"<pre>".$tmp."</pre>"));
-        $this->current = $s_entry;
-        return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
       }
     }
 
     /* Remove specified tasks */
     if(count($this->ids_to_remove) && isset($_POST['delete_multiple_confirm'])){
-      foreach($this->ids_to_remove as $id){
-        $this->o_queue->remove_entry($id);
-      }
+      $this->o_queue->remove_entries($this->ids_to_remove);
       $this->save();
     }
 
@@ -158,7 +137,7 @@ class gotomasses extends plugin
 
     /* Edit selected entry */
     if($s_action == "edit"){
-      $entry = $this->o_queue->get_entry($s_entry);
+      $entry = $this->o_queue->get_entry_by_id($s_entry);
       if($entry){
         $this->dialog = new goto_task($this->config,$this,$entry);
         $this->current = $s_entry;
@@ -188,7 +167,7 @@ class gotomasses extends plugin
       }else{  
 
         if($this->o_queue->id_exists($this->current)){
-          $this->o_queue->update_entry($this->current,$this->dialog->save());
+          $this->o_queue->update_entries(array($this->current),$this->dialog->save());
         }else{
           $tmp = $this->dialog->save();
           $tmp2= array();
@@ -220,45 +199,103 @@ class gotomasses extends plugin
      ************/
 
     $this->divlist->execute();
-    $entries = $this->get_queue_entries();
+    $entries = $this->get_queued_entries();
     $this->divlist->SetEntries($entries);
-    return($this->divlist->Draw());
+
+
+    $smarty = get_smarty();
+    $smarty->assign("start",$this->start);
+    $smarty->assign("start_real", ($this->start + 1));
+    $smarty->assign("ranges", array("10" => "10",
+                                    "20" => "20",
+                                    "25" => "25",
+                                    "50" => "50",
+                                    "100"=> "100",
+                                    "200"=> "200",
+                                    "9999" => "*"));
+
+    $count = $this->o_queue->number_of_queued_entries();
+    $smarty->assign("range_selector", range_selector($count, $this->start, $this->range,"range"));
+    $smarty->assign("range",$this->range);
+    $smarty->assign("div",$this->divlist->Draw());
+    return($smarty->fetch (get_template_path('gotomasses.tpl', TRUE, dirname(__FILE__))));
+  }
+
+
+  /*! \brief  Force queue job to be done as far as possible.
+   *  @return Boolean TRUE in case of success, else FALSE. 
+   */
+  private function start_queue_entries($ids)
+  {
+    if(!count($ids)){
+      return;
+    }
+  
+    $data = array("timestamp" => date("YmdHis"),
+                  "status"    => "-");
+    if(!$this->o_queue->update_entries($ids,$data)){
+      msg_dialog::display(_("Error"), sprintf(_("Could not update queue entry: %s"),$id) , ERROR_DIALOG);
+      return(FALSE);
+    }
+    return(TRUE);
+  }
+
+
+  /*! \brief Stops the specified queue entry from execution.
+   *  @return Boolean TRUE in case of success, else FALSE. 
+   */
+  private function stop_queue_entries($ids)
+  {
+    if(!count($ids)){
+      return;
+    }
+    $data = array("timestamp" => "20490101010101",
+                  "status"    => "Stoppped");
+    if(!$this->o_queue->update_entries($ids,$data)){
+      msg_dialog::display(_("Error"), sprintf(_("Could not update queue entry: %s"),$id) , ERROR_DIALOG);
+      return(FALSE);
+    }
+    return(TRUE);
   }
 
 
   /*! \brief  Request list of queued jobs.
    *  @return Returns an array of all queued jobs.
    */
-  function get_queue_entries()
+  function get_queued_entries()
   {
-    if(!$this->o_queue->load()){
+    $map = array(
+        "QueuePosition" => "id",
+        "Action"        => "status",
+        "TaskID"        => "headertag",
+        "TargetName"    => "macaddress",
+        "Schedule"      => "timestamp");
+
+    if(!isset($map[$this->sort_by])){
+      $sort = "id DESC";
+    }else{
+      $sort   = $map[$this->sort_by]; 
+      if($this->sort_dir == "up"){
+        $sort.= " ASC";
+      }else{
+        $sort.= " DESC";
+      }
+    }
+      
+    $start  = $this->start; 
+    $stop   = $this->range;
+    $entries = $this->o_queue->get_queued_entries($start,$stop,$sort);
+    if(!is_array($entries) || !isset($entries['XML']) || !is_array($entries['XML'])){
       msg_dialog::display(_("Error"), sprintf(_("Cannot load queue entries: %s"), "<br><br>".$this->o_queue->get_error()), ERROR_DIALOG);
       return(array());
     }
-    $tasks = array();
+
     $ret = array();
-    while($entry = $this->o_queue->fetch()){
+    foreach($entries['XML'] as $entry){
       $task = $entry['ID']; 
       $ret[]= $entry;
     }
-    $map = array("QueuePosition" => "ID",
-        "Action"        => "STATUS",
-        "TaskID"        => "HEADERTAG",
-        "TargetName"    => "MACADDRESS",
-        "Schedule"      => "TIMESTAMP");
-    $sort_tmp = array();
-    foreach($ret as $entry_id => $entry){
-      $sort_tmp[$entry_id] = $entry[$map[$this->sort_by]];
-    } 
-    natcasesort($sort_tmp);
-    $return = array();
-    foreach($sort_tmp as $entry_id => $value){
-      $return[] = $ret[$entry_id];
-    } 
-    if($this->sort_dir != "up"){
-      $return = array_reverse($return);
-    } 
-    return($return);
+    return($ret);
   }
 
 
@@ -279,6 +316,24 @@ class gotomasses extends plugin
       }
       $this->sort_by = $sort;
     }
+    if(isset($_POST['range']) && is_numeric($_POST['range'])){
+      $this->range = $_POST['range'];
+    }
+    if(isset($_GET['start'])){
+      $start = $_GET['start'];
+      if(is_numeric($start) || $start == 0){
+        $this->start = $start;
+      }
+    }
+
+    /* Check start stop and reset if necessary */
+    $count = $this->o_queue->number_of_queued_entries();
+    if($this->start >= $count){
+      $this->start = $count -1;
+    }
+    if($this->start < 0){
+      $this->start = 0;
+    }
   }