Code

Removed duplicated code
[gosa.git] / plugins / admin / departments / class_departmentGeneric.inc
1 <?php
2 /*
3   This code is part of GOsa (https://gosa.gonicus.de)
4   Copyright (C) 2003  Cajus Pollmeier
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2 of the License, or
9   (at your option) any later version.
11   This program is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
16   You should have received a copy of the GNU General Public License
17   along with this program; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
21 class department extends plugin
22 {
23         /* department attributes */
24         var $ou= "";
25         var $description= "";
26         var $base= "";
27         var $st= "";
28         var $l= "";
29         var $postalAddress= "";
30         var $businessCategory= "";
31         var $telephoneNumber= "";
32         var $facsimileTelephoneNumber= "";
33         var $orig_dn= "";
34         var $is_administrational_unit= false;
35         var $gosaUnitTag= "";
37         var $rec_dst=false;     // Destination for recursive move
38         var $rec_src=false;     // Source for recursive move 
39         var $rec_cpy=false;     // Is recursive move requested ? 
41         /* Headpage attributes */
42         var $last_dep_sorting= "invalid";
43         var $departments= array();
45   var $must_be_tagged = false;
47         /* attribute list for save action */
48         var $attributes= array("ou", "description", "businessCategory", "st", "l", "postalAddress",
49                         "telephoneNumber", "facsimileTelephoneNumber", "gosaUnitTag");
50         var $objectclasses= array("top", "gosaDepartment", "organizationalUnit");
52         function department ($config, $dn)
53         {
55                 plugin::plugin($config, $dn);
56                 $this->is_account= TRUE;
57                 $this->ui= get_userinfo();
58                 $this->dn= $dn;
59                 $this->orig_dn= $dn;
60                 $this->config= $config;
62                 /* Set base */
63                 if ($this->dn == "new"){
64                         $ui= get_userinfo();
65                         if(isset($_SESSION['CurrentMainBase'])){
66                                 $this->base= $_SESSION['CurrentMainBase'];
67                         }else{
68                                 $this->base= dn2base($ui->dn);
69                         }
70                 } else {
71                         $this->base= preg_replace ("/^[^,]+,/", "", $this->dn);
72                 }
74                 /* set permissions */
75                 $ui= get_userinfo();
76                 $acl= get_permissions ($ui->dn, $ui->subtreeACL);
77                 $this->acl= get_module_permission($acl, "department", $ui->dn);
79                 /* Is administrational Unit? */
80                 if ($dn != "new" && in_array_ics('gosaAdministrativeUnit', $this->attrs['objectClass'])){
81                         $this->is_administrational_unit= true;
82                 }
83         }
85         function execute()
86         {
87                 /* Call parent execute */
88                 plugin::execute();
90                 /* Reload departments */
91                 $this->config->get_departments($this->dn);
92                 $this->config->make_idepartments();
93                 $smarty= get_smarty();
95                 /* Base select dialog */
96                 $once = true;
97                 foreach($_POST as $name => $value){
98                         if(preg_match("/^chooseBase/",$name) && $once){
99                                 $once = false;
100                                 $this->dialog = new baseSelectDialog($this->config);
101                                 $this->dialog->setCurrentBase($this->base);
102                         }
103                 }
105                 /* Dialog handling */
106                 if(is_object($this->dialog)){
107                         /* Must be called before save_object */
108                         $this->dialog->save_object();
110                         if($this->dialog->isClosed()){
111                                 $this->dialog = false;
112                         }elseif($this->dialog->isSelected()){
113                                 $this->base = $this->dialog->isSelected();
114                                 $this->dialog= false;
115                         }else{
116                                 return($this->dialog->execute());
117                         }
118                 }
120                 /* Hide all departments, that are subtrees of this department */
121                 $bases  = $this->config->idepartments;
122                 if(($this->dn == "new")||($this->dn == "")){
123                         $tmp = $bases;
124                 }else{
125                         $tmp    = array();      
126                         foreach($bases as $dn=>$base){
127                                 $fixed = str_replace("/","\\",$this->dn);
128                                 /* Only attach departments which are not a subtree of this one */
129                                 if(!preg_match("/".$fixed."/",$dn)){
130                                         $tmp[$dn]=$base;
131                                 }
132                         }
133                 }
134                 $smarty->assign("bases", $tmp);
136                 foreach ($this->attributes as $val){
137                         $smarty->assign("$val", $this->$val);
138                         $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
139                 }
140                 $smarty->assign("base_select", $this->base);
142                 /* Set admin unit flag */
143                 if ($this->is_administrational_unit) {
144                         $smarty->assign("unitTag", "checked");
145                 } else {
146                         $smarty->assign("unitTag", "");
147                 }
148                 $smarty->assign("unitTag"."ACL", chkacl($this->acl, "unitTag"));
150                 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
151         }
153         function clear_fields()
154         {
155                 $this->dn= "";
156                 $this->base= "";
157                 $this->acl= "#none#";
159                 foreach ($this->attributes as $val){
160                         $this->$val= "";
161                 }
162         }
165         function remove_from_parent()
166         {
167                 $ldap= $this->config->get_ldap_link();
168                 $ldap->cd ($this->dn);
169                 $ldap->recursive_remove();
171                 /* Optionally execute a command after we're done */
172                 $this->handle_post_events('remove');
173         }
176         /* Save data to object */
177         function save_object()
178         {
179                 if (isset($_POST['base'])){
180                         plugin::save_object();
182                         /* Save base, since this is no LDAP attribute */
183                         if (chkacl($this->acl, "create") == ""){
184                                 $this->base= $_POST['base'];
185                         }
187                         /* Save tagging flag */
188                         if (chkacl($this->acl, "unitTag") == ""){
189                                 if (isset($_POST['unitTag'])){
190                                         $this->is_administrational_unit= true;
191                                 } else {
192                                         $this->is_administrational_unit= false;
193                                 }
194                         }
195                 }
196         }
199         /* Check values */
200         function check()
201         {
202                 /* Call common method to give check the hook */
203                 $message= plugin::check();
205                 /* Permissions for that base? */
206                 //      $this->dn= "ou=$this->ou,".$this->base;
207                 if (chkacl($this->acl, "create") != ""){
208                         $message[]= _("You have no permissions to create a department on this 'Base'.");
209                 }
211                 /* Check for presence of this department */
212                 $ldap= $this->config->get_ldap_link();
213                 $attrs= $ldap->cat ($this->dn, array('dn'));
214                 if ($this->orig_dn == "new" && !($attrs === FALSE)){
215                         $message[]= _("Department with that 'Name' already exists.");
216                 } elseif ($this->orig_dn != $this->dn && !($attrs === FALSE)){
217                         $message[]= _("Department with that 'Name' already exists.");
218                 }
220                 /* All required fields are set? */
221                 if ($this->ou == ""){
222                         $message[]= _("Required field 'Name' is not set.");
223                 }
224                 if ($this->description == ""){
225                         $message[]= _("Required field 'Description' is not set.");
226                 }
228                 /* Validate and modify - or: spaghetti rules! */
229                 $SkipNames = array(     "incoming","apps","systems","fai","config",     
230                                 preg_replace("/ou=(.*),/","\\1",get_people_ou()),
231                                 preg_replace("/ou=(.*),/","\\1",get_groups_ou())
232                                 );      
233                 foreach($SkipNames as $name){
234                         if ($this->ou == $name){
235                                 $message[]= sprintf(_("The field 'Name' contains the reserved word '%s'. Please choose another name."),$name);
236                         }
237                 }
240                 if (preg_match ('/[#+:=>\\\\\/]/', $this->ou)){
241                         $message[]= _("The field 'Name' contains invalid characters.");
242                 }
243                 if (!is_phone_nr($this->telephoneNumber)){
244                         $message[]= _("The field 'Phone' contains an invalid phone number.");
245                 }
246                 if (!is_phone_nr($this->facsimileTelephoneNumber)){
247                         $message[]= _("The field 'Fax' contains an invalid phone number.");
248                 }
250                 return $message;
251         }
254         /* Save to LDAP */
255         function save()
256         {
257                 $ldap= $this->config->get_ldap_link();
259                 /* Add tag objects if needed */
260                 if ($this->is_administrational_unit){
261                         $this->objectclasses[]= "gosaAdministrativeUnit";
262                         if ($this->gosaUnitTag == ""){
264                                 /* It's unlikely, but check if already used... */
265                                 $try= 5;
266                                 $ldap->cd($this->config->current['BASE']);
267                                 while ($try--){
269                                         /* Generate microtime stamp as tag */
270                                         list($usec, $sec)= explode(" ", microtime());
271                                         $time_stamp= preg_replace("/\./", "", $sec.$usec);
273                                         $ldap->search("(&(objectClass=gosaAdministrativeUnit)(gosaUnitTag=$time_stamp))",array("gosaUnitTag"));
274                                         if ($ldap->count() == 0){
275                                                 break;
276                                         }
277                                 }
278                                 if($try == 0) {
279                                         print_red(_("Fatal error: Can't find an unused tag to mark the administrative unit!"));
280                                         return;
281                                 }
282                                 $this->gosaUnitTag= preg_replace("/\./", "", $sec.$usec);
283                         }
284                 } else {
285                         $this->gosaUnitTag= "";
286                 }
288                 plugin::save();
290                 /* Remove tag information if needed */
291                 if (!$this->is_administrational_unit){
292                         $tmp= array();
294                         /* Remove gosaAdministrativeUnit from this plugin */
295                         foreach($this->attrs['objectClass'] as $oc){
296                                 if (!preg_match("/^gosaAdministrativeUnit$/i", $oc)){
297                                         $tmp[]= $oc;
298                                 }
299                         }
300                         $this->attrs['objectClass']= $tmp;
301                         if(isset($this->attrs['gosaUnitTag'])){
302                                 $this->attrs['gosaUnitTag']= array();
303                         }
304                 }
306                 /* Write back to ldap */
307                 $ldap= $this->config->get_ldap_link();
308                 $ldap->cat($this->dn, array('dn'));
309                 $a= $ldap->fetch();
310                 $ldap->cd($this->dn);
312                 if (count($a)){
313                         $this->cleanup();
314                         $ldap->modify ($this->attrs); 
316                         $this->handle_post_events('modify');
317                 } else {
318                         $ldap->add($this->attrs);
319                         $this->handle_post_events('add');
320                 }
321                 show_ldap_error($ldap->get_error());
323                 
324     /* Optionally execute a command after we're done */
325     $this->must_be_tagged =true;
326                 $this->postcreate();
327     return(true);
328         }
331   function ShowTagFrame()
332   {
333     $smarty = get_smarty();
334     $smarty->assign("src","?plug=".$_GET['plug']."&TagDepartment");
335     $display=  $smarty->fetch(get_template_path("recursive_move.tpl",TRUE));
336     return($display);
337   }
339         /* Tag objects to have the gosaAdministrativeUnitTag */
340         function tag_objects()
341         {
342                 $add= $this->is_administrational_unit;
343                 $len= strlen($this->dn);
344                 $ldap= $this->config->get_ldap_link();
345                 $ldap->cd($this->dn);
346                 if ($add){
347                         $ldap->search('(!(&(objectClass=gosaAdministrativeUnitTag)(gosaUnitTag='.
348                                                                 $this->gosaUnitTag.')))', array('dn'));
349                 } else {
350                         $ldap->search('objectClass=gosaAdministrativeUnitTag', array('dn'));
351                 }
352                 while ($attrs= $ldap->fetch()){
354                         /* Skip self */
355                         if ($attrs['dn'] == $this->dn){
356                                 continue;
357                         }
359                         /* Check for confilicting administrative units */
360                         $fix= true;
361                         foreach ($this->config->adepartments as $key => $tag){
362                                 /* This one is shorter than our dn, its not relevant... */
363                                 if ($len >= strlen($key)){
364                                         continue;
365                                 }
367                                 /* This one matches with the latter part. Break and don't fix this entry */
368                                 if (preg_match('/(^|,)'.normalizePreg($key).'$/', $attrs['dn'])){
369                                         $fix= false;
370                                         break;
371                                 }
372                         }
374                         /* Fix entry if needed */
375                         if ($fix){
376         echo "asdfasdfasdfasdf";
377                                 $this->handle_object_tagging($attrs['dn'], $this->gosaUnitTag, true);
378                         }
379                 }
380                         echo '<p class="seperator">&nbsp;</p>';
382                         echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
383                                 <br><input type='submit' name='back' value='"._("Continue")."'>
384                                 </form></div>";
386         }
389         /* Move/Rename complete trees */
390         function recursive_move($src_dn, $dst_dn,$force = false)
391         {
392                 if(!$force){
394                         $this->rec_cpy  = true;
395                         $this->rec_src  = $src_dn;
396                         $this->rec_dst  = $dst_dn;
398                         $smarty = get_smarty();
400                         $smarty->assign("src","?plug=".$_GET['plug']."&PerformRecMove");
402                         $display=  $smarty->fetch(get_template_path("recursive_move.tpl",TRUE));
403                         return($display);
405                 }else{
406                         if(!$this->rec_cpy){ 
407                                 return;
408                         }
410                         $src_dn = $this->rec_src;
411                         $dst_dn = $this->rec_dst;
413                         /* Print header to have styles included */
414                         $smarty= get_smarty();
415                         echo "<!-- headers.tpl-->".$smarty->fetch(get_template_path('headers.tpl'));
416                         echo "<body style='background-image:none;margin:3px;color:black'>";
418                         echo "<h3>".sprintf(_("Moving '%s' to '%s'"),"<i>".$src_dn."</i>","<i>".$dst_dn."</i>")."</h3>";
421                         /* Check if the destination entry exists */
422                         $ldap= $this->config->get_ldap_link();
424                         /* Check if destination exists - abort */
425                         $ldap->cat($dst_dn, array('dn'));
426                         if ($ldap->fetch()){
427                                 trigger_error("Recursive_move $dst_dn already exists.",
428                                                 E_USER_WARNING);
429                                 echo sprintf_("Recursive_move: '%s' already exists", $dst_dn)."<br>"; 
430                                 return (FALSE);
431                         }
433                         /* Perform a search for all objects to be moved */
434                         $objects= array();
435                         $ldap->cd($src_dn);
436                         $ldap->search("(objectClass=*)", array("dn"));
437                         while($attrs= $ldap->fetch()){
438                                 $dn= $attrs['dn'];
439                                 $objects[$dn]= strlen($dn);
440                         }
442                         /* Sort objects by indent level */
443                         asort($objects);
444                         reset($objects);
446                         /* Copy objects from small to big indent levels by replacing src_dn by dst_dn */
447                         foreach ($objects as $object => $len){
450                                 $src= str_replace("\\","\\\\",$object);
451                                 $dst= preg_replace("/".str_replace("\\","\\\\",$src_dn)."$/", "$dst_dn", $object);
452                                 $dst= str_replace($src_dn,$dst_dn,$object);
454                                 echo "<b>"._("Object").":</b> $src<br>";
456                                 if (!$this->copy($src, $dst)){
457                                         echo "<font color='#FF0000'><br>".sprintf(_("FAILED to copy %s, aborting operation"),$src)."</font>";
458                                         return (FALSE);
459                                 }
461                                 flush();
462                         }
464                         /* Remove src_dn */
465                         $ldap->cd($src_dn);
466                         $ldap->recursive_remove();
467                         $this->rec_src = $this->rec_dst = "";
468                         $this->rec_cpy =false;
470                         echo '<p class="seperator">&nbsp;</p>';
472                         echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
473                                 <br><input type='submit' name='back' value='"._("Continue")."'>
474                                 </form></div>";
476                         echo "</body></html>";
478                         return (TRUE);
479                 }
480         }
482         function must_be_tagged()
483         {
484                 return $this->must_be_tagged;
485         }
487         function am_i_moved()
488         {
489                 return $this->rec_cpy;
490         }
493 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
494 ?>