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