Code

a6d4048e90008b9b959d9c3519d9cf78da9d5967
[gosa.git] / gosa-core / include / class_userFilterEditor.inc
1 <?php
3 class userFilterEditor extends plugin 
4 {
5   public $pathTitle= "Edit";
7   // The original entry else array(), allows us to perform existence checks.
8   public $entry = array();
10   // The values
11   public $name = "";
12   public $description = "";
13   public $parent = "";
14   public $selectedCategories = array();
15   public $share = FALSE;
16   public $enabled = TRUE;
17   public $filter = "(objectClass=*)";
19   // The list of all categories mangaged by the current filter object.
20   // Used in the grop-down box.
21   public $availableCategories = array();
22   public $fixedFilters = array();
23   public $orig_name = "";
26   /*! \brief    Instantiate the filter editing dialog. 
27    *            Parses the filter info into editable data.
28    */
29   function __construct($entry, $categories, $fixedFilters)
30   {
31     $this->availableCategories = $categories;
32     $this->fixedFilters = $fixedFilters;
33     if($entry){
34       $this->entry = $entry;
35       $this->parent = $entry['parent'];
36       $this->name = $entry['name'];
37       $this->description = $entry['description'];
38       $this->filter = $entry['filter'];
39       $this->selectedCategories = $entry['categories'];
40       $this->share = in_array("share",$entry['flags']);
41       $this->enable = in_array("enable",$entry['flags']);
42     }
43     $this->orig_name = $this->name;
44   }
47   /*! \brief    Retunrs the filters original name 
48    *  @param    The original name of the filter (if none was given 
49    *             an empty string is returned)
50    */
51   function getOriginalName()
52   {
53     return($this->orig_name);
54   }
57   /*! \brief    Retunrs the filters name.
58    *  @param    The name of the filter
59    */
60   function getCurrentName()
61   {
62     return($this->name);
63   }
66   /*! \brief    Generates the <HTML> content, to edit the filter settings.
67    *  @return   String  HTML form.
68    */
69   function execute()
70   {
71     plugin::execute();
72     $smarty = get_smarty();
73     $smarty->assign('parent', $this->parent);
74     $smarty->assign('name', htmlentities($this->name,ENT_COMPAT,'UTF-8'));
75     $smarty->assign('filter', htmlentities($this->filter,ENT_COMPAT,'UTF-8'));
76     $smarty->assign('share', $this->share);
77     $smarty->assign('enable', $this->enabled);
78     $smarty->assign('description', htmlentities($this->description,ENT_COMPAT,'UTF-8'));
79     $smarty->assign('selectedCategories', $this->selectedCategories);
80     $smarty->assign('availableCategories', $this->availableCategories);
81     $smarty->assign('fixedFilters', $this->fixedFilters);
82     return($smarty->fetch(get_template_path('userFilterEditor.tpl', FALSE)));
83   }
86   /*! \brief    Keep values entered in the input form of the dialog. (POST/GET)
87    */
88   function save_object()
89   {
90     if(isset($_POST['userFilterEditor'])){
92       // Get posted strings
93       foreach(array('name','description', 'parent') as $attr){
94         if(isset($_POST[$attr])){
95           $this->$attr = get_post($attr);
96         }
97       }
99       // Filter needs special handling, it may contain charactes like < and >
100       //  wich are stipped out by get_post() && validate()
101       if(isset($_POST['filter'])){
102         $f = $_POST['filter'];
103         if(get_magic_quotes_gpc()){
104           $f = stripcslashes($f);
105         }
106         $f = mb_convert_encoding($_POST['filter'], 'UTF-8');
107         $this->filter = $f;
108       }
110       // Get posted flags 
111       $this->share = isset($_POST['shareFilter']);
112       $this->enable = isset($_POST['enableFilter']);
114       // Get additional category  
115       if(isset($_POST['addCategory'])){
116         if(isset($_POST['manualCategory']) && !empty($_POST['manualCategory'])){
117           $this->selectedCategories[] = get_post('manualCategory');
118         }elseif(isset($_POST['availableCategory']) && !empty($_POST['availableCategory'])){
119           $this->selectedCategories[] = get_post('availableCategory');
120         }
121       }
123       // Remove categories
124       if(isset($_POST['delCategory']) && isset($_POST['usedCategory'])){
125         foreach($_POST['usedCategory'] as $cat){
126           if(isset($this->selectedCategories[$cat])) unset($this->selectedCategories[$cat]);
127         }
128       }
129     }
130   }
132   
133   /*! \brief    Validate user input 
134    *  @return   Array   An Array containing potential error messages
135    */
136   function check()
137   {
138     $msgs = plugin::check();
139   
140     // Check if the name is given
141     if(empty($this->name)){
142       $msgs[] = msgPool::required(_("Name"));
143     }elseif(preg_match("/[^a-z0-9\-_ ]/i", $this->name)){
144       
145       // Check for a valid name, no special chars here - in particular no ; 
146       $msgs[] = msgPool::invalid(_("Name"), $this->name,"/[a-z0-9\-_ ]/i");
147     }  
149     // Description is a must value.
150     if(empty($this->description)){
151       $msgs[] = msgPool::required(_("Description"));
152     }
154     // Count the number of opening and closing brackets - exclude escaped ones.
155     $f = preg_replace('/\\\\[\(\)]/',"",$this->filter);
156     $o = substr_count($f, '(');
157     $c = substr_count($f, ')');
158     if($o != $c){
159       $msgs[] = sprintf(_("Please check your filter. You have '%s' opening and '%s' closing brackets!"), $o, $c);
160     }
162     return($msgs);
163   }
166   /*! \brief    Transforms the entered values into a filter object (array) which is useable
167    *             for the userFilter overview dialog.
168    *  @return   Returns transformed filter data.
169    */
170   function save()
171   {
172     $ret= array();
173     $ret['parent'] = $this->parent;
174     $ret['name'] = $this->name;
175     $ret['description'] = $this->description;
176     $ret['categories'] = $this->selectedCategories;
177     $ret['filter'] = $this->filter;
178     $ret['flags'] = array();
179     if($this->share){
180       $ret['flags'][] = "share";
181     }
182     if($this->enable){
183       $ret['flags'][] = "enable";
184     }
185     return($ret);
186   }
189 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
190 ?>