1 <?php
3 class userFilter extends plugin
4 {
5 public $pathTitle= "Filter";
7 protected $filters = array();
8 protected $availableCategories = array();
10 public $objectclasses = array('gosaProperties');
11 public $attributes = array('gosaUserDefinedFilter');
12 public $gosaUserDefinedFilter = array();
14 private $listing = NULL;
15 private $fixedFilter = NULL;
17 /*! \brief Returns true if we are able to read and write userFilters
18 * (schema has to be present, gosaProperties)
19 */
20 static function userFilteringAvailable()
21 {
22 if(!session::is_set('userFilter::userFilteringAvailable')){
23 global $config;
24 $ldap = $config->get_ldap_link();
25 $ocs = $ldap->get_objectclasses();
26 session::set('userFilter::userFilteringAvailable', isset($ocs['gosaProperties']));
27 }
28 return(session::get('userFilter::userFilteringAvailable'));
29 }
32 /*! \brief Initiates the filter editing dialog.
33 */
34 function __construct($config, $listing)
35 {
36 // Initialize this plugin with the users dn to gather user defined filters.
37 $ui = get_userinfo();
38 plugin::plugin($config, $ui->dn);
39 $this->listing = &$listing;
40 $filter= $this->listing->getFilter();
41 $this->fixedFilter = $filter->getFixedFilters();
43 // Keep list of currently managed categories.
44 $this->availableCategories = array_unique($this->listing->categories);
46 // Load list of filters
47 if(isset($this->attrs['gosaUserDefinedFilter'])){
48 for($i=0; $i< $this->attrs['gosaUserDefinedFilter']['count']; $i++){
49 $tmp = userFilter::explodeFilterString($this->attrs['gosaUserDefinedFilter'][$i]);
50 if(isset($tmp['name'])){
51 $this->filters[$tmp['name']]= $tmp;
52 }
53 }
54 }
56 // Create the filter list
57 $this->filterWidget= new sortableListing($this->filters, $this->convertFilterList());
58 $this->filterWidget->setDeleteable(true);
59 $this->filterWidget->setEditable(true);
60 $this->filterWidget->setWidth("100%");
61 $this->filterWidget->setHeight("270px");
62 $this->filterWidget->setHeader(array(_("Parent filter"),_("Name"),_("Description"),_("Category"),_("Options"),""));
63 $this->filterWidget->setColspecs(array('80px', '100px', '200px', '120px','150px'));
64 $this->filterWidget->setAcl($ui->get_permissions($ui->dn,'users/user','gosaUserDefinedFilter'));
65 }
68 /*! \brief Parses a filter string into an array.
69 */
70 static function explodeFilterString($filter)
71 {
72 list($parent,$categories, $name, $description, $filter, $flags) = split(";", $filter);
74 // Ensure that we no empty category in our category list.
75 if(empty($categories)){
76 $categories = array();
77 }else{
78 $categories = split(',', $categories);
79 }
81 // Ensure that we no empty entry in out flags list.
82 if(empty($flags)){
83 $flags = array();
84 }else{
85 $flags = split(',', $flags);
86 }
88 // build up filter entry.
89 $tmp = array(
90 'parent' => $parent,
91 'name' => $name,
92 'categories' => $categories,
93 'description' => base64_decode($description),
94 'filter' => base64_decode($filter),
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->availableCategories, $this->fixedFilter);
173 }
174 }
176 // Act on new requests
177 if(isset($_POST['addFilter'])){
178 $this->dialog=new userFilterEditor(array(), $this->availableCategories, $this->fixedFilter);
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;
209 $ldap=$config->get_ldap_link();
210 $ui = get_userinfo();
211 $ldap->cd($config->current['BASE']);
212 $ldap->search("(&(objectClass=gosaProperties)(gosaUserDefinedFilter=*))",array('gosaUserDefinedFilter'));
213 $filter = array();
214 while($attrs = $ldap->fetch()){
215 for($i=0; $i < $attrs['gosaUserDefinedFilter']['count']; $i++){
216 $tmp = userFilter::explodeFilterString($attrs['gosaUserDefinedFilter'][$i]);
217 if(!isset($tmp['name'])) continue;
219 // Remove line breaks from the filter, which may were added for better reading.
220 $c = preg_split('/\n/',$tmp['filter']);
222 foreach($c as $key => $str) $c[$key] = trim($str);
223 $tmp['filter'] = implode($c);
225 // The filter is visible if it is shared or if is one of our own creations.
226 // ... and enabled.
227 $visible = in_array('enable', $tmp['flags']) &&
228 ($attrs['dn'] == $ui->dn || in_array('share', $tmp['flags']));
230 // Convert filter encoding
231 $tmp['filter'] = mb_convert_encoding($tmp['filter'], 'UTF-8');
233 // Add filter if it matches the category list
234 if($visible && (count($category) == 0 || array_intersect($category, $tmp['categories']))){
235 $filter[$tmp['name']] = $tmp;
236 }
237 }
238 }
239 return($filter);
240 }
243 /*! \brief Write user-filter modifications back to the ldap.
244 */
245 function save()
246 {
247 // Build up new list of filters
248 $attrs = array();
249 foreach($this->filters as $filter){
250 $tmp = $filter['parent'].";";
251 $tmp.= implode(',', $filter['categories']).";";
252 $tmp.= $filter['name'].";";
253 $tmp.= base64_encode($filter['description']).";";
254 $tmp.= base64_encode($filter['filter']).";";
255 $tmp.= implode(',', $filter['flags']);
256 $attrs[] = $tmp;
257 }
258 $this->gosaUserDefinedFilter = $attrs;
260 plugin::save();
262 $ldap = $this->config->get_ldap_link();
263 $ldap->cd($this->dn);
264 $ldap->modify($this->attrs);
266 new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
268 if (!$ldap->success()){
269 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
270 }
271 }
274 /*! \brief Do not save any posted values here.
275 */
276 function save_object(){}
277 }
279 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
280 ?>