Code

Added execute methods
[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   /* Headpage attributes */
36   var $last_dep_sorting= "invalid";
37   var $departments= array();
39   /* attribute list for save action */
40   var $attributes= array("ou", "description", "businessCategory", "st", "l", "postalAddress",
41                         "telephoneNumber", "facsimileTelephoneNumber");
42   var $objectclasses= array("top", "gosaDepartment", "organizationalUnit");
44   function department ($config, $dn)
45   {
46         plugin::plugin($config, $dn);
47         $this->is_account= TRUE;
48         $this->ui= get_userinfo();
49         $this->dn= $dn;
50         $this->orig_dn= $dn;
51         $this->config= $config;
53         /* Set base */
54     if ($this->dn == "new"){
55                 $ui= get_userinfo();
56                 if(isset($_SESSION['depfilter']['depselect'])){
57                         $this->base = $_SESSION['depfilter']['depselect'];
58                 }else{
59                         $this->base= dn2base($ui->dn);
60                 }
61         } else {
62                 $this->base= preg_replace ("/^[^,]+,/", "", $this->dn);
63         }
64   }
66   function execute()
67   {
68         /* Call parent execute */
69         plugin::execute();
70         /* Reload departments */
71         $this->config->departments= get_departments($this->dn);
72         $this->config->make_idepartments();
73         $smarty= get_smarty();
75         /* Hide all departments, that are subtrees of this department */
76         $bases  = $this->config->idepartments;
77         $tmp    = array();      
78         foreach($bases as $dn=>$base){
79                 $fixed = str_replace("/","\\",$this->dn);
80                 /* Only attach departments which are not a subtree of this one */
81                 if(!preg_match("/".$fixed."/",$dn)){
82                         $tmp[$dn]=$base;
83                 }
84         }
85         $smarty->assign("bases", $tmp);
87         foreach ($this->attributes as $val){
88                 $smarty->assign("$val", $this->$val);
89                 $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
90         }
91         $smarty->assign("base_select", $this->base);
92         return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
93   }
95   function clear_fields()
96   {
97         $this->dn= "";
98         $this->base= "";
99         $this->acl= "#none#";
101         foreach ($this->attributes as $val){
102                 $this->$val= "";
103         }
104   }
107   function remove_from_parent()
108   {
109         $ldap= $this->config->get_ldap_link();
110         $ldap->cd ($this->dn);
111         $ldap->recursive_remove();
113         /* Optionally execute a command after we're done */
114         $this->handle_post_events('remove');
115   }
118   /* Save data to object */
119   function save_object()
120   {
121         if (isset($_POST['base'])){
122                 plugin::save_object();
124                 /* Save base, since this is no LDAP attribute */
125                 if (chkacl($this->acl, "create") == ""){
126                         $this->base= $_POST['base'];
127                 }
128         }
129   }
132   /* Check values */
133   function check()
134   {
135         $message= array();
137         /* Permissions for that base? */
138         $this->dn= "ou=$this->ou,".$this->base;
139         $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
140         $acl= get_module_permission($acl, "department", $this->dn);
141         if (chkacl($acl, "create") != ""){
142                 $message[]= _("You have no permissions to create a department on this 'Base'.");
143         }
145         /* Check for presence of this department */
146         $ldap= $this->config->get_ldap_link();
147         $attrs= $ldap->cat ($this->dn);
148         if ($this->orig_dn == "new" && !($attrs === FALSE)){
149                 $message[]= _("Department with that 'Name' already exists.");
150         } elseif ($this->orig_dn != $this->dn && !($attrs === FALSE)){
151                 $message[]= _("Department with that 'Name' already exists.");
152         }
154         /* All required fields are set? */
155         if ($this->ou == ""){
156                 $message[]= _("Required field 'Name' is not set.");
157         }
158         if ($this->description == ""){
159                 $message[]= _("Required field 'Description' is not set.");
160         }
162         /* Validate and modify - or: spaghetti rules! */
163         if ($this->ou == "incoming"){
164                 $message[]= _("The field 'Name' contains the reserved word 'incoming'.".
165                                 " Please choose another name.");
166         }
167         if (preg_match ('/[,#+:=>\\\\\/]/', $this->ou)){
168                 $message[]= _("The field 'Name' contains invalid characters.");
169         }
170         if (!is_phone_nr($this->telephoneNumber)){
171                 $message[]= _("The field 'Phone' contains an invalid phone number.");
172         }
173         if (!is_phone_nr($this->facsimileTelephoneNumber)){
174                 $message[]= _("The field 'Fax' contains an invalid phone number.");
175         }
177         return $message;
178   }
181   /* Save to LDAP */
182   function save()
183   {
184         plugin::save();
186         /* Write back to ldap */
187         $ldap= $this->config->get_ldap_link();
188         $ldap->cat($this->dn);
189         $a= $ldap->fetch();
190         $ldap->cd($this->dn);
191         if (count($a)){
192                 $ldap->modify($this->attrs);
193                 $this->handle_post_events('modify');
194         } else {
195                 $ldap->add($this->attrs);
196                 $this->handle_post_events('add');
197         }
198         show_ldap_error($ldap->get_error());
200         /* Optionally execute a command after we're done */
201         $this->postcreate();
202   }
206 ?>