1 <?php
3 class userFilter extends plugin
4 {
5 protected $filters = array();
6 protected $availableCategories = array();
8 public $objectclasses = array('gosaProperties');
9 public $attributes = array('gosaUserDefinedFilter');
10 public $gosaUserDefinedFilter = array();
12 private $listing = NULL;
13 private $fixedFilter = NULL;
15 /*! \brief Returns true if we are able to read and write userFilters
16 * (schema has to be present, gosaProperties)
17 */
18 static function userFilteringAvailable()
19 {
20 if(!session::is_set('userFilter::userFilteringAvailable')){
21 global $config;
22 $ldap = $config->get_ldap_link();
23 $ocs = $ldap->get_objectclasses();
24 session::set('userFilter::userFilteringAvailable', isset($ocs['gosaProperties']));
25 }
26 return(session::get('userFilter::userFilteringAvailable'));
27 }
30 /*! \brief Initiates the filter editing dialog.
31 */
32 function __construct($config, $listing)
33 {
34 // Initialize this plugin with the users dn to gather user defined filters.
35 $ui = get_userinfo();
36 plugin::plugin($config, $ui->dn);
37 $this->listing = &$listing;
38 $filter= $this->listing->getFilter();
39 $this->fixedFilter = $filter->getFixedFilters();
41 // Keep list of currently managed categories.
42 $this->availableCategories = array_unique($this->listing->categories);
44 // Load list of filters
45 if(isset($this->attrs['gosaUserDefinedFilter'])){
46 for($i=0; $i< $this->attrs['gosaUserDefinedFilter']['count']; $i++){
47 $tmp = userFilter::explodeFilterString($this->attrs['gosaUserDefinedFilter'][$i]);
48 if(isset($tmp['name'])){
49 $this->filters[$tmp['name']]= $tmp;
50 }
51 }
52 }
54 // Create the filter list
55 $this->filterWidget= new sortableListing();
56 $this->filterWidget->setDeleteable(true);
57 $this->filterWidget->setEditable(true);
58 $this->filterWidget->setWidth("100%");
59 $this->filterWidget->setHeight("270px");
60 $this->filterWidget->setColspecs(array('100px', '200px', '100px', '70px','150px'));
61 $this->filterWidget->setAcl($ui->get_permissions($ui->dn,'users/user','gosaUserDefinedFilter'));
62 $this->filterWidget->setListData($this->filters, $this->convertFilterList());
63 }
66 /*! \brief Parses a filter string into an array.
67 */
68 static function explodeFilterString($filter)
69 {
70 list($parent,$categories, $name, $description, $filter, $flags) = split(";", $filter);
72 // Ensure that we no empty category in our category list.
73 if(empty($categories)){
74 $categories = array();
75 }else{
76 $categories = split(',', $categories);
77 }
79 // Ensure that we no empty entry in out flags list.
80 if(empty($flags)){
81 $flags = array();
82 }else{
83 $flags = split(',', $flags);
84 }
86 // build up filter entry.
87 $tmp = array(
88 'parent' => $parent,
89 'name' => $name,
90 'categories' => $categories,
91 'description' => base64_decode($description),
92 'filter' => base64_decode($filter),
93 'flags' => $flags);
95 return($tmp);
96 }
99 /*! \brief Converts the list of filters ($this->filters) into data which is useable
100 * for the sortableList object ($this->filterWidget).
101 * @return Array An array containg data useable for sortableLists ($this->filterWidget)
102 */
103 function convertFilterList()
104 {
105 $data = array();
106 foreach($this->filters as $name => $filter){
107 $data[$name] = array('data' =>
108 array(
109 $filter['parent'],
110 $filter['name'],
111 $filter['description'],
112 implode(", ",$filter['categories']),
113 implode(", ",$filter['flags'])));
114 }
115 return($data);
116 }
119 /*! \brief Display the user-filter overview as HTML content.
120 * @return string HTML-content showing the user-filter editing dialog.
121 */
122 function execute()
123 {
124 plugin::execute();
126 // Cancel filter modifications (edit dialog)
127 if(isset($_POST['cancelFilterSettings'])){
128 $this->dialog = NULL;
129 }
131 // Save modified filter entries (edit dialog)
132 if(isset($_POST['saveFilterSettings']) && $this->dialog instanceOf userFilterEditor){
133 $this->dialog->save_object();
134 $msgs = $this->dialog->check();
135 if(count($msgs)){
136 msg_dialog::displayChecks($msgs);
137 }else{
138 $orig_name = $this->dialog->getOriginalName();
139 $new_name = $this->dialog->getCurrentName();
141 // The object was renamed and
142 if($orig_name != $new_name && isset($this->filters[$new_name])){
143 $msgs = array(msgPool::duplicated(_("Name")));
144 msg_dialog::displayChecks($msgs);
145 }else{
147 // Remove old entry if filter was renamed
148 if($orig_name != "" && isset($this->filters[$orig_name])){
149 unset($this->filters[$orig_name]);
150 }
152 // Now append the new filter object.
153 $this->filters[$new_name] = $this->dialog->save();
154 $this->dialog = NULL;
155 $this->filterWidget->setListData($this->filters, $this->convertFilterList());
156 $this->filterWidget->update();
157 }
158 }
159 }
161 // Act on edit requests
162 $this->filterWidget->save_object();
163 $action = $this->filterWidget->getAction();
164 if($action['action'] == 'edit' && count($action['targets']) == 1){
165 $key= $this->filterWidget->getKey($action['targets'][0]);
166 if(isset($this->filters[$key])){
167 $this->dialog=new userFilterEditor($this->filters[$key], $this->availableCategories, $this->fixedFilter);
168 }
169 }
171 // Act on new requests
172 if(isset($_POST['addFilter'])){
173 $this->dialog=new userFilterEditor(array(), $this->availableCategories, $this->fixedFilter);
174 }
176 // Act on remove requests
177 $action = $this->filterWidget->getAction();
178 if($action['action'] == 'delete' && count($action['targets']) == 1){
179 $key= $this->filterWidget->getKey($action['targets'][0]);
180 if(isset($this->filters[$key])){
181 unset($this->filters[$key]);
182 $this->filterWidget->update();
183 }
184 }
186 // Display edit dialog
187 if($this->dialog instanceOf userFilterEditor){
188 $this->dialog->save_object();
189 return($this->dialog->execute());
190 }
192 $smarty = get_smarty();
193 $smarty->assign("list", $this->filterWidget->render());
194 return($smarty->fetch(get_template_path('userFilter.tpl', FALSE)));
195 }
198 /*! \brief Returns user defined filter for a given list of categories,
199 * if no categories were specified all enabled filters will be returned.
200 */
201 static function getFilter($category=array())
202 {
203 global $config;
204 $ldap=$config->get_ldap_link();
205 $ui = get_userinfo();
206 $ldap->cd($config->current['BASE']);
207 $ldap->search("(&(objectClass=gosaProperties)(gosaUserDefinedFilter=*))",array('gosaUserDefinedFilter'));
208 $filter = array();
209 while($attrs = $ldap->fetch()){
210 for($i=0; $i < $attrs['gosaUserDefinedFilter']['count']; $i++){
211 $tmp = userFilter::explodeFilterString($attrs['gosaUserDefinedFilter'][$i]);
212 if(!isset($tmp['name'])) continue;
214 // Remove line breaks from the filter, which may were added for better reading.
215 $c = preg_split('/\n/',$tmp['filter']);
217 foreach($c as $key => $str) $c[$key] = trim($str);
218 $tmp['filter'] = implode($c);
220 // The filter is visible if it is shared or if is one of our own creations.
221 // ... and enabled.
222 $visible = in_array('enable', $tmp['flags']) &&
223 ($attrs['dn'] == $ui->dn || in_array('share', $tmp['flags']));
225 // Convert filter encoding
226 $tmp['filter'] = mb_convert_encoding($tmp['filter'], 'UTF-8');
228 // Add filter if it matches the category list
229 if($visible && (count($category) == 0 || array_intersect($category, $tmp['categories']))){
230 $filter[$tmp['name']] = $tmp;
231 }
232 }
233 }
234 return($filter);
235 }
238 /*! \brief Write user-filter modifications back to the ldap.
239 */
240 function save()
241 {
242 // Build up new list of filters
243 $attrs = array();
244 foreach($this->filters as $filter){
245 $tmp = $filter['parent'].";";
246 $tmp.= implode(',', $filter['categories']).";";
247 $tmp.= $filter['name'].";";
248 $tmp.= base64_encode($filter['description']).";";
249 $tmp.= base64_encode($filter['filter']).";";
250 $tmp.= implode(',', $filter['flags']);
251 $attrs[] = $tmp;
252 }
253 $this->gosaUserDefinedFilter = $attrs;
255 plugin::save();
257 $ldap = $this->config->get_ldap_link();
258 $ldap->cd($this->dn);
259 $ldap->modify($this->attrs);
261 new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
263 if (!$ldap->success()){
264 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
265 }
266 }
269 /*! \brief Do not save any posted values here.
270 */
271 function save_object(){}
272 }
274 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
275 ?>