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