Code

Updated divlists.
[gosa.git] / plugins / admin / departments / class_departmentManagement.inc
index dbfcab0b4ad7a6797c2b7799ede3d4ab4b4a02e4..f4bb2fbac6c3f9bbd41d690c955c8dd2932e5091 100644 (file)
@@ -39,8 +39,11 @@ class departmentManagement extends plugin
   var $attributes= array();
   var $objectclasses= array();
 
-  /* Tagging || Moving in progress */
-  var $AfterSaveProcedure   = false;
+  /* Vars to handle operations after saving the department 
+     Recursive move && tagging   */
+  var $ObjectInSaveMode         = false;    // Is true, if current object wasn't saved right now
+  var $ObjectTaggingRequested   = false;    // Object must be tagged, an iframe will be shown. 
+  var $RecursiveRemoveRequested = false;    // Is true, if this object must be moved, an iframe will be displayed in this case
 
   function departmentManagement ($config, $ui)
   {
@@ -61,7 +64,7 @@ class departmentManagement extends plugin
       Var init 
      ***************/
 
-    $_SESSION['LOCK_VARS_TO_USE'] = array("/^act$/","/^id$/","/^dep_edit_.*/","/^dep_del_.*/");
+    $_SESSION['LOCK_VARS_TO_USE'] = array("/^act$/","/^id$/","/^dep_edit_.*/","/^dep_del_.*/","/^item_selected/","/^remove_multiple_departments/");
 
     /* Reload departments */
     $smarty                                            = get_smarty();
@@ -89,10 +92,11 @@ class departmentManagement extends plugin
         $s_entry  = preg_replace("/_.*$/","",$s_entry);
         $s_entry  = base64_decode($s_entry);
         // Post for new
+      }elseif(preg_match("/^remove_multiple_departments/",$key)){
+        $s_action="del_multiple";
       }elseif(preg_match("/dep_new.*/",$key)){
         $s_action="new";
       }
-
     }
 
 
@@ -104,8 +108,8 @@ class departmentManagement extends plugin
      */
     if ($s_action=="new"){
       $this->dn= "new";
-      $this->deptabs= new deptabs($this->config,
-          $this->config->data['TABS']['DEPTABS'], $this->dn);
+      $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $this->dn,"department");
+      $this->deptabs->set_acl_base($this->DivListDepartment->selectedBase);
     }
 
 
@@ -117,7 +121,6 @@ class departmentManagement extends plugin
      * The entry which will be edited is defined in $s_entry
      */
     if (( $s_action=="edit") && (!isset($this->deptabs->config))){
-      $s_entry = convert_department_dn(base64_decode($s_entry));
       $this->dn= $this->config->departments[trim($s_entry)];
 
       if (($user= get_lock($this->dn)) != ""){
@@ -127,16 +130,89 @@ class departmentManagement extends plugin
       /* Lock the current entry, so everyone will get the  above dialog */
       add_lock ($this->dn, $this->ui->dn);
 
-      /* Set up the users ACL's for this 'dn' */
-      $this->acl= get_permissions ($this->dn, $this->ui->subtreeACL);
-
       /* Register deptabs to trigger edit dialog */
-      $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $this->dn);
-      $this->deptabs->set_acl($this->acl);
+      $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $this->dn,"department");
+      $this->deptabs->set_acl_base($this->dn);
+
       $_SESSION['objectinfo']= $this->dn;
     }
 
 
