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 {
47 plugin::plugin($config, $dn);
48 $this->is_account= TRUE;
49 $this->ui= get_userinfo();
50 $this->dn= $dn;
51 $this->orig_dn= $dn;
52 $this->config= $config;
54 /* Set base */
55 if ($this->dn == "new"){
56 $ui= get_userinfo();
57 if(isset($_SESSION['depfilter']['depselect'])){
58 $this->base = $_SESSION['depfilter']['depselect'];
59 }else{
60 $this->base= dn2base($ui->dn);
61 }
62 } else {
63 $this->base= preg_replace ("/^[^,]+,/", "", $this->dn);
64 }
65 /* set permissions */
66 $ui= get_userinfo();
67 $acl= get_permissions ($ui->dn, $ui->subtreeACL);
68 $this->acl= get_module_permission($acl, "department", $ui->dn);
69 }
71 function execute()
72 {
73 /* Call parent execute */
74 plugin::execute();
76 /* Reload departments */
77 $this->config->departments= get_departments($this->dn);
78 $this->config->make_idepartments();
79 $smarty= get_smarty();
82 /* Base select dialog */
83 $once = true;
84 foreach($_POST as $name => $value){
85 if(preg_match("/^chooseBase/",$name) && $once){
86 $once = false;
87 $this->dialog = new baseSelectDialog($this->config);
88 $this->dialog->setCurrentBase($this->base);
89 }
90 }
92 /* Dialog handling */
93 if(is_object($this->dialog)){
94 /* Must be called before save_object */
95 $this->dialog->save_object();
97 if($this->dialog->isClosed()){
98 $this->dialog = false;
99 }elseif($this->dialog->isSelected()){
100 $this->base = $this->dialog->isSelected();
101 $this->dialog= false;
102 }else{
103 return($this->dialog->execute());
104 }
105 }
107 /* Hide all departments, that are subtrees of this department */
108 $bases = $this->config->idepartments;
109 if(($this->dn == "new")||($this->dn == "")){
110 $tmp = $bases;
111 }else{
112 $tmp = array();
113 foreach($bases as $dn=>$base){
114 $fixed = str_replace("/","\\",$this->dn);
115 /* Only attach departments which are not a subtree of this one */
116 if(!preg_match("/".$fixed."/",$dn)){
117 $tmp[$dn]=$base;
118 }
119 }
120 }
121 $smarty->assign("bases", $tmp);
123 foreach ($this->attributes as $val){
124 $smarty->assign("$val", $this->$val);
125 $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
126 }
127 $smarty->assign("base_select", $this->base);
128 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
129 }
131 function clear_fields()
132 {
133 $this->dn= "";
134 $this->base= "";
135 $this->acl= "#none#";
137 foreach ($this->attributes as $val){
138 $this->$val= "";
139 }
140 }
143 function remove_from_parent()
144 {
145 $ldap= $this->config->get_ldap_link();
146 $ldap->cd ($this->dn);
147 $ldap->recursive_remove();
149 /* Optionally execute a command after we're done */
150 $this->handle_post_events('remove');
151 }
154 /* Save data to object */
155 function save_object()
156 {
157 if (isset($_POST['base'])){
158 plugin::save_object();
160 /* Save base, since this is no LDAP attribute */
161 if (chkacl($this->acl, "create") == ""){
162 $this->base= $_POST['base'];
163 }
164 }
165 }
168 /* Check values */
169 function check()
170 {
171 $message= array();
173 /* Permissions for that base? */
174 // $this->dn= "ou=$this->ou,".$this->base;
175 if (chkacl($this->acl, "create") != ""){
176 $message[]= _("You have no permissions to create a department on this 'Base'.");
177 }
179 /* Check for presence of this department */
180 $ldap= $this->config->get_ldap_link();
181 $attrs= $ldap->cat ($this->dn);
182 if ($this->orig_dn == "new" && !($attrs === FALSE)){
183 $message[]= _("Department with that 'Name' already exists.");
184 } elseif ($this->orig_dn != $this->dn && !($attrs === FALSE)){
185 $message[]= _("Department with that 'Name' already exists.");
186 }
188 /* All required fields are set? */
189 if ($this->ou == ""){
190 $message[]= _("Required field 'Name' is not set.");
191 }
192 if ($this->description == ""){
193 $message[]= _("Required field 'Description' is not set.");
194 }
196 /* Validate and modify - or: spaghetti rules! */
197 if ($this->ou == "incoming"){
198 $message[]= _("The field 'Name' contains the reserved word 'incoming'.".
199 " Please choose another name.");
200 }
201 if (preg_match ('/[#+:=>\\\\\/]/', $this->ou)){
202 $message[]= _("The field 'Name' contains invalid characters.");
203 }
204 if (!is_phone_nr($this->telephoneNumber)){
205 $message[]= _("The field 'Phone' contains an invalid phone number.");
206 }
207 if (!is_phone_nr($this->facsimileTelephoneNumber)){
208 $message[]= _("The field 'Fax' contains an invalid phone number.");
209 }
211 return $message;
212 }
215 /* Save to LDAP */
216 function save()
217 {
218 plugin::save();
220 /* Write back to ldap */
221 $ldap= $this->config->get_ldap_link();
222 $ldap->cat($this->dn);
223 $a= $ldap->fetch();
224 $ldap->cd($this->dn);
225 if (count($a)){
226 $this->cleanup();
227 $ldap->modify ($this->attrs);
229 $this->handle_post_events('modify');
230 } else {
231 $ldap->add($this->attrs);
232 $this->handle_post_events('add');
233 }
234 show_ldap_error($ldap->get_error());
236 /* Optionally execute a command after we're done */
237 $this->postcreate();
238 }
240 }
242 ?>