Code

Skip action if nothing is selected.
[gosa.git] / gosa-core / plugins / addons / gotomasses / class_gotomasses.inc
index 3f85caf56002b2549d6f982edbe2746bf57af612..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,2);
+    $this->o_queue = new gosaSupportDaemon("10.3.67.111","20081","secret-gosa-password",TRUE,10);
   }
 
 
@@ -35,12 +38,12 @@ class gotomasses extends plugin
     /************
      * Handle posts 
      ************/
-
+    
     $s_entry = $s_action = "";
     $arr = array( 
-        "/^stop/"      => "stop",
+        "/^stop_/"      => "stop",
         "/^stop_all/"  => "stop_all",
-        "/^start/"      => "start",
+        "/^start_/"      => "start",
         "/^start_all/"  => "start_all",
 
         "/^edit_task_/"             =>  "edit",
@@ -58,8 +61,6 @@ class gotomasses extends plugin
       }
     }
 
-    #print_a(array($s_entry,$s_action));;
-
     /* Edit posted from list link */
     if(isset($_GET['act']) && $_GET['act'] == "edit" && isset($_GET['id']) && isset($this->tasks[$_GET['id']])){
       $s_action = "edit";
@@ -71,19 +72,13 @@ class gotomasses extends plugin
      * Handle Priority modifications  
      ************/
 
-    if(preg_match("/^start_/",$s_action) || preg_match("/^stop_/",$s_action)){
+    if(preg_match("/^start/",$s_action) || preg_match("/^stop/",$s_action)){
 
       switch($s_action){
-        case 'start_all'   :foreach($this->list_get_selected_items() as $id){
-                                $this->start_queue_entry($id);
-                            }
-                            break;
-        case 'start'       :$this->start_queue_entry($s_entry);break;
-        case 'stop_all'    :foreach($this->list_get_selected_items() as $id){
-                                $this->stop_queue_entry($id);
-                            }
-                            break;
-        case 'stop'        : $this->stop_queue_entry($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()){
@@ -107,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['HEADERTAG']." ".$task['MACADDRESS'];
-          }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();
     }
 
@@ -145,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;
@@ -175,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();
@@ -209,47 +201,61 @@ class gotomasses extends plugin
     $this->divlist->execute();
     $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_entry($id)
+  private function start_queue_entries($ids)
   {
-    $attr = $this->o_queue->get_entry($id); 
-    if(is_array($attr)){
-      $data = array("timestamp" => date("YmdHis"),
-                    "status"  => "-");
-      if(!$this->o_queue->update_entry($id,$data)){
-        msg_dialog::display(_("Error"), sprintf(_("Could not update queue entry: %s"),$id) , ERROR_DIALOG);
-        return(FALSE);
-      }else{
-        return(FALSE);
-      }
+    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(FALSE);
+    return(TRUE);
   }
 
 
   /*! \brief Stops the specified queue entry from execution.
    *  @return Boolean TRUE in case of success, else FALSE. 
    */
-  private function stop_queue_entry($id)
+  private function stop_queue_entries($ids)
   {
-    $attr = $this->o_queue->get_entry($id); 
-    if(is_array($attr)){
-      $data = array("timestamp" => "20370101010000",
-                    "status"  => "stopped");
-      if(!$this->o_queue->update_entry($id,$data)){
-        msg_dialog::display(_("Error"), sprintf(_("Could not update queue entry: %s"),$id) , ERROR_DIALOG);
-        return(FALSE);
-      }else{
-        return(FALSE);
-      }
+    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(FALSE);
+    return(TRUE);
   }
 
 
@@ -258,38 +264,38 @@ class gotomasses extends plugin
    */
   function get_queued_entries()
   {
-    
-    $entries = $this->o_queue->get_queued_entries();
-
-    if(!is_array($entries)){
+    $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();
-    foreach($entries as $entry){
+    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);
   }
 
 
@@ -310,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;
+    }
   }