+    /********************
+      Delete MULTIPLE entries requested, display confirm dialog
+     ********************/
+
+    if ($s_action=="del_multiple"){
+      $ids = $this->list_get_selected_items();
+
+
+      if(count($ids)){
+        foreach($ids as $id){
+          $id = base64_decode($id);
+          $dn = $this->config->departments[$id];
+  
+          if (($user= get_lock($dn)) != ""){
+            return(gen_locked_message ($user, $dn));
+          }
+          $this->dns[$id] = $dn;
+        }
+
+        $dns_names = "<br><pre>";
+        foreach($this->dns as $dn){
+          add_lock ($dn, $this->ui->dn);
+          $dns_names .= $dn."\n";
+        }
+        $dns_names .="</pre>";
+
+        /* Lock the current entry, so nobody will edit it during deletion */
+        $smarty->assign("info",     sprintf(_("You're about to delete the following department(s) %s"), @LDAP::fix($dns_names)));
+        $smarty->assign("multiple", true);
+        return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
+      }
+    }
+   
+
+    /********************
+      Delete MULTIPLE entries confirmed
+     ********************/
+
+    /* Confirmation for deletion has been passed. Users should be deleted. */
+    if (isset($_POST['delete_multiple_department_confirm'])){
+
+      /* Remove user by user and check acls before removeing them */
+      foreach($this->dns as $key => $dn){
+        $acl = $this->ui->get_permissions($dn,"department/department");
+        if (preg_match('/d/', $acl)){
+
+          /* Delete request is permitted, perform LDAP action */
+          $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $dn,"department");
+          $this->deptabs->set_acl_base();
+          $this->deptabs->delete ();
+          $this->deptabs = NULL;
+          gosa_log ("Department object'".$this->dn."' has been removed");
+        } else {
+          print_red (_("You have no permission to remove this department."));
+        }
+        /* Remove lock file after successfull deletion */
+        del_lock ($dn);
+        unset($this->dns[$key]);
+      }
+    }
+
+    /********************
+      Delete MULTIPLE entries Canceled
+     ********************/
+
+    /* Remove lock */
+    if(isset($_POST['delete_multiple_department_cancel'])){
+      foreach($this->dns as $key => $dn){
+        del_lock ($dn);
+        unset($this->dns[$key]);
+      }
+    }
+
+
     /***************
       Delete entry 
      ***************/
