Code

Updated filter
[gosa.git] / gosa-core / include / class_userFilter.inc
1 <?php
3 class userFilter extends plugin 
4 {
5   protected $fitlers = array();
6   protected $availableCategories = array();
7   
8   public $objectClass = array('gosaProperties');
9   public $attributes = array('gosaUserDefinedFilter');
10   public $gosaUserDefinedFilter = array();
13   /*! \brief    Returns true if we are able to read and write userFilters 
14    *            (schema has to be present, gosaProperties)
15    */
16   static function userFilteringAvailable()
17   {
18     if(!session::is_set('userFilter::userFilteringAvailable')){
19       global $config;
20       $ldap = $config->get_ldap_link();
21       $ocs = $ldap->get_objectclasses();
22       session::set('userFilter::userFilteringAvailable', isset($ocs['gosaProperties']));
23     }
24     return(session::get('userFilter::userFilteringAvailable'));
25   }
26   
27  
28   /*! \brief  Initiates the filter editing dialog.
29    */ 
30   function __construct($config, $categories)
31   {
32     // Initialize this plugin with the users dn to gather user defined filters.
33     $ui = get_userinfo();
34     plugin::plugin($config, $ui->dn);
36     // Keep list of currently managed categories.
37     $this->availableCategories = array_unique($categories);
38     $this->availableCategories[] = 'systems';
39     $this->availableCategories[] = 'phones';
40     $this->availableCategories[] = 'printer';
41     $this->availableCategories[] = 'component';
43     // Load list of filters
44     if(isset($this->attrs['gosaUserDefinedFilter'])){
45       for($i=0; $i< $this->attrs['gosaUserDefinedFilter']['count']; $i++){
46         $tmp = userFilter::explodeFilterString($this->attrs['gosaUserDefinedFilter'][$i]);
47         if(isset($tmp['name'])){
48           $this->filters[$tmp['name']]= $tmp; 
49         }
50       }
51     }
53     // Create the filter list
54     $this->filterWidget= new sortableListing();
55     $this->filterWidget->setDeleteable(true);
56     $this->filterWidget->setEditable(true);
57     $this->filterWidget->setWidth("100%");
58     $this->filterWidget->setHeight("270px");
59     $this->filterWidget->setColspecs(array('100px', '200px', '100px', '70px','150px'));
60     $this->filterWidget->setAcl($ui->get_permissions($ui->dn,'users/user','gosaUserDefinedFilter'));
61     $this->filterWidget->setListData($this->filters, $this->convertFilterList());
62   }
64   
65   /*! \brief    Parses a filter string into an array.
66    */
67   static function explodeFilterString($filter)
68   {
69     list($categories, $name, $description, $filter, $flags) = split(";", $filter);
71     // Ensure that we no empty category in our category list.
72     if(empty($categories)){
73       $categories = array();
74     }else{
75       $categories = split(',', $categories);
76     }
78     // Ensure that we no empty entry in out flags list.
79     if(empty($flags)){
80       $flags = array();
81     }else{
82       $flags = split(',', $flags);
83     }
85     // build up filter entry.
86     $tmp = array(
87         'name' => $name, 
88         'categories' => $categories,
89         'description' => base64_decode($description),
90         'filter' => base64_decode($filter),
91         'flags' => $flags);
93     return($tmp);
94   }
95   
97   /*! \brief    Converts the list of filters ($this->filters) into data which is useable
98    *             for the sortableList object ($this->filterWidget).
99    *  @return   Array   An array containg data useable for sortableLists ($this->filterWidget)
100    */
101   function convertFilterList()
102   { 
103     $data = array();
104     foreach($this->filters as $name => $filter){
105       $data[$name] = array('data' =>
106           array(
107             $filter['name'],
108             $filter['description'],
109             implode(", ",$filter['categories']),
110             implode(", ",$filter['flags'])));
111     }
112     return($data); 
113   }
116   /*! \brief    Display the user-filter overview as HTML content.
117    *  @return   string    HTML-content showing the user-filter editing dialog.
118    */
119   function execute()
120   {
121     plugin::execute();
123     // Cancel filter modifications (edit dialog)
124     if(isset($_POST['cancelFilterSettings'])){
125       $this->dialog = NULL;
126     }
128     // Save modified filter entries (edit dialog)
129     if(isset($_POST['saveFilterSettings']) && $this->dialog instanceOf userFilterEditor){
130       $this->dialog->save_object();
131       $msgs = $this->dialog->check();
132       if(count($msgs)){
133         msg_dialog::displayChecks($msgs);
134       }else{
135         $orig_name = $this->dialog->getOriginalName();
136         $new_name = $this->dialog->getCurrentName();
138         // The object was renamed and
139         if($orig_name != $new_name && isset($this->filters[$new_name])){
140           $msgs = array(msgPool::duplicated(_("Name")));
141           msg_dialog::displayChecks($msgs);
142         }else{
144           // Remove old entry if filter was renamed
145           if($orig_name != "" && isset($this->filters[$orig_name])){
146             unset($this->filters[$orig_name]);
147           }
148           
149           // Now append the new filter object.
150           $this->filters[$new_name] = $this->dialog->save();
151           $this->dialog = NULL;
152           $this->filterWidget->setListData($this->filters, $this->convertFilterList());
153           $this->filterWidget->update();
154         }
155       }
156     }
158     // Act on edit requests 
159     $this->filterWidget->save_object();
160     $action = $this->filterWidget->getAction();
161     if($action['action'] == 'edit' && count($action['targets']) == 1){
162       $key= $this->filterWidget->getKey($action['targets'][0]);
163       if(isset($this->filters[$key])){
164         $this->dialog=new userFilterEditor($this->filters[$key], $this->availableCategories);
165       }
166     }
168     // Act on new requests
169     if(isset($_POST['addFilter'])){
170       $this->dialog=new userFilterEditor(array(), $this->availableCategories);
171     }
173     // Act on remove requests 
174     $action = $this->filterWidget->getAction();
175     if($action['action'] == 'delete' && count($action['targets']) == 1){
176       $key= $this->filterWidget->getKey($action['targets'][0]);
177       if(isset($this->filters[$key])){
178         unset($this->filters[$key]);
179         $this->filterWidget->update();
180       }
181     }
183     // Display edit dialog
184     if($this->dialog instanceOf userFilterEditor){
185       $this->dialog->save_object();
186       return($this->dialog->execute());
187     }
189     $smarty = get_smarty();
190     $smarty->assign("list", $this->filterWidget->render());
191     return($smarty->fetch(get_template_path('userFilter.tpl', FALSE)));
192   }
195   /*! \brief    Returns user defined filter for a given list of categories,
196    *             if no categories were specified all enabled filters will be returned.
197    */
198   static function getFilter($category=array())
199   {
200     global $config;
201     $ldap=$config->get_ldap_link();
202     $ui = get_userinfo();
203     $ldap->cd($config->current['BASE']);
204     $ldap->search("(&(objectClass=gosaProperties)(gosaUserDefinedFilter=*))",array('gosaUserDefinedFilter'));
205     $filter = array();
206     while($attrs = $ldap->fetch()){
207       for($i=0; $i < $attrs['gosaUserDefinedFilter']['count']; $i++){
208         $tmp = userFilter::explodeFilterString($attrs['gosaUserDefinedFilter'][$i]);
209         if(!isset($tmp['name'])) continue;
210            
211         // The filter is visible if it is shared or if is one of our own creations.
212         //  ... and enabled.
213         $visible = in_array('enable', $tmp['flags']) && 
214           ($attrs['dn'] == $ui->dn || in_array('share', $tmp['flags']));
215           
216         // Add filter if it matches the category list
217         if($visible && (count($category) == 0 || array_intersect($category, $tmp['categories']))){ 
218           $filter[$tmp['name']] = $tmp;
219         }
220       }
221     }
222     return($filter);
223   }
226   /*! \brief    Write user-filter modifications back to the ldap.  
227    */
228   function save()
229   {
230     // Build up new list of filters 
231     $attrs = array();
232     foreach($this->filters as $filter){
233       $tmp = implode(',', $filter['categories']).";";
234       $tmp.= $filter['name'].";";
235       $tmp.= base64_encode($filter['description']).";";
236       $tmp.= base64_encode($filter['filter']).";";
237       $tmp.= implode(',', $filter['flags']);
238       $attrs[] = $tmp;
239     }
240     $this->gosaUserDefinedFilter = $attrs;
242     plugin::save();
244     $ldap = $this->config->get_ldap_link();
245     $ldap->cd($this->dn);
246     $ldap->modify($this->attrs);
247     
248     new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
250     if (!$ldap->success()){
251       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MODIFY, get_class()));
252     }
253   }  
255   
256   /*! \brief    Do not save any posted values here.
257    */
258   function save_object(){}
261 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
262 ?>