Code

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