@@ -145,18 +221,24 @@ class departmentManagement extends plugin
      * The entry which will be deleted is defined in $s_entry
      */
     if ($s_action =="del"){
-      $s_entry = convert_department_dn(base64_decode($s_entry));
       $this->dn= $this->config->departments[trim($s_entry)];
 
-      /* Check locking */
-      if (($user= get_lock($this->dn)) != ""){
-        $_SESSION['dn']= $this->dn;
-        return(gen_locked_message($user, $this->dn));
-      } else {
-        add_lock ($this->dn, $this->ui->dn);
-        $smarty->assign("info", sprintf(_("You're about to delete the whole LDAP subtree placed under '%s'."), @LDAP::fix($this->dn)));
-        $display.= $smarty->fetch (get_template_path('remove.tpl', TRUE));
-        return ($display);
+      /* check acls */
+      $acl = $this->ui->get_permissions($this->dn,"department/department");
+      if(preg_match("/d/",$acl)){
+
+        /* Check locking */
+        if (($user= get_lock($this->dn)) != ""){
+          $_SESSION['dn']= $this->dn;
+          return(gen_locked_message($user, $this->dn));
+        } else {
+          add_lock ($this->dn, $this->ui->dn);
+          $smarty->assign("info", sprintf(_("You're about to delete the whole LDAP subtree placed under '%s'."), @LDAP::fix($this->dn)));
+          $display.= $smarty->fetch (get_template_path('remove.tpl', TRUE));
+          return ($display);
+        }
+      }else{
+        print_red (_("You have no permission to remove this department."));
       }
     }
 
@@ -169,9 +251,10 @@ class departmentManagement extends plugin
      * Finally delete department 
      */
     if (isset($_POST['delete_department_confirm'])){
-      $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
-      $acl= get_module_permission($acl, "department", $this->dn);
-      if (chkacl($acl, "delete") == ""){
+
+      /* check acls */
+      $acl = $this->ui->get_permissions($this->dn,"department/department");
+      if(preg_match("/d/",$acl)){
         $this->remove_from_parent();
         gosa_log ("Department object'".$this->dn."' has been removed");
       } else {
@@ -179,14 +262,61 @@ class departmentManagement extends plugin
       }
     }
 
+
+    /***************
+      Edit department finished 
+     ***************/
+
+    if ((isset($_POST['edit_finish'])) && (isset($this->deptabs->config)) && !isset($_POST['dep_move_confirm'])){
+      $this->deptabs->save_object();
+      $obj = $this->deptabs->by_object['department'];
+      if($obj->orig_dn != "new"){
+        if($obj->orig_ou != $obj->ou || $obj->base != $obj->orig_base){
+          return($smarty->fetch(get_template_path("dep_move_confirm.tpl",TRUE)));
+        }
+      }
+    }
+
+    /* Save changes */
+    if ((isset($_POST['edit_finish'])|| isset($_POST['dep_move_confirm'])) && (isset($this->deptabs->config))){
+
+      /* Check tabs, will feed message array */
+      $message= $this->deptabs->check();
+
+      /* Save, or display error message? */
+      if (count($message) == 0){
+
+        $this->deptabs->save(true);
+        $this->config->get_departments();
+
+        /* This object must be tagged, so set ObjectTaggingRequested to true */
+        if($this->deptabs->by_object['department']->must_be_tagged()){
+          $this->ObjectTaggingRequested   = true; 
+        }              
+
+        /* Get recursive move is required, set RecursiveRemoveRequested to true */
+        if($this->deptabs->by_object['department']->am_i_moved()){     
+          $this->RecursiveRemoveRequested = true;
+        }              
+      
+        /* This var indicated that there is an object which isn't saved right now. */
+        $this->ObjectInSaveMode = true;
+
+      } else {
+        /* Ok. There seem to be errors regarding to the tab data,
+           show message and continue as usual. */
+        show_errors($message);
+      }
+    }
+
+
     /***************
       Handle Tagging (Return output for an iframe)
      ***************/
 
-    /* This department must be tagged (Is called from iframe, generates output) */
+    /* This department must be tagged (Is called from iframe, generates output)*/
     if(isset($_GET['TagDepartment'])){
       $this->deptabs->by_object['department']->tag_objects();
-      $this->deptabs->by_object['department']->must_be_tagged = false;
       exit();  
     }
 
@@ -197,57 +327,45 @@ class departmentManagement extends plugin
 
     /* initiate recursive remove  (Is called from iframe, generates output)*/
     if(isset($_GET['PerformRecMove'])){
-      $this->deptabs->by_object['department']->recursive_move();
-      $this->deptabs->by_object['department']->MustBeMoved = false;
+      $this->deptabs->by_object['department']->recursive_move("","",true);
+      $this->DivListDepartment->selectedBase = $this->deptabs->by_object['department']->dn;  
       exit();
     }
 
 
-
     /***************
-      Edit department finished 
+      Return iframes, which call tagging / recusrsive move 
      ***************/
 
-    if (($this->AfterSaveProcedure || isset($_POST['edit_finish'])) && (isset($this->deptabs->config))){
-
-      /* Check tabs, will feed message array */
-      $message= $this->deptabs->check();
-
-      /* Save, or display error message? */
-      if (count($message) == 0){
+    /* While one of these vars below isset, we must return an iframe, 
+       to perform requested operation */
+    if($this->ObjectTaggingRequested){
+      $this->ObjectTaggingRequested = false;
+      return($this->deptabs->by_object['department']->ShowTagFrame());
+    }
+    if($this->RecursiveRemoveRequested){
+      $this->RecursiveRemoveRequested = false;
+      return($this->deptabs->by_object['department']->ShowMoveFrame());
+    }
 
-        /* Save current changes */
-        if(!$this->AfterSaveProcedure){
-          $this->deptabs->save(true);
-        }
 
-        /* Check if we have to tag this department */
-        if($this->deptabs->by_object['department']->must_be_tagged){
-          $this->AfterSaveProcedure = true;
-          $this->deptabs->by_object['department']->must_be_tagged = false;
-          return($this->deptabs->by_object['department']->ShowTagFrame());
-        }
-        
-        /* Saving and tagging, is done now, lets move the dep if required */ 
-        if($this->deptabs->by_object['department']->MustBeMoved){
-          $this->AfterSaveProcedure = true;
-          $this->deptabs->by_object['department']->MustBeMoved = false;
-          return($this->deptabs->by_object['department']->ShowMoveFrame());
-        }
+    /***************
+      In case of tagging/moving the object wasn't deleted, do it know
+     ***************/
 
-        if ($this->dn != "new"){
-          del_lock ($this->dn);
-        }
-        gosa_log ("Department object'".$this->dn."' has been saved");
-        unset ($this->deptabs);
-        $this->deptabs= NULL;
-        $this->AfterSaveProcedure = false;
-        unset ($_SESSION['objectinfo']);
-      } else {
-        /* Ok. There seem to be errors regarding to the tab data,
-           show message and continue as usual. */
-        show_errors($message);
+    /* If there is an unsaved object and all operations are done
+       remove locks & save object tab & unset current object */
+    if($this->ObjectInSaveMode && (!$this->RecursiveRemoveRequested) && (!$this->ObjectTaggingRequested)){
+      $this->deptabs->save();
+      $this->config->get_departments();
+      $this->ObjectInSaveMode = false;
+      if ($this->dn != "new"){
+        del_lock ($this->dn);
       }
+      gosa_log ("Department object'".$this->dn."' has been saved");
+      unset ($this->deptabs);
+      $this->deptabs= NULL;
+      unset ($_SESSION['objectinfo']);
     }
 
 
@@ -272,19 +390,13 @@ class departmentManagement extends plugin
       $display= $this->deptabs->execute();
       if (!$this->deptabs->by_object[$this->deptabs->current]->dialog){
         $display.= "<p style=\"text-align:right\">\n";
-        $display.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
+        $display.= "<input type=submit name=\"edit_finish\" value=\""._("Save")."\">\n";
         $display.= "&nbsp;\n";
         $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
         $display.= "</p>";
       }
       return ($display);
     }else{
-
-      /* Check if there is a snapshot dialog open */
-      if($str = $this->showSnapshotDialog($this->DivListDepartment->selectedBase,$this->DivListDepartment->selectedBase)){
-        return($str);
-      }
-
       /* Display dialog with group list */
       $this->DivListDepartment->parent = $this;
       $this->DivListDepartment->execute();
@@ -320,28 +432,37 @@ class departmentManagement extends plugin
 
     if($this->DivListDepartment->SubSearch){
       $res= get_list("(&(|(ou=$Regex)(description=$Regex))(objectClass=gosaDepartment))",
-          $this->ui->subtreeACL, $base, array("ou", "description"), GL_SIZELIMIT | GL_SUBSEARCH);
+          "department", $base, array("ou", "description"), GL_SIZELIMIT | GL_SUBSEARCH);
     }else{
       $res= get_list("(&(|(ou=$Regex)(description=$Regex))(objectClass=gosaDepartment))",
-          $this->ui->subtreeACL, $base, array("ou", "description"), GL_SIZELIMIT );
+          "department", $base, array("ou", "description"), GL_SIZELIMIT );
     }
 
     $this->departments= array();
-    $tmp = array();
-    foreach ($res as $key => $value){
-      #$tmp[get_sub_department(convert_department_dn($value['dn']))] = $key;
-      $tmp[$key] = get_sub_department(convert_department_dn($value['dn']));
+
+    /* Add current base to the list of available departments, but only if its naming attribute is 'ou' */
+    if(preg_match("/^ou=/",$base)){
+      $this->departments [ convert_department_dn($base) ] = ".";
     }
-    natcasesort($tmp);
-    foreach($tmp as $key => $q){
-      $value = $res [$key];
+
+    foreach ($res as $key => $value){
+
       /* Don't display base as entry on subsearch */
       if(($value['dn'] == $base) && ($this->DivListDepartment->SubSearch)){
         continue;
       }
 
-      $this->departments[base64_encode($value['dn'])]= $value;
+      $cdn= convert_department_dn($value['dn']);
+
+      /* Append to dep list */
+      if(isset($value["description"][0])){
+        $this->departments[$cdn]= get_sub_department($cdn)." - [".$value["description"][0]."]";
+      }else{
+        $this->departments[$cdn]= get_sub_department($cdn);//$value["description"][0];
+      }
     }
+    natcasesort ($this->departments);
+    reset ($this->departments);
   }
 
   function remove_from_parent()
@@ -364,6 +485,20 @@ class departmentManagement extends plugin
 
   }
 
+
+  function list_get_selected_items()
+  {
+    $ids = array();
+    foreach($_POST as $name => $value){
+      if(preg_match("/^item_selected_[a-z0-9\=]*$/i",$name)){
+        $id   = preg_replace("/^item_selected_/","",$name);
+        $ids[$id] = $id;
+      }
+    }
+    return($ids);
+  }
+
+
   function remove_lock()
   {
     if (isset($this->dn)){
@@ -375,12 +510,12 @@ class departmentManagement extends plugin
   {
     /* reload department */
     $this->config->get_departments();
-    $config = $this->config;
 
     $this->config->make_idepartments();
+    $this->DivListDepartment->config= $this->config;
     $this->DivListDepartment->save_object();
   }
-  
+
 }
 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
 ?>