Code

Fixed lock handling for macros.
[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("/^menu_action/","/^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       $this->remove_lock();
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             $this->remove_lock();
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);
236         /* Register macrotabs to trigger edit dialog */
237         $this->macrotabs= new macrotabs($this->config,
238             $this->config->data['TABS']['MACROTABS'], $this->dn,"gofonmacro");
239         session::set('objectinfo',$this->dn);
240       }
241     }
245     /********************
246       Delete MULTIPLE entries requested, display confirm dialog
247      ********************/
249     if ($s_action=="del_multiple"){
250       $ids = $this->list_get_selected_items();
252       if(count($ids)){
254         /* Collect dns */
255         foreach($ids as $id){
256           $this->dns[$id] = $this->macros[$id]['dn'];
257         }
259         /* Check locks */
260         if ($user= get_multiple_locks($this->dns)){
261           return(gen_locked_message($user,$this->dns));
262         }
264         $dns_names = array();
265         foreach($this->dns as $dn){
266           $dns_names[] = @LDAP::fix($dn);
267         }
269         /* Add entry locks */
270         add_lock ($this->dns, $this->ui->dn);
272         /* Lock the current entry, so nobody will edit it during deletion */
273         $smarty->assign("intro", msgPool::deleteInfo($dns_names,("macro")));
274         $smarty->assign("multiple", true);
275         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
276       }
277     }
280     /********************
281       Delete MULTIPLE entries confirmed
282      ********************/
284     /* Confirmation for deletion has been passed. Users should be deleted. */
285     if (isset($_POST['delete_multiple_macro_confirm'])){
287       /* Remove user by user and check acls before removeing them */
288       foreach($this->dns as $key => $dn){
290         $acl = $this->ui->get_permissions($dn,"gofonmacro/macro");
291         if(preg_match("/d/",$acl)){
293           /* Delete request is permitted, perform LDAP action */
294           $this->macrotabs= new macrotabs($this->config,$this->config->data['TABS']['MACROTABS'], $dn,"gofonmacro");
295           $this->macrotabs->delete ();
296           unset ($this->macrotabs);
297           $this->macrotabs= NULL;
299         } else {
300           msg_dialog::display(_("Permission error"),msgPool::permDelete(), ERROR_DIALOG);
301         }
303       }
305       /* Remove lock file after successfull deletion */
306       $this->remove_lock();
307       $this->dns = array();
308     }
311     /********************
312       Delete MULTIPLE entries Canceled
313      ********************/
315     /* Remove lock */
316     if(isset($_POST['delete_multiple_macro_cancel'])){
317       $this->remove_lock();
318       $this->dns = array();
319     }
322     /*****************
323       Remove macro
324      *****************/
326     /* Remove user was requested */
327     if ($s_action=="del"){
329       /* Get 'dn' from posted 'uid' */
330       $dn  = $this->macros[$s_entry]['dn'];
331       $acl = $this->ui->get_permissions($dn,"gofonmacro/macro");
333       if(preg_match("/d/",$acl)){
335         $this->dn = $dn;
336   
337         /* Check locking, save current plugin in 'back_plugin', so
338            the dialog knows where to return. */
339         if (($user= get_lock($this->dn)) != ""){
340           return (gen_locked_message ($user, $this->dn));
341         }
343         /* Lock the current entry, so nobody will edit it during deletion */
344         add_lock ($this->dn, $this->ui->dn);
345         $smarty= get_smarty();
346         $smarty->assign("intro", msgPool::deleteInfo(@LDAP::fix($this->dn),_("macro")));
347         $smarty->assign("multiple", false);
348         return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
349       } else {
351         /* Obviously the user isn't allowed to delete. Show message and
352            clean session. */
353         msg_dialog::display(_("Permission error"), msgPool::permDelete(), ERROR_DIALOG);
354       }
355     }
358     /*****************
359       Remove macro
360      *****************/
362     /* Confirmation for deletion has been passed. Macro should be deleted. */
363     if (isset($_POST['delete_macro_confirm'])){
365       $acl = $this->ui->get_permissions($this->dn,"gofonmacro/macro");
367       if(preg_match("/r/",$acl)){
369         /* Delete request is permitted, perform LDAP action */
370         $this->macrotabs= new macrotabs($this->config,$this->config->data['TABS']['MACROTABS'], $this->dn,"gofonmacro");
371         $this->macrotabs->delete ();
372         unset ($this->macrotabs);
373         $this->macrotabs= NULL;
374       } else {
375         /* Normally this shouldn't be reached, send some extra
376            logs to notify the administrator */
377         msg_dialog::display(_("Permission error"), msgPool::permDelete(), ERROR_DIALOG);
378         new log("security","gofonmacro/".get_class($this),$this->dn,array(),"Tried to trick deletion.");
379       }
381       /* Remove lock file after successfull deletion */
382       $this->remove_lock();
383     }
386     /*****************
387       Display open dialogs  
388      *****************/
390     /* Show tab dialog if object is present */
391     if (($this->macrotabs) && (isset($this->macrotabs->config))){
392       $display= $this->macrotabs->execute();
394       /* Don't show buttons if tab dialog requests this */
395       if (!$this->macrotabs->by_object[$this->macrotabs->current]->dialog){
396         $display.= "<p style=\"text-align:right\">\n";
397         $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
398         $display.= "&nbsp;\n";
399         if ($this->dn != "new"){
400           $display.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
401           $display.= "&nbsp;\n";
402         }
403         $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
404         $display.= "</p>";
405       }
406       return ($display);
407     }
410     /*****************
411       Display entry list 
412      *****************/
414     /* Check if there is a snapshot dialog open */
415     $base = $this->DivListMacro->selectedBase;
416     if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
417       return($str);
418     }
420     /* Return rendered main page */
421     /* Display dialog with system list */
422     $this->DivListMacro->parent = $this;
423     $this->DivListMacro->execute();
425     /* Add departments if subsearch is disabled */
426     if(!$this->DivListMacro->SubSearch){
427       $this->DivListMacro->AddDepartments($this->DivListMacro->selectedBase,4,1);
428     }
429     $this->reload();
430     $this->DivListMacro->setEntries($this->macros);
431     return($this->DivListMacro->Draw());
432   }
435   function copyPasteHandling_from_queue($s_action,$s_entry)
436   {
437     /* Check if Copy & Paste is disabled */
438     if(!is_object($this->CopyPasteHandler)){
439       return("");
440     }
442     /* Add a single entry to queue */
443     if($s_action == "cut" || $s_action == "copy"){
445       /* Cleanup object queue */
446       $this->CopyPasteHandler->cleanup_queue();
447       $dn = $this->macros[$s_entry]['dn'];
448       $this->CopyPasteHandler->add_to_queue($dn,$s_action,"macrotabs","MACROTABS","gofonmacro");
449     }
451     /* Add entries to queue */
452     if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
454       /* Cleanup object queue */
455       $this->CopyPasteHandler->cleanup_queue();
457       /* Add new entries to CP queue */
458       foreach($this->list_get_selected_items() as $id){
459         $dn = $this->macros[$id]['dn'];
461         if($s_action == "copy_multiple"){
462           $this->CopyPasteHandler->add_to_queue($dn,"copy","macrotabs","MACROTABS","gofonmacro");
463         }
464         if($s_action == "cut_multiple"){
465           $this->CopyPasteHandler->add_to_queue($dn,"cut","macrotabs","MACROTABS","gofonmacro");
466         }
467       }
468     }
470     /* Start pasting entries */
471     if($s_action == "editPaste"){
472       $this->start_pasting_copied_objects = TRUE;
473     }
475     /* Return C&P dialog */
476     if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
478       /* Get dialog */
479       $data = $this->CopyPasteHandler->execute();
480       $this->CopyPasteHandler->SetVar("base",$this->DivListMacro->selectedBase);
482       /* Return dialog data */
483       if(!empty($data)){
484         return($data);
485       }
486     }
488     /* Automatically disable status for pasting */
489     if(!$this->CopyPasteHandler->entries_queued()){
490       $this->start_pasting_copied_objects = FALSE;
491     }
492     return("");
493   }
497   /* Return departments, that will be included within snapshot detection */
498   function get_used_snapshot_bases()
499   {
500     return(array(get_ou('macroou').$this->DivListMacro->selectedBase));
501   }
504   function reload()
505   {
506     /* Set base for all searches */
507     $base       = $this->DivListMacro->selectedBase;
508     $SubSearch  = $this->DivListMacro->SubSearch;
509     $Regex      = $this->DivListMacro->Regex;
510     $Filter     = "(&(cn=".$Regex.")(objectClass=goFonMacro))";
511     $Flags      = GL_SIZELIMIT;
512     $Attrs      = array("cn","description","displayName","goFonMacroVisible");
514     /* Prepare for ls or search*/        
515     if($SubSearch){
516       $Flags |= GL_SUBSEARCH;
517     }else{
518       $base = get_ou('macroou').$base;
519     }
521     /* Generate macro list */
522     $res= get_list($Filter, "gofonmacro", $base, $Attrs, $Flags);
524     $tmp=array();
525     foreach($res as $tkey => $val ){
526       $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
527     }
528     ksort($tmp);
529     $this->macros=array();
530     foreach($tmp as $val){
531       $this->macros[]=$val;
532     }
534     reset ($this->macros);
535   }
538   /* Save data to object */
539   function save_object()
540   {
541     $this->DivListMacro->save_object();
542     if(is_object($this->CopyPasteHandler)){
543       $this->CopyPasteHandler->save_object();
544     }
545   }
548   function list_get_selected_items()
549   {
550     $ids = array();
551     foreach($_POST as $name => $value){
552       if(preg_match("/^item_selected_[0-9]*$/",$name)){
553         $id   = preg_replace("/^item_selected_/","",$name);
554         $ids[$id] = $id;
555       }
556     }
557     return($ids);
558   }
560   /*! \brief Remove entry locks if the plugin was aborted. 
561    */
562   function remove_lock()
563   {
564     if($this->dn) del_lock($this->dn);
565     if(is_array($this->dns) && count($this->dns)) del_lock($this->dns);
566   }
568   function adapt_from_template($dn, $skip= array())  { }
569   function check() { }
571 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
572 ?>