Code

checked in first changes for gotomasses.
authorhickert <hickert@594d385d-05f5-0310-b6e9-bd551577e9d8>
Thu, 16 Aug 2007 13:32:26 +0000 (13:32 +0000)
committerhickert <hickert@594d385d-05f5-0310-b6e9-bd551577e9d8>
Thu, 16 Aug 2007 13:32:26 +0000 (13:32 +0000)
Not yet finished

git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@7065 594d385d-05f5-0310-b6e9-bd551577e9d8

plugins/addons/gotomasses/class_gotomasses.inc
plugins/addons/gotomasses/contents.tpl

index e88d43d0b1ee9b68e08718d87921364a29f3082a..8588da5cef03b24891cf00585fa2874216f39a7a 100644 (file)
@@ -13,16 +13,10 @@ class gotomasses extends plugin
   /* Source file that contains the csv data */
   var $file_to_read = "Undefined"; #Set in constructor 
 
-  /* Parsed csv content */
-  var $contents         = array();
+  /* Queue tasks */
+  var $tasks = array();
 
-  /* Used to detect changes made on the csv content.
-   *  js warnings will be 
-   *  shown if the content wasn't saved or discarded
-   */
-  var $contents_backup  = array();
-  var $view_logged = FALSE;
-  var $contents_initially_loaded = FALSE;
+  var $dialog = NULL;
 
   function gotomasses($config, $dn= NULL)
   {
@@ -34,236 +28,207 @@ class gotomasses extends plugin
   }
 
 
