Code

fixed macro plugin image.
[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";
26   var $plIcon           = "plugins/gofon/images/macros.png";
29   /* Dialog attributes */
30   var $macrotabs                    = NULL;
31   var $macros                   = array();
32   var $ui                                       = NULL;
33   var $DivListMacro     = NULL;
35   var $CopyPasteHandler = NULL;
36   var $start_pasting_copied_objects = FALSE;
38   var $dns              = array();
40   var $acl_module = array("gofonmacro");
42   function gofonMacro(&$config, $ui)
43   {
44     /* Save configuration for internal use */
45     $this->config= $config;
46     $this->ui= $ui;
48     $this->DivListMacro = new divListMacro($this->config,$this);
50     /* Copy & Paste enabled ?*/
51     if ($this->config->get_cfg_value("copyPaste") == "true"){
52       $this->CopyPasteHandler = new CopyPasteHandler($this->config);
53     }
54   }
56   function execute()
57   {
58     /* Call parent execute */
59     plugin::execute();
61     session::set('LOCK_VARS_TO_USE',array("/^menu_action/","/^goFonMacro_/","/^act$/","/^id$/","/^item_selected/","/^remove_multiple_macros/"));
63     /*****************
64       Variable initialisation
65      *****************/
67     $s_action     = "";                       // Contains the action to proceed
68     $s_entry      = "";                       // The value for s_action
69     $base_back    = "";                       // The Link for Backbutton
70     $smarty= get_smarty();
73     /*****************
74       Check Posts 
75      *****************/
77     /* Test Posts */
78     foreach($_POST as $key => $val){
79       // Post for delete
80       if(preg_match("/^goFonMacro_del/",$key)){
81         $s_action = "del";
82         $s_entry  = preg_replace("/^goFonMacro_del_/i","",$key);
83         // Post for edit
84       }elseif(preg_match("/^goFonMacro_edit_/",$key)){
85         $s_action="edit";
86         $s_entry  = preg_replace("/^goFonMacro_edit_/i","",$key);
87         // Post for new
88       }elseif(preg_match("/^goFonMacro_new/",$key)){
89         $s_action="new";
90       }elseif(preg_match("/^remove_multiple_macros/",$key)){
91         $s_action="del_multiple";
92       }elseif(preg_match("/^editPaste.*/i",$key)){
93         $s_action="editPaste";
94       }elseif(preg_match("/^copy_.*/",$key)){
95         $s_action="copy";
96         $s_entry  = preg_replace("/^copy_/i","",$key);
97       }elseif(preg_match("/^cut_.*/",$key)){
98         $s_action="cut";
99         $s_entry  = preg_replace("/^cut_/i","",$key);
100       }elseif(preg_match("/^multiple_copy_objects/",$key)){
101         $s_action = "copy_multiple";
102       }elseif(preg_match("/^multiple_cut_objects/",$key)){
103         $s_action = "cut_multiple";
104       }
105     }
106     if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
107       $s_action ="edit";
108       $s_entry  = $_GET['id'];
109     }
110     $s_entry  = preg_replace("/_.$/","",$s_entry);
113     /********************
114       Copy & Paste Handling  ...
115      ********************/
117     
118     /* handle C&P from layers menu */
119     if(isset($_POST['menu_action']) && preg_match("/^multiple_copy_systems/",$_POST['menu_action'])){
120       $s_action = "copy_multiple";
121     }
122     if(isset($_POST['menu_action']) && preg_match("/^multiple_cut_systems/",$_POST['menu_action'])){
123       $s_action = "cut_multiple";
124     }
125     if(isset($_POST['menu_action']) && preg_match("/^editPaste/",$_POST['menu_action'])){
126       $s_action = "editPaste";
127     }
129     /* Create options */
130     if(isset($_POST['menu_action']) && $_POST['menu_action'] == "goFonMacro_new"){
131       $s_action = "new";
132     }
134     /* handle remove from layers menu */
135     if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
136       $s_action = "del_multiple";
137     }
139     /* Display the copy & paste dialog, if it is currently open */
140     $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
141     if($ret){
142       return($ret);
143     }
146     /*****************
147       Create a new Macro 
148      *****************/
150     /* New macro? */
151     if ($s_action=="new"){
153       /* By default we set 'dn' to 'new', all relevant plugins will
154          react on this. */
155       $this->dn= "new";
157       /* Create new usertab object */
158       $this->macrotabs= new macrotabs($this->config,$this->config->data['TABS']['MACROTABS'], $this->dn,"gofonmacro");
159       $this->macrotabs->set_acl_base($this->DivListMacro->selectedBase);
160     }
162     /*****************
163       Edit || Password canceled  
164      *****************/
166     /* Cancel dialogs */
167     if (isset($_POST['edit_cancel'])){
168       $this->remove_lock();
169       unset ($this->macrotabs);
170       $this->macrotabs= NULL;
171       session::un_set('objectinfo');
172     }
175     /*****************
176       Edit finised
177      *****************/
179     /* Finish mac edit is triggered by the tabulator dialog, so
180        the user wants to save edited data. Check and save at this
181        point. */
182     if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (isset($this->macrotabs->config))){
184       /* Check tabs, will feed message array */
185       $this->macrotabs->last= $this->macrotabs->current;
186       $this->macrotabs->save_object();
187       $message= $this->macrotabs->check();
189       /* Save, or display error message? */
190       if (count($message) == 0){
192         /* Save data data to ldap */
193         $this->macrotabs->save();
195         if (!isset($_POST['edit_apply'])){
196           /* macro has been saved successfully, remove lock from
197              LDAP. */
198           if ($this->dn != "new"){
199             $this->remove_lock();
200           }
202           unset ($this->macrotabs);
203           $this->macrotabs= NULL;
204           session::un_set('objectinfo');
205         }else{
207           /* Reinitialize tab */
208           if($this->macrotabs instanceof tabs){
209             $this->macrotabs->re_init();
210           }
211         }
212       } else {
213         /* Ok. There seem to be errors regarding to the tab data,
214            show message and continue as usual. */
215         msg_dialog::displayChecks($message);
216       }
217     }
220     /*****************
221       Edit macro 
222      *****************/
224     /* User wants to edit data */
225     if (($s_action=="edit") && (!isset($this->macrotabs->config))){
227       $dn  = $this->macros[$s_entry]['dn'];
228       $acl = $this->ui->get_permissions($dn,"gofonmacro/macro");
229       if(preg_match("/r/",$acl)){
230         $this->dn = $dn;
232         /* Check locking, save current plugin in 'back_plugin', so
233            the dialog knows where to return. */
234         if (($user= get_lock($this->dn)) != ""){
235           return(gen_locked_message ($user, $this->dn));
236         }
238         /* Lock the current entry, so everyone will get the
239            above dialog */
240         add_lock ($this->dn, $this->ui->dn);
242         /* Register macrotabs to trigger edit dialog */
243         $this->macrotabs= new macrotabs($this->config,
244             $this->config->data['TABS']['MACROTABS'], $this->dn,"gofonmacro");
245         session::set('objectinfo',$this->dn);
246       }
247     }
251     /********************
252       Delete MULTIPLE entries requested, display confirm dialog
253      ********************/
255     if ($s_action=="del_multiple"){
256       $ids = $this->list_get_selected_items();
259       $this->dns = array();
260       if(count($ids)){
261         $disallowed = array();
262         foreach($ids as $id){
263           $dn = $this->macros[$id]['dn'];
264           $acl = $this->ui->get_permissions($dn, "gofonmacro/macro");
265           if(preg_match("/d/",$acl)){
266             $this->dns[$id] = $dn;
267           }else{
268             $disallowed[] = $dn;
269           }
270         }
272         if(count($disallowed)){
273           msg_dialog::display(_("Permission"),msgPool::permDelete($disallowed),INFO_DIALOG);
274         }
276         if(count($this->dns)){
278           /* Check locks */
279           if ($user= get_multiple_locks($this->dns)){
280             return(gen_locked_message($user,$this->dns));
281           }
283           $dns_names = array();
284           foreach($this->dns as $dn){
285             $dns_names[] = @LDAP::fix($dn);
286           }
288           /* Add entry locks */
289           add_lock ($this->dns, $this->ui->dn);
291           /* Lock the current entry, so nobody will edit it during deletion */
292           $smarty->assign("intro", msgPool::deleteInfo($dns_names,("macro")));
293           $smarty->assign("multiple", true);
294           return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
295         }
296       }
297     }
300     /********************
301       Delete MULTIPLE entries confirmed
302      ********************/
304     /* Confirmation for deletion has been passed. Users should be deleted. */
305     if (isset($_POST['delete_multiple_macro_confirm'])){
307       /* Remove user by user and check acls before removeing them */
308       foreach($this->dns as $key => $dn){
310         $acl = $this->ui->get_permissions($dn,"gofonmacro/macro");
311         if(preg_match("/d/",$acl)){
313           /* Delete request is permitted, perform LDAP action */
314           $this->macrotabs= new macrotabs($this->config,$this->config->data['TABS']['MACROTABS'], $dn,"gofonmacro");
315           $this->macrotabs->delete ();
316           unset ($this->macrotabs);
317           $this->macrotabs= NULL;
319         } else {
320           msg_dialog::display(_("Permission error"),msgPool::permDelete(), ERROR_DIALOG);
321         }
323       }
325       /* Remove lock file after successfull deletion */
326       $this->remove_lock();
327       $this->dns = array();
328     }
331     /********************
332       Delete MULTIPLE entries Canceled
333      ********************/
335     /* Remove lock */
336     if(isset($_POST['delete_multiple_macro_cancel'])){
337       $this->remove_lock();
338       $this->dns = array();
339     }
342     /*****************
343       Remove macro
344      *****************/
346     /* Remove user was requested */
347     if ($s_action=="del"){
349       /* Get 'dn' from posted 'uid' */
350       $dn  = $this->macros[$s_entry]['dn'];
351       $acl = $this->ui->get_permissions($dn,"gofonmacro/macro");
353       if(preg_match("/d/",$acl)){
355         $this->dn = $dn;
356   
357         /* Check locking, save current plugin in 'back_plugin', so
358            the dialog knows where to return. */
359         if (($user= get_lock($this->dn)) != ""){
360           return (gen_locked_message ($user, $this->dn));
361         }
363         /* Lock the current entry, so nobody will edit it during deletion */
364         add_lock ($this->dn, $this->ui->dn);
365         $smarty= get_smarty();
366         $smarty->assign("intro", msgPool::deleteInfo(@LDAP::fix($this->dn),_("macro")));
367         $smarty->assign("multiple", false);
368         return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
369       } else {
371         /* Obviously the user isn't allowed to delete. Show message and
372            clean session. */
373         msg_dialog::display(_("Permission error"), msgPool::permDelete(), ERROR_DIALOG);
374       }
375     }
378     /*****************
379       Remove macro
380      *****************/
382     /* Confirmation for deletion has been passed. Macro should be deleted. */
383     if (isset($_POST['delete_macro_confirm'])){
385       $acl = $this->ui->get_permissions($this->dn,"gofonmacro/macro");
387       if(preg_match("/r/",$acl)){
389         /* Delete request is permitted, perform LDAP action */
390         $this->macrotabs= new macrotabs($this->config,$this->config->data['TABS']['MACROTABS'], $this->dn,"gofonmacro");
391         $this->macrotabs->delete ();
392         unset ($this->macrotabs);
393         $this->macrotabs= NULL;
394       } else {
395         /* Normally this shouldn't be reached, send some extra
396            logs to notify the administrator */
397         msg_dialog::display(_("Permission error"), msgPool::permDelete(), ERROR_DIALOG);
398         new log("security","gofonmacro/".get_class($this),$this->dn,array(),"Tried to trick deletion.");
399       }
401       /* Remove lock file after successfull deletion */
402       $this->remove_lock();
403     }
406     /*****************
407       Display open dialogs  
408      *****************/
410     /* Show tab dialog if object is present */
411     if (($this->macrotabs) && (isset($this->macrotabs->config))){
412       $display= $this->macrotabs->execute();
414       /* Don't show buttons if tab dialog requests this */
415       if (!$this->macrotabs->by_object[$this->macrotabs->current]->dialog){
416         $display.= "<p style=\"text-align:right\">\n";
417         $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
418         $display.= "&nbsp;\n";
419         if ($this->dn != "new"){
420           $display.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
421           $display.= "&nbsp;\n";
422         }
423         $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
424         $display.= "</p>";
425       }
426       return ($display);
427     }
430     /*****************
431       Display entry list 
432      *****************/
434     /* Check if there is a snapshot dialog open */
435     $base = $this->DivListMacro->selectedBase;
436     if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases(),$this)){
437       return($str);
438     }
440     /* Return rendered main page */
441     /* Display dialog with system list */
442     $this->DivListMacro->parent = $this;
443     $this->DivListMacro->execute();
445     /* Add departments if subsearch is disabled */
446     if(!$this->DivListMacro->SubSearch){
447       $this->DivListMacro->AddDepartments($this->DivListMacro->selectedBase,4,1);
448     }
449     $this->reload();
450     $this->DivListMacro->setEntries($this->macros);
451     return($this->DivListMacro->Draw());
452   }
455   function copyPasteHandling_from_queue($s_action,$s_entry)
456   {
457     /* Check if Copy & Paste is disabled */
458     if(!is_object($this->CopyPasteHandler)){
459       return("");
460     }
462     $ui = get_userinfo();
464     /* Add a single entry to queue */
465     if($s_action == "cut" || $s_action == "copy"){
467       /* Cleanup object queue */
468       $this->CopyPasteHandler->cleanup_queue();
469       $dn = $this->macros[$s_entry]['dn'];
471       if($s_action == "copy" && $ui->is_copyable($dn,"gofonmacro","macro")){ 
472         $this->CopyPasteHandler->add_to_queue($dn,$s_action,"macrotabs","MACROTABS","gofonmacro");
473       }
474       if($s_action == "cut" && $ui->is_cutable($dn,"gofonmacro","macro")){
475         $this->CopyPasteHandler->add_to_queue($dn,$s_action,"macrotabs","MACROTABS","gofonmacro");
476       }
477     }
479     /* Add entries to queue */
480     if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
482       /* Cleanup object queue */
483       $this->CopyPasteHandler->cleanup_queue();
485       /* Add new entries to CP queue */
486       foreach($this->list_get_selected_items() as $id){
487         $dn = $this->macros[$id]['dn'];
489         if($s_action == "copy_multiple" && $ui->is_copyable($dn,"gofonmacro","macro")){ 
490           $this->CopyPasteHandler->add_to_queue($dn,"copy","macrotabs","MACROTABS","gofonmacro");
491         }
492         if($s_action == "cut_multiple" && $ui->is_cutable($dn,"gofonmacro","macro")){
493           $this->CopyPasteHandler->add_to_queue($dn,"cut","macrotabs","MACROTABS","gofonmacro");
494         }
495       }
496     }
498     /* Start pasting entries */
499     if($s_action == "editPaste"){
500       $this->start_pasting_copied_objects = TRUE;
501     }
503     /* Return C&P dialog */
504     if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
506       /* Get dialog */
507       $this->CopyPasteHandler->SetVar("base",$this->DivListMacro->selectedBase);
508       $data = $this->CopyPasteHandler->execute();
510       /* Return dialog data */
511       if(!empty($data)){
512         return($data);
513       }
514     }
516     /* Automatically disable status for pasting */
517     if(!$this->CopyPasteHandler->entries_queued()){
518       $this->start_pasting_copied_objects = FALSE;
519     }
520     return("");
521   }
525   /* Return departments, that will be included within snapshot detection */
526   function get_used_snapshot_bases()
527   {
528     return(array(get_ou('phoneMacroRDN').$this->DivListMacro->selectedBase));
529   }
532   function reload()
533   {
534     /* Set base for all searches */
535     $base       = $this->DivListMacro->selectedBase;
536     $SubSearch  = $this->DivListMacro->SubSearch;
537     $Regex      = $this->DivListMacro->Regex;
538     $Filter     = "(&(cn=".$Regex.")(objectClass=goFonMacro))";
539     $Flags      = GL_SIZELIMIT;
540     $Attrs      = array("cn","description","displayName","goFonMacroVisible");
542     /* Prepare for ls or search*/        
543     if($SubSearch){
544       $Flags |= GL_SUBSEARCH;
545     }else{
546       $base = get_ou('phoneMacroRDN').$base;
547     }
549     /* Generate macro list */
550     $res= get_list($Filter, "gofonmacro", $base, $Attrs, $Flags);
552     $tmp=array();
553     foreach($res as $tkey => $val ){
554       $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
555     }
556     ksort($tmp);
557     $this->macros=array();
558     foreach($tmp as $val){
559       $this->macros[]=$val;
560     }
562     reset ($this->macros);
563   }
566   /* Save data to object */
567   function save_object()
568   {
569     $this->DivListMacro->save_object();
570     if(is_object($this->CopyPasteHandler)){
571       $this->CopyPasteHandler->save_object();
572     }
573   }
576   function list_get_selected_items()
577   {
578     $ids = array();
579     foreach($_POST as $name => $value){
580       if(preg_match("/^item_selected_[0-9]*$/",$name)){
581         $id   = preg_replace("/^item_selected_/","",$name);
582         $ids[$id] = $id;
583       }
584     }
585     return($ids);
586   }
588   /*! \brief Remove entry locks if the plugin was aborted. 
589    */
590   function remove_lock()
591   {
592     if($this->dn) del_lock($this->dn);
593     if(is_array($this->dns) && count($this->dns)) del_lock($this->dns);
594   }
596   function adapt_from_template($dn, $skip= array())  { }
597   function check() { }
599 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
600 ?>