Code

msgPool
[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         }
200       } else {
201         /* Ok. There seem to be errors regarding to the tab data,
202            show message and continue as usual. */
203         msg_dialog::displayChecks($message);
204       }
205     }
208     /*****************
209       Edit macro 
210      *****************/
212     /* User wants to edit data */
213     if (($s_action=="edit") && (!isset($this->macrotabs->config))){
215       $dn  = $this->macros[$s_entry]['dn'];
216       $acl = $this->ui->get_permissions($dn,"gofonmacro/macro");
217       if(preg_match("/r/",$acl)){
218         $this->dn = $dn;
220         /* Check locking, save current plugin in 'back_plugin', so
221            the dialog knows where to return. */
222         if (($user= get_lock($this->dn)) != ""){
223           return(gen_locked_message ($user, $this->dn));
224         }
226         /* Lock the current entry, so everyone will get the
227            above dialog */
228         add_lock ($this->dn, $this->ui->dn);
229         /* Register macrotabs to trigger edit dialog */
230         $this->macrotabs= new macrotabs($this->config,
231             $this->config->data['TABS']['MACROTABS'], $this->dn,"gofonmacro");
232         session::set('objectinfo',$this->dn);
233       }
234     }
238     /********************
239       Delete MULTIPLE entries requested, display confirm dialog
240      ********************/
242     if ($s_action=="del_multiple"){
243       $ids = $this->list_get_selected_items();
245       if(count($ids)){
247         foreach($ids as $id){
248           $dn = $this->macros[$id]['dn'];
249           if (($user= get_lock($dn)) != ""){
250             return(gen_locked_message ($user, $dn));
251           }
252           $this->dns[$id] = $dn;
253         }
255         $dns_names = array();
256         foreach($this->dns as $dn){
257           add_lock ($dn, $this->ui->dn);
258           $dns_names[] = @LDAP::fix($dn);
259         }
261         /* Lock the current entry, so nobody will edit it during deletion */
262         $smarty->assign("intro", msgPool::deleteInfo($dns_names,("macro")));
263         $smarty->assign("multiple", true);
264         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
265       }
266     }
269     /********************
270       Delete MULTIPLE entries confirmed
271      ********************/
273     /* Confirmation for deletion has been passed. Users should be deleted. */
274     if (isset($_POST['delete_multiple_macro_confirm'])){
276       /* Remove user by user and check acls before removeing them */
277       foreach($this->dns as $key => $dn){
279         $acl = $this->ui->get_permissions($dn,"gofonmacro/macro");
280         if(preg_match("/d/",$acl)){
282           /* Delete request is permitted, perform LDAP action */
283           $this->macrotabs= new macrotabs($this->config,$this->config->data['TABS']['MACROTABS'], $dn,"gofonmacro");
284           $this->macrotabs->delete ();
285           unset ($this->macrotabs);
286           $this->macrotabs= NULL;
288         } else {
289           msg_dialog::display(_("Permission error"), _("You have not permission to delete this entry!"), ERROR_DIALOG);
290         }
292         /* Remove lock file after successfull deletion */
293         del_lock ($dn);
294         unset($this->dns[$key]);
295       }
296     }
299     /********************
300       Delete MULTIPLE entries Canceled
301      ********************/
303     /* Remove lock */
304     if(isset($_POST['delete_multiple_macro_cancel'])){
305       foreach($this->dns as $key => $dn){
306         del_lock ($dn);
307         unset($this->dns[$key]);
308       }
309     }
312     /*****************
313       Remove macro
314      *****************/
316     /* Remove user was requested */
317     if ($s_action=="del"){
319       /* Get 'dn' from posted 'uid' */
320       $dn  = $this->macros[$s_entry]['dn'];
321       $acl = $this->ui->get_permissions($dn,"gofonmacro/macro");
323       if(preg_match("/d/",$acl)){
325         $this->dn = $dn;
326   
327         /* Check locking, save current plugin in 'back_plugin', so
328            the dialog knows where to return. */
329         if (($user= get_lock($this->dn)) != ""){
330           return (gen_locked_message ($user, $this->dn));
331         }
333         /* Lock the current entry, so nobody will edit it during deletion */
334         add_lock ($this->dn, $this->ui->dn);
335         $smarty= get_smarty();
336         $smarty->assign("intro", msgPool::deleteInfo(@LDAP::fix($this->dn),_("macro")));
337         $smarty->assign("multiple", false);
338         return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
339       } else {
341         /* Obviously the user isn't allowed to delete. Show message and
342            clean session. */
343         msg_dialog::display(_("Permission error"), _("You have not permission to delete this entry!"), ERROR_DIALOG);
344       }
345     }
348     /*****************
349       Remove macro
350      *****************/
352     /* Confirmation for deletion has been passed. Macro should be deleted. */
353     if (isset($_POST['delete_macro_confirm'])){
355       $acl = $this->ui->get_permissions($this->dn,"gofonmacro/macro");
357       if(preg_match("/r/",$acl)){
359         /* Delete request is permitted, perform LDAP action */
360         $this->macrotabs= new macrotabs($this->config,$this->config->data['TABS']['MACROTABS'], $this->dn,"gofonmacro");
361         $this->macrotabs->delete ();
362         unset ($this->macrotabs);
363         $this->macrotabs= NULL;
364       } else {
365         /* Normally this shouldn't be reached, send some extra
366            logs to notify the administrator */
367         msg_dialog::display(_("Permission error"), _("You have not permission to delete this entry!"), ERROR_DIALOG);
368         new log("security","gofonmacro/".get_class($this),$this->dn,array(),"Tried to trick deletion.");
369       }
370       /* Remove lock file after successfull deletion */
371       del_lock ($this->dn);
372     }
375     /*****************
376       Display open dialogs  
377      *****************/
379     /* Show tab dialog if object is present */
380     if (($this->macrotabs) && (isset($this->macrotabs->config))){
381       $display= $this->macrotabs->execute();
383       /* Don't show buttons if tab dialog requests this */
384       if (!$this->macrotabs->by_object[$this->macrotabs->current]->dialog){
385         $display.= "<p style=\"text-align:right\">\n";
386         $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
387         $display.= "&nbsp;\n";
388         if ($this->dn != "new"){
389           $display.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
390           $display.= "&nbsp;\n";
391         }
392         $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
393         $display.= "</p>";
394       }
395       return ($display);
396     }
399     /*****************
400       Display entry list 
401      *****************/
403     /* Check if there is a snapshot dialog open */
404     $base = $this->DivListMacro->selectedBase;
405     if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
406       return($str);
407     }
409     /* Return rendered main page */
410     /* Display dialog with system list */
411     $this->DivListMacro->parent = $this;
412     $this->DivListMacro->execute();
414     /* Add departments if subsearch is disabled */
415     if(!$this->DivListMacro->SubSearch){
416       $this->DivListMacro->AddDepartments($this->DivListMacro->selectedBase,4,1);
417     }
418     $this->reload();
419     $this->DivListMacro->setEntries($this->macros);
420     return($this->DivListMacro->Draw());
421   }
424   function copyPasteHandling_from_queue($s_action,$s_entry)
425   {
426     /* Check if Copy & Paste is disabled */
427     if(!is_object($this->CopyPasteHandler)){
428       return("");
429     }
431     /* Add a single entry to queue */
432     if($s_action == "cut" || $s_action == "copy"){
434       /* Cleanup object queue */
435       $this->CopyPasteHandler->cleanup_queue();
436       $dn = $this->macros[$s_entry]['dn'];
437       $this->CopyPasteHandler->add_to_queue($dn,$s_action,"macrotabs","MACROTABS","gofonmacro");
438     }
440     /* Add entries to queue */
441     if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
443       /* Cleanup object queue */
444       $this->CopyPasteHandler->cleanup_queue();
446       /* Add new entries to CP queue */
447       foreach($this->list_get_selected_items() as $id){
448         $dn = $this->macros[$id]['dn'];
450         if($s_action == "copy_multiple"){
451           $this->CopyPasteHandler->add_to_queue($dn,"copy","macrotabs","MACROTABS","gofonmacro");
452         }
453         if($s_action == "cut_multiple"){
454           $this->CopyPasteHandler->add_to_queue($dn,"cut","macrotabs","MACROTABS","gofonmacro");
455         }
456       }
457     }
459     /* Start pasting entries */
460     if($s_action == "editPaste"){
461       $this->start_pasting_copied_objects = TRUE;
462     }
464     /* Return C&P dialog */
465     if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
467       /* Get dialog */
468       $data = $this->CopyPasteHandler->execute();
469       $this->CopyPasteHandler->SetVar("base",$this->DivListMacro->selectedBase);
471       /* Return dialog data */
472       if(!empty($data)){
473         return($data);
474       }
475     }
477     /* Automatically disable status for pasting */
478     if(!$this->CopyPasteHandler->entries_queued()){
479       $this->start_pasting_copied_objects = FALSE;
480     }
481     return("");
482   }
486   /* Return departments, that will be included within snapshot detection */
487   function get_used_snapshot_bases()
488   {
489     return(array(get_ou('macroou').$this->DivListMacro->selectedBase));
490   }
493   function reload()
494   {
495     /* Set base for all searches */
496     $base       = $this->DivListMacro->selectedBase;
497     $SubSearch  = $this->DivListMacro->SubSearch;
498     $Regex      = $this->DivListMacro->Regex;
499     $Filter     = "(&(cn=".$Regex.")(objectClass=goFonMacro))";
500     $Flags      = GL_SIZELIMIT;
501     $Attrs      = array("cn","description","displayName","goFonMacroVisible");
503     /* Prepare for ls or search*/        
504     if($SubSearch){
505       $Flags |= GL_SUBSEARCH;
506     }else{
507       $base = get_ou('macroou').$base;
508     }
510     /* Generate macro list */
511     $res= get_list($Filter, "gofonmacro", $base, $Attrs, $Flags);
513     $tmp=array();
514     foreach($res as $tkey => $val ){
515       $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
516     }
517     ksort($tmp);
518     $this->macros=array();
519     foreach($tmp as $val){
520       $this->macros[]=$val;
521     }
523     reset ($this->macros);
524   }
527   /* Save data to object */
528   function save_object()
529   {
530     $this->DivListMacro->save_object();
531   }
534   function list_get_selected_items()
535   {
536     $ids = array();
537     foreach($_POST as $name => $value){
538       if(preg_match("/^item_selected_[0-9]*$/",$name)){
539         $id   = preg_replace("/^item_selected_/","",$name);
540         $ids[$id] = $id;
541       }
542     }
543     return($ids);
544   }
547   function adapt_from_template($dn)  { }
548   function check() { }
550 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
551 ?>