Code

Added a first set of reference changes - nearly untested
[gosa.git] / plugins / 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  */
20 require "tabs_macros.inc";
22 class gofonMacro extends plugin
23 {
24   /* Definitions */
25   var $plHeadline                   = "Phone macros";
26   var $plDescription      = "This does something";
28   /* Dialog attributes */
29   var $macrotabs                    = NULL;
30   var $macros                   = array();
31   var $ui                                       = NULL;
32   var $DivListMacro     = NULL;
34   var $CopyPasteHandler = NULL;
35   var $start_pasting_copied_objects = FALSE;
37   function gofonMacro(&$config, $ui)
38   {
39     /* Save configuration for internal use */
40     $this->config= $config;
41     $this->ui= $ui;
43     $this->DivListMacro = new divListMacro($this->config,$this);
45     /* Copy & Paste enabled ?*/
46     if((isset($this->config->data['MAIN']['ENABLECOPYPASTE']))&&(preg_match("/true/i",$this->config->data['MAIN']['ENABLECOPYPASTE']))){
47       $this->CopyPasteHandler = new CopyPasteHandler($this->config);
48     }
49   }
51   function execute()
52   {
53     /* Call parent execute */
54     plugin::execute();
56     $_SESSION['LOCK_VARS_TO_USE'] = array("/^goFonMacro_/","/^act$/","/^id$/","/^item_selected/","/^remove_multiple_macros/");
58     /*****************
59       Variable initialisation
60      *****************/
62     $s_action     = "";                       // Contains the action to proceed
63     $s_entry      = "";                       // The value for s_action
64     $base_back    = "";                       // The Link for Backbutton
65     $smarty= get_smarty();
68     /*****************
69       Check Posts 
70      *****************/
72     /* Test Posts */
73     foreach($_POST as $key => $val){
74       // Post for delete
75       if(preg_match("/^goFonMacro_del/",$key)){
76         $s_action = "del";
77         $s_entry  = preg_replace("/^goFonMacro_del_/i","",$key);
78         // Post for edit
79       }elseif(preg_match("/^goFonMacro_edit_/",$key)){
80         $s_action="edit";
81         $s_entry  = preg_replace("/^goFonMacro_edit_/i","",$key);
82         // Post for new
83       }elseif(preg_match("/^goFonMacro_new/",$key)){
84         $s_action="new";
85       }elseif(preg_match("/^remove_multiple_macros/",$key)){
86         $s_action="del_multiple";
87       }elseif(preg_match("/^editPaste.*/i",$key)){
88         $s_action="editPaste";
89       }elseif(preg_match("/^copy_.*/",$key)){
90         $s_action="copy";
91         $s_entry  = preg_replace("/^copy_/i","",$key);
92       }elseif(preg_match("/^cut_.*/",$key)){
93         $s_action="cut";
94         $s_entry  = preg_replace("/^cut_/i","",$key);
95       }elseif(preg_match("/^multiple_copy_objects/",$key)){
96         $s_action = "copy_multiple";
97       }elseif(preg_match("/^multiple_cut_objects/",$key)){
98         $s_action = "cut_multiple";
99       }
100     }
101     if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
102       $s_action ="edit";
103       $s_entry  = $_GET['id'];
104     }
105     $s_entry  = preg_replace("/_.$/","",$s_entry);
108     /********************
109       Copy & Paste Handling  ...
110      ********************/
112     /* Display the copy & paste dialog, if it is currently open */
113     $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
114     if($ret){
115       return($ret);
116     }
119     /*****************
120       Create a new Macro 
121      *****************/
123     /* New macro? */
124     if ($s_action=="new"){
126       /* By default we set 'dn' to 'new', all relevant plugins will
127          react on this. */
128       $this->dn= "new";
130       /* Create new usertab object */
131       $this->macrotabs= new macrotabs($this->config,$this->config->data['TABS']['MACROTABS'], $this->dn,"gofonmacro");
132       $this->macrotabs->set_acl_base($this->DivListMacro->selectedBase);
133     }
135     /*****************
136       Edit || Password canceled  
137      *****************/
139     /* Cancel dialogs */
140     if (isset($_POST['edit_cancel'])){
141       del_lock ($this->macrotabs->dn);
142       unset ($this->macrotabs);
143       $this->macrotabs= NULL;
144       unset ($_SESSION['objectinfo']);
145     }
148     /*****************
149       Edit finised
150      *****************/
152     /* Finish mac edit is triggered by the tabulator dialog, so
153        the user wants to save edited data. Check and save at this
154        point. */
155     if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (isset($this->macrotabs->config))){
157       /* Check tabs, will feed message array */
158       $this->macrotabs->last= $this->macrotabs->current;
159       $this->macrotabs->save_object();
160       $message= $this->macrotabs->check();
162       /* Save, or display error message? */
163       if (count($message) == 0){
165         /* Save data data to ldap */
166         $this->macrotabs->save();
168         if (!isset($_POST['edit_apply'])){
169           /* macro has been saved successfully, remove lock from
170              LDAP. */
171           if ($this->dn != "new"){
172             del_lock ($this->dn);
173           }
175           unset ($this->macrotabs);
176           $this->macrotabs= NULL;
177           unset ($_SESSION['objectinfo']);
178         }
179       } else {
180         /* Ok. There seem to be errors regarding to the tab data,
181            show message and continue as usual. */
182         show_errors($message);
183       }
184     }
187     /*****************
188       Edit macro 
189      *****************/
191     /* User wants to edit data */
192     if (($s_action=="edit") && (!isset($this->macrotabs->config))){
194       $dn  = $this->macros[$s_entry]['dn'];
195       $acl = $this->ui->get_permissions($dn,"gofonmacro/macro");
196       if(preg_match("/r/",$acl)){
197         $this->dn = $dn;
199         /* Check locking, save current plugin in 'back_plugin', so
200            the dialog knows where to return. */
201         if (($user= get_lock($this->dn)) != ""){
202           return(gen_locked_message ($user, $this->dn));
203         }
205         /* Lock the current entry, so everyone will get the
206            above dialog */
207         add_lock ($this->dn, $this->ui->dn);
208         /* Register macrotabs to trigger edit dialog */
209         $this->macrotabs= new macrotabs($this->config,
210             $this->config->data['TABS']['MACROTABS'], $this->dn,"gofonmacro");
211         $_SESSION['objectinfo']= $this->dn;
212       }
213     }
217     /********************
218       Delete MULTIPLE entries requested, display confirm dialog
219      ********************/
221     if ($s_action=="del_multiple"){
222       $ids = $this->list_get_selected_items();
224       if(count($ids)){
226         foreach($ids as $id){
227           $dn = $this->macros[$id]['dn'];
228           if (($user= get_lock($dn)) != ""){
229             return(gen_locked_message ($user, $dn));
230           }
231           $this->dns[$id] = $dn;
232         }
234         $dns_names = "<br><pre>";
235         foreach($this->dns as $dn){
236           add_lock ($dn, $this->ui->dn);
237           $dns_names .= $dn."\n";
238         }
239         $dns_names .="</pre>";
241         /* Lock the current entry, so nobody will edit it during deletion */
242         $smarty->assign("intro",     sprintf(_("You're about to delete the following user(s) %s"), @LDAP::fix($dns_names)));
243         $smarty->assign("multiple", true);
244         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
245       }
246     }
249     /********************
250       Delete MULTIPLE entries confirmed
251      ********************/
253     /* Confirmation for deletion has been passed. Users should be deleted. */
254     if (isset($_POST['delete_multiple_macro_confirm'])){
256       /* Remove user by user and check acls before removeing them */
257       foreach($this->dns as $key => $dn){
259         $acl = $this->ui->get_permissions($dn,"gofonmacro/macro");
260         if(preg_match("/d/",$acl)){
262           /* Delete request is permitted, perform LDAP action */
263           $this->macrotabs= new macrotabs($this->config,$this->config->data['TABS']['MACROTABS'], $dn,"gofonmacro");
264           $this->macrotabs->delete ();
265           unset ($this->macrotabs);
266           $this->macrotabs= NULL;
268         } else {
269           print_red (_("You are not allowed to delete this macro!"));
270         }
272         /* Remove lock file after successfull deletion */
273         del_lock ($dn);
274         unset($this->dns[$key]);
275       }
276     }
279     /********************
280       Delete MULTIPLE entries Canceled
281      ********************/
283     /* Remove lock */
284     if(isset($_POST['delete_multiple_macro_cancel'])){
285       foreach($this->dns as $key => $dn){
286         del_lock ($dn);
287         unset($this->dns[$key]);
288       }
289     }
292     /*****************
293       Remove macro
294      *****************/
296     /* Remove user was requested */
297     if ($s_action=="del"){
299       /* Get 'dn' from posted 'uid' */
300       $dn  = $this->macros[$s_entry]['dn'];
301       $acl = $this->ui->get_permissions($dn,"gofonmacro/macro");
303       if(preg_match("/d/",$acl)){
305         $this->dn = $dn;
306   
307         /* Check locking, save current plugin in 'back_plugin', so
308            the dialog knows where to return. */
309         if (($user= get_lock($this->dn)) != ""){
310           return (gen_locked_message ($user, $this->dn));
311         }
313         /* Lock the current entry, so nobody will edit it during deletion */
314         add_lock ($this->dn, $this->ui->dn);
315         $smarty= get_smarty();
316         $smarty->assign("intro", sprintf(_("You're about to delete the macro '%s'."), $this->dn));
317         $smarty->assign("multiple", false);
318         return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
319       } else {
321         /* Obviously the user isn't allowed to delete. Show message and
322            clean session. */
323         print_red (_("You are not allowed to delete this macro!"));
324       }
325     }
328     /*****************
329       Remove macro
330      *****************/
332     /* Confirmation for deletion has been passed. Macro should be deleted. */
333     if (isset($_POST['delete_macro_confirm'])){
335       $acl = $this->ui->get_permissions($this->dn,"gofonmacro/macro");
337       if(preg_match("/r/",$acl)){
339         /* Delete request is permitted, perform LDAP action */
340         $this->macrotabs= new macrotabs($this->config,$this->config->data['TABS']['MACROTABS'], $this->dn,"gofonmacro");
341         $this->macrotabs->delete ();
342         unset ($this->macrotabs);
343         $this->macrotabs= NULL;
344       } else {
345         /* Normally this shouldn't be reached, send some extra
346            logs to notify the administrator */
347         print_red (_("You are not allowed to delete this macro!"));
348         new log("security","gofonmacro/".get_class($this),$this->dn,array(),"Tried to trick deletion.");
349       }
350       /* Remove lock file after successfull deletion */
351       del_lock ($this->dn);
352     }
355     /*****************
356       Display open dialogs  
357      *****************/
359     /* Show tab dialog if object is present */
360     if (($this->macrotabs) && (isset($this->macrotabs->config))){
361       $display= $this->macrotabs->execute();
363       /* Don't show buttons if tab dialog requests this */
364       if (!$this->macrotabs->by_object[$this->macrotabs->current]->dialog){
365         $display.= "<p style=\"text-align:right\">\n";
366         $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
367         $display.= "&nbsp;\n";
368         if ($this->dn != "new"){
369           $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
370           $display.= "&nbsp;\n";
371         }
372         $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
373         $display.= "</p>";
374       }
375       return ($display);
376     }
379     /*****************
380       Display entry list 
381      *****************/
383     /* Check if there is a snapshot dialog open */
384     $base = $this->DivListMacro->selectedBase;
385     if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
386       return($str);
387     }
389     /* Return rendered main page */
390     /* Display dialog with system list */
391     $this->DivListMacro->parent = $this;
392     $this->DivListMacro->execute();
394     /* Add departments if subsearch is disabled */
395     if(!$this->DivListMacro->SubSearch){
396       $this->DivListMacro->AddDepartments($this->DivListMacro->selectedBase,4,1);
397     }
398     $this->reload();
399     $this->DivListMacro->setEntries($this->macros);
400     return($this->DivListMacro->Draw());
401   }
404   function copyPasteHandling_from_queue($s_action,$s_entry)
405   {
406     /* Check if Copy & Paste is disabled */
407     if(!is_object($this->CopyPasteHandler)){
408       return("");
409     }
411     /* Add a single entry to queue */
412     if($s_action == "cut" || $s_action == "copy"){
414       /* Cleanup object queue */
415       $this->CopyPasteHandler->cleanup_queue();
416       $dn = $this->macros[$s_entry]['dn'];
417       $this->CopyPasteHandler->add_to_queue($dn,$s_action,"macrotabs","MACROTABS","gofonmacro");
418     }
420     /* Add entries to queue */
421     if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
423       /* Cleanup object queue */
424       $this->CopyPasteHandler->cleanup_queue();
426       /* Add new entries to CP queue */
427       foreach($this->list_get_selected_items() as $id){
428         $dn = $this->macros[$id]['dn'];
430         if($s_action == "copy_multiple"){
431           $this->CopyPasteHandler->add_to_queue($dn,"copy","macrotabs","MACROTABS","gofonmacro");
432         }
433         if($s_action == "cut_multiple"){
434           $this->CopyPasteHandler->add_to_queue($dn,"cut","macrotabs","MACROTABS","gofonmacro");
435         }
436       }
437     }
439     /* Start pasting entries */
440     if($s_action == "editPaste"){
441       $this->start_pasting_copied_objects = TRUE;
442     }
444     /* Return C&P dialog */
445     if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
447       /* Load entry from queue and set base */
448       $this->CopyPasteHandler->load_entry_from_queue();
449       $this->CopyPasteHandler->SetVar("base",$this->DivListMacro->selectedBase);
451       /* Get dialog */
452       $data = $this->CopyPasteHandler->execute();
454       /* Return dialog data */
455       if(!empty($data)){
456         return($data);
457       }
458     }
460     /* Automatically disable status for pasting */
461     if(!$this->CopyPasteHandler->entries_queued()){
462       $this->start_pasting_copied_objects = FALSE;
463     }
464     return("");
465   }
469   /* Return departments, that will be included within snapshot detection */
470   function get_used_snapshot_bases()
471   {
472     return(array("ou=macros,ou=asterisk,ou=configs,ou=systems,".$this->DivListMacro->selectedBase));
473   }
476   function reload()
477   {
478     /* Set base for all searches */
479     $base       = $this->DivListMacro->selectedBase;
480     $SubSearch  = $this->DivListMacro->SubSearch;
481     $Regex      = $this->DivListMacro->Regex;
482     $Filter     = "(&(cn=".$Regex.")(objectClass=goFonMacro))";
483     $Flags      = GL_SIZELIMIT;
484     $Attrs      = array("cn","description","displayName","goFonMacroVisible");
486     /* Prepare for ls or search*/        
487     if($SubSearch){
488       $Flags |= GL_SUBSEARCH;
489     }else{
490       $base = "ou=macros,ou=asterisk,ou=configs,ou=systems,".$base;
491     }
493     /* Generate macro list */
494     $res= get_list($Filter, "gofonmacro", $base, $Attrs, $Flags);
496     $tmp=array();
497     foreach($res as $tkey => $val ){
498       $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
499     }
500     ksort($tmp);
501     $this->macros=array();
502     foreach($tmp as $val){
503       $this->macros[]=$val;
504     }
506     reset ($this->macros);
507   }
510   /* Save data to object */
511   function save_object()
512   {
513     $this->DivListMacro->save_object();
514   }
517   function list_get_selected_items()
518   {
519     $ids = array();
520     foreach($_POST as $name => $value){
521       if(preg_match("/^item_selected_[0-9]*$/",$name)){
522         $id   = preg_replace("/^item_selected_/","",$name);
523         $ids[$id] = $id;
524       }
525     }
526     return($ids);
527   }
530   function adapt_from_template($dn)  { }
531   function check() { }
533 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
534 ?>