Code

64d631deb83355f3e6e9d874f50cdac520e24764
[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("baseACL", chkacl($this->acl,"base"));
141                 $smarty->assign("base_select", $this->base);
143                 /* Set admin unit flag */
144                 if ($this->is_administrational_unit) {
145                         $smarty->assign("unitTag", "checked");
146                 } else {
147                         $smarty->assign("unitTag", "");
148                 }
149                 $smarty->assign("unitTag"."ACL", chkacl($this->acl, "unitTag"));
151                 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
152         }
154         function clear_fields()
155         {
156                 $this->dn= "";
157                 $this->base= "";
158                 $this->acl= "#none#";
160                 foreach ($this->attributes as $val){
161                         $this->$val= "";
162                 }
163         }
165         function remove_from_parent()
166         {
167                 $ldap= $this->config->get_ldap_link();
168                 $ldap->cd ($this->dn);
169                 $ldap->recursive_remove();
170                 show_ldap_error($ldap->get_error(), _("Removing department failed"));
172                 /* Optionally execute a command after we're done */
173                 $this->handle_post_events('remove');
174         }
176         function must_be_tagged()
177         {
178                 return $this->must_be_tagged;
179         }
181         function am_i_moved()
182         {
183                 return $this->rec_cpy;
184         }
187         /* Save data to object */
188         function save_object()
189         {
190                 if (isset($_POST['base'])){
191                         plugin::save_object();
193                         /* Save base, since this is no LDAP attribute */
194                         if (chkacl($this->acl, "create") == ""){
195                                 $this->base= $_POST['base'];
196                         }
198                         /* Save tagging flag */
199                         if (chkacl($this->acl, "unitTag") == ""){
200                                 if (isset($_POST['unitTag'])){
201                                         $this->is_administrational_unit= true;
202                                 } else {
203                                         $this->is_administrational_unit= false;
204                                 }
205                         }
206                 }
207         }
210         /* Check values */
211         function check()
212         {
213                 /* Call common method to give check the hook */
214                 $message= plugin::check();
216                 /* Permissions for that base? */
217                 //      $this->dn= "ou=$this->ou,".$this->base;
218                 if (chkacl($this->acl, "create") != ""){
219                         $message[]= _("You have no permissions to create a department on this 'Base'.");
220                 }
222                 /* Check for presence of this department */
223                 $ldap= $this->config->get_ldap_link();
224                 $attrs= $ldap->cat ($this->dn, array('dn'));
225                 if ($this->orig_dn == "new" && !($attrs === FALSE)){
226                         $message[]= _("Department with that 'Name' already exists.");
227                 } elseif ($this->orig_dn != $this->dn && !($attrs === FALSE)){
228                         $message[]= _("Department with that 'Name' already exists.");
229                 }
231                 /* All required fields are set? */
232                 if ($this->ou == ""){
233                         $message[]= _("Required field 'Name' is not set.");
234                 }
235                 if ($this->description == ""){
236                         $message[]= _("Required field 'Description' is not set.");
237                 }
239     if(is_department_name_reserved($this->ou,$this->base)){
240       $message[]= sprintf(_("The field 'Name' contains the reserved word '%s'. Please choose another name."),$this->ou);
241     }
243                 if (preg_match ('/[#+:=>\\\\\/]/', $this->ou)){
244                         $message[]= _("The field 'Name' contains invalid characters.");
245                 }
246                 if (!is_phone_nr($this->telephoneNumber)){
247                         $message[]= _("The field 'Phone' contains an invalid phone number.");
248                 }
249                 if (!is_phone_nr($this->facsimileTelephoneNumber)){
250                         $message[]= _("The field 'Fax' contains an invalid phone number.");
251                 }
253                 return $message;
254         }
257         /* Save to LDAP */
258         function save()
259         {
260                 $ldap= $this->config->get_ldap_link();
262                 /* Add tag objects if needed */
263                 if ($this->is_administrational_unit){
264       if(!in_array_ics("gosaAdministrativeUnit",$this->objectclasses)){
265                         $this->objectclasses[]= "gosaAdministrativeUnit";
266       }
267                         if ($this->gosaUnitTag == ""){
269                                 /* It's unlikely, but check if already used... */
270                                 $try= 5;
271                                 $ldap->cd($this->config->current['BASE']);
272                                 while ($try--){
274                                         /* Generate microtime stamp as tag */
275                                         list($usec, $sec)= explode(" ", microtime());
276                                         $time_stamp= preg_replace("/\./", "", $sec.$usec);
278                                         $ldap->search("(&(objectClass=gosaAdministrativeUnit)(gosaUnitTag=$time_stamp))",array("gosaUnitTag"));
279                                         if ($ldap->count() == 0){
280                                                 break;
281                                         }
282                                 }
283                                 if($try == 0) {
284                                         print_red(_("Fatal error: Can't find an unused tag to mark the administrative unit!"));
285                                         return;
286                                 }
287                                 $this->gosaUnitTag= preg_replace("/\./", "", $sec.$usec);
288                         }
289                 } else {
290                         $this->gosaUnitTag= "";
291                 }
293                 plugin::save();
295                 /* Remove tag information if needed */
296                 if (!$this->is_administrational_unit){
297                         $tmp= array();
299                         /* Remove gosaAdministrativeUnit from this plugin */
300                         foreach($this->attrs['objectClass'] as $oc){
301                                 if (!preg_match("/^gosaAdministrativeUnit$/i", $oc)){
302                                         $tmp[]= $oc;
303                                 }
304                         }
305                         $this->attrs['objectClass']= $tmp;
306                         if(isset($this->attrs['gosaUnitTag'])){
307                                 $this->attrs['gosaUnitTag']= array();
308                         }
309                 }
311                 /* Write back to ldap */
312                 $ldap= $this->config->get_ldap_link();
313                 $ldap->cat($this->dn, array('dn'));
314                 $a= $ldap->fetch();
315                 $ldap->cd($this->dn);
317                 if (count($a)){
318                         $this->cleanup();
319                         $ldap->modify ($this->attrs); 
321                         $this->handle_post_events('modify');
322                 } else {
323                         $ldap->add($this->attrs);
324                         $this->handle_post_events('add');
325                 }
326                 show_ldap_error($ldap->get_error(), _("Saving department failed"));
328     /* The parameter forces only to set must_be_tagged, and don't touch any objects 
329         This will be done later */
330     $this->tag_objects(true);
331     
332     /* Optionally execute a command after we're done */
333                 $this->postcreate();
334     return(true);
335         }
338   function ShowMoveFrame()
339   {
340     $smarty = get_smarty();
341     $smarty->assign("src","?plug=".$_GET['plug']."&PerformRecMove");
342     $smarty->assign("message","As soon as the move operation has finished, you can scroll down to end of the page and press the 'Continue' button to continue with the department management dialog.");
343     $display=  $smarty->fetch(get_template_path("dep_iframe.tpl",TRUE));
344     return($display);
345   }
347   function ShowTagFrame()
348   {
349     $smarty = get_smarty();
350     $smarty->assign("src","?plug=".$_GET['plug']."&TagDepartment");
351     $smarty->assign("message","As soon as the tag operation has finished, you can scroll down to end of the page and press the 'Continue' button to continue with the department management dialog.");
352     $display=  $smarty->fetch(get_template_path("dep_iframe.tpl",TRUE));
353     return($display);
354   }
356         /* Tag objects to have the gosaAdministrativeUnitTag */
357         function tag_objects($OnlySetTagFlag = false)
358         {
359     if(!$OnlySetTagFlag){
360       $smarty= get_smarty();
361       echo "<!-- headers.tpl-->".$smarty->fetch(get_template_path('headers.tpl'));
362       echo "<body style='background-image:none;margin:3px;color:black'>";
363       echo "<h3>".sprintf(_("Tagging '%s'."),"<i>".@LDAP::fix($this->dn)."</i>")."</h3>";
364     }
366                 $add= $this->is_administrational_unit;
367                 $len= strlen($this->dn);
368                 $ldap= $this->config->get_ldap_link();
369                 $ldap->cd($this->dn);
370                 if ($add){
371                         $ldap->search('(!(&(objectClass=gosaAdministrativeUnitTag)(gosaUnitTag='.
372                                                                 $this->gosaUnitTag.')))', array('dn'));
373                 } else {
374                         $ldap->search('objectClass=gosaAdministrativeUnitTag', array('dn'));
375                 }
376                 while ($attrs= $ldap->fetch()){
378                         /* Skip self */
379                         if ($attrs['dn'] == $this->dn){
380                                 continue;
381                         }
383                         /* Check for confilicting administrative units */
384                         $fix= true;
385                         foreach ($this->config->adepartments as $key => $tag){
386                                 /* This one is shorter than our dn, its not relevant... */
387                                 if ($len >= strlen($key)){
388                                         continue;
389                                 }
391                                 /* This one matches with the latter part. Break and don't fix this entry */
392                                 if (preg_match('/(^|,)'.normalizePreg($key).'$/', $attrs['dn'])){
393                                         $fix= false;
394                                         break;
395                                 }
396                         }
398       /* Fix entry if needed */
399       if ($fix){
400         if($OnlySetTagFlag){
401           $this->must_be_tagged =true;
402           return;
403         }
404                                 $this->handle_object_tagging($attrs['dn'], $this->gosaUnitTag, TRUE );
405                         }
406                 }
407     if(!$OnlySetTagFlag){
408       echo '<p class="seperator">&nbsp;</p>';
409       echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
410         <br><input type='submit' name='back' value='"._("Continue")."'>
411         </form></div>";
412     }
413         }
416         /* Move/Rename complete trees */
417         function recursive_move($src_dn, $dst_dn,$force = false)
418         {
419     /* If force == false prepare to recursive move this object from src to dst 
420         on the next call. */
421                 if(!$force){
422                         $this->rec_cpy  = true;
423                         $this->rec_src  = $src_dn;
424                         $this->rec_dst  = $dst_dn;
425                 }else{
427       /* If this is called, but not wanted, abort */
428                         if(!$this->rec_cpy){ 
429                                 return;
430                         }
432                         $src_dn = $this->rec_src;
433                         $dst_dn = $this->rec_dst;
435                         /* Print header to have styles included */
436                         $smarty= get_smarty();
437                         echo "<!-- headers.tpl-->".$smarty->fetch(get_template_path('headers.tpl'));
438                         echo "<body style='background-image:none;margin:3px;color:black'>";
440                         echo "<h3>".sprintf(_("Moving '%s' to '%s'"),"<i>".$src_dn."</i>","<i>".$dst_dn."</i>")."</h3>";
443                         /* Check if the destination entry exists */
444                         $ldap= $this->config->get_ldap_link();
446                         /* Check if destination exists - abort */
447                         $ldap->cat($dst_dn, array('dn'));
448                         if ($ldap->fetch()){
449                                 trigger_error("Recursive_move $dst_dn already exists.",
450                                                 E_USER_WARNING);
451                                 echo sprintf("Recursive_move: '%s' already exists", $dst_dn)."<br>"; 
452                                 return (FALSE);
453                         }
455                         /* Perform a search for all objects to be moved */
456                         $objects= array();
457                         $ldap->cd($src_dn);
458                         $ldap->search("(objectClass=*)", array("dn"));
459                         while($attrs= $ldap->fetch()){
460                                 $dn= $attrs['dn'];
461                                 $objects[$dn]= strlen($dn);
462                         }
464                         /* Sort objects by indent level */
465                         asort($objects);
466                         reset($objects);
468                         /* Copy objects from small to big indent levels by replacing src_dn by dst_dn */
469                         foreach ($objects as $object => $len){
472                                 $src= str_replace("\\","\\\\",$object);
473                                 $dst= preg_replace("/".str_replace("\\","\\\\",$src_dn)."$/", "$dst_dn", $object);
474                                 $dst= str_replace($src_dn,$dst_dn,$object);
476                                 echo "<b>"._("Object").":</b> $src<br>";
478                                 if (!$this->copy($src, $dst)){
479                                         echo "<font color='#FF0000'><br>".sprintf(_("FAILED to copy %s, aborting operation"),$src)."</font>";
480                                         return (FALSE);
481                                 }
483                                 flush();
484                         }
486                         /* Remove src_dn */
487                         $ldap->cd($src_dn);
488                         $ldap->recursive_remove();
489                         $this->rec_src = $this->rec_dst = "";
490                         $this->rec_cpy =false;
492                         echo '<p class="seperator">&nbsp;</p>';
494                         echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
495                                 <br><input type='submit' name='back' value='"._("Continue")."'>
496                                 </form></div>";
498                         echo "</body></html>";
500                         return (TRUE);
501                 }
502         }
504 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
505 ?>