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