Code

Fixed removing, too
[gosa.git] / include / class_plugin.inc
index 0926763c92172d08f8ec41c2d7172f3a5c04a4c8..c769b8fc32a7a9e52ba2c2901928c293c50ad7de 100644 (file)
@@ -83,6 +83,8 @@ class plugin
    */
   var $attrs= array();
 
+  /* Save unit tags */
+  var $gosaUnitTag= "";
 
   /*!
     \brief Used standard values
@@ -130,13 +132,17 @@ class plugin
 
       /* Copy needed attributes */
       foreach ($this->attributes as $val){
-        #if (isset($this->attrs["$val"][0])){
         $found= array_key_ics($val, $this->attrs);
         if ($found != ""){
           $this->$val= $this->attrs["$found"][0];
         }
       }
 
+      /* gosaUnitTag loading... */
+      if (isset($this->attrs['gosaUnitTag'][0])){
+        $this->gosaUnitTag= $this->attrs['gosaUnitTag'][0];
+      }
+
       /* Set the template flag according to the existence of objectClass
          gosaUserTemplate */
       if (isset($this->attrs['objectClass'])){
@@ -319,7 +325,6 @@ class plugin
       }
     }
 
-    $this->handle_object_tagging();
   }
 
 
@@ -850,20 +855,103 @@ class plugin
   }
 
 
-  function handle_object_tagging()
+  function handle_object_tagging($dn= "", $tag= "")
   {
-    echo "Handle tagging<br>";
-    /* Watch out for an administrative unit below own base */
-    #echo "<b>handle_object_tagging()</b><br><pre>";
-    #echo "DN  : ".$this->dn."\n";
-    #echo "Base: ".$this->config->current['BASE']."\n";
-    #echo "</pre>";
-
-    /* Make a base search on every department */
-    //FIXME: evaluate if these informations should be cached, too
-    #$parts= split(',', preg_replace("/,".normalizePreg($this->config->current['BASE'])."$/", '', $this->dn));
-
-    /* Set objectclass and attribute */
+    //FIXME: How to optimize this? We have at least two
+    //       LDAP accesses per object. It would be a good
+    //       idea to have it integrated.
+  
+    /* No dn? Self-operation... */
+    if ($dn == ""){
+      $dn= $this->dn;
+    
+      /* No tag? Find it yourself... */
+      if ($tag == ""){
+        $len= strlen($dn);
+
+        echo "DEBUG: No tag for $dn - looking for one...<br>";
+        $relevant= array();
+        foreach ($this->config->adepartments as $key => $ntag){
+
+          /* This one is bigger than our dn, its not relevant... */
+          if ($len <= strlen($key)){
+            continue;
+          }
+
+          /* This one matches with the latter part. Break and don't fix this entry */
+          if (preg_match('/(^|,)'.normalizePreg($key).'$/', $dn)){
+            echo "DEBUG: Possibly relevant: $key<br>";
+            $relevant[strlen($key)]= $ntag;
+            continue;
+          }
+
+        }
+
+        /* If we've some relevant tags to set, just get the longest one */
+        if (count($relevant)){
+          ksort($relevant);
+          $tmp= array_keys($relevant);
+          $idx= end($tmp);
+          $tag= $relevant[$idx];
+          $this->gosaUnitTag= $tag;
+        }
+      }
+    }
+
+    /* Set tag? */
+    if ($tag != ""){
+      /* Set objectclass and attribute */
+      $ldap= $this->config->get_ldap_link();
+      $ldap->cat($dn);
+      $attrs= $ldap->fetch();
+      if(isset($attrs['gosaUnitTag'][0]) && $attrs['gosaUnitTag'][0] == $tag){
+        echo "DEBUG: $dn is already tagged<br>";
+        return;
+      }
+      if (count($attrs)){
+        echo "DEBUG: Add tagging ($tag) to $dn.<br>";
+        $nattrs= array("gosaUnitTag" => $this->gosaUnitTag);
+        $nattrs['objectClass']= array();
+        for ($i= 0; $i<$attrs['objectClass']['count']; $i++){
+          $oc= $attrs['objectClass'][$i];
+          if ($oc != "gosaAdministrativeUnitTag"){
+            $nattrs['objectClass'][]= $oc;
+          }
+        }
+        $nattrs['objectClass'][]= "gosaAdministrativeUnitTag";
+        $ldap->cd($dn);
+        $ldap->modify($nattrs);
+      } else {
+        echo "DEBUG: not tagging ($tag) $dn - seems to have moved away<br>";
+      }
+      
+    } else {
+      /* Remove objectclass and attribute */
+      $ldap= $this->config->get_ldap_link();
+      $ldap->cat($dn);
+      $attrs= $ldap->fetch();
+      if (!in_array_ics("gosaAdministrativeUnitTag", $attrs['objectClass'])){
+        echo "DEBUG: $dn is not tagged<br>";
+        return;
+      }
+      if (count($attrs)){
+        echo "DEBUG: Remove tagging from $dn.<br>";
+        $nattrs= array("gosaUnitTag" => array());
+        $nattrs['objectClass']= array();
+        for ($i= 0; $i<$attrs['objectClass']['count']; $i++){
+          $oc= $attrs['objectClass'][$i];
+          if ($oc != "gosaAdministrativeUnitTag"){
+            $nattrs['objectClass'][]= $oc;
+          }
+        }
+        $ldap->cd($dn);
+        $ldap->modify($nattrs);
+        show_ldap_error($ldap->get_error());
+      } else {
+        echo "DEBUG: not removing tag ($tag) $dn - seems to have moved away<br>";
+      }
+    }
     
   }