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