Code

Updated conference lock handling.
[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   var $dns              = array();
38   function gofonMacro(&$config, $ui)
39   {
40     /* Save configuration for internal use */
41     $this->config= $config;
42     $this->ui= $ui;
44     $this->DivListMacro = new divListMacro($this->config,$this);
46     /* Copy & Paste enabled ?*/
47     if((isset($this->config->data['MAIN']['ENABLECOPYPASTE']))&&(preg_match("/true/i",$this->config->data['MAIN']['ENABLECOPYPASTE']))){
48       $this->CopyPasteHandler = new CopyPasteHandler($this->config);
49     }
50   }
52   function execute()
53   {
54     /* Call parent execute */
55     plugin::execute();
57     session::set('LOCK_VARS_TO_USE',array("/^menu_action/","/^goFonMacro_/","/^act$/","/^id$/","/^item_selected/","/^remove_multiple_macros/"));
59     /*****************
60       Variable initialisation
61      *****************/
63     $s_action     = "";                       // Contains the action to proceed
64     $s_entry      = "";                       // The value for s_action
65     $base_back    = "";                       // The Link for Backbutton
66     $smarty= get_smarty();
69     /*****************
70       Check Posts 
71      *****************/
73     /* Test Posts */
74     foreach($_POST as $key => $val){
75       // Post for delete
76       if(preg_match("/^goFonMacro_del/",$key)){
77         $s_action = "del";
78         $s_entry  = preg_replace("/^goFonMacro_del_/i","",$key);
79         // Post for edit
80       }elseif(preg_match("/^goFonMacro_edit_/",$key)){
81         $s_action="edit";
82         $s_entry  = preg_replace("/^goFonMacro_edit_/i","",$key);
83         // Post for new
84       }elseif(preg_match("/^goFonMacro_new/",$key)){
85         $s_action="new";
86       }elseif(preg_match("/^remove_multiple_macros/",$key)){
87         $s_action="del_multiple";
88       }elseif(preg_match("/^editPaste.*/i",$key)){
89         $s_action="editPaste";
90       }elseif(preg_match("/^copy_.*/",$key)){
91         $s_action="copy";
92         $s_entry  = preg_replace("/^copy_/i","",$key);
93       }elseif(preg_match("/^cut_.*/",$key)){
94         $s_action="cut";
95         $s_entry  = preg_replace("/^cut_/i","",$key);
96       }elseif(preg_match("/^multiple_copy_objects/",$key)){
97         $s_action = "copy_multiple";
98       }elseif(preg_match("/^multiple_cut_objects/",$key)){
99         $s_action = "cut_multiple";
100       }
101     }
102     if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
103       $s_action ="edit";
104       $s_entry  = $_GET['id'];
105     }
106     $s_entry  = preg_replace("/_.$/","",$s_entry);
109     /********************
110       Copy & Paste Handling  ...
111      ********************/
113     
114     /* handle C&P from layers menu */
115     if(isset($_POST['menu_action']) && preg_match("/^multiple_copy_systems/",$_POST['menu_action'])){
116       $s_action = "copy_multiple";
117     }
118     if(isset($_POST['menu_action']) && preg_match("/^multiple_cut_systems/",$_POST['menu_action'])){
119       $s_action = "cut_multiple";
120     }
121     if(isset($_POST['menu_action']) && preg_match("/^editPaste/",$_POST['menu_action'])){
122       $s_action = "editPaste";
123     }
125     /* Create options */
126     if(isset($_POST['menu_action']) && $_POST['menu_action'] == "goFonMacro_new"){
127       $s_action = "new";
128     }
130     /* handle remove from layers menu */
131     if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
132       $s_action = "del_multiple";
133     }
135     /* Display the copy & paste dialog, if it is currently open */
136     $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
137     if($ret){
138       return($ret);
139     }
142     /*****************
143       Create a new Macro 
144      *****************/
146     /* New macro? */
147     if ($s_action=="new"){
149       /* By default we set 'dn' to 'new', all relevant plugins will
150          react on this. */
151       $this->dn= "new";
153       /* Create new usertab object */
154       $this->macrotabs= new macrotabs($this->config,$this->config->data['TABS']['MACROTABS'], $this->dn,"gofonmacro");
155       $this->macrotabs->set_acl_base($this->DivListMacro->selectedBase);
156     }
158     /*****************
159       Edit || Password canceled  
160      *****************/
162     /* Cancel dialogs */
163     if (isset($_POST['edit_cancel'])){
164       $this->remove_lock();
165       unset ($this->macrotabs);
166       $this->macrotabs= NULL;
167       session::un_set('objectinfo');
168     }
171     /*****************
172       Edit finised
173      *****************/
175     /* Finish mac edit is triggered by the tabulator dialog, so
176        the user wants to save edited data. Check and save at this
177        point. */
178     if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (isset($this->macrotabs->config))){
180       /* Check tabs, will feed message array */
181       $this->macrotabs->last= $this->macrotabs->current;
182       $this->macrotabs->save_object();
183       $message= $this->macrotabs->check();
185       /* Save, or display error message? */
186       if (count($message) == 0){
188         /* Save data data to ldap */
189         $this->macrotabs->save();
191         if (!isset($_POST['edit_apply'])){
192           /* macro has been saved successfully, remove lock from
193              LDAP. */
194           if ($this->dn != "new"){
195             $this->remove_lock();
196           }
198           unset ($this->macrotabs);
199           $this->macrotabs= NULL;
200           session::un_set('objectinfo');
201         }else{
203           /* Reinitialize tab */
204           if($this->macrotabs instanceof tabs){
205             $this->macrotabs->re_init();
206           }
207         }
208       } else {
209         /* Ok. There seem to be errors regarding to the tab data,
210            show message and continue as usual. */
211         msg_dialog::displayChecks($message);
212       }
213     }
216     /*****************
217       Edit macro 
218      *****************/
220     /* User wants to edit data */
221     if (($s_action=="edit") && (!isset($this->macrotabs->config))){
223       $dn  = $this->macros[$s_entry]['dn'];
224       $acl = $this->ui->get_permissions($dn,"gofonmacro/macro");
225       if(preg_match("/r/",$acl)){
226         $this->dn = $dn;
228         /* Check locking, save current plugin in 'back_plugin', so
229            the dialog knows where to return. */
230         if (($user= get_lock($this->dn)) != ""){
231           return(gen_locked_message ($user, $this->dn));
232         }
234         /* Lock the current entry, so everyone will get the
235            above dialog */
236         add_lock ($this->dn, $this->ui->dn);
238         /* Register macrotabs to trigger edit dialog */
239         $this->macrotabs= new macrotabs($this->config,
240             $this->config->data['TABS']['MACROTABS'], $this->dn,"gofonmacro");
241         session::set('objectinfo',$this->dn);
242       }
243     }
247     /********************
248       Delete MULTIPLE entries requested, display confirm dialog
249      ********************/
251     if ($s_action=="del_multiple"){
252       $ids = $this->list_get_selected_items();
254       if(count($ids)){
256         /* Collect dns */
257         foreach($ids as $id){
258           $this->dns[$id] = $this->macros[$id]['dn'];
259         }
261         /* Check locks */
262         if ($user= get_multiple_locks($this->dns)){
263           return(gen_locked_message($user,$this->dns));
264         }
266         $dns_names = array();
267         foreach($this->dns as $dn){
268           $dns_names[] = @LDAP::fix($dn);
269         }
271         /* Add entry locks */
272         add_lock ($this->dns, $this->ui->dn);
274         /* Lock the current entry, so nobody will edit it during deletion */
275         $smarty->assign("intro", msgPool::deleteInfo($dns_names,("macro")));
276         $smarty->assign("multiple", true);
277         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
278       }
279     }
282     /********************
283       Delete MULTIPLE entries confirmed
284      ********************/
286     /* Confirmation for deletion has been passed. Users should be deleted. */
287     if (isset($_POST['delete_multiple_macro_confirm'])){
289       /* Remove user by user and check acls before removeing them */
290       foreach($this->dns as $key => $dn){
292         $acl = $this->ui->get_permissions($dn,"gofonmacro/macro");
293         if(preg_match("/d/",$acl)){
295           /* Delete request is permitted, perform LDAP action */
296           $this->macrotabs= new macrotabs($this->config,$this->config->data['TABS']['MACROTABS'], $dn,"gofonmacro");
297           $this->macrotabs->delete ();
298           unset ($this->macrotabs);
299           $this->macrotabs= NULL;
301         } else {
302           msg_dialog::display(_("Permission error"),msgPool::permDelete(), ERROR_DIALOG);
303         }
305       }
307       /* Remove lock file after successfull deletion */
308       $this->remove_lock();
309       $this->dns = array();
310     }
313     /********************
314       Delete MULTIPLE entries Canceled
315      ********************/
317     /* Remove lock */
318     if(isset($_POST['delete_multiple_macro_cancel'])){
319       $this->remove_lock();
320       $this->dns = array();
321     }
324     /*****************
325       Remove macro
326      *****************/
328     /* Remove user was requested */
329     if ($s_action=="del"){
331       /* Get 'dn' from posted 'uid' */
332       $dn  = $this->macros[$s_entry]['dn'];
333       $acl = $this->ui->get_permissions($dn,"gofonmacro/macro");
335       if(preg_match("/d/",$acl)){
337         $this->dn = $dn;
338   
339         /* Check locking, save current plugin in 'back_plugin', so
340            the dialog knows where to return. */
341         if (($user= get_lock($this->dn)) != ""){
342           return (gen_locked_message ($user, $this->dn));
343         }
345         /* Lock the current entry, so nobody will edit it during deletion */
346         add_lock ($this->dn, $this->ui->dn);
347         $smarty= get_smarty();
348         $smarty->assign("intro", msgPool::deleteInfo(@LDAP::fix($this->dn),_("macro")));
349         $smarty->assign("multiple", false);
350         return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
351       } else {
353         /* Obviously the user isn't allowed to delete. Show message and
354            clean session. */
355         msg_dialog::display(_("Permission error"), msgPool::permDelete(), ERROR_DIALOG);
356       }
357     }
360     /*****************
361       Remove macro
362      *****************/
364     /* Confirmation for deletion has been passed. Macro should be deleted. */
365     if (isset($_POST['delete_macro_confirm'])){
367       $acl = $this->ui->get_permissions($this->dn,"gofonmacro/macro");
369       if(preg_match("/r/",$acl)){
371         /* Delete request is permitted, perform LDAP action */
372         $this->macrotabs= new macrotabs($this->config,$this->config->data['TABS']['MACROTABS'], $this->dn,"gofonmacro");
373         $this->macrotabs->delete ();
374         unset ($this->macrotabs);
375         $this->macrotabs= NULL;
376       } else {
377         /* Normally this shouldn't be reached, send some extra
378            logs to notify the administrator */
379         msg_dialog::display(_("Permission error"), msgPool::permDelete(), ERROR_DIALOG);
380         new log("security","gofonmacro/".get_class($this),$this->dn,array(),"Tried to trick deletion.");
381       }
383       /* Remove lock file after successfull deletion */
384       $this->remove_lock();
385     }
388     /*****************
389       Display open dialogs  
390      *****************/
392     /* Show tab dialog if object is present */
393     if (($this->macrotabs) && (isset($this->macrotabs->config))){
394       $display= $this->macrotabs->execute();
396       /* Don't show buttons if tab dialog requests this */
397       if (!$this->macrotabs->by_object[$this->macrotabs->current]->dialog){
398         $display.= "<p style=\"text-align:right\">\n";
399         $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
400         $display.= "&nbsp;\n";
401         if ($this->dn != "new"){
402           $display.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
403           $display.= "&nbsp;\n";
404         }
405         $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
406         $display.= "</p>";
407       }
408       return ($display);
409     }
412     /*****************
413       Display entry list 
414      *****************/
416     /* Check if there is a snapshot dialog open */
417     $base = $this->DivListMacro->selectedBase;
418     if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
419       return($str);
420     }
422     /* Return rendered main page */
423     /* Display dialog with system list */
424     $this->DivListMacro->parent = $this;
425     $this->DivListMacro->execute();
427     /* Add departments if subsearch is disabled */
428     if(!$this->DivListMacro->SubSearch){
429       $this->DivListMacro->AddDepartments($this->DivListMacro->selectedBase,4,1);
430     }
431     $this->reload();
432     $this->DivListMacro->setEntries($this->macros);
433     return($this->DivListMacro->Draw());
434   }
437   function copyPasteHandling_from_queue($s_action,$s_entry)
438   {
439     /* Check if Copy & Paste is disabled */
440     if(!is_object($this->CopyPasteHandler)){
441       return("");
442     }
444     /* Add a single entry to queue */
445     if($s_action == "cut" || $s_action == "copy"){
447       /* Cleanup object queue */
448       $this->CopyPasteHandler->cleanup_queue();
449       $dn = $this->macros[$s_entry]['dn'];
450       $this->CopyPasteHandler->add_to_queue($dn,$s_action,"macrotabs","MACROTABS","gofonmacro");
451     }
453     /* Add entries to queue */
454     if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
456       /* Cleanup object queue */
457       $this->CopyPasteHandler->cleanup_queue();
459       /* Add new entries to CP queue */
460       foreach($this->list_get_selected_items() as $id){
461         $dn = $this->macros[$id]['dn'];
463         if($s_action == "copy_multiple"){
464           $this->CopyPasteHandler->add_to_queue($dn,"copy","macrotabs","MACROTABS","gofonmacro");
465         }
466         if($s_action == "cut_multiple"){
467           $this->CopyPasteHandler->add_to_queue($dn,"cut","macrotabs","MACROTABS","gofonmacro");
468         }
469       }
470     }
472     /* Start pasting entries */
473     if($s_action == "editPaste"){
474       $this->start_pasting_copied_objects = TRUE;
475     }
477     /* Return C&P dialog */
478     if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
480       /* Get dialog */
481       $data = $this->CopyPasteHandler->execute();
482       $this->CopyPasteHandler->SetVar("base",$this->DivListMacro->selectedBase);
484       /* Return dialog data */
485       if(!empty($data)){
486         return($data);
487       }
488     }
490     /* Automatically disable status for pasting */
491     if(!$this->CopyPasteHandler->entries_queued()){
492       $this->start_pasting_copied_objects = FALSE;
493     }
494     return("");
495   }
499   /* Return departments, that will be included within snapshot detection */
500   function get_used_snapshot_bases()
501   {
502     return(array(get_ou('macroou').$this->DivListMacro->selectedBase));
503   }
506   function reload()
507   {
508     /* Set base for all searches */
509     $base       = $this->DivListMacro->selectedBase;
510     $SubSearch  = $this->DivListMacro->SubSearch;
511     $Regex      = $this->DivListMacro->Regex;
512     $Filter     = "(&(cn=".$Regex.")(objectClass=goFonMacro))";
513     $Flags      = GL_SIZELIMIT;
514     $Attrs      = array("cn","description","displayName","goFonMacroVisible");
516     /* Prepare for ls or search*/        
517     if($SubSearch){
518       $Flags |= GL_SUBSEARCH;
519     }else{
520       $base = get_ou('macroou').$base;
521     }
523     /* Generate macro list */
524     $res= get_list($Filter, "gofonmacro", $base, $Attrs, $Flags);
526     $tmp=array();
527     foreach($res as $tkey => $val ){
528       $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
529     }
530     ksort($tmp);
531     $this->macros=array();
532     foreach($tmp as $val){
533       $this->macros[]=$val;
534     }
536     reset ($this->macros);
537   }
540   /* Save data to object */
541   function save_object()
542   {
543     $this->DivListMacro->save_object();
544     if(is_object($this->CopyPasteHandler)){
545       $this->CopyPasteHandler->save_object();
546     }
547   }
550   function list_get_selected_items()
551   {
552     $ids = array();
553     foreach($_POST as $name => $value){
554       if(preg_match("/^item_selected_[0-9]*$/",$name)){
555         $id   = preg_replace("/^item_selected_/","",$name);
556         $ids[$id] = $id;
557       }
558     }
559     return($ids);
560   }
562   /*! \brief Remove entry locks if the plugin was aborted. 
563    */
564   function remove_lock()
565   {
566     if($this->dn) del_lock($this->dn);
567     if(is_array($this->dns) && count($this->dns)) del_lock($this->dns);
568   }
570   function adapt_from_template($dn, $skip= array())  { }
571   function check() { }
573 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
574 ?>