Code

Macrolist fixed
[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 $acl                              = "";
34   function gofonMacro($config, $ui)
35   {
36     /* Save configuration for internal use */
37     $this->config= $config;
38     $this->ui= $ui;
40     /* Get global filter config */
41     if (!is_global("macrofilter")){
42       $base= get_base_from_people($ui->dn);
43       $macrofilter= array("depselect" => $base,  "regex" => "*");
44       register_global("macrofilter", $macrofilter);
45     }
46   }
48   function execute()
49   {
50     /* Save data */
51     $macrofilter= get_global("macrofilter");
52     foreach( array("depselect", "regex") as $type){
53       if (isset($_POST[$type])){
54         $macrofilter[$type]= $_POST[$type];
55       }
56     }
57     if (isset($_GET['search'])){
58       $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
59       if ($s == "**"){
60         $s= "*";
61       }
62       $macrofilter['regex']= $s;
63     }
65     $s_action     = "";                       // Contains the action to proceed
66     $s_entry      = "";                       // The value for s_action
67     $base_back    = "";                       // The Link for Backbutton
69     /* Start for New List Managment */
70     if(isset($_GET['act'])&&($_GET['act']=="dep_open")){
71             $s_action="open";
72             $s_entry = base64_decode($_GET['dep_id']);
73             $macrofilter['depselect']= "".$this->config->departments[trim($s_entry)];
74     }
76     /* Test Posts */
77     foreach($_POST as $key => $val){
78       // Post for delete
79       if(preg_match("/user_del.*/",$key)){
80         $s_action = "del";
81         $s_entry  = preg_replace("/user_".$s_action."_/i","",$key);
82         // Post for edit
83       }elseif(preg_match("/user_edit_.*/",$key)){
84         $s_action="edit";
85         $s_entry  = preg_replace("/user_".$s_action."_/i","",$key);
86         // Post for new
87       }elseif(preg_match("/dep_back.*/i",$key)){
88         $s_action="back";
89       }elseif(preg_match("/user_new.*/",$key)){
90         $s_action="new";
91       }elseif(preg_match("/dep_home.*/i",$key)){
92         $s_action="home";
93       }elseif(preg_match("/user_chgpw.*/i",$key)){
94         $s_action="change_pw";
95         $s_entry  = preg_replace("/user_chgpw_/i","",$key);
96       }elseif(preg_match("/dep_root.*/i",$key)){
97         $s_action="root";
98       }
99     }
100   if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
101       $s_action ="edit";
102       $s_entry  = $_GET['id'];
103     }
105     $s_entry  = preg_replace("/_.$/","",$s_entry);
107     /* Department changed? */
108     if(isset($_POST['depselect']) && $_POST['depselect']){
109       $macrofilter['depselect']= $_POST['depselect'];
110     }
112     /* Homebutton is posted */
113     if($s_action=="home"){
114       $macrofilter['depselect']=(preg_replace("/^[^,]+,/","",$this->ui->dn));
115       $macrofilter['depselect']=(preg_replace("/^[^,]+,/","",$macrofilter['depselect']));
116     }
118     if($s_action=="root"){
119       $macrofilter['depselect']=($this->config->current['BASE']);
120     }
123     /* If Backbutton is Posted */
124     if($s_action=="back"){
125       $base_back          = preg_replace("/^[^,]+,/","",$macrofilter['depselect']);
126       $base_back          = convert_department_dn($base_back);
128       if(isset($this->config->departments[trim($base_back)])){
129         $macrofilter['depselect']= $this->config->departments[trim($base_back)];
130       }else{
131         $macrofilter['depselect']= $this->config->departments["/"];
132       }
133     }
135     register_global("macrofilter", $macrofilter);
136     $this->reload();
137     
138     $smarty= get_smarty();
140     /* Check for exeeded sizelimit */
141     if (($message= check_sizelimit()) != ""){
142       return($message);
143     }
145     /* New macro? */
146     if ($s_action=="new"){
148       /* By default we set 'dn' to 'new', all relevant plugins will
149          react on this. */
150       $this->dn= "new";
152       /* Create new usertab object */
153       $this->macrotabs= new macrotabs($this->config,$this->config->data['TABS']['MACROTABS'], $this->dn);
154       $this->macrotabs->set_acl(array(':all'));
155     }
157     /* Cancel dialogs */
158     if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
159       del_lock ($this->macrotabs->dn);
160       unset ($this->macrotabs);
161       $this->macrotabs= NULL;
162       unset ($_SESSION['objectinfo']);
163     }
165     /* Finish mac edit is triggered by the tabulator dialog, so
166        the user wants to save edited data. Check and save at this
167        point. */
168     if (isset($_POST['edit_finish'])){
170       /* Check tabs, will feed message array */
171       $this->macrotabs->last= $this->macrotabs->current;
172       $this->macrotabs->save_object();
173       $message= $this->macrotabs->check();
175       /* Save, or display error message? */
176       if (count($message) == 0){
178         /* Save data data to ldap */
179         $this->macrotabs->save();
180         gosa_log ("Macro object'".$this->dn."' has been saved");
182         /* macro has been saved successfully, remove lock from
183            LDAP. */
184         if ($this->dn != "new"){
185           del_lock ($this->dn);
186         }
188         /* There's no page reload so we have to read new mac at
189            this point. */
190         $this->reload ();
191         unset ($this->macrotabs);
192         $this->macrotabs= NULL;
193         unset ($_SESSION['objectinfo']);
194       } else {
195         /* Ok. There seem to be errors regarding to the tab data,
196            show message and continue as usual. */
197         show_errors($message);
198       }
199     }
201     /* User wants to edit data? */
202     if ($s_action=="edit"){
204       /* Get 'dn' from posted 'macrolists', must be unique */
205       $this->dn= $this->macros[$s_entry]['dn']; 
207       /* Check locking, save current plugin in 'back_plugin', so
208          the dialog knows where to return. */
209       if (($user= get_lock($this->dn)) != ""){
210         return(gen_locked_message ($user, $this->dn));
211       }
213       /* Lock the current entry, so everyone will get the
214          above dialog */
215       add_lock ($this->dn, $this->ui->dn);
217       /* Set up the users ACL's for this 'dn' */
218       $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
220       /* Register macrotabs to trigger edit dialog */
221       $this->macrotabs= new macrotabs($this->config,
222           $this->config->data['TABS']['MACROTABS'], $this->dn);
223       $this->macrotabs->set_acl($acl);
224       $_SESSION['objectinfo']= $this->dn;
225     }
227     /* Remove user was requested */
228     if ($s_action=="del"){
230       /* Get 'dn' from posted 'uid' */
231       $this->dn= $this->macros[$s_entry]['dn'];
233       /* Load permissions for selected 'dn' and check if
234          we're allowed to remove this 'dn' */
235       $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
236       $this->acl= get_module_permission($acl, "macro", $this->dn);
237       if (chkacl($this->acl, "delete") == ""){
239         /* Check locking, save current plugin in 'back_plugin', so
240            the dialog knows where to return. */
241         if (($user= get_lock($this->dn)) != ""){
242           return (gen_locked_message ($user, $this->dn));
243         }
245         /* Lock the current entry, so nobody will edit it during deletion */
246         add_lock ($this->dn, $this->ui->dn);
247         $smarty= get_smarty();
248         $smarty->assign("intro", sprintf(_("You're about to delete the macro '%s'."), $this->dn));
249         return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
250       } else {
252         /* Obviously the user isn't allowed to delete. Show message and
253            clean session. */
254         print_red (_("You are not allowed to delete this macro!"));
255       }
256     }
258     /* Confirmation for deletion has been passed. Group should be deleted. */
259     if (isset($_POST['delete_macro_confirm'])){
261       /* Some nice guy may send this as POST, so we've to check
262          for the permissions again. */
263       if (chkacl($this->acl, "delete") == ""){
264       
265         /* Delete request is permitted, perform LDAP action */
266         $this->macrotabs= new macrotabs($this->config,$this->config->data['TABS']['MACROTABS'], $this->dn);
267         $this->macrotabs->set_acl(array($this->acl));
268       
269         $this->macrotabs->delete ();
270         gosa_log ("macro object'".$this->dn."' has been removed");
271         unset ($this->macrotabs);
272         $this->macrotabs= NULL;
274         /* Group list has changed, reload it. */
275         $this->reload ();
276       } else {
277         /* Normally this shouldn't be reached, send some extra
278            logs to notify the administrator */
279         print_red (_("You are not allowed to delete this macro!"));
280         gosa_log ("Warning: '".$this->ui->uid."' tried to trick group deletion.");
281       }
282       /* Remove lock file after successfull deletion */
283       del_lock ($this->dn);
284     }
287     /* Delete macro canceled? */
288     if (isset($_POST['delete_cancel'])){
289       del_lock ($this->dn);
290       unset($_SESSION['objectinfo']);
291     }
293     /* Show tab dialog if object is present */
294     if ($this->macrotabs){
295       $display= $this->macrotabs->execute();
297       /* Don't show buttons if tab dialog requests this */
298       if (!$this->macrotabs->by_object[$this->macrotabs->current]->dialog){
299         $display.= "<p style=\"text-align:right\">\n";
300         $display.= "<input type=submit name=\"edit_finish\" value=\""._("Finish")."\">\n";
301         $display.= "&nbsp;\n";
302         $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
303         $display.= "</p>";
304       }
305       return ($display);
306     }
311     /* Prepare departments */
312     $options= "";
313     foreach ($this->config->idepartments as $key => $value){
314             if ($macrofilter['depselect'] == $key){
315                     $options.= "<option selected value='$key'>$value</option>";
316             } else {
317                     $options.= "<option value='$key'>$value</option>";
318             }
319     }
322    /* NEW LIST MANAGMENT */
323     $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
324             " <input type='image' align='middle' src='images/list_back.png' title='"._("Go up one department")."' alt='"._("Up")."' name='dep_back'>&nbsp;".
325             " <input type='image' src='images/list_root.png' align='middle' title='"._("Go to root department")."' name='dep_root' alt='"._("Root").        "'>&nbsp;".
326             " <input type='image' align='middle' src='images/list_home.png' title='"._("Go to users department")."' alt='"._("Home")."'                     name='dep_home'>&nbsp;".
327             " <img src='images/list_seperator.png' align='middle' alt='' height='16' width='1'>&nbsp;".
328             " <input type='image' align='middle' src='images/list_new_macro.png' title='"._("Create new phone macro")."' alt='"._("New")."' name='user_new'>&nbsp;".
329             " <img src='images/list_seperator.png' align='middle' alt='' height='16' width='1'>&nbsp;".
330             _("Current base")."&nbsp;<select name='depselect' onChange='mainform.submit()'>$options</select>".
331             " <input type='image' src='images/list_submit.png' align='middle' title='"._("Submit department")."' name='submit_department' alt='"._("Submit")."'>&nbsp;".
332             "</div>";
335     /* Options */
336     $action= "<input type='image' src='images/edit.png' alt='"._("edit")."'     name='user_edit_%KEY%' title='"._("Edit user")."'>";
337     $action.= "<input type='image' src='images/editdelete.png' alt='"._("delete")."'   name='user_del_%KEY%' title='"._("Delete user")."'>";
339     $divlist = new divlist("macrotabs");
340     $divlist->SetHeader(array(
341                               array("string" => "&nbsp;"),
342                               array("string" => _("Username")." / "._("Department")),
343                               array("string" => _("Visible"), "attach" => "style='width:30px;'"),
344                               array("string" => _("Actions"), "attach" => "style='border:none'" )));
347     $divlist->SetSummary(_("This table displays all phone macros, in the selected tree."));
348     $divlist->SetEntriesPerPage(20);
350     // Defining Links
351     $linkopen = "<a href='?plug=".$_GET['plug']."&amp;act=dep_open&amp;dep_id=%s'>%s</a>";
353     foreach($this->departments as $key=> $val){
354       
355       if(!isset($this->config->departments[trim($key)])){
356         $this->config->departments[trim($key)]="";
357       }
359       $non_empty="";
360       $keys= str_replace("/","\/",$key);
361       foreach($this->config->departments as $keyd=>$vald ){
362         if(preg_match("/".$keys."\/.*/",$keyd)){
363           $non_empty="full";
364         }
365       }
367       $field1 = array("string" => "<img src='images/".$non_empty."folder.png' alt='department'>");
368       $field2 = array("string" => sprintf($linkopen,base64_encode($key),$val));
369       $field3 = array("string" => "&nbsp;");
370       $field4 = array("string" => "&nbsp;", "attach" => "style='border:none'");
371       $divlist->AddEntry(array($field1,$field2,$field3,$field4));
372     }
374     // Pictures for Extensions
375     $empty    = "<img src='images/nothing.png' style='width:16px;height:16px;' alt=''>";
377     // User and Template  Images
378     $macroimg   = "<img src='images/list_macro.png' alt='"._("Macro")."' title='%s'>";
379     $visible    = "<img src='images/true.png' alt='"._("yes")."' title='"._("visible")."'>";
380     $invisible  = "<img src='images/false.png' alt='"._("no")."'title='"._("invisible")."'>";
382     // Link for edit
383     $editlink = "<a href='?plug=".$_GET['plug']."&amp;id=%s&amp;act=edit_entry'>%s</a>";
384   
385     // Test Every Entry and generate divlist Array
386     foreach($this->macros as $key => $val){
387       // Generate Array to Add
388       $display= $val["displayName"][0];
389     
390       
391       if(isset($val['goFonMacroVisible']['0'])&&($val['goFonMacroVisible']['0'] == "1")){
392         $pic1 = $visible;
393       }else{
394         $pic1 = $invisible;
395       }
397       $field1 = array("string" => sprintf($macroimg,$val['dn']),"attach"=>"style='width:20px;align:middle;'");
398       $field2 = array("string" => sprintf($editlink,$key,$display),"attach"=>"title='".$val['dn']."'");
399       $field3 = array("string" => $pic1, "attach" => "style='width:20px;text-align:right;'");
400       $field4 = array("string" => preg_replace("/%KEY%/", "$key", $action),"attach" => "style='border:none;text-align:right;width:48px'");
402       $divlist->AddEntry(array($field1,$field2,$field3,$field4));
403     }
405     /* Show main page */
406     $smarty->assign("macroshead", $listhead);
407     $smarty->assign("macros", $divlist->DrawList());
408     $smarty->assign("search_image", get_template_path('images/search.png'));
409     $smarty->assign("tree_image", get_template_path('images/tree.png'));
410     $smarty->assign("infoimage", get_template_path('images/info.png'));
411     $smarty->assign("launchimage", get_template_path('images/launch.png'));
412     $smarty->assign("deplist", $this->config->idepartments);
413     foreach( array("depselect", "regex") as $type){
414       $smarty->assign("$type", $macrofilter[$type]);
415     }
417     /* Extend if we are not using javascript */
418     $smarty->assign("apply", apply_filter());
419     $smarty->assign("alphabet", generate_alphabet());
420     $smarty->assign("hint", print_sizelimit_warning());
422     return($smarty->fetch(get_template_path('headpage.tpl', TRUE)));
423   }
426   function reload()
427   {
428     /* Get config */
429     $macrofilter= get_global('macrofilter');
431     /* Set base for all searches */
432     $base= $macrofilter['depselect'];
434     /* Regex filter? */
435     if ($macrofilter['regex'] != ""){
436       $regex= $macrofilter['regex'];
437     } else {
438       $regex= "*";
439     }
441     /* Generate macro list */
442     $res= get_list($this->ui->subtreeACL, "(&(cn=$regex)(objectClass=goFonMacro))", FALSE, "ou=macros,ou=asterisk,ou=config,".$base, array("*"), TRUE);
443     $this->macros= $res;
445     /* NEW LIST MANAGMENT
446      * We also need to search for the departments
447      * So we are able to navigate like in konquerer
448      */
449     $base2 = preg_replace("/ou=people,/i","",$base);
451     $res3 =  get_list2($this->ui->subtreeACL, "(&(|(ou=$regex)(description=$regex))(objectClass=gosaDepartment))",
452         TRUE, $base2, array("ou", "description"), TRUE);
454     $this->departments= array();
455     $tmp = array();
456     foreach ($res3 as $value){
457       $tmp[strtolower($value['dn']).$value['dn']]=$value;
458     }
459     ksort($tmp);
460     foreach($tmp as $value){
461       if($value["description"][0]!=".."){
462         $this->departments[$value['dn']]=convert_department_dn2($value['dn'])." - [".$value["description"][0]."]";
463       }else{
464         $this->departments[$value['dn']]=$value["description"][0];
465       }
466     }
468     /* END NEW LIST MANAGMENT
469      */
471    $tmp=array();
472     foreach($this->macros as $tkey => $val ){
473       $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
474     }
475     ksort($tmp);
476     $this->macros=array();
477     foreach($tmp as $val){
478       $this->macros[]=$val;
479     }
481     reset ($this->macros);
482   }
484   function remove_from_parent()
485   {
486     /* Optionally execute a command after we're done */
487     $this->postremove();
488   }
491   /* Save data to object */
492   function save_object()
493   {
494   }
497   /* Check values */
498   function check()
499   {
500   }
503   /* Save to LDAP */
504   function save()
505   {
506     /* Optionally execute a command after we're done */
507     $this->postcreate();
508   }
510   function adapt_from_template($dn)
511   {
512   }
514   function password_change_needed()
515   {
516   }
518   function show_header($button_text, $text, $disabled= FALSE)
519   {
520   }
522   function remove_lock()
523   {
524     if (isset($this->macrotabs->dn)){
525       del_lock ($this->macrotabs->dn);
526     }
527   }
530 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
531 ?>