Code

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