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 /* Reload departments */
69 $this->config->departments= get_departments($this->dn);
70 $this->config->make_idepartments();
71 $smarty= get_smarty();
73 /* Hide all departments, that are subtrees of this department */
74 $bases = $this->config->idepartments;
75 $tmp = array();
76 foreach($bases as $dn=>$base){
77 $fixed = str_replace("/","\\",$this->dn);
78 /* Only attach departments which are not a subtree of this one */
79 if(!preg_match("/".$fixed."/",$dn)){
80 $tmp[$dn]=$base;
81 }
82 }
83 $smarty->assign("bases", $tmp);
85 foreach ($this->attributes as $val){
86 $smarty->assign("$val", $this->$val);
87 $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
88 }
89 $smarty->assign("base_select", $this->base);
90 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
91 }
93 function clear_fields()
94 {
95 $this->dn= "";
96 $this->base= "";
97 $this->acl= "#none#";
99 foreach ($this->attributes as $val){
100 $this->$val= "";
101 }
102 }
105 function remove_from_parent()
106 {
107 $ldap= $this->config->get_ldap_link();
108 $ldap->cd ($this->dn);
109 $ldap->recursive_remove();
111 /* Optionally execute a command after we're done */
112 $this->handle_post_events('remove');
113 }
116 /* Save data to object */
117 function save_object()
118 {
119 if (isset($_POST['base'])){
120 plugin::save_object();
122 /* Save base, since this is no LDAP attribute */
123 if (chkacl($this->acl, "create") == ""){
124 $this->base= $_POST['base'];
125 }
126 }
127 }
130 /* Check values */
131 function check()
132 {
133 $message= array();
135 /* Permissions for that base? */
136 $this->dn= "ou=$this->ou,".$this->base;
137 $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
138 $acl= get_module_permission($acl, "department", $this->dn);
139 if (chkacl($acl, "create") != ""){
140 $message[]= _("You have no permissions to create a department on this 'Base'.");
141 }
143 /* Check for presence of this department */
144 $ldap= $this->config->get_ldap_link();
145 $attrs= $ldap->cat ($this->dn);
146 if ($this->orig_dn == "new" && !($attrs === FALSE)){
147 $message[]= _("Department with that 'Name' already exists.");
148 } elseif ($this->orig_dn != $this->dn && !($attrs === FALSE)){
149 $message[]= _("Department with that 'Name' already exists.");
150 }
152 /* All required fields are set? */
153 if ($this->ou == ""){
154 $message[]= _("Required field 'Name' is not set.");
155 }
156 if ($this->description == ""){
157 $message[]= _("Required field 'Description' is not set.");
158 }
160 /* Validate and modify - or: spaghetti rules! */
161 if ($this->ou == "incoming"){
162 $message[]= _("The field 'Name' contains the reserved word 'incoming'.".
163 " Please choose another name.");
164 }
165 if (preg_match ('/[,#+:=>\\\\\/]/', $this->ou)){
166 $message[]= _("The field 'Name' contains invalid characters.");
167 }
168 if (!is_phone_nr($this->telephoneNumber)){
169 $message[]= _("The field 'Phone' contains an invalid phone number.");
170 }
171 if (!is_phone_nr($this->facsimileTelephoneNumber)){
172 $message[]= _("The field 'Fax' contains an invalid phone number.");
173 }
175 return $message;
176 }
179 /* Save to LDAP */
180 function save()
181 {
182 plugin::save();
184 /* Write back to ldap */
185 $ldap= $this->config->get_ldap_link();
186 $ldap->cat($this->dn);
187 $a= $ldap->fetch();
188 $ldap->cd($this->dn);
189 if (count($a)){
190 $ldap->modify($this->attrs);
191 $this->handle_post_events('modify');
192 } else {
193 $ldap->add($this->attrs);
194 $this->handle_post_events('add');
195 }
196 show_ldap_error($ldap->get_error());
198 /* Optionally execute a command after we're done */
199 $this->postcreate();
200 }
202 }
204 ?>