Code

Added wake action to gotomasses
[gosa.git] / plugins / addons / gotomasses / class_gotomasses.inc
index 216e9966531a9612f7fb37643c8959b116ec2bf3..dc2bb580ed7f0e843595837c1fd5797729f52536 100644 (file)
@@ -3,42 +3,41 @@
 class gotomasses extends plugin
 {
   /* Definitions */
-  var $plHeadline     = "System mass deployment";
+  var $plHeadline     = "System deployment";
   var $plDescription  = "This does something";
 
   /* attribute list for save action */
   var $attributes= array();
   var $objectclasses= array();
 
-  /* Source file that contains the csv data */
-  var $file_to_read = "Undefined"; #Set in constructor 
+  /* Source file that contains the gotomasses data */
+  var $data_file = "Undefined"; #Set in constructor 
 
   /* Queue tasks */
   var $tasks = array();
   var $current =false;
-  var $dialog = NULL;
+  var $dialog = FALSE;
 
   function gotomasses($config, $dn= NULL)
   {
     /* Define source file */
-    $this->file_to_read = CONFIG_DIR."/gotomasses_machines";
+    $this->data_file = CONFIG_DIR."/gotomasses_machines";
   
     /* Include config object */
     $this->config= $config;
-
-    $this->load_csv_data();
+    $this->load_gotomasses_data();
   }
 
 
-
   function execute()
   {
+    $smarty = get_smarty();
     /************
      * Handle posts 
      ************/
 
     $s_entry = $s_action = "";
-    $arr = array("/^edit_/"=>"edit","/^remove_/"=>"remove");
+    $arr = array("/^edit_task_/"=>"edit","/^remove_task_/"=>"remove","/^new_task_/"=>"new_task");
     foreach($arr as $regex => $action){
       foreach($_POST as $name => $value){
         if(preg_match($regex,$name)){
@@ -55,7 +54,20 @@ class gotomasses extends plugin
 
     /* Remove entry from list */
     if($s_action == "remove" && isset($this->tasks[$s_entry])){
-      unset($this->tasks[$s_entry]);
+      if(!$this->acl_is_removeable()){
+        print_red(_("You are not allowed to remove a task."));
+      }else{
+        $smarty->assign("info",_("Your are about to delete a gotomasses task."));
+        $this->current = $s_entry;
+        return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
+      }
+    }
+
+    /* Remove entry, remove is confirmed */
+    if($this->current != -1 && isset($_POST['delete_confirm'])){
+      unset($this->tasks[$this->current]);
+      $this->current = -1;
+      $this->save();
     }
 
     /* Edit selected entry */
@@ -64,14 +76,21 @@ class gotomasses extends plugin
       $this->dialog = new goto_task($this->config,$this,$entry);
       $this->current = $s_entry;
     }
+
+    /* New entry */
+    if($s_action== "new_task" && $this->acl_is_createable()){
+      $this->dialog = new goto_task($this->config,$this);
+      $this->current = -1;
+    }
   
     /* Close dialog */
     if(isset($_POST['close_goto_task'])){
-      $this->dialog = NULL;
+      $this->dialog = FALSE;
+      $this->current = -1;
     }
 
     /* Close dialog */
-    if(isset($_POST['save_goto_task'])){
+    if((isset($_POST['save_goto_task']) || isset($_POST['apply_goto_task'])) && is_object($this->dialog) ){
       $this->dialog->save_object();
       $msgs = $this->dialog->check();
       if(count($msgs)){
@@ -79,11 +98,19 @@ class gotomasses extends plugin
           print_red($msg);  
         }
       }else{
-        $this->tasks[$this->current] = $this->dialog->save();
-        $this->dialog = NULL;
+        if(isset($this->tasks[$this->current]) && $this->current != -1){
+          $this->tasks[$this->current] = $this->dialog->save();
+        }else{
+          $this->tasks[] = $this->dialog->save();
+        }
+        if(!isset($_POST['apply_goto_task'])){
+          $this->dialog = FALSE;
+          $this->current = -1;
+        }
+        $this->save();
       }
     }
+
     /* Display dialogs if currently opened */
     if(is_object($this->dialog)){
       $this->dialog->save_object();
@@ -95,45 +122,70 @@ class gotomasses extends plugin
      * Handle Divlist 
      ************/
 
-    $smarty = get_smarty();
     $plug = $_GET['plug'];
-    $divlist = new divlist("gotomasses");
-    $divlist->SetWidth("100%");
-    $divlist->SetHeader(array(  array("string"=>"<a href='?plug=".$plug."&amp;sort=object'>"._("Target")."</a>"),
-                                array("string"=>"<a href='?plug=".$plug."&amp;sort=schedule'>"._("Schedule")."</a>",
-                                      "attach"=>"style='width:150px;'"),
-                                array("string"=>"<a href='?plug=".$plug."&amp;sort=action'>"._("Type")."</a>",
-                                      "attach"=>"style='width:50px;'"),
-                                array("string"=>_("Action"),
-                                      "attach"=>"style='border-right:0px;width:50px;'")));
-
-   
-    foreach($this->tasks as $key => $task){
-  
-      $action = "<input type='image' src='images/edit.png' name='edit_".$key."' class='center' alt='"._("Edit")."'>";
-      $action.= "<input type='image' src='images/edittrash.png' name='remove_".$key."' class='center' alt='"._("Reomve")."'>";
-
-      $field1 = array("string" => $this->target_to_string($task));
-      $field2 = array("string" => $this->time_to_string($task));
-      $field3 = array("string" => $this->action_to_string($task));
-      $field4 = array("string" => $action,"attach" => "style='border-right:0px;'");
-      
+    $divlist = new MultiSelectWindow($this->config,"GotoMasses","gotomassses");
+    $divlist->SetSummary(_("Gotomasses tasks"));
+    $divlist->SetHeadpageMode();
+    $divlist->EnableCloseButton(FALSE);
+    $divlist->EnableSaveButton(FALSE);
+    $divlist->SetInformation(_("This menu allows you to add, remove and change the properties of gotomasses tasks."));
+    /* Display add button if allowed */ 
+    if($this->acl_is_createable()){ 
+      $header = "<div style='padding:5px'>
+        <input type='image' src='images/gotomasses_new_task.png' name='new_task' class='center'>
+        </div>";
+    }else{
+      $header = "";
+    }
+
+    /* Get Permissions */ 
+    $ui = get_userinfo();
+    $acls = $this->getacl("");
+    foreach($ui->get_module_departments("gotomasses") as $dep){
+      $acls .= $ui->get_category_permissions($dep,"gotomasses");
+    }
  
-      $divlist->AddEntry(array($field1,$field2,$field3,$field4));
+    /* Create divlist */
+    $divlist->SetListHeader($header);
+    $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=object'>"._("Target")."</a>"));
+    $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=schedule'>"._("Schedule")."</a>",
+                                      "attach"=>"style='width:100px;'"));
+    $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=action'>"._("Type")."</a>",
+                                      "attach"=>"style='width:80px;'"));
+    $divlist->AddHeader(array("string"=>_("Action"),
+                                      "attach"=>"style='border-right:0px;width:40px;'"));
+
+    if(!empty($acls)){
+      foreach($this->tasks as $key => $task){
+        $action = "<input type='image' src='images/edit.png' name='edit_task_".$key."' class='center' alt='"._("Edit")."'>";
+        if($this->acl_is_removeable()){
+          $action.= "<input type='image' src='images/edittrash.png' name='remove_task_".$key."' class='center' alt='"._("Reomve")."'>";
+        }
+        $field1 = array("string" => "<div style='width:100%;overflow:hidden;'><nobr>".$this->target_to_string($task)."</nobr></div>");
+        $field2 = array("string" => $this->time_to_string($task),"attach" => "style='width:100px;'");
+        $field3 = array("string" => $this->action_to_string($task),"attach" => "style='width:80px;'");
+        $field4 = array("string" => $action,"attach" => "style='text-align:right;width:40px;border-right:0px;'");
+        $divlist->AddElement(array($field1,$field2,$field3,$field4));
+      }
     }
  
-    $smarty->assign("divlist" , $divlist->DrawList());
-    $smarty->assign("contents_modified" , TRUE );
-    return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
+    return($divlist->Draw());
   }
 
   
   function target_to_string($data)
   {
     $ret = "";
-    foreach($data['Target'] as $target){
-      $ret .= preg_replace("/^[^:]+:/i","",$target).", ";
-    } 
+    if($data['Action'] == "initial_install"){
+      foreach($data['Initial_Target'] as $target){
+        $ret .= $target['MAC'].", ";
+      } 
+    }else{
+      foreach($data['Target'] as $target){
+        $ret .= $target.", ";
+      } 
+    }
     return(preg_replace("/, $/","",$ret));
   }
 
@@ -146,30 +198,29 @@ class gotomasses extends plugin
   
   function action_to_string($data)
   {
-    switch($data['Action']){
-
-      case 'reinstall' : return("Reinstall");break;
-      case 'install' : return("Install");break;
-      case 'reboot' : return("Restart");break;
-      case 'update' : return("Update");break;
-      default : return("Unknown");
+    $tmp = $this->get_actions();
+    if(isset($tmp[$data['Action']])){
+      return($tmp[$data['Action']]);
+    }else{
+      return(_("Unknown"));
     }
   }
 
   
-  function load_csv_data()
+  function load_gotomasses_data()
   {
     $ui = get_userinfo();
 
-    
-    if(!file_exists($this->file_to_read) || !is_readable($this->file_to_read)){
-      print_red(sprintf(_("Can't locate or read csv storage file '%s'."),$this->file_to_read));
+    /* Check file existence */
+    if(!file_exists($this->data_file) || !is_readable($this->data_file)){
+      print_red(sprintf(_("Can't locate or read gotomasses storage file '%s'."),$this->data_file));
       return(FALSE);
     }
 
-    $fp = @fopen($this->file_to_read,"r");
+    /* check if file is readable */
+    $fp = @fopen($this->data_file,"r");
     if(!$fp){
-      print_red(sprintf(_("Can't read csv storage file '%s'."),$this->file_to_read));
+      print_red(sprintf(_("Can't read gotomasses storage file '%s'."),$this->data_file));
       return(FALSE);
     }
 
@@ -216,7 +267,17 @@ class gotomasses extends plugin
         $entry['Weekday'] = $parts[4];  
         $entry['Action']  = $parts[5];  
         $entry['OGroup']  = $parts[6];  
-        $entry['Target']  = split(",",$parts[7]);  
+        if($entry['Action'] == "initial_install"){
+          $tmp2 = split(";",$parts[7]);
+          foreach($tmp2 as $target){
+            $tmp = split(",",$target);
+            $entry['Initial_Target'][]  = array("MAC" => $tmp[0],"IP"=>$tmp[1],"NAME" => $tmp[2]);
+          }
+          $entry['Target']  = array();
+        }else{
+          $entry['Initial_Target']  = array();
+          $entry['Target']  = split(";",$parts[7]);
+        }
         $entry['Comment'] = $comment;  
         $this->tasks []   = $entry;
       }
@@ -224,15 +285,58 @@ class gotomasses extends plugin
   }
 
 
-  function save_csv_data()
+  function save_gotomasses_data()
   {
+    $str = "#GOsa generated file, please just modify if you realy know what you do.";
+    foreach($this->tasks as $task){
+      $str .= "\n#".trim($task['Comment']);
+      $str .= "\n";
+      if($task['Action'] == "initial_install"){
+        $str .= "*     *     *     *     *     ";
+      }else{
+        $str .= str_pad($task['Minute'] ,5," ")." ";
+        $str .= str_pad($task['Hour']   ,5," ")." ";
+        $str .= str_pad($task['Day']    ,5," ")." ";
+        $str .= str_pad($task['Month']  ,5," ")." ";
+        $str .= str_pad($task['Weekday'],5," ")." ";
+      }
+      $str .= str_pad($task['Action'] ,5," ")." ";
+      $str .= str_pad($task['OGroup'] ,5," ")." ";
+      if($task['Action'] == "initial_install"){
+        foreach($task['Initial_Target'] as $target){
+          $str .= $target['MAC'].",".$target['IP'].",".$target['NAME'].";";
+        }
+      }else{
+        foreach($task['Target'] as $target){
+          $str .= $target.";";
+        }
+      }
+      $str = preg_replace("/;$/","",$str);
+    }
+
+    /* check file existence*/
+    $ui = get_userinfo();
+    if(!file_exists($this->data_file) || !is_writeable($this->data_file)){
+      print_red(sprintf(_("Can't locate or write gotomasses storage file '%s'."),$this->data_file));
+      return(FALSE);
+    }
+
+    /* check permissions */
+    $fp = @fopen($this->data_file,"w");
+    if(!$fp){
+      print_red(sprintf(_("Can't read gotomasses storage file '%s'."),$this->data_file));
+      return(FALSE);
+    }
+
+    /* Write contents */
+    $str .= "\n";
+    fwrite($fp,$str);
+    fclose($fp);
   }
 
 
   function save_object()
   {
-    if(isset($_POST['gotomasses'])){
-    }
   }
 
 
@@ -249,16 +353,24 @@ class gotomasses extends plugin
     return($ret); 
   }
 
+  
+  function save()
+  {
+    $this->save_gotomasses_data();
+  }
+
 
   function get_actions()
   {
     /* Prepare list of available actions */
-    $actions = array( "reboot"          => _("Reboot"),
+    $actions = array(       "reboot"          => _("Reboot"),
                             "localboot"       => _("Localboot"),
                             "halt"            => _("Halt system"),
+                            "initial_install" => _("Initial installation"),
                             "update"          => _("Update"),
                             "reinstall"       => _("(Re)Install"),
                             "rescan"          => _("Rescan"),
+                            "wake"            => _("Wake"),
                             "memcheck"        => _("Memory check"));
     return($actions);
   }