Code

Updated gotomasses handling
authorhickert <hickert@594d385d-05f5-0310-b6e9-bd551577e9d8>
Wed, 24 Oct 2007 11:39:50 +0000 (11:39 +0000)
committerhickert <hickert@594d385d-05f5-0310-b6e9-bd551577e9d8>
Wed, 24 Oct 2007 11:39:50 +0000 (11:39 +0000)
git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@7638 594d385d-05f5-0310-b6e9-bd551577e9d8

include/class_hostActionQueue.inc
plugins/addons/gotomasses/class_divListMasses.inc
plugins/addons/gotomasses/class_goto_task.inc
plugins/addons/gotomasses/class_gotomasses.inc
plugins/addons/gotomasses/goto_task.tpl

index d01d05511a864981106f414f88cbad9f14a9740a..a6aa4a697e22212c13d20e361d96a50fe9c0ae78 100644 (file)
@@ -40,6 +40,8 @@ class hostActionQueue {
   private $b_queue_loaded = false;
   private $i_fileversion  = 0;
 
+  private $id_entry_map   = array();
+
   public function __construct($config)
   {
     $this->o_config= $config;
@@ -90,7 +92,9 @@ class hostActionQueue {
     $this->a_queue  = array();
     $comment        = "";
     $rows           = split("\n",$data);
-  
+    $used_ids = array();
     /* Walk trough rows and parse data */
     foreach($rows as $row){
 
@@ -106,6 +110,29 @@ class hostActionQueue {
         continue;
       }
 
+      $entry_id = $taks_id= 0;    
+  
+      /* Comment must be set correctly */
+      if(empty($comment)){
+        $desc     = "";
+      }else{
+        $task_id =preg_replace("/^.*taskid:([0-9]*).*$/","\\1",$comment);
+        $entry_id=preg_replace("/^.*entryid:([0-9]*).*$/","\\1",$comment);
+        $desc    =preg_replace("/^.*desc:(.*)$/","\\1",$comment);
+      }
+      if($task_id == 0 || !is_numeric($task_id)){ 
+        $task_id  = preg_replace("/[^0-9]*/","",microtime());
+      }
+      if($entry_id == 0 || !is_numeric($entry_id)){
+        $entry_id = preg_replace("/[^0-9]*/","",microtime());
+      }
+   
+      /* Get unige id */ 
+      while(in_array($entry_id,$used_ids)){
+        $entry_id = preg_replace("/[^0-9]*/","",microtime());
+      }
+      $used_ids[] = $entry_id;
       /* Split row into minutes/ hours ...*/
       $row    = preg_replace('/[\t ]/umi'," ",$row);
       $row    = preg_replace('/  */umi'," ",$row);
@@ -116,6 +143,8 @@ class hostActionQueue {
       }else{
 
         $entry = array();
+        $entry['TASK_ID'] = $task_id;
+        $entry['ID']      = $entry_id;
         $entry['Minute']  = $parts[0];
         $entry['Hour']    = $parts[1];
         $entry['Day']     = $parts[2];
@@ -137,12 +166,19 @@ class hostActionQueue {
           $entry['Target']  = array();
         }else{
           $entry['Initial_Target']  = array();
-          $entry['Target']  = split(";",$parts[7]);
+          $entry['Target']  = split(";",$parts[9]);
         }
-        $entry['Comment'] = $comment;
+        $entry['Comment'] = $desc;
         $this->a_queue[]   = $entry;
+        
       }
     }
+   
+    /* Udpate ENTRY_ID -> id mapping */ 
+    foreach($this->a_queue as $id => $entry){
+      $this->id_entry_map[$entry['ID']] = $id;
+    }   
     return(TRUE);
   }
 
@@ -172,7 +208,7 @@ class hostActionQueue {
 
     $str = "#GOsa generated file, please just modify if you really know what you do.";
     foreach($this->a_queue as $task){
-      $str .= "\n#".trim($task['Comment']);
+      $str .= "\n#taskid:".trim($task['TASK_ID']).";entryid:".$task['ID'].";desc:".trim($task['Comment']);
       $str .= "\n";
       if($task['Action'] == "initial_install"){
         $str .= "*     *     *     *     *     ";
@@ -207,9 +243,61 @@ class hostActionQueue {
     return(TRUE);
   }
 
-  public function add($entry)
+  private function _add_entry($entry,$task_id = 0)
   {
+    if($task_id == 0 || !is_numeric($task_id) || !isset($entry['TASK_ID']) || !is_numeric($entry['TASK_ID'])){
+      $task_id=preg_replace("/[^0-9]*/","",microtime());
+      $entry['TASK_ID'] = $task_id;
+    }
+    if(!isset($entry['ID']) || !is_numeric($entry['ID'])){
+      $entry['ID'] = preg_replace("/[^0-9]*/","",microtime());
+    }
     $this->a_queue[] = $entry;
+    
+    return(true);
+  }
+
+  public function get_entry($id)
+  {
+    if(isset($this->a_queue[$this->id_entry_map[$id]])){
+      return($this->a_queue[$this->id_entry_map[$id]]);
+    }else{
+      $this->set_error(sprintf(_("Entry with id '%s' not found."),$id));
+    }
+  }
+
+  public function update_entry($id,$entry)
+  {
+    if(isset($this->a_queue[$this->id_entry_map[$id]])){
+      $this->a_queue[$this->id_entry_map[$id]] = $entry;
+      return($this->_save_data());
+    }else{
+      $this->set_error(sprintf(_("Could not update entry, entry with id '%s' not found."),$id));
+    }
+  }
+
+  public function id_exists($id)
+  {
+    return(isset($this->id_entry_map[$id]));
+  }
+
+
+  public function add($entry)
+  {
+    if(!$this->_add_entry($entry)){
+      return(FALSE);
+    }
+    return($this->_save_data());
+  }
+
+  public function add_multiple($array)
+  {
+    $task_id = preg_replace("/[^0-9]*/","",microtime()); 
+    foreach($array as $entry){
+      if(!$this->_add_entry($entry,$task_id)){
+        return(FALSE);
+      }
+    }
     return($this->_save_data());
   }
 
index 7c9f1da648bbd9140fa5b074dc42a242b8458c27..92bf31e3584b25ac8a10870b2c7a0f0190a74499 100644 (file)
@@ -78,7 +78,7 @@ class divListMasses extends MultiSelectWindow
   {
     /* Create edit link */
     $plug = $_GET['plug'];
-    $edit_link = "<div style='width:100%;overflow:hidden;'><nobr><a href='?plug=".$_GET['plug']."&act=edit&id=%key%'>%str%</nobr></div>";
+    $edit_link = "<div style='width:100%;overflow:hidden;'><nobr><a href='?plug=".$_GET['plug']."&act=edit&id=%id%'>%str%</nobr></div>";
 
     /* Create action filter array, to sort out those actions we do not want to see */
     $allowed_action = array();
@@ -94,16 +94,18 @@ class divListMasses extends MultiSelectWindow
       if(!in_array($task['Action'],$allowed_action)){
         continue;
       }
+
+      $id = $task['ID'];
  
-      $action = "<input type='image' src='images/edit.png' name='edit_task_".$key."' class='center' alt='"._("Edit")."'>";
+      $action = "<input type='image' src='images/edit.png' name='edit_task_".$id."' class='center' alt='"._("Edit")."'>";
       if($this->parent->acl_is_removeable()){
-        $action.= "<input type='image' src='images/edittrash.png' name='remove_task_".$key."' class='center' alt='"._("Reomve")."'>";
+        $action.= "<input type='image' src='images/edittrash.png' name='remove_task_".$id."' class='center' alt='"._("Reomve")."'>";
       }
       /* Create each field */
-      $field0 = array("string" => "<input type='checkbox' id='item_selected_".$key."' name='item_selected_".$key."'>" ,
+      $field0 = array("string" => "<input type='checkbox' id='item_selected_".$id."' name='item_selected_".$id."'>" ,
           "attach" => "style='width:20px;'");
 
-      $field1 = array("string" => preg_replace(array("/%key%/","/%str%/"),array($key,$this->parent->target_to_string($task)),$edit_link));
+      $field1 = array("string" => preg_replace(array("/%id%/","/%str%/"),array($id,$this->parent->target_to_string($task)),$edit_link));
       $field2 = array("string" => $this->parent->time_to_string($task),"attach" => "style='width:100px;'");
       $field3 = array("string" => $this->parent->action_to_string($task),"attach" => "style='width:80px;'");
       $field4 = array("string" => $action,"attach" => "style='text-align:right;width:40px;border-right:0px;'");
index e081d836043015d376c355388bf98cc63c38a292..bbc58faedb3798bf085723397c0606d03b458c80 100644 (file)
@@ -28,7 +28,9 @@ class goto_task extends plugin
   var $Initial_Target   = array();
   var $Actions  = array();
   var $new      = FALSE;
-  var $attributes     = array("Zone","Section","OGroup","Minute","Hour","Day",
+  var $ID = 0;
+  var $TASK_ID =0;
+  var $attributes     = array("ID","TASK_ID","Zone","Section","OGroup","Minute","Hour","Day",
                               "Month","Weekday","Action","Comment","Target","Initial_Target");
 
   var $configure_dhcp = FALSE;
@@ -99,7 +101,7 @@ class goto_task extends plugin
     if($this->Action != "initial_install"){
       
       /* Add target */
-      if(isset($_POST['add_target']) && !empty($_POST['target_text'])){
+      if($this->ID == 0 && isset($_POST['add_target']) && !empty($_POST['target_text'])){
         $target = get_post("target_text");
         if($this->is_valid_target($target) && !in_array($target,$this->Target)){
           $this->Target[] = $target;
@@ -108,7 +110,7 @@ class goto_task extends plugin
     }else{
 
       /* Add target */
-      if(isset($_POST['add_target']) && !empty($_POST['task_MAC'])){
+      if($this->ID ==0 && isset($_POST['add_target']) && !empty($_POST['task_MAC'])){
         $MAC = $_POST['task_MAC'];
         $NAME= "";
         $IP  = "";
@@ -133,7 +135,7 @@ class goto_task extends plugin
     }
 
     /* Add via csv */
-    if(isset($_FILES['import_file'])){
+    if($this->ID == 0 && isset($_FILES['import_file'])){
       $file = $_FILES['import_file']['tmp_name'];
       if(file_exists($file) && is_readable($file)){
         $str ="";
@@ -240,6 +242,8 @@ class goto_task extends plugin
     $smarty->assign("Zones", $this->Zones);
     $smarty->assign("Sections", $this->Sections);
 
+    $smarty->assign("ID",$this->ID);
+  
     $smarty->assign("Zone", $this->Zone);
     $smarty->assign("Section", $this->Section);
 
index 9fb3c9769c231ed37b2611600d5462f7f88ca088..a71170fd7a395a335d7c8ec7124bbe5f3089c892 100644 (file)
@@ -96,10 +96,12 @@ class gotomasses extends plugin
      ************/
 
     /* Edit selected entry */
-    if($s_action == "edit" && isset($this->tasks[$s_entry])){
-      $entry = $this->tasks[$s_entry];
-      $this->dialog = new goto_task($this->config,$this,$entry);
-      $this->current = $s_entry;
+    if($s_action == "edit"){
+      $entry = $this->o_queue->get_entry($s_entry);
+      if($entry){
+        $this->dialog = new goto_task($this->config,$this,$entry);
+        $this->current = $s_entry;
+      }
     }
 
     /* New entry */
@@ -122,18 +124,20 @@ class gotomasses extends plugin
         foreach($msgs as $msg){
           print_red($msg);  
         }
-      }else{
-        if(isset($this->tasks[$this->current]) && $this->current != -1){
-          $this->tasks[$this->current] = $this->dialog->save();
+      }else{  
+        
+        if($this->o_queue->id_exists($this->current)){
+          $this->o_queue->update_entry($this->current,$this->dialog->save());
         }else{
           $tmp = $this->dialog->save();
-
+          $tmp2= array();
           $targets =$tmp['Target'];
           foreach($targets as $target){
             $tmp['Target'] = array($target);
-            if(!$this->o_queue->add($tmp)){
-              print_red($this->o_queue->get_error());
-            }
+            $tmp2[] = $tmp;
+          }
+          if(!$this->o_queue->add_multiple($tmp2)){
+            print_red($this->o_queue->get_error());
           }
         }
         if(!isset($_POST['apply_goto_task'])){
@@ -150,7 +154,6 @@ class gotomasses extends plugin
       return($this->dialog->execute());
     }
 
-
     /************
      * Handle Divlist 
      ************/
@@ -169,21 +172,12 @@ class gotomasses extends plugin
    }
     $ret = array();
     while($entry = $this->o_queue->fetch()){
-      $ret[] = $entry;
+      $ret[]= $entry;
     }
     return($ret);
   }
  
 
-
-
-
-
-
-
-
-
   function target_to_string($data)
   {
     $ret = "";
index 0c93fcf560b6e707b47a22631f95768d186d3bfb..f841a4fd8501750d433c1f93afb59fffa9892add 100644 (file)
@@ -78,6 +78,7 @@
        </tr>
 </table>
 {if $Action == "initial_install"}
+{if $ID == 0}
 <p class='seperator'>&nbsp;</p>
 <table style='width:100%;'>
        <tr>
                </td>
        </tr>
 </table>
-
+{/if}
 {else}
 <p class='seperator'>&nbsp;</p>
 <table style='width:100%;'>
                </td>
        </tr>
 </table>
+{if $ID == 0}
 <p class='seperator'>&nbsp;</p>
 <table style='width:100%;'>
        <tr>
        </tr>
 </table>
 {/if}
+{/if}
 <p class='seperator'>&nbsp;</p>
 <input type='hidden' name='goto_task_posted' value='1'>
 <p style="text-align:right">