Code

97bc429efb0fa555ffcd40184a475ebd6e7a555e
[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         /* attribute list for save action */
46         var $attributes= array("ou", "description", "businessCategory", "st", "l", "postalAddress",
47                         "telephoneNumber", "facsimileTelephoneNumber");
48         var $objectclasses= array("top", "gosaDepartment", "organizationalUnit");
50         function department ($config, $dn)
51         {
53                 plugin::plugin($config, $dn);
54                 $this->is_account= TRUE;
55                 $this->ui= get_userinfo();
56                 $this->dn= $dn;
57                 $this->orig_dn= $dn;
58                 $this->config= $config;
60                 /* Set base */
61                 if ($this->dn == "new"){
62                         $ui= get_userinfo();
63                         if(isset($_SESSION['CurrentMainBase'])){
64                                 $this->base= $_SESSION['CurrentMainBase'];
65                         }else{
66                                 $this->base= dn2base($ui->dn);
67                         }
68                 } else {
69                         $this->base= preg_replace ("/^[^,]+,/", "", $this->dn);
70                 }
72                 /* set permissions */
73                 $ui= get_userinfo();
74                 $acl= get_permissions ($ui->dn, $ui->subtreeACL);
75                 $this->acl= get_module_permission($acl, "department", $ui->dn);
77                 /* Is administrational Unit? */
78                 if ($dn != "new" && in_array_ics('gosaAdministrationalUnit', $this->attrs['objectClass'])){
79                         $this->is_administrational_unit= true;
80                 }
81         }
83         function execute()
84         {
85                 /* Call parent execute */
86                 plugin::execute();
88                 /* Reload departments */
89                 $this->config->get_departments($this->dn);
90                 $this->config->make_idepartments();
91                 $smarty= get_smarty();
93                 /* Base select dialog */
94                 $once = true;
95                 foreach($_POST as $name => $value){
96                         if(preg_match("/^chooseBase/",$name) && $once){
97                                 $once = false;
98                                 $this->dialog = new baseSelectDialog($this->config);
99                                 $this->dialog->setCurrentBase($this->base);
100                         }
101                 }
103                 /* Dialog handling */
104                 if(is_object($this->dialog)){
105                         /* Must be called before save_object */
106                         $this->dialog->save_object();
108                         if($this->dialog->isClosed()){
109                                 $this->dialog = false;
110                         }elseif($this->dialog->isSelected()){
111                                 $this->base = $this->dialog->isSelected();
112                                 $this->dialog= false;
113                         }else{
114                                 return($this->dialog->execute());
115                         }
116                 }
118                 /* Hide all departments, that are subtrees of this department */
119                 $bases  = $this->config->idepartments;
120                 if(($this->dn == "new")||($this->dn == "")){
121                         $tmp = $bases;
122                 }else{
123                         $tmp    = array();      
124                         foreach($bases as $dn=>$base){
125                                 $fixed = str_replace("/","\\",$this->dn);
126                                 /* Only attach departments which are not a subtree of this one */
127                                 if(!preg_match("/".$fixed."/",$dn)){
128                                         $tmp[$dn]=$base;
129                                 }
130                         }
131                 }
132                 $smarty->assign("bases", $tmp);
134                 foreach ($this->attributes as $val){
135                         $smarty->assign("$val", $this->$val);
136                         $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
137                 }
138                 $smarty->assign("base_select", $this->base);
140                 /* Set admin unit flag */
141                 if ($this->is_administrational_unit) {
142                         $smarty->assign("unitTag", "checked");
143                 } else {
144                         $smarty->assign("unitTag", "");
145                 }
146                 $smarty->assign("unitTag"."ACL", chkacl($this->acl, "unitTag"));
147                 
148                 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
149         }
151         function clear_fields()
152         {
153                 $this->dn= "";
154                 $this->base= "";
155                 $this->acl= "#none#";
157                 foreach ($this->attributes as $val){
158                         $this->$val= "";
159                 }
160         }
163         function remove_from_parent()
164         {
165                 $ldap= $this->config->get_ldap_link();
166                 $ldap->cd ($this->dn);
167                 $ldap->recursive_remove();
169                 /* Optionally execute a command after we're done */
170                 $this->handle_post_events('remove');
171         }
174         /* Save data to object */
175         function save_object()
176         {
177                 if (isset($_POST['base'])){
178                         plugin::save_object();
180                         /* Save base, since this is no LDAP attribute */
181                         if (chkacl($this->acl, "create") == ""){
182                                 $this->base= $_POST['base'];
183                         }
185                         /* Save tagging flag */
186                         if (chkacl($this->acl, "unitTag") == ""){
187                                 if (isset($_POST['unitTag'])){
188                                         $this->is_administrational_unit= true;
189                                 } else {
190                                         $this->is_administrational_unit= false;
191                                 }
192                         }
193                 }
194         }
197         /* Check values */
198         function check()
199         {
200                 /* Call common method to give check the hook */
201                 $message= plugin::check();
203                 /* Permissions for that base? */
204                 //      $this->dn= "ou=$this->ou,".$this->base;
205                 if (chkacl($this->acl, "create") != ""){
206                         $message[]= _("You have no permissions to create a department on this 'Base'.");
207                 }
209                 /* Check for presence of this department */
210                 $ldap= $this->config->get_ldap_link();
211                 $attrs= $ldap->cat ($this->dn);
212                 if ($this->orig_dn == "new" && !($attrs === FALSE)){
213                         $message[]= _("Department with that 'Name' already exists.");
214                 } elseif ($this->orig_dn != $this->dn && !($attrs === FALSE)){
215                         $message[]= _("Department with that 'Name' already exists.");
216                 }
218                 /* All required fields are set? */
219                 if ($this->ou == ""){
220                         $message[]= _("Required field 'Name' is not set.");
221                 }
222                 if ($this->description == ""){
223                         $message[]= _("Required field 'Description' is not set.");
224                 }
226                 /* Validate and modify - or: spaghetti rules! */
227                 if ($this->ou == "incoming"){
228                         $message[]= _("The field 'Name' contains the reserved word 'incoming'.".
229                                         " Please choose another name.");
230                 }
231                 if (preg_match ('/[#+:=>\\\\\/]/', $this->ou)){
232                         $message[]= _("The field 'Name' contains invalid characters.");
233                 }
234                 if (!is_phone_nr($this->telephoneNumber)){
235                         $message[]= _("The field 'Phone' contains an invalid phone number.");
236                 }
237                 if (!is_phone_nr($this->facsimileTelephoneNumber)){
238                         $message[]= _("The field 'Fax' contains an invalid phone number.");
239                 }
241                 return $message;
242         }
245         /* Save to LDAP */
246         function save()
247         {
248                 /* Add tag objects if needed */
249                 if ($this->is_administrational_unit){
250                         $this->objectclasses[]= "gosaAdministrationalUnit";
251                         $this->attributes[]= "gosaUnitTag";
252                 } else {
253                         plugin::handle_object_tagging();
254                 }
256                 plugin::save();
258                 /* Write back to ldap */
259                 $ldap= $this->config->get_ldap_link();
260                 $ldap->cat($this->dn);
261                 $a= $ldap->fetch();
262                 $ldap->cd($this->dn);
263                 
264                 if (count($a)){
265                         $this->cleanup();
266                         $ldap->modify ($this->attrs); 
268                         $this->handle_post_events('modify');
269                 } else {
270                         $ldap->add($this->attrs);
271                         $this->handle_post_events('add');
272                 }
273                 show_ldap_error($ldap->get_error());
275                 if ($this->is_administrational_unit){
276                         $this->tag_objects();
277                 } else {
278                         $this->untag_objects();
279                 }
281                 /* Optionally execute a command after we're done */
282                 $this->postcreate();
283         }
286         /* Tag objects to have the gosaAdministrationalUnitTag */
287         function tag_objects()
288         {
289                 echo "Tag";
290         }
292         /* Remove the gosaAdministrationalUnitTag from objects */
293         function untag_objects()
294         {
295                 echo "Untag";
296         }
298         /* Move/Rename complete trees */
299         function recursive_move($src_dn, $dst_dn,$force = false)
300         {
301                 if(!$force){
303                         $this->rec_cpy  = true;
304                         $this->rec_src  = $src_dn;
305                         $this->rec_dst  = $dst_dn;
307                         $smarty = get_smarty();
309                         $smarty->assign("src","?plug=".$_GET['plug']."&PerformRecMove");
311                         $display=  $smarty->fetch(get_template_path("recursive_move.tpl",TRUE));
312                         return($display);
313                         exit();
314                 }else{
315                         if(!$this->rec_cpy){ 
316                                 return;
317                         }
319                         $src_dn = $this->rec_src;
320                         $dst_dn = $this->rec_dst;
322                         /* Print header to have styles included */
323                         $smarty= get_smarty();
324                         echo "<!-- headers.tpl-->".$smarty->fetch(get_template_path('headers.tpl'));
325                         echo "<body style='background-image:none;margin:3px;color:black'>";
327                         echo "<h3>".sprintf(_("Moving %s to %s"),"<i>".$src_dn."</i>","<i>".$dst_dn."</i>")."</h3>";
330                         /* Check if the destination entry exists */
331                         $ldap= $this->config->get_ldap_link();
333                         /* Check if destination exists - abort */
334                         $ldap->cat($dst_dn);
335                         if ($ldap->fetch()){
336                                 trigger_error("Recursive_move $dst_dn already exists.",
337                                                 E_USER_WARNING);
338                                 echo "Recursive_move :$dst_dn already exists.<br>"; 
339                                 return (FALSE);
340                         }
341         
342                         /* Perform a search for all objects to be moved */
343                         $objects= array();
344                         $ldap->cd($src_dn);
345                         $ldap->search("(objectClass=*)", array("dn"));
346                         while($attrs= $ldap->fetch()){
347                                 $dn= $attrs['dn'];
348                                 $objects[$dn]= strlen($dn);
349                         }
351                         /* Sort objects by indent level */
352                         asort($objects);
353                         reset($objects);
355                         /* Copy objects from small to big indent levels by replacing src_dn by dst_dn */
356                         foreach ($objects as $object => $len){
358                                 
359                                 $src= str_replace("\\","\\\\",$object);
360                                 $dst= preg_replace("/".str_replace("\\","\\\\",$src_dn)."$/", "$dst_dn", $object);
361                                 $dst= str_replace($src_dn,$dst_dn,$object);
363                                 echo "<b>"._("Object").":</b> $src<br>";
364                                 
365                                 if (!$this->copy($src, $dst)){
366                                         echo "<font color='#FF0000'><br>".sprintf(_("FAILED to copy %s, aborting operation"),$src)."</font>";
367                                         return (FALSE);
368                                 }
370                                 flush();
371                         }
373                         /* Remove src_dn */
374                         $ldap->cd($src_dn);
375                         $ldap->recursive_remove();
376                         $this->rec_src = $this->rec_dst = "";
377                         $this->rec_cpy =false;
379                         echo '<p class="seperator">&nbsp;</p>';
381                         echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
382                                 <br><input type='submit' name='back' value='"._("Continue")."'>
383                                 </form></div>";
385                         echo "</body></html>";
386                         
387                         return (TRUE);
388                 }
389         }
391         function am_i_moved()
392         {
393                 return $this->rec_cpy;
394         }
398 ?>