Code

Updated management classes to re-initialize after apply was pressed.
[gosa.git] / gosa-plugins / gofon / gofon / macro / class_gofonMacroManagement.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 goFonMacro extends plugin
22 {
23   /* Definitions */
24   var $plHeadline                   = "Phone macros";
25   var $plDescription      = "This does something";
27   /* Dialog attributes */
28   var $macrotabs                    = NULL;
29   var $macros                   = array();
30   var $ui                                       = NULL;
31   var $DivListMacro     = NULL;
33   var $CopyPasteHandler = NULL;
34   var $start_pasting_copied_objects = FALSE;
36   function gofonMacro(&$config, $ui)
37   {
38     /* Save configuration for internal use */
39     $this->config= $config;
40     $this->ui= $ui;
42     $this->DivListMacro = new divListMacro($this->config,$this);
44     /* Copy & Paste enabled ?*/
45     if((isset($this->config->data['MAIN']['ENABLECOPYPASTE']))&&(preg_match("/true/i",$this->config->data['MAIN']['ENABLECOPYPASTE']))){
46       $this->CopyPasteHandler = new CopyPasteHandler($this->config);
47     }
48   }
50   function execute()
51   {
52     /* Call parent execute */
53     plugin::execute();
55     session::set('LOCK_VARS_TO_USE',array("/^goFonMacro_/","/^act$/","/^id$/","/^item_selected/","/^remove_multiple_macros/"));
57     /*****************
58       Variable initialisation
59      *****************/
61     $s_action     = "";                       // Contains the action to proceed
62     $s_entry      = "";                       // The value for s_action
63     $base_back    = "";                       // The Link for Backbutton
64     $smarty= get_smarty();
67     /*****************
68       Check Posts 
69      *****************/
71     /* Test Posts */
72     foreach($_POST as $key => $val){
73       // Post for delete
74       if(preg_match("/^goFonMacro_del/",$key)){
75         $s_action = "del";
76         $s_entry  = preg_replace("/^goFonMacro_del_/i","",$key);
77         // Post for edit
78       }elseif(preg_match("/^goFonMacro_edit_/",$key)){
79         $s_action="edit";
80         $s_entry  = preg_replace("/^goFonMacro_edit_/i","",$key);
81         // Post for new
82       }elseif(preg_match("/^goFonMacro_new/",$key)){
83         $s_action="new";
84       }elseif(preg_match("/^remove_multiple_macros/",$key)){
85         $s_action="del_multiple";
86       }elseif(preg_match("/^editPaste.*/i",$key)){
87         $s_action="editPaste";
88       }elseif(preg_match("/^copy_.*/",$key)){
89         $s_action="copy";
90         $s_entry  = preg_replace("/^copy_/i","",$key);
91       }elseif(preg_match("/^cut_.*/",$key)){
92         $s_action="cut";
93         $s_entry  = preg_replace("/^cut_/i","",$key);
94       }elseif(preg_match("/^multiple_copy_objects/",$key)){
95         $s_action = "copy_multiple";
96       }elseif(preg_match("/^multiple_cut_objects/",$key)){
97         $s_action = "cut_multiple";
98       }
99     }
100     if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
101       $s_action ="edit";
102       $s_entry  = $_GET['id'];
103     }
104     $s_entry  = preg_replace("/_.$/","",$s_entry);
107     /********************
108       Copy & Paste Handling  ...
109      ********************/
111     
112     /* handle C&P from layers menu */
113     if(isset($_POST['menu_action']) && preg_match("/^multiple_copy_systems/",$_POST['menu_action'])){
114       $s_action = "copy_multiple";
115     }
116     if(isset($_POST['menu_action']) && preg_match("/^multiple_cut_systems/",$_POST['menu_action'])){
117       $s_action = "cut_multiple";
118     }
119     if(isset($_POST['menu_action']) && preg_match("/^editPaste/",$_POST['menu_action'])){
120       $s_action = "editPaste";
121     }
123     /* Create options */
124     if(isset($_POST['menu_action']) && $_POST['menu_action'] == "goFonMacro_new"){
125       $s_action = "new";
126     }
128     /* handle remove from layers menu */
129     if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
130       $s_action = "del_multiple";
131     }
133     /* Display the copy & paste dialog, if it is currently open */
134     $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
135     if($ret){
136       return($ret);
137     }
140     /*****************
141       Create a new Macro 
142      *****************/
144     /* New macro? */
145     if ($s_action=="new"){
147       /* By default we set 'dn' to 'new', all relevant plugins will
148          react on this. */
149       $this->dn= "new";
151       /* Create new usertab object */
152       $this->macrotabs= new macrotabs($this->config,$this->config->data['TABS']['MACROTABS'], $this->dn,"gofonmacro");
153       $this->macrotabs->set_acl_base($this->DivListMacro->selectedBase);
154     }
156     /*****************
157       Edit || Password canceled  
158      *****************/
160     /* Cancel dialogs */
161     if (isset($_POST['edit_cancel'])){
162       del_lock ($this->macrotabs->dn);
163       unset ($this->macrotabs);
164       $this->macrotabs= NULL;
165       session::un_set('objectinfo');
166     }
169     /*****************
170       Edit finised
171      *****************/
173     /* Finish mac edit is triggered by the tabulator dialog, so
174        the user wants to save edited data. Check and save at this
175        point. */
176     if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (isset($this->macrotabs->config))){
178       /* Check tabs, will feed message array */
179       $this->macrotabs->last= $this->macrotabs->current;
180       $this->macrotabs->save_object();
181       $message= $this->macrotabs->check();
183       /* Save, or display error message? */
184       if (count($message) == 0){
186         /* Save data data to ldap */
187         $this->macrotabs->save();
189         if (!isset($_POST['edit_apply'])){
190           /* macro has been saved successfully, remove lock from
191              LDAP. */
192           if ($this->dn != "new"){
193             del_lock ($this->dn);
194           }
196           unset ($this->macrotabs);
197           $this->macrotabs= NULL;
198           session::un_set('objectinfo');
199         }else{
201           /* Reinitialize tab */
202           if($this->macrotabs instanceof tabs){
203             $this->macrotabs->re_init();
204           }
205         }
206       } else {
207         /* Ok. There seem to be errors regarding to the tab data,
208            show message and continue as usual. */
209         msg_dialog::displayChecks($message);
210       }
211     }
214     /*****************
215       Edit macro 
216      *****************/
218     /* User wants to edit data */
219     if (($s_action=="edit") && (!isset($this->macrotabs->config))){
221       $dn  = $this->macros[$s_entry]['dn'];
222       $acl = $this->ui->get_permissions($dn,"gofonmacro/macro");
223       if(preg_match("/r/",$acl)){
224         $this->dn = $dn;
226         /* Check locking, save current plugin in 'back_plugin', so
227            the dialog knows where to return. */
228         if (($user= get_lock($this->dn)) != ""){
229           return(gen_locked_message ($user, $this->dn));
230         }
232         /* Lock the current entry, so everyone will get the
233            above dialog */
234         add_lock ($this->dn, $this->ui->dn);
235         /* Register macrotabs to trigger edit dialog */
236         $this->macrotabs= new macrotabs($this->config,
237             $this->config->data['TABS']['MACROTABS'], $this->dn,"gofonmacro");
238         session::set('objectinfo',$this->dn);
239       }
240     }
244     /********************
245       Delete MULTIPLE entries requested, display confirm dialog
246      ********************/
248     if ($s_action=="del_multiple"){
249       $ids = $this->list_get_selected_items();
251       if(count($ids)){
253         foreach($ids as $id){
254           $dn = $this->macros[$id]['dn'];
255           if (($user= get_lock($dn)) != ""){
256             return(gen_locked_message ($user, $dn));
257           }
258           $this->dns[$id] = $dn;
259         }
261         $dns_names = array();
262         foreach($this->dns as $dn){
263           add_lock ($dn, $this->ui->dn);
264           $dns_names[] = @LDAP::fix($dn);
265         }
267         /* Lock the current entry, so nobody will edit it during deletion */
268         $smarty->assign("intro", msgPool::deleteInfo($dns_names,("macro")));
269         $smarty->assign("multiple", true);
270         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
271       }
272     }
275     /********************
276       Delete MULTIPLE entries confirmed
277      ********************/
279     /* Confirmation for deletion has been passed. Users should be deleted. */
280     if (isset($_POST['delete_multiple_macro_confirm'])){
282       /* Remove user by user and check acls before removeing them */
283       foreach($this->dns as $key => $dn){
285         $acl = $this->ui->get_permissions($dn,"gofonmacro/macro");
286         if(preg_match("/d/",$acl)){
288           /* Delete request is permitted, perform LDAP action */
289           $this->macrotabs= new macrotabs($this->config,$this->config->data['TABS']['MACROTABS'], $dn,"gofonmacro");
290           $this->macrotabs->delete ();
291           unset ($this->macrotabs);
292           $this->macrotabs= NULL;
294         } else {
295           msg_dialog::display(_("Permission error"),msgPool::permDelete(), ERROR_DIALOG);
296         }
298         /* Remove lock file after successfull deletion */
299         del_lock ($dn);
300         unset($this->dns[$key]);
301       }
302     }
305     /********************
306       Delete MULTIPLE entries Canceled
307      ********************/
309     /* Remove lock */
310     if(isset($_POST['delete_multiple_macro_cancel'])){
311       foreach($this->dns as $key => $dn){
312         del_lock ($dn);
313         unset($this->dns[$key]);
314       }
315     }
318     /*****************
319       Remove macro
320      *****************/
322     /* Remove user was requested */
323     if ($s_action=="del"){
325       /* Get 'dn' from posted 'uid' */
326       $dn  = $this->macros[$s_entry]['dn'];
327       $acl = $this->ui->get_permissions($dn,"gofonmacro/macro");
329       if(preg_match("/d/",$acl)){
331         $this->dn = $dn;
332   
333         /* Check locking, save current plugin in 'back_plugin', so
334            the dialog knows where to return. */
335         if (($user= get_lock($this->dn)) != ""){
336           return (gen_locked_message ($user, $this->dn));
337         }
339         /* Lock the current entry, so nobody will edit it during deletion */
340         add_lock ($this->dn, $this->ui->dn);
341         $smarty= get_smarty();
342         $smarty->assign("intro", msgPool::deleteInfo(@LDAP::fix($this->dn),_("macro")));
343         $smarty->assign("multiple", false);
344         return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
345       } else {
347         /* Obviously the user isn't allowed to delete. Show message and
348            clean session. */
349         msg_dialog::display(_("Permission error"), msgPool::permDelete(), ERROR_DIALOG);
350       }
351     }
354     /*****************
355       Remove macro
356      *****************/
358     /* Confirmation for deletion has been passed. Macro should be deleted. */
359     if (isset($_POST['delete_macro_confirm'])){
361       $acl = $this->ui->get_permissions($this->dn,"gofonmacro/macro");
363       if(preg_match("/r/",$acl)){
365         /* Delete request is permitted, perform LDAP action */
366         $this->macrotabs= new macrotabs($this->config,$this->config->data['TABS']['MACROTABS'], $this->dn,"gofonmacro");
367         $this->macrotabs->delete ();
368         unset ($this->macrotabs);
369         $this->macrotabs= NULL;
370       } else {
371         /* Normally this shouldn't be reached, send some extra
372            logs to notify the administrator */
373         msg_dialog::display(_("Permission error"), msgPool::permDelete(), ERROR_DIALOG);
374         new log("security","gofonmacro/".get_class($this),$this->dn,array(),"Tried to trick deletion.");
375       }
376       /* Remove lock file after successfull deletion */
377       del_lock ($this->dn);
378     }
381     /*****************
382       Display open dialogs  
383      *****************/
385     /* Show tab dialog if object is present */
386     if (($this->macrotabs) && (isset($this->macrotabs->config))){
387       $display= $this->macrotabs->execute();
389       /* Don't show buttons if tab dialog requests this */
390       if (!$this->macrotabs->by_object[$this->macrotabs->current]->dialog){
391         $display.= "<p style=\"text-align:right\">\n";
392         $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
393         $display.= "&nbsp;\n";
394         if ($this->dn != "new"){
395           $display.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
396           $display.= "&nbsp;\n";
397         }
398         $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
399         $display.= "</p>";
400       }
401       return ($display);
402     }
405     /*****************
406       Display entry list 
407      *****************/
409     /* Check if there is a snapshot dialog open */
410     $base = $this->DivListMacro->selectedBase;
411     if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
412       return($str);
413     }
415     /* Return rendered main page */
416     /* Display dialog with system list */
417     $this->DivListMacro->parent = $this;
418     $this->DivListMacro->execute();
420     /* Add departments if subsearch is disabled */
421     if(!$this->DivListMacro->SubSearch){
422       $this->DivListMacro->AddDepartments($this->DivListMacro->selectedBase,4,1);
423     }
424     $this->reload();
425     $this->DivListMacro->setEntries($this->macros);
426     return($this->DivListMacro->Draw());
427   }
430   function copyPasteHandling_from_queue($s_action,$s_entry)
431   {
432     /* Check if Copy & Paste is disabled */
433     if(!is_object($this->CopyPasteHandler)){
434       return("");
435     }
437     /* Add a single entry to queue */
438     if($s_action == "cut" || $s_action == "copy"){
440       /* Cleanup object queue */
441       $this->CopyPasteHandler->cleanup_queue();
442       $dn = $this->macros[$s_entry]['dn'];
443       $this->CopyPasteHandler->add_to_queue($dn,$s_action,"macrotabs","MACROTABS","gofonmacro");
444     }
446     /* Add entries to queue */
447     if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
449       /* Cleanup object queue */
450       $this->CopyPasteHandler->cleanup_queue();
452       /* Add new entries to CP queue */
453       foreach($this->list_get_selected_items() as $id){
454         $dn = $this->macros[$id]['dn'];
456         if($s_action == "copy_multiple"){
457           $this->CopyPasteHandler->add_to_queue($dn,"copy","macrotabs","MACROTABS","gofonmacro");
458         }
459         if($s_action == "cut_multiple"){
460           $this->CopyPasteHandler->add_to_queue($dn,"cut","macrotabs","MACROTABS","gofonmacro");
461         }
462       }
463     }
465     /* Start pasting entries */
466     if($s_action == "editPaste"){
467       $this->start_pasting_copied_objects = TRUE;
468     }
470     /* Return C&P dialog */
471     if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
473       /* Get dialog */
474       $data = $this->CopyPasteHandler->execute();
475       $this->CopyPasteHandler->SetVar("base",$this->DivListMacro->selectedBase);
477       /* Return dialog data */
478       if(!empty($data)){
479         return($data);
480       }
481     }
483     /* Automatically disable status for pasting */
484     if(!$this->CopyPasteHandler->entries_queued()){
485       $this->start_pasting_copied_objects = FALSE;
486     }
487     return("");
488   }
492   /* Return departments, that will be included within snapshot detection */
493   function get_used_snapshot_bases()
494   {
495     return(array(get_ou('macroou').$this->DivListMacro->selectedBase));
496   }
499   function reload()
500   {
501     /* Set base for all searches */
502     $base       = $this->DivListMacro->selectedBase;
503     $SubSearch  = $this->DivListMacro->SubSearch;
504     $Regex      = $this->DivListMacro->Regex;
505     $Filter     = "(&(cn=".$Regex.")(objectClass=goFonMacro))";
506     $Flags      = GL_SIZELIMIT;
507     $Attrs      = array("cn","description","displayName","goFonMacroVisible");
509     /* Prepare for ls or search*/        
510     if($SubSearch){
511       $Flags |= GL_SUBSEARCH;
512     }else{
513       $base = get_ou('macroou').$base;
514     }
516     /* Generate macro list */
517     $res= get_list($Filter, "gofonmacro", $base, $Attrs, $Flags);
519     $tmp=array();
520     foreach($res as $tkey => $val ){
521       $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
522     }
523     ksort($tmp);
524     $this->macros=array();
525     foreach($tmp as $val){
526       $this->macros[]=$val;
527     }
529     reset ($this->macros);
530   }
533   /* Save data to object */
534   function save_object()
535   {
536     $this->DivListMacro->save_object();
537     if(is_object($this->CopyPasteHandler)){
538       $this->CopyPasteHandler->save_object();
539     }
540   }
543   function list_get_selected_items()
544   {
545     $ids = array();
546     foreach($_POST as $name => $value){
547       if(preg_match("/^item_selected_[0-9]*$/",$name)){
548         $id   = preg_replace("/^item_selected_/","",$name);
549         $ids[$id] = $id;
550       }
551     }
552     return($ids);
553   }
556   function adapt_from_template($dn, $skip= array())  { }
557   function check() { }
559 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
560 ?>