Code

Added support for general check hooks
[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= "";
35         var $rec_dst=false;     // Destination for recursive move
36         var $rec_src=false;     // Source for recursive move 
37         var $rec_cpy=false;     // Is recursive move requested ? 
39         function am_i_moved()
40         {
41                 return $this->rec_cpy;
42         }
44         /* Headpage attributes */
45         var $last_dep_sorting= "invalid";
46         var $departments= array();
48         /* attribute list for save action */
49         var $attributes= array("ou", "description", "businessCategory", "st", "l", "postalAddress",
50                         "telephoneNumber", "facsimileTelephoneNumber");
51         var $objectclasses= array("top", "gosaDepartment", "organizationalUnit");
53         function department ($config, $dn)
54         {
56                 plugin::plugin($config, $dn);
57                 $this->is_account= TRUE;
58                 $this->ui= get_userinfo();
59                 $this->dn= $dn;
60                 $this->orig_dn= $dn;
61                 $this->config= $config;
63                 /* Set base */
64                 if ($this->dn == "new"){
65                         $ui= get_userinfo();
66                         if(isset($_SESSION['depfilter']['depselect'])){
67                                 $this->base = $_SESSION['depfilter']['depselect'];
68                         }else{
69                                 $this->base= dn2base($ui->dn);
70                         }
71                 } else {
72                         $this->base= preg_replace ("/^[^,]+,/", "", $this->dn);
73                 }
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);
78         }
80         function execute()
81         {
82                 /* Call parent execute */
83                 plugin::execute();
85                 /* Reload departments */
86                 $this->config->departments= get_departments($this->dn);
87                 $this->config->make_idepartments();
88                 $smarty= get_smarty();
91                 /* Base select dialog */
92                 $once = true;
93                 foreach($_POST as $name => $value){
94                         if(preg_match("/^chooseBase/",$name) && $once){
95                                 $once = false;
96                                 $this->dialog = new baseSelectDialog($this->config);
97                                 $this->dialog->setCurrentBase($this->base);
98                         }
99                 }
101                 /* Dialog handling */
102                 if(is_object($this->dialog)){
103                         /* Must be called before save_object */
104                         $this->dialog->save_object();
106                         if($this->dialog->isClosed()){
107                                 $this->dialog = false;
108                         }elseif($this->dialog->isSelected()){
109                                 $this->base = $this->dialog->isSelected();
110                                 $this->dialog= false;
111                         }else{
112                                 return($this->dialog->execute());
113                         }
114                 }
116                 /* Hide all departments, that are subtrees of this department */
117                 $bases  = $this->config->idepartments;
118                 if(($this->dn == "new")||($this->dn == "")){
119                         $tmp = $bases;
120                 }else{
121                         $tmp    = array();      
122                         foreach($bases as $dn=>$base){
123                                 $fixed = str_replace("/","\\",$this->dn);
124                                 /* Only attach departments which are not a subtree of this one */
125                                 if(!preg_match("/".$fixed."/",$dn)){
126                                         $tmp[$dn]=$base;
127                                 }
128                         }
129                 }
130                 $smarty->assign("bases", $tmp);
132                 foreach ($this->attributes as $val){
133                         $smarty->assign("$val", $this->$val);
134                         $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
135                 }
136                 $smarty->assign("base_select", $this->base);
137                 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
138         }
140         function clear_fields()
141         {
142                 $this->dn= "";
143                 $this->base= "";
144                 $this->acl= "#none#";
146                 foreach ($this->attributes as $val){
147                         $this->$val= "";
148                 }
149         }
152         function remove_from_parent()
153         {
154                 $ldap= $this->config->get_ldap_link();
155                 $ldap->cd ($this->dn);
156                 $ldap->recursive_remove();
158                 /* Optionally execute a command after we're done */
159                 $this->handle_post_events('remove');
160         }
163         /* Save data to object */
164         function save_object()
165         {
166                 if (isset($_POST['base'])){
167                         plugin::save_object();
169                         /* Save base, since this is no LDAP attribute */
170                         if (chkacl($this->acl, "create") == ""){
171                                 $this->base= $_POST['base'];
172                         }
173                 }
174         }
177         /* Check values */
178         function check()
179         {
180                 /* Call common method to give check the hook */
181                 $message= plugin::check();
183                 /* Permissions for that base? */
184                 //      $this->dn= "ou=$this->ou,".$this->base;
185                 if (chkacl($this->acl, "create") != ""){
186                         $message[]= _("You have no permissions to create a department on this 'Base'.");
187                 }
189                 /* Check for presence of this department */
190                 $ldap= $this->config->get_ldap_link();
191                 $attrs= $ldap->cat ($this->dn);
192                 if ($this->orig_dn == "new" && !($attrs === FALSE)){
193                         $message[]= _("Department with that 'Name' already exists.");
194                 } elseif ($this->orig_dn != $this->dn && !($attrs === FALSE)){
195                         $message[]= _("Department with that 'Name' already exists.");
196                 }
198                 /* All required fields are set? */
199                 if ($this->ou == ""){
200                         $message[]= _("Required field 'Name' is not set.");
201                 }
202                 if ($this->description == ""){
203                         $message[]= _("Required field 'Description' is not set.");
204                 }
206                 /* Validate and modify - or: spaghetti rules! */
207                 if ($this->ou == "incoming"){
208                         $message[]= _("The field 'Name' contains the reserved word 'incoming'.".
209                                         " Please choose another name.");
210                 }
211                 if (preg_match ('/[#+:=>\\\\\/]/', $this->ou)){
212                         $message[]= _("The field 'Name' contains invalid characters.");
213                 }
214                 if (!is_phone_nr($this->telephoneNumber)){
215                         $message[]= _("The field 'Phone' contains an invalid phone number.");
216                 }
217                 if (!is_phone_nr($this->facsimileTelephoneNumber)){
218                         $message[]= _("The field 'Fax' contains an invalid phone number.");
219                 }
221                 return $message;
222         }
225         /* Save to LDAP */
226         function save()
227         {
228                 plugin::save();
230                 /* Write back to ldap */
231                 $ldap= $this->config->get_ldap_link();
232                 $ldap->cat($this->dn);
233                 $a= $ldap->fetch();
234                 $ldap->cd($this->dn);
235                 if (count($a)){
236                         $this->cleanup();
237                         $ldap->modify ($this->attrs); 
239                         $this->handle_post_events('modify');
240                 } else {
241                         $ldap->add($this->attrs);
242                         $this->handle_post_events('add');
243                 }
244                 show_ldap_error($ldap->get_error());
246                 /* Optionally execute a command after we're done */
247                 $this->postcreate();
248         }
250         /* Move/Rename complete trees */
251         function recursive_move($src_dn, $dst_dn,$force = false)
252         {
253                 if(!$force){
255                         $this->rec_cpy  = true;
256                         $this->rec_src  = $src_dn;
257                         $this->rec_dst  = $dst_dn;
259                         $smarty = get_smarty();
261                         $smarty->assign("src","?plug=".$_GET['plug']."&PerformRecMove");
263                         $display=  $smarty->fetch(get_template_path("recursive_move.tpl",TRUE));
264                         return($display);
265                         exit();
266                 }else{
267                         if(!$this->rec_cpy){ 
268                                 return;
269                         }
271                         $src_dn = $this->rec_src;
272                         $dst_dn = $this->rec_dst;
274                         /* Print header to have styles included */
275                         $smarty= get_smarty();
276                         echo "<!-- headers.tpl-->".$smarty->fetch(get_template_path('headers.tpl'));
277                         echo "<body style='background-image:none;margin:3px;color:black'>";
279                         echo "<h3>".sprintf(_("Moving %s to %s"),"<i>".$src_dn."</i>","<i>".$dst_dn."</i>")."</h3>";
282                         /* Check if the destination entry exists */
283                         $ldap= $this->config->get_ldap_link();
285                         /* Check if destination exists - abort */
286                         $ldap->cat($dst_dn);
287                         if ($ldap->fetch()){
288                                 trigger_error("Recursive_move $dst_dn already exists.",
289                                                 E_USER_WARNING);
290                                 echo "Recursive_move :$dst_dn already exists.<br>"; 
291                                 return (FALSE);
292                         }
293         
294                         /* Perform a search for all objects to be moved */
295                         $objects= array();
296                         $ldap->cd($src_dn);
297                         $ldap->search("(objectClass=*)", array("dn"));
298                         while($attrs= $ldap->fetch()){
299                                 $dn= $attrs['dn'];
300                                 $objects[$dn]= strlen($dn);
301                         }
303                         /* Sort objects by indent level */
304                         asort($objects);
305                         reset($objects);
307                         /* Copy objects from small to big indent levels by replacing src_dn by dst_dn */
308                         foreach ($objects as $object => $len){
310                                 
311                                 $src= str_replace("\\","\\\\",$object);
312                                 $dst= preg_replace("/".str_replace("\\","\\\\",$src_dn)."$/", "$dst_dn", $object);
313                                 $dst= str_replace($src_dn,$dst_dn,$object);
315                                 echo "<b>"._("Object").":</b> $src<br>";
316                                 
317                                 if (!$this->copy($src, $dst)){
318                                         echo "<font color='#FF0000'><br>".sprintf(_("FAILED to copy %s, aborting operation"),$src)."</font>";
319                                         return (FALSE);
320                                 }
322                                 flush();
323                         }
325                         /* Remove src_dn */
326                         $ldap->cd($src_dn);
327                         $ldap->recursive_remove();
328                         $this->rec_src = $this->rec_dst = "";
329                         $this->rec_cpy =false;
331                         echo '<p class="seperator">&nbsp;</p>';
333                         echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
334                                 <br><input type='submit' name='back' value='"._("Continue")."'>
335                                 </form></div>";
337                         echo "</body></html>";
338                         
339                         return (TRUE);
340                 }
341         }
346 ?>