-  function get_object_groups()
-  {
-    $ret = array();
-    $ldap = $this->config->get_ldap_link();
-    $ldap->cd($this->config->current['BASE']);
-    $ldap->search("(&(objectClass=gosaGroupOfNames)(cn=*))",array("cn"));
-    while($attrs = $ldap->fetch()){
-      $ret [$attrs['cn'][0]] = $attrs['cn'][0];
-    }
-    return($ret); 
-  }
-
 
   function execute()
   {
-    /* Load contents */
-    if(!$this->contents_initially_loaded){
-      $this->load_csv_data();
-      $this->contents_initially_loaded = TRUE;
+    /* Check posts */
+    $s_entry = $s_action = "";
+    $arr = array("/^edit_/"=>"edit","/^remove_/"=>"remove");
+    foreach($arr as $regex => $action){
+      foreach($_POST as $name => $value){
+        if(preg_match($regex,$name)){
+          $s_action = $action;
+          $s_entry  = preg_replace($regex,"",$name);
+          $s_entry  = preg_replace("/_(x|y)$/","",$s_entry);
+        }
+      }
     }
 
-    /* Log view */
-    if(!$this->view_logged){
-      $this->view_logged = TRUE;
-      new log("view","gotomasses/".get_class($this),$this->dn);
+    /* Remove entry from list */
+    if($s_action == "remove" && isset($this->tasks[$s_entry])){
+      unset($this->tasks[$s_entry]);
     }
 
-    if(isset($_POST['export_gotomass_csv']) && $this->acl_is_writeable("something")){
-        $data = "";
-        foreach($this->contents as $val){
-          $data .= $val['MAC'].", ".$val['OG']."\n";
-        }
-        header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
-        header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
-        header("Cache-Control: no-cache");
-        header("Pragma: no-cache");
-        header("Cache-Control: post-check=0, pre-check=0");
-        header("Content-type: text/plain");
-        if (preg_match('/MSIE 5.5/', $_SERVER['HTTP_USER_AGENT']) ||
-            preg_match('/MSIE 6.0/', $_SERVER['HTTP_USER_AGENT'])){
-          header('Content-Disposition: filename="gotomass.csv"');
-        } else {
-          header('Content-Disposition: attachment; filename="gotomass.csv";');
-        }
-        echo $data;
-        exit();
+    /* Edit selected entry */
+    if($s_action == "edit" && isset($this->tasks[$s_entry])){
+      $entry = $this->tasks[$s_entry];
+      $this->dialog = new goto_task($this->config,$entry);
     }
-   
-    /* Import given file */ 
-    if(isset($_POST['import_gotomass_csv']) && isset($_FILES['mass_file'])){
-      if(!$this->acl_is_writeable("something")){
-        print_red(_("Your are not allowed to import csv data into this plugin."));
-      }else{
-        $str = @file_get_contents($_FILES['mass_file']['tmp_name']);
-        if(empty($str)){
-          print_red(_("The uploaded file seams to be empty, import aborted."));
-        }else{
-          $this->load_csv_data($str); 
-        }
-      }
+  
+    /* Close dialog */
+    if(isset($_POST['close_goto_task'])){
+      $this->dialog = NULL;
     }
 
-    /* Add a new empty entry to the list */ 
-    if(isset($_POST['add_new_entry'])){
-      $this->contents[] = array("MAC" => "", "OG" => "","VALID_MAC" => FALSE);
+    /* Close dialog */
+    if(isset($_POST['close_goto_task'])){
+      $this->dialog = NULL;
     }
+    /* Display dialogs if currently opened */
+    if(is_object($this->dialog)){
+      $this->dialog->save_object();
+      return($this->dialog->execute());
+    }
+
+
+    $this->load_csv_data();
+    $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;'")));
 
-    /* Call parent execute */
-    plugin::execute();
-    $smarty= get_smarty();
-    $smarty->assign("is_writeable",$this->acl_is_writeable("something"));
-    $smarty->assign("is_readable", $this->acl_is_readable("something"));
-    $smarty->assign("contents_modified",$this->contents_modified());
-    $smarty->assign("ogs", $this->get_object_groups());
-    $smarty->assign("contents", $this->contents);
-    $smarty->assign("launchimage","images/launch.png");
+   
+    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->AddEntry(array($field1,$field2,$field3,$field4));
+    }
+    $smarty->assign("divlist" , $divlist->DrawList());
+    $smarty->assign("contents_modified" , TRUE );
     return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
   }
 
   
-  /* Check if something is modified */
-  function contents_modified($display = FALSE)
+  function target_to_string($data)
   {
-    $a = $this->contents;
-    $b = $this->contents_backup;
-    if(count($a) != count($b)){
-      if($display){
-        print_a(array_diff_assoc($a,$b));
-      }
-      return(TRUE);
-    }else{
-      foreach($a as $a_key => $a_val){
-        if(count(array_diff($a_val, $b[$a_key]))){
-
-          if($display){
-            print_a(array_diff($a_val, $b[$a_key]));
-          }
-          return(TRUE);
-        }
-      }
-    }
-    return(FALSE); 
+    $ret = "";
+    foreach($data['Target'] as $target){
+      $ret .= preg_replace("/^[^:]+:/i","",$target).", ";
+    } 
+    return(preg_replace("/, $/","",$ret));
   }
 
+  
+  function time_to_string($data)
+  {
+    return($data['Minute']." ".$data['Hour']." ".$data['Day']." ".$data['Month']." ".$data['Weekday']);
+  }
+
+  
+  function action_to_string($data)
+  {
+    switch($data['Action']){
 
-  function load_csv_data($data = NULL)
+      case 'reinstall' : return("Reinstall");break;
+      case 'install' : return("Install");break;
+      case 'reboot' : return("Restart");break;
+      case 'update' : return("Update");break;
+      default : return("Unknown");
+    }
+  }
+
+  
+  function load_csv_data()
   {
     $ui = get_userinfo();
 
-    if(!$this->acl_is_readable("something")){
-      $this->contents =array(); 
+    if(!$this->acl_is_readable("ACL missing")){
+      $this->tasks =array(); 
       print_red(_("Your are not allowed to view contents of this plugin."));
       return(FALSE);
     }
 
-    if($data == NULL){
-      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));
-        return(FALSE);
-      }
+    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));
+      return(FALSE);
+    }
 
-      $fp = @fopen($this->file_to_read,"r");
-      if(!$fp){
-        print_red(sprintf(_("Can't read csv storage file '%s'."),$this->file_to_read));
-        return(FALSE);
-      }
+    $fp = @fopen($this->file_to_read,"r");
+    if(!$fp){
+      print_red(sprintf(_("Can't read csv storage file '%s'."),$this->file_to_read));
+      return(FALSE);
+    }
 
-      $this->contents =array(); 
+    /* Get file contents */
+    $data ="";
+    while(!feof($fp)){
+      $data.= fread($fp,512);
+    }
 
-      while(!feof($fp)){
-        $str = trim(fgets($fp,512));
+    /* Get lines from file */
+    $this->tasks  = array(); 
+    $comment      = "";
+    $rows         = split("\n",$data);
 
-        /* Get mac address */
-        $og = trim(preg_replace("/^[^,;]*(,|;)/","",$str));
-        $mac = preg_replace("/(,|;).*$/","",$str);
+    /* Walk trough rows and parse data */
+    foreach($rows as $row){
 
-        if(!empty($og) || !empty($mac)){
-          $this->contents[] = array("MAC" => $mac , "OG" => $og,"VALID_MAC" => is_mac($mac));
-        }
+      /* Skip empty lines */
+      $row = trim($row);
+      if(empty($row)){
+        continue;
       }
-      fclose($fp);
-      $this->contents_backup = $this->contents;
-    }else{
-      $this->contents =array(); 
-      $rows = split("\n",$data);
-      foreach($rows as $str){
-        
-        /* Get mac address */
-        $og = trim(preg_replace("/^[^,;]*(,|;)/","",$str));
-        $mac = preg_replace("/(,|;).*$/","",$str);
-
-        if(!empty($og) || !empty($mac)){
-          $this->contents[] = array("MAC" => $mac , "OG" => $og, "VALID_MAC" => is_mac($mac));
-        }
+
+      /* Get comment, if available */     
+      if(preg_match("/^#/",$row)){
+        $comment = preg_replace("/^#/","",$row);
+        continue;
       }
-    }
+
+      /* Split row into minutes/ hours ...*/ 
+      $row    = preg_replace('/[\t ]/umi'," ",$row);
+      $row    = preg_replace('/  */umi'," ",$row);
+      $parts  = split(" ",$row);
+
+      if(count($parts) != 7){
+        print_red(_("Entry broken, skipped."));
+      }else{
+
+        $entry = array();
+        $entry['Minute']  = $parts[0];  
+        $entry['Hour']    = $parts[1];  
+        $entry['Day']     = $parts[2];  
+        $entry['Month']   = $parts[3];  
+        $entry['Weekday'] = $parts[4];  
+        $entry['Action']  = $parts[5];  
+        $entry['Target']  = split(",",$parts[6]);  
+        $entry['Comment'] = $comment;  
+        $this->tasks []   = $entry;
+      }
+    } 
   }
 
 
   function save_csv_data()
   {
-    if(!$this->acl_is_writeable("something")){
-      $this->contents =array(); 
-      print_red(_("Your are not allowed to write the content of this plugin."));
-      return(FALSE);
-    }
-
-    if(!file_exists($this->file_to_read) || !is_writeable($this->file_to_read)){
-      print_red(sprintf(_("Can't locate or write csv storage file '%s'."),$this->file_to_read));
-    }else{
-      $fp = @fopen($this->file_to_read,"w");
-      if(!$fp){
-        print_red(sprintf(_("Can't write csv storage file '%s'."),$this->file_to_read));
-      }else{  
-        $data = "";
-        foreach($this->contents as $val){
-          $data .= $val['MAC'].", ".$val['OG']."\n";
-        }
-        fwrite($fp,$data,strlen($data));
-        fclose($fp);
-      }
-    }
   }
 
 
   function save_object()
   {
     if(isset($_POST['gotomasses'])){
+    }
+  }
 
-      /* Check for input changes */
-      $ogs = $this->get_object_groups();
-      foreach($this->contents as $id => $data){
-        if(isset($_POST['mac_'.$id])){
-          $this->contents[$id]['MAC']       = $_POST['mac_'.$id];
-          $this->contents[$id]['VALID_MAC'] = is_mac($_POST['mac_'.$id]);
-        }
-        if(isset($_POST['og_'.$id]) && in_array_ics($_POST['og_'.$id],$ogs)){
-          $this->contents[$id]['OG'] = $_POST['og_'.$id];
-        }
-      }
-
-      /* check for remove requests */
-      $once = TRUE;
-      foreach($_POST as $name => $value){
-        if(preg_match("/^remove_[0-9]*_(x|y)$/",$name) && $once){
-          $once = FALSE;
-          $id = preg_replace("/^remove_/","",$name);
-          $id = preg_replace("/_(x|y)$/","",$id);
-
-          if(isset($this->contents[$id])){
-            unset($this->contents[$id]);
-          }
-        }
-      }
-
-      /* Write back all changes */
-      if(isset($_POST['save_gotomass_changes'])){
-        $this->save_csv_data();
 
-        /* Call load again, so we will see if everything is fine. 
-         * And load_csv_data causes the contents_backup to be updated 
-         */
-        $this->load_csv_data();
-      }
-
-      /* Reload data from csv file ? */
-      if(isset($_POST['reload_gotomass_data'])){
-        $this->load_csv_data();
-      }
+  /* Return list of object groups */
+  function get_object_groups()
+  {
+    $ret = array();
+    $ldap = $this->config->get_ldap_link();
+    $ldap->cd($this->config->current['BASE']);
+    $ldap->search("(&(objectClass=gosaGroupOfNames)(cn=*))",array("cn"));
+    while($attrs = $ldap->fetch()){
+      $ret[$attrs['cn'][0]] = $attrs['cn'][0];
     }
+    return($ret); 
   }
 
 
index d2a245061b4db7127570bb42d2f04e1be8821626..97eeea1b5bf783471ec43ee8e6fcb821a3b6adab 100644 (file)
-<div class="contentboxh">
- <p class="contentboxh"><img src="{$launchimage}" align="right" alt="[F]">
-       {t}Options{/t}&nbsp;
- </p>
-</div>
-<div class="contentboxb" style="margin-bottom:10px">
+<h2>
+       <img class="center" alt="" src="images/fai_script.png" align="middle" title="{t}Objects{/t}">&nbsp;
+       {t}List of scheduled tasts{/t}
+</h2>
+<table width="100%" summary="" style="border:1px solid #B0B0B0; " cellspacing=0 cellpadding=0>
+       <tr>
+               <td>
+                       {$divlist}
+               </td>
+       </tr>
+</table>
 
-       <table summary="" width="100%" class="contentboxb" style="border-top:1px solid #B0B0B0; padding:0px;">
-               <tr>
-                       <td>
-                               {t}Export{/t}&nbsp;
-                               {if $is_readable}
-                                       <input type='submit' name='export_gotomass_csv' value='{t}Export{/t}'>
-                               {else}
-                                       <input type='button' name='empty_1' value='{t}Export{/t}' disabled>
-                               {/if}
-                       </td>
-                       <td style='width:20px;'>
-                       </td>
-                       <td>
-                               {t}Import{/t}&nbsp;
-               
-                               {if $is_writeable}      
-                                       <input type='file' name='mass_file'>
-                                       <input type='submit' name='import_gotomass_csv' value='{t}Import{/t}'>
-                               {else}
-                                       <input type='file' name='epmpty_file' disabled>
-                                       <input type='button' name='empty_2' value='{t}Import{/t}' disabled>
-                               {/if}
-                       </td>
-               </tr>
-       </table>
-</div>
-       <table summary="" width="100%" class="contentboxb" cellspacing=0>
-               <tr>
-                       <td style='width:40px'>
-                               <b>{t}No.{/t}</b>
-                       </td>
-                       <td style='width:200px'>
-                               <b>{t}Mac address{/t}</b>
-                       </td>
-                       <td>&nbsp;
-                       </td>
-                       <td style='width:40%;'>
-                               <b>{t}Assigned object group{/t}</b>
-                       </td>
-                       <td style='text-align:right ; width:20%;'>
-                               <b>{t}Action{/t}</b>
-                       </td>
-               </tr>
-       {foreach from=$contents item=val key=key}
 
-               {if ($key+2)%2 ==0 }
-               <tr class='rowxp1'>
-               {else}
-               <tr class='rowxp0'>
-               {/if}
-                       <td>
-                               {$key+1}
-                       </td>
-
-                       {if !$contents.$key.VALID_MAC}
-                       <td  style='background-color: #FF0000;' >
-                       {else}
-                       <td>
-                       {/if}
-                               <input name='mac_{$key}' type='text' value='{$contents.$key.MAC}' style='width:200px'>
-                       </td>
-                       <td>
-                       </td>
-                       <td>
-                               <select name='og_{$key}' title='{t}Select object group{/t}'>
-                                {html_options options=$ogs selected=$contents.$key.OG}
-                               </select>
-                       </td>
-                       <td style='text-align:right'>
-                               {if $is_writeable}
-                               <input type='image' name='remove_{$key}' src='images/edittrash.png'
-                                       title='{t}Remove this entry{/t}'>
-                               {else}
-                                       &nbsp;  
-                               {/if}
-                       </td>
-               </tr>
-
-       {/foreach}
-               {if $is_writeable}
-               <tr>
-                       <td>
-                               &nbsp;- 
-                       </td>
-                       <td>
-                               &nbsp;- 
-                       </td>
-                       <td>
-                               &nbsp;- 
-                       </td>
-                       <td>
-                               &nbsp;- 
-                       </td>
-                       <td style='text-align:right'>
-                               <input type='submit' name='add_new_entry' value='{t}New entry{/t}'>
-                       </td>
-               </tr>
-               {/if}
-       </table>
-<div class="contentboxb" style="border-top: 1px solid #B0B0B0; padding:0px; margin-top:10px;">
- <p class="contentboxb" style='text-align:right'>
-       {if $is_writeable}
-               <input type='submit' name='save_gotomass_changes' value='{t}Apply{/t}'>
-       {else}
-               <input type='submit' name='save_gotomass_changes' value='{t}Apply{/t}' disabled>
-       {/if}
-       <input type='submit' name='reload_gotomass_data' value='{t}Cancel{/t}'>
- </p>
-</div>
 <input type='hidden' name='gotomasses' value='1'>
 
 <!-- Display a warning if the content was modified, but not saved yet -->