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($this->filters, $this->convertFilterList());
56 $this->filterWidget->setDeleteable(true);
57 $this->filterWidget->setEditable(true);
58 $this->filterWidget->setWidth("100%");
59 $this->filterWidget->setHeight("270px");
60 $this->filterWidget->setHeader(array(_("Parent filter"),_("Name"),_("Description"),_("Category"),_("Options"),""));
61 $this->filterWidget->setColspecs(array('80px', '100px', '200px', '120px','150px'));
62 $this->filterWidget->setAcl($ui->get_permissions($ui->dn,'users/user','gosaUserDefinedFilter'));
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 // Let the filter widget update itself
127 $this->filterWidget->update();
129 // Cancel filter modifications (edit dialog)
130 if(isset($_POST['cancelFilterSettings'])){
131 $this->dialog = NULL;
132 }
134 // Save modified filter entries (edit dialog)
135 if(isset($_POST['saveFilterSettings']) && $this->dialog instanceOf userFilterEditor){
136 $this->dialog->save_object();
137 $msgs = $this->dialog->check();
138 if(count($msgs)){
139 msg_dialog::displayChecks($msgs);
140 }else{
141 $orig_name = $this->dialog->getOriginalName();
142 $new_name = $this->dialog->getCurrentName();
144 // The object was renamed and
145 if($orig_name != $new_name && isset($this->filters[$new_name])){
146 $msgs = array(msgPool::duplicated(_("Name")));
147 msg_dialog::displayChecks($msgs);
148 }else{
150 // Remove old entry if filter was renamed
151 if($orig_name != "" && isset($this->filters[$orig_name])){
152 unset($this->filters[$orig_name]);
153 }
155 // Now append the new filter object.
156 $this->filters[$new_name] = $this->dialog->save();
157 $this->dialog = NULL;
158 $this->filterWidget->setListData($this->filters, $this->convertFilterList());
159 $this->filterWidget->update();
160 }
161 }
162 }
164 // Act on edit requests
165 $this->filterWidget->save_object();
166 $action = $this->filterWidget->getAction();
167 if($action['action'] == 'edit' && count($action['targets']) == 1){
168 $key= $this->filterWidget->getKey($action['targets'][0]);
169 if(isset($this->filters[$key])){
170 $this->dialog=new userFilterEditor($this->filters[$key], $this->availableCategories, $this->fixedFilter);
171 }
172 }
174 // Act on new requests
175 if(isset($_POST['addFilter'])){
176 $this->dialog=new userFilterEditor(array(), $this->availableCategories, $this->fixedFilter);
177 }
179 // Act on remove requests
180 $action = $this->filterWidget->getAction();
181 if($action['action'] == 'delete' && count($action['targets']) == 1){
182 $key= $this->filterWidget->getKey($action['targets'][0]);
183 if(isset($this->filters[$key])){
184 unset($this->filters[$key]);
185 $this->filterWidget->update();
186 }
187 }
189 // Display edit dialog
190 if($this->dialog instanceOf userFilterEditor){
191 $this->dialog->save_object();
192 return($this->dialog->execute());
193 }
195 $smarty = get_smarty();
196 $smarty->assign("list", $this->filterWidget->render());
197 return($smarty->fetch(get_template_path('userFilter.tpl', FALSE)));
198 }
201 /*! \brief Returns user defined filter for a given list of categories,
202 * if no categories were specified all enabled filters will be returned.
203 */
204 static function getFilter($category=array())
205 {
206 global $config;
207 $ldap=$config->get_ldap_link();
208 $ui = get_userinfo();
209 $ldap->cd($config->current['BASE']);
210 $ldap->search("(&(objectClass=gosaProperties)(gosaUserDefinedFilter=*))",array('gosaUserDefinedFilter'));
211 $filter = array();
212 while($attrs = $ldap->fetch()){
213 for($i=0; $i < $attrs['gosaUserDefinedFilter']['count']; $i++){
214 $tmp = userFilter::explodeFilterString($attrs['gosaUserDefinedFilter'][$i]);
215 if(!isset($tmp['name'])) continue;
217 // Remove line breaks from the filter, which may were added for better reading.
218 $c = preg_split('/\n/',$tmp['filter']);
220 foreach($c as $key => $str) $c[$key] = trim($str);
221 $tmp['filter'] = implode($c);
223 // The filter is visible if it is shared or if is one of our own creations.
224 // ... and enabled.
225 $visible = in_array('enable', $tmp['flags']) &&
226 ($attrs['dn'] == $ui->dn || in_array('share', $tmp['flags']));
228 // Convert filter encoding
229 $tmp['filter'] = mb_convert_encoding($tmp['filter'], 'UTF-8');
231 // Add filter if it matches the category list
232 if($visible && (count($category) == 0 || array_intersect($category, $tmp['categories']))){
233 $filter[$tmp['name']] = $tmp;
234 }
235 }
236 }
237 return($filter);
238 }
241 /*! \brief Write user-filter modifications back to the ldap.
242 */
243 function save()
244 {
245 // Build up new list of filters
246 $attrs = array();
247 foreach($this->filters as $filter){
248 $tmp = $filter['parent'].";";
249 $tmp.= implode(',', $filter['categories']).";";
250 $tmp.= $filter['name'].";";
251 $tmp.= base64_encode($filter['description']).";";
252 $tmp.= base64_encode($filter['filter']).";";
253 $tmp.= implode(',', $filter['flags']);
254 $attrs[] = $tmp;
255 }
256 $this->gosaUserDefinedFilter = $attrs;
258 plugin::save();
260 $ldap = $this->config->get_ldap_link();
261 $ldap->cd($this->dn);
262 $ldap->modify($this->attrs);
264 new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
266 if (!$ldap->success()){
267 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
268 }
269 }
272 /*! \brief Do not save any posted values here.
273 */
274 function save_object(){}
275 }
277 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
278 ?>