Code

[COSMETIC] some more centering
[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     }
47     $acl= get_permissions ($ui->dn, $ui->subtreeACL);
48     $this->acl= get_module_permission($acl, "goFonMacro", $ui->dn);
49   }
51   function execute()
52   {
53         /* Call parent execute */
54         plugin::execute();
56     /* Save data */
57     $macrofilter= get_global("macrofilter");
58     foreach( array("depselect", "regex") as $type){
59       if (isset($_POST[$type])){
60         $macrofilter[$type]= $_POST[$type];
61       }
62     }
63     if (isset($_GET['search'])){
64       $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
65       if ($s == "**"){
66         $s= "*";
67       }
68       $macrofilter['regex']= $s;
69     }
71     $s_action     = "";                       // Contains the action to proceed
72     $s_entry      = "";                       // The value for s_action
73     $base_back    = "";                       // The Link for Backbutton
75     /* Start for New List Managment */
76     if(isset($_GET['act'])&&($_GET['act']=="dep_open")){
77             $s_action="open";
78             $s_entry = base64_decode($_GET['dep_id']);
79             $macrofilter['depselect']= "".$this->config->departments[trim($s_entry)];
80     }
82     /* Test Posts */
83     foreach($_POST as $key => $val){
84       // Post for delete
85       if(preg_match("/user_del.*/",$key)){
86         $s_action = "del";
87         $s_entry  = preg_replace("/user_".$s_action."_/i","",$key);
88         // Post for edit
89       }elseif(preg_match("/user_edit_.*/",$key)){
90         $s_action="edit";
91         $s_entry  = preg_replace("/user_".$s_action."_/i","",$key);
92         // Post for new
93       }elseif(preg_match("/dep_back.*/i",$key)){
94         $s_action="back";
95       }elseif(preg_match("/user_new.*/",$key)){
96         $s_action="new";
97       }elseif(preg_match("/dep_home.*/i",$key)){
98         $s_action="home";
99       }elseif(preg_match("/user_chgpw.*/i",$key)){
100         $s_action="change_pw";
101         $s_entry  = preg_replace("/user_chgpw_/i","",$key);
102       }elseif(preg_match("/dep_root.*/i",$key)){
103         $s_action="root";
104       }
105     }
106   if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
107       $s_action ="edit";
108       $s_entry  = $_GET['id'];
109     }
111     $s_entry  = preg_replace("/_.$/","",$s_entry);
113     /* Department changed? */
114     if(isset($_POST['depselect']) && $_POST['depselect']){
115       $macrofilter['depselect']= $_POST['depselect'];
116     }
118     /* Homebutton is posted */
119     if($s_action=="home"){
120       $macrofilter['depselect']=(preg_replace("/^[^,]+,/","",$this->ui->dn));
121       $macrofilter['depselect']=(preg_replace("/^[^,]+,/","",$macrofilter['depselect']));
122     }
124     if($s_action=="root"){
125       $macrofilter['depselect']=($this->config->current['BASE']);
126     }
129     /* If Backbutton is Posted */
130     if($s_action=="back"){
131       $base_back          = preg_replace("/^[^,]+,/","",$macrofilter['depselect']);
132       $base_back          = convert_department_dn($base_back);
134       if(isset($this->config->departments[trim($base_back)])){
135         $macrofilter['depselect']= $this->config->departments[trim($base_back)];
136       }else{
137         $macrofilter['depselect']= $this->config->departments["/"];
138       }
139     }
141     register_global("macrofilter", $macrofilter);
142     $this->reload();
143     
144     $smarty= get_smarty();
146     /* Check for exeeded sizelimit */
147     if (($message= check_sizelimit()) != ""){
148       return($message);
149     }
151     /* New macro? */
152     if ($s_action=="new"){
154       /* By default we set 'dn' to 'new', all relevant plugins will
155          react on this. */
156       $this->dn= "new";
158       /* Create new usertab object */
159       $this->macrotabs= new macrotabs($this->config,$this->config->data['TABS']['MACROTABS'], $this->dn);
160     }
162     /* Cancel dialogs */
163     if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
164       del_lock ($this->macrotabs->dn);
165       unset ($this->macrotabs);
166       $this->macrotabs= NULL;
167       unset ($_SESSION['objectinfo']);
168     }
170     /* Finish mac edit is triggered by the tabulator dialog, so
171        the user wants to save edited data. Check and save at this
172        point. */
173     if (isset($_POST['edit_finish'])){
175       /* Check tabs, will feed message array */
176       $this->macrotabs->last= $this->macrotabs->current;
177       $this->macrotabs->save_object();
178       $message= $this->macrotabs->check();
180       /* Save, or display error message? */
181       if (count($message) == 0){
183         /* Save data data to ldap */
184         $this->macrotabs->save();
185         gosa_log ("Macro object'".$this->dn."' has been saved");
187         /* macro has been saved successfully, remove lock from
188            LDAP. */
189         if ($this->dn != "new"){
190           del_lock ($this->dn);
191         }
193         /* There's no page reload so we have to read new mac at
194            this point. */
195         $this->reload ();
196         unset ($this->macrotabs);
197         $this->macrotabs= NULL;
198         unset ($_SESSION['objectinfo']);
199       } else {
200         /* Ok. There seem to be errors regarding to the tab data,
201            show message and continue as usual. */
202         show_errors($message);
203       }
204     }
206     /* User wants to edit data? */
207     if ($s_action=="edit"){
209       /* Get 'dn' from posted 'macrolists', must be unique */
210       $this->dn= $this->macros[$s_entry]['dn']; 
212       /* Check locking, save current plugin in 'back_plugin', so
213          the dialog knows where to return. */
214       if (($user= get_lock($this->dn)) != ""){
215         return(gen_locked_message ($user, $this->dn));
216       }
218       /* Lock the current entry, so everyone will get the
219          above dialog */
220       add_lock ($this->dn, $this->ui->dn);
221       /* Register macrotabs to trigger edit dialog */
222       $this->macrotabs= new macrotabs($this->config,
223       $this->config->data['TABS']['MACROTABS'], $this->dn);
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, "goFonMacro", $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='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 class='center' type='image' align='middle' src='images/list_back.png' title='"._("Go up one department")."' alt='"._("Up")."' name='dep_back'>&nbsp;".
325             " <input class='center' type='image' src='images/list_root.png' align='middle' title='"._("Go to root department")."' name='dep_root' alt='"._("Root").        "'>&nbsp;".
326             " <input class='center' type='image' align='middle' src='images/list_home.png' title='"._("Go to users department")."' alt='"._("Home")."'                     name='dep_home'>&nbsp;".
327             " <img class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'>&nbsp;".
328             " <input class='center' type='image' align='middle' src='images/list_new_macro.png' title='"._("Create new phone macro")."' alt='"._("New")."' name='user_new'>&nbsp;".
329             " <img class='center' 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 class='center' 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 class='center' type='image' src='images/edit.png' alt='"._("edit")."'     name='user_edit_%KEY%' title='"._("Edit user")."'>";
337     $action.= "<input class='center' type='image' src='images/edittrash.png' alt='"._("delete")."'   name='user_del_%KEY%' title='"._("Delete user")."'>";
339     $divlist = new divlist("macrotabs");
340     $divlist->SetHeader(array(
341                               array("string" => "&nbsp;", "attach" => "style='text-align:center;width:20px;'"),
342                               array("string" => _("Username")." / "._("Department"), "attach" => "style=''"),
343                               array("string" => _("Visible"), "attach" => "style='width:50px;'"),
344                               array("string" => _("Actions"), "attach" => "style='width:60px;border-right:0px;text-align:right;'" )));
347     $divlist->SetSummary(_("This table displays all phone macros, in the selected tree."));
348     $divlist->SetEntriesPerPage(0);
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'>", "attach" => "style='text-align:center;width:20px;'");
368       $field2 = array("string" => sprintf($linkopen,base64_encode($key),$val), "attach" => "style=''");
369       $field3 = array("string" => "&nbsp;", "attach" => "style='width:50px;'");
370       $field4 = array("string" => "&nbsp;", "attach" => "style='width:60px;border-right:0px;text-align:right;'");
371       $divlist->AddEntry(array($field1,$field2,$field3,$field4));
372     }
374     // Pictures for Extensions
375     $empty    = "<img class='center' src='images/nothing.png' style='width:16px;height:16px;' alt=''>";
377     // User and Template  Images
378     $macroimg   = "<img class='center' src='images/list_macro.png' alt='"._("Macro")."' title='%s'>";
379     $visible    = "<img class='center' src='images/true.png' alt='"._("yes")."' title='"._("visible")."'>";
380     $invisible  = "<img class='center' 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='text-align:center;width:20px;'");
398       $field2 = array("string" => sprintf($editlink,$key,$display), "attach" => "style='' title='".$val['dn']."'");
399       $field3 = array("string" => $pic1, "attach" => "style='width:50px;'");
400       $field4 = array("string" => preg_replace("/%KEY%/", "$key", $action), "attach" => "style='width:60px;border-right:0px;text-align:right;'");
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=configs,ou=systems,".$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(isset($value["description"][0])){
462         $this->departments[$value['dn']]=convert_department_dn2($value['dn'])." - [".$value["description"][0]."]";
463       }else{
464         $this->departments[$value['dn']]=convert_department_dn2($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 ?>