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 $this->base= dn2base($ui->dn);
57 } else {
58 $this->base= preg_replace ("/^[^,]+,/", "", $this->dn);
59 }
60 }
62 function execute()
63 {
64 /* Reload departments */
65 $this->config->departments= get_departments($this->dn);
66 $this->config->make_idepartments();
67 $smarty= get_smarty();
70 /* Show main page */
71 $smarty->assign("bases", $this->config->idepartments);
73 foreach ($this->attributes as $val){
74 $smarty->assign("$val", $this->$val);
75 $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
76 }
77 $smarty->assign("bases", $this->config->idepartments);
78 $smarty->assign("base_select", $this->base);
79 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
80 }
82 function clear_fields()
83 {
84 $this->dn= "";
85 $this->base= "";
86 $this->acl= "#none#";
88 foreach ($this->attributes as $val){
89 $this->$val= "";
90 }
91 }
94 function remove_from_parent()
95 {
96 $ldap= $this->config->get_ldap_link();
97 $ldap->cd ($this->dn);
98 $ldap->recursive_remove();
100 /* Optionally execute a command after we're done */
101 $this->handle_post_events('remove');
102 }
105 /* Save data to object */
106 function save_object()
107 {
108 if (isset($_POST['base'])){
109 plugin::save_object();
111 /* Save base, since this is no LDAP attribute */
112 if (chkacl($this->acl, "create") == ""){
113 $this->base= $_POST['base'];
114 }
115 }
116 }
119 /* Check values */
120 function check()
121 {
122 $message= array();
124 /* Permissions for that base? */
125 $this->dn= "ou=$this->ou,".$this->base;
126 $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
127 $acl= get_module_permission($acl, "department", $this->dn);
128 if (chkacl($acl, "create") != ""){
129 $message[]= _("You have no permissions to create a department on this 'Base'.");
130 }
132 /* Check for presence of this department */
133 $ldap= $this->config->get_ldap_link();
134 $attrs= $ldap->cat ($this->dn);
135 if ($this->orig_dn == "new" && !($attrs === FALSE)){
136 $message[]= _("Department with that 'Name' already exists.");
137 } elseif ($this->orig_dn != $this->dn && !($attrs === FALSE)){
138 $message[]= _("Department with that 'Name' already exists.");
139 }
141 /* All required fields are set? */
142 if ($this->ou == ""){
143 $message[]= _("Required field 'Name' is not set.");
144 }
145 if ($this->description == ""){
146 $message[]= _("Required field 'Description' is not set.");
147 }
149 /* Validate and modify - or: spaghetti rules! */
150 if ($this->ou == "incoming"){
151 $message[]= _("The field 'Name' contains the reserved word 'incoming'.".
152 " Please choose another name.");
153 }
154 if (preg_match ('/[,#+:=>\\\\]/', $this->ou)){
155 $message[]= _("The field 'Name' contains invalid characters.");
156 }
157 if (!is_phone_nr($this->telephoneNumber)){
158 $message[]= _("The field 'Phone' contains an invalid phone number.");
159 }
160 if (!is_phone_nr($this->facsimileTelephoneNumber)){
161 $message[]= _("The field 'Fax' contains an invalid phone number.");
162 }
164 return $message;
165 }
168 /* Save to LDAP */
169 function save()
170 {
171 plugin::save();
173 /* Write back to ldap */
174 $ldap= $this->config->get_ldap_link();
175 $ldap->cat($this->dn);
176 $a= $ldap->fetch();
177 $ldap->cd($this->dn);
178 if (count($a)){
179 $ldap->modify($this->attrs);
180 $this->handle_post_events('modify');
181 } else {
182 $ldap->add($this->attrs);
183 $this->handle_post_events('add');
184 }
185 show_ldap_error($ldap->get_error());
187 /* Optionally execute a command after we're done */
188 $this->postcreate();
189 }
191 }
193 ?>