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 */
20 require "tabs_macros.inc";
22 class gofonMacro extends plugin
23 {
24 /* Definitions */
25 var $plHeadline = "Phone macros";
26 var $plDescription = "This does something";
28 /* Dialog attributes */
29 var $macrotabs = NULL;
30 var $macros = array();
31 var $ui = NULL;
32 var $DivListMacro = NULL;
34 function gofonMacro($config, $ui)
35 {
36 /* Save configuration for internal use */
37 $this->config= $config;
38 $this->ui= $ui;
40 $this->DivListMacro = new divListMacro($this->config,$this);
41 }
43 function execute()
44 {
45 /* Call parent execute */
46 plugin::execute();
48 $_SESSION['LOCK_VARS_TO_USE'] = array("/^goFonMacro_/","/^act$/","/^id$/");
50 /*****************
51 Variable initialisation
52 *****************/
54 $s_action = ""; // Contains the action to proceed
55 $s_entry = ""; // The value for s_action
56 $base_back = ""; // The Link for Backbutton
57 $smarty= get_smarty();
60 /*****************
61 Check Posts
62 *****************/
64 /* Test Posts */
65 foreach($_POST as $key => $val){
66 // Post for delete
67 if(preg_match("/^goFonMacro_del/",$key)){
68 $s_action = "del";
69 $s_entry = preg_replace("/^goFonMacro_del_/i","",$key);
70 // Post for edit
71 }elseif(preg_match("/^goFonMacro_edit_/",$key)){
72 $s_action="edit";
73 $s_entry = preg_replace("/^goFonMacro_edit_/i","",$key);
74 // Post for new
75 }elseif(preg_match("/^goFonMacro_new/",$key)){
76 $s_action="new";
77 }
78 }
79 if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
80 $s_action ="edit";
81 $s_entry = $_GET['id'];
82 }
83 $s_entry = preg_replace("/_.$/","",$s_entry);
86 /*****************
87 Create a new Macro
88 *****************/
90 /* New macro? */
91 if ($s_action=="new"){
93 /* By default we set 'dn' to 'new', all relevant plugins will
94 react on this. */
95 $this->dn= "new";
97 /* Create new usertab object */
98 $this->macrotabs= new macrotabs($this->config,$this->config->data['TABS']['MACROTABS'], $this->dn,"gofonmacro");
99 $this->macrotabs->set_acl_base($this->DivListMacro->selectedBase);
100 }
102 /*****************
103 Edit || Password canceled
104 *****************/
106 /* Cancel dialogs */
107 if (isset($_POST['edit_cancel'])){
108 del_lock ($this->macrotabs->dn);
109 unset ($this->macrotabs);
110 $this->macrotabs= NULL;
111 unset ($_SESSION['objectinfo']);
112 }
115 /*****************
116 Edit finised
117 *****************/
119 /* Finish mac edit is triggered by the tabulator dialog, so
120 the user wants to save edited data. Check and save at this
121 point. */
122 if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (isset($this->macrotabs->config))){
124 /* Check tabs, will feed message array */
125 $this->macrotabs->last= $this->macrotabs->current;
126 $this->macrotabs->save_object();
127 $message= $this->macrotabs->check();
129 /* Save, or display error message? */
130 if (count($message) == 0){
132 /* Save data data to ldap */
133 $this->macrotabs->save();
134 gosa_log ("Macro object'".$this->dn."' has been saved");
136 if (!isset($_POST['edit_apply'])){
137 /* macro has been saved successfully, remove lock from
138 LDAP. */
139 if ($this->dn != "new"){
140 del_lock ($this->dn);
141 }
143 unset ($this->macrotabs);
144 $this->macrotabs= NULL;
145 unset ($_SESSION['objectinfo']);
146 }
147 } else {
148 /* Ok. There seem to be errors regarding to the tab data,
149 show message and continue as usual. */
150 show_errors($message);
151 }
152 }
155 /*****************
156 Edit macro
157 *****************/
159 /* User wants to edit data */
160 if (($s_action=="edit") && (!isset($this->macrotabs->config))){
162 $dn = $this->macros[$s_entry]['dn'];
163 $acl = $this->ui->get_permissions($dn,"gofonmacro/macro");
164 if(preg_match("/r/",$acl)){
165 $this->dn = $dn;
167 /* Check locking, save current plugin in 'back_plugin', so
168 the dialog knows where to return. */
169 if (($user= get_lock($this->dn)) != ""){
170 return(gen_locked_message ($user, $this->dn));
171 }
173 /* Lock the current entry, so everyone will get the
174 above dialog */
175 add_lock ($this->dn, $this->ui->dn);
176 /* Register macrotabs to trigger edit dialog */
177 $this->macrotabs= new macrotabs($this->config,
178 $this->config->data['TABS']['MACROTABS'], $this->dn,"gofonmacro");
179 $_SESSION['objectinfo']= $this->dn;
180 }
181 }
183 /*****************
184 Remove macro
185 *****************/
187 /* Remove user was requested */
188 if ($s_action=="del"){
190 /* Get 'dn' from posted 'uid' */
191 $dn = $this->macros[$s_entry]['dn'];
192 $acl = $this->ui->get_permissions($dn,"gofonmacro/macro");
194 if(preg_match("/d/",$acl)){
196 $this->dn = $dn;
198 /* Check locking, save current plugin in 'back_plugin', so
199 the dialog knows where to return. */
200 if (($user= get_lock($this->dn)) != ""){
201 return (gen_locked_message ($user, $this->dn));
202 }
204 /* Lock the current entry, so nobody will edit it during deletion */
205 add_lock ($this->dn, $this->ui->dn);
206 $smarty= get_smarty();
207 $smarty->assign("intro", sprintf(_("You're about to delete the macro '%s'."), $this->dn));
208 return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
209 } else {
211 /* Obviously the user isn't allowed to delete. Show message and
212 clean session. */
213 print_red (_("You are not allowed to delete this macro!"));
214 }
215 }
218 /*****************
219 Remove macro
220 *****************/
222 /* Confirmation for deletion has been passed. Macro should be deleted. */
223 if (isset($_POST['delete_macro_confirm'])){
225 $acl = $this->ui->get_permissions($this->dn,"gofonmacro/macro");
227 if(preg_match("/r/",$acl)){
229 /* Delete request is permitted, perform LDAP action */
230 $this->macrotabs= new macrotabs($this->config,$this->config->data['TABS']['MACROTABS'], $this->dn,"gofonmacro");
231 $this->macrotabs->delete ();
232 gosa_log ("macro object'".$this->dn."' has been removed");
233 unset ($this->macrotabs);
234 $this->macrotabs= NULL;
235 } else {
236 /* Normally this shouldn't be reached, send some extra
237 logs to notify the administrator */
238 print_red (_("You are not allowed to delete this macro!"));
239 gosa_log ("Warning: '".$this->ui->uid."' tried to trick group deletion.");
240 }
241 /* Remove lock file after successfull deletion */
242 del_lock ($this->dn);
243 }
246 /*****************
247 Display open dialogs
248 *****************/
250 /* Show tab dialog if object is present */
251 if (($this->macrotabs) && (isset($this->macrotabs->config))){
252 $display= $this->macrotabs->execute();
254 /* Don't show buttons if tab dialog requests this */
255 if (!$this->macrotabs->by_object[$this->macrotabs->current]->dialog){
256 $display.= "<p style=\"text-align:right\">\n";
257 $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
258 $display.= " \n";
259 if ($this->dn != "new"){
260 $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
261 $display.= " \n";
262 }
263 $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
264 $display.= "</p>";
265 }
266 return ($display);
267 }
270 /*****************
271 Display entry list
272 *****************/
274 /* Check if there is a snapshot dialog open */
275 $base = $this->DivListMacro->selectedBase;
276 if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
277 return($str);
278 }
280 /* Return rendered main page */
281 /* Display dialog with system list */
282 $this->DivListMacro->parent = $this;
283 $this->DivListMacro->execute();
285 /* Add departments if subsearch is disabled */
286 if(!$this->DivListMacro->SubSearch){
287 $this->DivListMacro->AddDepartments($this->DivListMacro->selectedBase,4);
288 }
289 $this->reload();
290 $this->DivListMacro->setEntries($this->macros);
291 return($this->DivListMacro->Draw());
292 }
295 /* Return departments, that will be included within snapshot detection */
296 function get_used_snapshot_bases()
297 {
298 return(array("ou=macros,ou=asterisk,ou=configs,ou=systems,".$this->DivListMacro->selectedBase));
299 }
302 function reload()
303 {
304 /* Set base for all searches */
305 $base = $this->DivListMacro->selectedBase;
306 $SubSearch = $this->DivListMacro->SubSearch;
307 $Regex = $this->DivListMacro->Regex;
308 $Filter = "(&(cn=".$Regex.")(objectClass=goFonMacro))";
309 $Flags = GL_SIZELIMIT;
310 $Attrs = array("cn","description","displayName","goFonMacroVisible");
312 /* Prepare for ls or search*/
313 if($SubSearch){
314 $Flags |= GL_SUBSEARCH;
315 }else{
316 $base = "ou=macros,ou=asterisk,ou=configs,ou=systems,".$base;
317 }
319 /* Generate macro list */
320 $res= get_list($Filter, "gofonmacro", $base, $Attrs, $Flags);
322 $tmp=array();
323 foreach($res as $tkey => $val ){
324 $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
325 }
326 ksort($tmp);
327 $this->macros=array();
328 foreach($tmp as $val){
329 $this->macros[]=$val;
330 }
332 reset ($this->macros);
333 }
336 /* Save data to object */
337 function save_object()
338 {
339 $this->DivListMacro->save_object();
340 }
343 function adapt_from_template($dn) { }
344 function check() { }
345 }
346 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
347 ?>