Code

Moved to trunk/branches/tags structure
[gosa.git] / plugins / admin / departments / class_departmentGeneric.inc
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         plugin::save_object();
110         /* Save base, since this is no LDAP attribute */
111         if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
112                 $this->base= $_POST['base'];
113         }
114   }
117   /* Check values */
118   function check()
119   {
120         $message= array();
122         /* Permissions for that base? */
123         $this->dn= "ou=$this->ou,".$this->base;
124         $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
125         $acl= get_module_permission($acl, "department", $this->dn);
126         if (chkacl($acl, "create") != ""){
127                 $message[]= _("You have no permissions to create a department on this 'Base'.");
128         }
130         /* Check for presence of this department */
131         $ldap= $this->config->get_ldap_link();
132         $attrs= $ldap->cat ($this->dn);
133         if ($this->orig_dn == "new" && !($attrs === FALSE)){
134                 $message[]= _("Department with that 'Name' already exists.");
135         } elseif ($this->orig_dn != $this->dn && !($attrs === FALSE)){
136                 $message[]= _("Department with that 'Name' already exists.");
137         }
139         /* All required fields are set? */
140         if ($_POST["ou"] == ""){
141                 $message[]= _("Required field 'Name' is not set.");
142         }
143         if ($_POST["description"] == ""){
144                 $message[]= _("Required field 'Description' is not set.");
145         }
147         /* Validate and modify - or: spaghetti rules! */
148         if ($_POST['ou'] == "incoming"){
149                 $message[]= _("The field 'Name' contains the reserved word 'incoming'.".
150                                 " Please choose another name.");
151         }
152         if (preg_match ('/[,#+:=>\\\\]/', $_POST['ou'])){
153                 $message[]= _("The field 'Name' contains invalid characters.");
154         }
155         if (!is_phone_nr($_POST['telephoneNumber'])){
156                 $message[]= _("The field 'Phone' contains an invalid phone number.");
157         }
158         if (!is_phone_nr($_POST['facsimileTelephoneNumber'])){
159                 $message[]= _("The field 'Fax' contains an invalid phone number.");
160         }
162         return $message;
163   }
166   /* Save to LDAP */
167   function save()
168   {
169         plugin::save();
171         /* Write back to ldap */
172         $ldap= $this->config->get_ldap_link();
173         $ldap->cat($this->dn);
174         $a= $ldap->fetch();
175         $ldap->cd($this->dn);
176         if (count($a)){
177                 $ldap->modify($this->attrs);
178                 $this->handle_post_events('modify');
179         } else {
180                 $ldap->add($this->attrs);
181                 $this->handle_post_events('add');
182         }
183         show_ldap_error($ldap->get_error());
185         /* Optionally execute a command after we're done */
186         $this->postcreate();
187   }
191 ?>