Code

Updated filters
[gosa.git] / gosa-core / include / class_userFilter.inc
1 <?php
3 class userFilter extends plugin 
4 {
5   protected $filters = array();
6   protected $availableCategories = array();
7   
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   }
28   
29  
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->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     $this->filterWidget->setListData($this->filters, $this->convertFilterList());
64   }
66   
67   /*! \brief    Parses a filter string into an array.
68    */
69   static function explodeFilterString($filter)
70   {
71     list($parent,$categories, $name, $description, $filter, $flags) = split(";", $filter);
73     // Ensure that we no empty category in our category list.
74     if(empty($categories)){
75       $categories = array();
76     }else{
77       $categories = split(',', $categories);
78     }
80     // Ensure that we no empty entry in out flags list.
81     if(empty($flags)){
82       $flags = array();
83     }else{
84       $flags = split(',', $flags);
85     }
87     // build up filter entry.
88     $tmp = array(
89         'parent' => $parent, 
90         'name' => $name, 
91         'categories' => $categories,
92         'description' => base64_decode($description),
93         'filter' => base64_decode($filter),
94         'flags' => $flags);
96     return($tmp);
97   }
98   
100   /*! \brief    Converts the list of filters ($this->filters) into data which is useable
101    *             for the sortableList object ($this->filterWidget).
102    *  @return   Array   An array containg data useable for sortableLists ($this->filterWidget)
103    */
104   function convertFilterList()
105   { 
106     $data = array();
107     foreach($this->filters as $name => $filter){
108       $data[$name] = array('data' =>
109           array(
110             $filter['parent'],
111             $filter['name'],
112             $filter['description'],
113             implode(", ",$filter['categories']),
114             implode(", ",$filter['flags'])));
115     }
116     return($data); 
117   }
120   /*! \brief    Display the user-filter overview as HTML content.
121    *  @return   string    HTML-content showing the user-filter editing dialog.
122    */
123   function execute()
124   {
125     plugin::execute();
127     // Cancel filter modifications (edit dialog)
128     if(isset($_POST['cancelFilterSettings'])){
129       $this->dialog = NULL;
130     }
132     // Save modified filter entries (edit dialog)
133     if(isset($_POST['saveFilterSettings']) && $this->dialog instanceOf userFilterEditor){
134       $this->dialog->save_object();
135       $msgs = $this->dialog->check();
136       if(count($msgs)){
137         msg_dialog::displayChecks($msgs);
138       }else{
139         $orig_name = $this->dialog->getOriginalName();
140         $new_name = $this->dialog->getCurrentName();
142         // The object was renamed and
143         if($orig_name != $new_name && isset($this->filters[$new_name])){
144           $msgs = array(msgPool::duplicated(_("Name")));
145           msg_dialog::displayChecks($msgs);
146         }else{
148           // Remove old entry if filter was renamed
149           if($orig_name != "" && isset($this->filters[$orig_name])){
150             unset($this->filters[$orig_name]);
151           }
152           
153           // Now append the new filter object.
154           $this->filters[$new_name] = $this->dialog->save();
155           $this->dialog = NULL;
156           $this->filterWidget->setListData($this->filters, $this->convertFilterList());
157           $this->filterWidget->update();
158         }
159       }
160     }
162     // Act on edit requests 
163     $this->filterWidget->save_object();
164     $action = $this->filterWidget->getAction();
165     if($action['action'] == 'edit' && count($action['targets']) == 1){
166       $key= $this->filterWidget->getKey($action['targets'][0]);
167       if(isset($this->filters[$key])){
168         $this->dialog=new userFilterEditor($this->filters[$key], $this->availableCategories, $this->fixedFilter);
169       }
170     }
172     // Act on new requests
173     if(isset($_POST['addFilter'])){
174       $this->dialog=new userFilterEditor(array(), $this->availableCategories, $this->fixedFilter);
175     }
177     // Act on remove requests 
178     $action = $this->filterWidget->getAction();
179     if($action['action'] == 'delete' && count($action['targets']) == 1){
180       $key= $this->filterWidget->getKey($action['targets'][0]);
181       if(isset($this->filters[$key])){
182         unset($this->filters[$key]);
183         $this->filterWidget->update();
184       }
185     }
187     // Display edit dialog
188     if($this->dialog instanceOf userFilterEditor){
189       $this->dialog->save_object();
190       return($this->dialog->execute());
191     }
193     $smarty = get_smarty();
194     $smarty->assign("list", $this->filterWidget->render());
195     return($smarty->fetch(get_template_path('userFilter.tpl', FALSE)));
196   }
199   /*! \brief    Returns user defined filter for a given list of categories,
200    *             if no categories were specified all enabled filters will be returned.
201    */
202   static function getFilter($category=array())
203   {
204     global $config;
205     $ldap=$config->get_ldap_link();
206     $ui = get_userinfo();
207     $ldap->cd($config->current['BASE']);
208     $ldap->search("(&(objectClass=gosaProperties)(gosaUserDefinedFilter=*))",array('gosaUserDefinedFilter'));
209     $filter = array();
210     while($attrs = $ldap->fetch()){
211       for($i=0; $i < $attrs['gosaUserDefinedFilter']['count']; $i++){
212         $tmp = userFilter::explodeFilterString($attrs['gosaUserDefinedFilter'][$i]);
213         if(!isset($tmp['name'])) continue;
214           
215         // Remove line breaks from the filter, which may were added for better reading. 
216         $c = preg_split('/\n/',$tmp['filter']);
218         foreach($c as $key => $str) $c[$key] = trim($str);
219         $tmp['filter'] = implode($c);
220  
221         // The filter is visible if it is shared or if is one of our own creations.
222         //  ... and enabled.
223         $visible = in_array('enable', $tmp['flags']) && 
224           ($attrs['dn'] == $ui->dn || in_array('share', $tmp['flags']));
225          
226         // Convert filter encoding
227         $tmp['filter'] = mb_convert_encoding($tmp['filter'], 'UTF-8');
228  
229         // Add filter if it matches the category list
230         if($visible && (count($category) == 0 || array_intersect($category, $tmp['categories']))){ 
231           $filter[$tmp['name']] = $tmp;
232         }
233       }
234     }
235     return($filter);
236   }
239   /*! \brief    Write user-filter modifications back to the ldap.  
240    */
241   function save()
242   {
243     // Build up new list of filters 
244     $attrs = array();
245     foreach($this->filters as $filter){
246       $tmp = $filter['parent'].";";
247       $tmp.= implode(',', $filter['categories']).";";
248       $tmp.= $filter['name'].";";
249       $tmp.= base64_encode($filter['description']).";";
250       $tmp.= base64_encode($filter['filter']).";";
251       $tmp.= implode(',', $filter['flags']);
252       $attrs[] = $tmp;
253     }
254     $this->gosaUserDefinedFilter = $attrs;
256     plugin::save();
258     $ldap = $this->config->get_ldap_link();
259     $ldap->cd($this->dn);
260     $ldap->modify($this->attrs);
261     
262     new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
264     if (!$ldap->success()){
265       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
266     }
267   }  
269   
270   /*! \brief    Do not save any posted values here.
271    */
272   function save_object(){}
275 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
276 ?>