Code

Updated gotomasses
[gosa.git] / gosa-plugins / goto / addons / gotomasses / class_gotomasses.inc
index eb34a8feb560233df7a1ea0076aa411777aed15a..4d609392cc4473cd82ea269c2d9fcc07500e0c1d 100644 (file)
@@ -17,6 +17,7 @@ class gotomasses extends plugin
   var $divlist        = NULL;
 
   var $events         = array();
+  var $event_tags     = array();
 
   var $sort_by  = "Schedule";
   var $sort_dir = "down";
@@ -29,7 +30,14 @@ class gotomasses extends plugin
     /* Include config object */
     $this->config= &$config;
     $this->o_queue = new gosaSupportDaemon(TRUE,10);
-    $this->events  = DaemonEvent::get_event_types();
+    $this->events  = DaemonEvent::get_event_types( USER_EVENT | SYSTEM_EVENT);
+
+    /* Get tags that will be used in queue searches */
+    foreach($this->events['BY_CLASS'] as $evt){
+      if(isset($evt['s_Queued_Action'])){
+        $this->event_tags[] = $evt['s_Queued_Action'];
+      }
+    }
   }
 
 
@@ -43,8 +51,11 @@ class gotomasses extends plugin
     
     $s_entry = $s_action = "";
     $arr = array( 
-        "/^stop_/"        => "stop",
-        "/^start_/"       => "start",
+
+        "/^pause_/"           => "pause",
+        "/^resume_/"          => "resume",
+        "/^execute_process_/" => "execute_process",
+        "/^abort_process_/"   => "abort_process",
 
         "/^prio_up_/"     => "prio_up",
         "/^prio_down_/"   => "prio_down",
@@ -81,26 +92,32 @@ class gotomasses extends plugin
      ************/
 
     if(preg_match("/^prio_/",$s_action)){
-
       switch($s_action){
-
         case 'prio_down'  : $this->update_priority($s_entry,"down");break;
         case 'prio_up'    : $this->update_priority($s_entry,"up");break;
       }
     }
 
     /************
-     * Handle start/stop modifications  
+     * Handle pause/resume/execute modifications  
      ************/
 
-    if(preg_match("/^start/",$s_action) || preg_match("/^stop/",$s_action)){
+    if(preg_match("/^resume/",$s_action) || 
+       preg_match("/^pause/",$s_action) || 
+       preg_match("/^abort_process/",$s_action) || 
+       preg_match("/^execute_process/",$s_action)){
 
       switch($s_action){
-        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.");
+        case 'resume'         : $this->resume_queue_entries   (array($s_entry));break; 
+        case 'pause'          : $this->pause_queue_entries    (array($s_entry));break; 
+        case 'execute_process': $this->execute_queue_entries  (array($s_entry));break; 
+        case 'abort_process'  : $this->abort_queue_entries    (array($s_entry));break; 
+        case 'resume_all'         : $this->resume_queue_entries   ($this->list_get_selected_items());break; 
+        case 'pause_all'          : $this->pause_queue_entries    ($this->list_get_selected_items());break; 
+        case 'execute_process_all': $this->execute_queue_entries  ($this->list_get_selected_items());break; 
+        case 'abort_process_all'  : $this->abort_queue_entries    ($this->list_get_selected_items());break; 
+
+        default : trigger_error("Undefined action setting used (".$s_action.").");
       }
       if($this->o_queue->is_error()){
         msg_dialog::display(_("Error"), $this->o_queue->get_error(), ERROR_DIALOG);
@@ -128,7 +145,9 @@ class gotomasses extends plugin
       $type = FALSE;
       if(isset($this->entries[$id])){
         $event = $this->entries[$s_entry];
-        if(isset($this->events['BY_QUEUED_ACTION'][$event['HEADERTAG']])){
+      
+        
+        if($event['STATUS'] == "waiting" && isset($this->events['BY_QUEUED_ACTION'][$event['HEADERTAG']])){
           $type = $this->events['BY_QUEUED_ACTION'][$event['HEADERTAG']];
           $this->dialog = new $type['CLASS_NAME']($this->config,$event);
         }
@@ -151,17 +170,27 @@ class gotomasses extends plugin
         }else{
           $ids = $this->list_get_selected_items();
         }
+
         if(count($ids)){
-          $this->ids_to_remove = $ids;
-          $ret = $this->o_queue->ids_exist($this->ids_to_remove);
+          $ret = $this->o_queue->ids_exist($ids);
           $ret = $this->o_queue->get_entries_by_id($ret);
 
           $tmp = "";
           foreach($ret as $task){
-            $tmp.= "\n".$task['ID']." - ".$task['HEADERTAG']." ".$task['MACADDRESS'];
+
+            /* Only remove WAITING or ERROR entries */
+            if(in_array($task['STATUS'],array("waiting","error"))){
+              $this->ids_to_remove[] = $task['ID'];
+              if(isset($this->events['BY_QUEUED_ACTION'][$task['HEADERTAG']])){
+                $evt = $this->events['BY_QUEUED_ACTION'][$task['HEADERTAG']];
+                $tmp.= "\n".$task['ID']." - ".$evt['s_Menu_Name']." ".$task['MACADDRESS'];
+              }else{
+                $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>"));
+          $smarty->assign("info",sprintf(_("You are about to remove the following actions from the GOsa support Daemon: %s"),"<pre>".$tmp."</pre>"));
           $this->current = $s_entry;
           return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
         }
@@ -186,34 +215,15 @@ class gotomasses extends plugin
 
     /* Close dialog */
     if(isset($_POST['save_event_dialog'])){
-
       if(is_object($this->dialog)){
-
-        if($this->dialog->is_new()){
-          $header     = $this->dialog->get_schedule_action();
-          $targets    = $this->dialog->get_targets();
-          $data       = $this->dialog->save();
-          $data['timestamp'] = $this->dialog->get_timestamp(); 
-
-
-          foreach($targets as $target){
-            $data['mac'] =  $target;
-            if(!$this->o_queue->send($header,$target,$data,TRUE)){
-              msg_dialog::display(_("Daemon"),sprintf(_("Something went wrong while talking to the daemon: %s."),
-                    $this->o_queue->get_error()),ERROR_DIALOG);
-            }  
-          }
+        $this->dialog->save_object();
+        if(!$this->o_queue->append($this->dialog)){
+          msg_dialog::display(_("Daemon"),sprintf(_("Something went wrong while talking to the daemon: %s."),
+                $this->o_queue->get_error()),ERROR_DIALOG);
+        }else{
           $this->dialog = FALSE; 
           $this->current = -1;
-        }else{
-          $id                 = $this->dialog->get_id();
-          $data               = $this->dialog->save();
-          $data['timestamp']  = $this->dialog->get_timestamp();
-          if($this->o_queue->update_entries(array($id),$data)){
-            $this->dialog = FALSE;
-            $this->current = -1;
-          }
-        }
+        } 
       }
     }
 
@@ -235,8 +245,8 @@ class gotomasses extends plugin
      ************/
 
     $divlist = new MultiSelectWindow($this->config,"gotoMasses",array("gotomasses"));
-    $divlist->SetInformation(_("This menu allows you to remove and change the properties of GOsa deamon tasks."));
-    $divlist->SetSummary(_("List of queued deamon jobs."));
+    $divlist->SetInformation(_("This menu allows you to remove and change the properties of GOsa tasks."));
+    $divlist->SetSummary(_("List of queued jobs"));
     $divlist->EnableCloseButton(FALSE);
     $divlist->EnableSaveButton(FALSE);
     $divlist->SetHeadpageMode();
@@ -251,8 +261,10 @@ class gotomasses extends plugin
     }
     if(preg_match("/w/",$this->getacl(""))){
       $s.= "..|---|\n";
-      $s.= "..|<img src='images/status_start_all.png' alt='' border='0' class='center'>&nbsp;"._("Start all")."|start_all\n";
-      $s.= "..|<img src='images/status_stop_all.png' alt='' border='0' class='center'>&nbsp;"._("Stop all")."|stop_all\n";
+      $s.= "..|<img src='images/status_start.png' alt='' border='0' class='center'>&nbsp;"._("Resume all")."|resume_all\n";
+      $s.= "..|<img src='images/status_pause.png' alt='' border='0' class='center'>&nbsp;"._("Pause all")."|pause_all\n";
+      $s.= "..|<img src='images/small_error.png'  alt='' border='0' class='center'>&nbsp;"._("Abort all")."|abort_process_all\n";
+      $s.= "..|<img src='images/rocket.png'       alt='' border='0' class='center'>&nbsp;"._("Execute all")."|execute_process_all\n";
     }
 
     $divlist->SetDropDownHeaderMenu($s);
@@ -293,17 +305,58 @@ class gotomasses extends plugin
 
     foreach($this->entries as $key => $task){
 
-      $prio_actions = "<input class='center' type='image' src='images/prio_increase.png' name='prio_up_".$key."'>&nbsp;";
-      $prio_actions.= "<input class='center' type='image' src='images/prio_decrease.png' name='prio_down_".$key."'>&nbsp;";
-      $prio_actions.= "<input class='center' type='image' src='images/status_stop_all.png' name='stop_".$key."'>&nbsp;";
-      $prio_actions.= "<input class='center' type='image' src='images/status_start_all.png' name='start_".$key."'>&nbsp;";
+      $prio_actions="";
+      $action = "";
 
-      $action = "<input type='image' src='images/edit.png' name='edit_task_".$key."' 
-        class='center' alt='"._("Edit")."'>";
+      /* If WAITING add priority action
+       */  
+      if(in_array($task['STATUS'],array("waiting"))){
+        $prio_actions.= "<input class='center' type='image' src='images/prio_increase.png' 
+          title='"._("Move up in execution queue")."' name='prio_up_".$key."'>&nbsp;";
+        $prio_actions.= "<input class='center' type='image' src='images/prio_decrease.png' 
+          title='"._("Move down in execution queue")."' name='prio_down_".$key."'>&nbsp;";
+      }
+    
+      /* If WAITING add pause action
+       */  
+      if(in_array($task['STATUS'],array("waiting"))){
+        $prio_actions.= "<input class='center' type='image' src='images/status_pause.png' 
+          title='"._("Pause job")."' name='pause_".$key."'>&nbsp;";
+      }
 
-      if($this->acl_is_removeable()){
+      /* If PAUSED add resume action
+       */  
+      if(in_array($task['STATUS'],array("paused"))){
+        $prio_actions.= "<input class='center' type='image' src='images/status_start.png' 
+          title='"._("Resume job")."' name='resume_".$key."'>&nbsp;";
+      }
+
+      /* If PROCESSING add abort action
+       */  
+      if(in_array($task['STATUS'],array("processing"))){
+        $prio_actions.= "<input class='center' type='image' src='images/small_error.png' 
+          title='"._("Abort execution")."' name='abort_process_".$key."'>";
+      }
+
+      /* If PAUSED or WAITING add execution action
+       */  
+      if(in_array($task['STATUS'],array("paused","waiting"))){
+        $prio_actions.= "<input class='center' type='image' src='images/rocket.png' 
+          title='"._("Force execution now!")."' name='execute_process_".$key."'>&nbsp;";
+      }
+
+      /* If PAUSED or WAITING add edit action
+       */  
+      if(in_array($task['STATUS'],array("waiting"))){
+        $action.= "<input type='image' src='images/edit.png' name='edit_task_".$key."' 
+          class='center' alt='"._("Edit")."'>";
+      }
+
+      /* If WAITING or ERROR add remove action
+       */  
+      if( $this->acl_is_removeable() && in_array($task['STATUS'],array("waiting","error"))){
         $action.= "<input type='image' src='images/edittrash.png' name='remove_task_".$key."' 
-          class='center' alt='"._("Reomve")."'>";
+          class='center' alt='"._("Remove")."'>";
       }
 
       $color = "";
@@ -316,11 +369,34 @@ class gotomasses extends plugin
       if(isset($this->events['BY_QUEUED_ACTION'][$task['HEADERTAG']]['s_Menu_Name'])){
         $event_type = $this->events['BY_QUEUED_ACTION'][$task['HEADERTAG']];
         $display2= $event_type['s_Menu_Name'];
+
+        if(strlen($display2) > 20){
+          $display2 = substr($display2,0,18)."...";
+        }
+
         if(isset($event_type['ListImage']) && !empty($event_type['ListImage'])){
           $display2 = $event_type['ListImage']."&nbsp;".$display2;
         }
       } 
 
+      $status = $task['STATUS'];
+  
+      if($status == "waiting"){
+        $status = "<img class='center' src='images/clock.png' alt=''>&nbsp;"._("Waiting");
+      }
+      if($status == "error"){
+        $status = "<img class='center' src='images/false.png' alt=''>&nbsp;"._("Error");
+      }
+
+      /* Special handling for all entries that have 
+          STATUS == "processing" && PROGRESS == NUMERIC
+       */
+      if($status == "processing" && isset($task['PROGRESS'])){
+        $percent = $task['PROGRESS'];
+        $status = "<img src='progress.php?x=80&amp;y=13&amp;p=".$percent."' alt='".$percent."&nbsp;%'>";
+      }
+
+
       /* Create each field */
       $field0 = array("string" => "<input type='checkbox' id='item_selected_".$task['ID']."' name='item_selected_".$key."'>" ,
                       "attach" => "style='width:20px;".$color."'");
@@ -329,7 +405,7 @@ class gotomasses extends plugin
       $field1a= array("string" => $display2,
                       "attach" => "style='".$color.";width:120px;'");
       $field2 = array("string" => date("d.m.Y H:i:s",strtotime($task['TIMESTAMP'])),"attach" => "style='".$color.";width:100px;'");
-      $field3 = array("string" => $task['STATUS'],"attach" => "style='".$color.";width:80px;'");
+      $field3 = array("string" => $status,"attach" => "style='".$color.";width:80px;'");
       $field4 = array("string" => $prio_actions.$action,"attach" => "style='".$color.";text-align:right;width:120px;border-right:0px;'");
       $divlist->AddElement(array($field0,$field1,$field1a,$field2,$field3,$field4));
     }
@@ -362,9 +438,9 @@ class gotomasses extends plugin
   public function update_priority($id,$type = "up")
   {
     if($type == "up"){
-      $tmp = $this->o_queue->get_queued_entries(-1,-1,"timestamp DESC");
+      $tmp = $this->o_queue->get_queued_entries($this->event_tags,-1,-1,"timestamp DESC");
     }else{
-      $tmp = $this->o_queue->get_queued_entries(-1,-1,"timestamp ASC");
+      $tmp = $this->o_queue->get_queued_entries($this->event_tags,-1,-1,"timestamp ASC");
     }
     $last = array();
     foreach($tmp as $entry){
@@ -388,38 +464,154 @@ class gotomasses extends plugin
   }
 
 
-  /*! \brief  Force queue job to be done as far as possible.
+  /*! \brief  Resumes to status 'waiting'.
    *  @return Boolean TRUE in case of success, else FALSE. 
    */
-  private function start_queue_entries($ids)
+  private function resume_queue_entries($ids)
   {
     if(!count($ids)){
       return;
     }
+
+    /* Entries are resumed by setting the status to 
+     *  'waiting'
+     */
+    $data = array("status"    => "waiting");
   
-    $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);
+    /* Check if given ids are valid and check if the status
+     *  allows resuming.
+     */
+    $update_ids = array();
+    foreach($this->o_queue->get_entries_by_id($ids) as $entry){
+      if(isset($entry['STATUS']) && preg_match("/paused/",$entry['STATUS'])){
+        $update_ids[] = $entry['ID'];
+      }
+    }
+
+    /* Tell the daemon that we have entries to update.
+     */
+    if(count($update_ids)){
+      if(!$this->o_queue->update_entries($update_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.
+  /*! \brief  Force queue job to be done as far as possible.
+   *  @return Boolean TRUE in case of success, else FALSE.
+   */
+  private function execute_queue_entries($ids)
+  {
+    if(!count($ids)){
+      return;
+    }
+
+    /* Execution is forced by updating the status to 
+     *  waiting and setting the timestamp to current time.
+     */
+    $data = array(  "timestamp" => date("YmdHis",time()), 
+                    "status"    => "waiting");
+
+    /* Only allow execution of paused or waiting entries 
+     */
+    $update_ids = array();
+    foreach($this->o_queue->get_entries_by_id($ids) as $entry){
+      if(in_array($entry['STATUS'],array("paused","waiting"))){
+        $update_ids[] = $entry['ID'];
+      }
+    }
+
+    /* Tell the daemon that we want to update some entries
+     */
+    if(count($update_ids)){
+      if(!$this->o_queue->update_entries($update_ids,$data)){
+        msg_dialog::display(_("Error"), sprintf(_("Could not update queue entries.")) , ERROR_DIALOG);
+        return(FALSE);
+      }
+    }
+    return(TRUE);
+  }
+
+
+  /*! \brief  Force queue job to be done as far as possible.
+   *  @return Boolean TRUE in case of success, else FALSE.
+   */
+  private function abort_queue_entries($ids)
+  {
+    if(!count($ids)){
+      return;
+    }
+
+    /* Entries are paused by setting the status to
+     *  something different from 'waiting'.
+     * We simply use 'paused'.
+     */
+    $data = array("status"    => "paused");
+
+    /* Detect if the ids we got are valid and
+     *  check if the status allows pausing.
+     */
+    $update_ids = array();
+    foreach($this->o_queue->get_entries_by_id($ids) as $entry){
+      if(isset($entry['STATUS']) && preg_match("/processing/",$entry['STATUS'])){
+        if(isset($entry['MACADDRESS'])){
+          $update_ids[] = $entry['MACADDRESS'];
+        }else{
+          trigger_error("No mac address found in event.");
+        }
+      }
+    }
+
+    if(class_available("DaemonEvent_faireboot")){
+      $tmp = new DaemonEvent_faireboot($this->config);
+      $tmp->add_targets($update_ids);
+      if(!$this->o_queue->append($tmp)){
+        msg_dialog::display(_("Error"), sprintf(_("Could not update queue entry: %s"),$id) , ERROR_DIALOG);
+        return(FALSE);
+      }
+    }else{
+      msg_dialog::display(_("Error"),
+          sprintf(_("The Job could not be aborted, the '%s' event class was not found."),
+            "DaemonEvent_faireboot") , ERROR_DIALOG);
+    }
+  }
+
+
+  /*! \brief Pauses the specified queue entry from execution.
    *  @return Boolean TRUE in case of success, else FALSE. 
    */
-  private function stop_queue_entries($ids)
+  private function pause_queue_entries($ids)
   {
     if(!count($ids)){
       return;
     }
-    $data = array("timestamp" => date("YmdHis",(time() + (60*60*24*365))),
-                  "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);
+
+    /* Entries are paused by setting the status to 
+     *  something different from 'waiting'.
+     * We simply use 'paused'.
+     */   
+    $data = array("status"    => "paused");
+
+    /* Detect if the ids we got are valid and
+     *  check if the status allows pausing.
+     */ 
+    $update_ids = array();
+    foreach($this->o_queue->get_entries_by_id($ids) as $entry){
+      if(isset($entry['STATUS']) && preg_match("/waiting/",$entry['STATUS'])){
+        $update_ids[] = $entry['ID'];
+      }
+    }
+
+    /* Tell the daemon that we want to update some entries
+     */
+    if(count($update_ids)){
+      if(!$this->o_queue->update_entries($update_ids,$data)){
+        msg_dialog::display(_("Error"), sprintf(_("Could not update queue entry: %s"),$id) , ERROR_DIALOG);
+        return(FALSE);
+      }
     }
     return(TRUE);
   }
@@ -430,6 +622,9 @@ class gotomasses extends plugin
    */
   function reload()
   {
+
+    /* Sort map   html-post-name => daemon-col-name
+     */
     $map = array(
         "QueuePosition" => "id",
         "Action"        => "status",
@@ -437,6 +632,8 @@ class gotomasses extends plugin
         "TargetName"    => "macaddress",
         "Schedule"      => "timestamp");
 
+    /* Create sort header 
+     */
     if(!isset($map[$this->sort_by])){
       $sort = "id DESC";
     }else{
@@ -447,16 +644,17 @@ class gotomasses extends plugin
         $sort.= " DESC";
       }
     }
-      
+     
+    /* Get entries. */ 
     $start  = $this->start; 
     $stop   = $this->range;
-
-    $entries = $this->o_queue->get_queued_entries($start,$stop,$sort);
+    $entries = $this->o_queue->get_queued_entries($this->event_tags,$start,$stop,$sort);
     if ($this->o_queue->is_error()){
-
       msg_dialog::display(_("Error"), sprintf(_("Cannot load queue entries: %s"), "<br><br>".$this->o_queue->get_error()), ERROR_DIALOG);
     }
 
+    /* Assign entries by id.
+     */
     $this->entries = array();
     foreach($entries as $entry){
       $this->entries[$entry['ID']]= $entry;
@@ -468,6 +666,8 @@ class gotomasses extends plugin
    */
   function save_object()
   {
+    /* Check for sorting changes 
+     */
     $sort_vals = array("Action","QueuePosition","TargetName","Schedule","TaskID");
     if(isset($_GET['sort']) && in_array($_GET['sort'],$sort_vals)){
       $sort = $_GET['sort'];
@@ -480,9 +680,13 @@ class gotomasses extends plugin
       }
       $this->sort_by = $sort;
     }
+
+    /* Range selection used? */
     if(isset($_POST['range']) && is_numeric($_POST['range'])){
       $this->range = $_POST['range'];
     }
+    
+    /* Page changed. */
     if(isset($_GET['start'])){
       $start = $_GET['start'];
       if(is_numeric($start) || $start == 0){