Code

5e34c36f1b5cd78467f28386effe7dd8a2628ba0
[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);
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();
51     $this->filterWidget->setDeleteable(true);
52     $this->filterWidget->setEditable(true);
53     $this->filterWidget->setWidth("100%");
54     $this->filterWidget->setHeight("270px");
55     $this->filterWidget->setColspecs(array('100px', '200px', '100px', '70px','150px'));
56     $this->filterWidget->setAcl($ui->get_permissions($ui->dn,'users/user','gosaUserDefinedFilter'));
57     $this->filterWidget->setListData($this->filters, $this->convertFilterList());
58   }
60   
61   /*! \brief    Parses a filter string into an array.
62    */
63   static function explodeFilterString($filter)
64   {
65     list($categories, $name, $description, $filter, $flags) = split(";", $filter);
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 = split(',', $flags);
79     }
81     // build up filter entry.
82     $tmp = array(
83         'name' => $name, 
84         'categories' => $categories,
85         'description' => base64_decode($description),
86         'filter' => base64_decode($filter),
87         'flags' => $flags);
89     return($tmp);
90   }
91   
93   /*! \brief    Converts the list of filters ($this->filters) into data which is useable
94    *             for the sortableList object ($this->filterWidget).
95    *  @return   Array   An array containg data useable for sortableLists ($this->filterWidget)
96    */
97   function convertFilterList()
98   { 
99     $data = array();
100     foreach($this->filters as $name => $filter){
101       $data[$name] = array('data' =>
102           array(
103             $filter['name'],
104             $filter['description'],
105             implode(", ",$filter['categories']),
106             implode(", ",$filter['flags'])));
107     }
108     return($data); 
109   }
112   /*! \brief    Display the user-filter overview as HTML content.
113    *  @return   string    HTML-content showing the user-filter editing dialog.
114    */
115   function execute()
116   {
117     plugin::execute();
119     // Cancel filter modifications (edit dialog)
120     if(isset($_POST['cancelFilterSettings'])){
121       $this->dialog = NULL;
122     }
124     // Save modified filter entries (edit dialog)
125     if(isset($_POST['saveFilterSettings']) && $this->dialog instanceOf userFilterEditor){
126       $this->dialog->save_object();
127       $msgs = $this->dialog->check();
128       if(count($msgs)){
129         msg_dialog::displayChecks($msgs);
130       }else{
131         $orig_name = $this->dialog->getOriginalName();
132         $new_name = $this->dialog->getCurrentName();
134         // The object was renamed and
135         if($orig_name != $new_name && isset($this->filters[$new_name])){
136           $msgs = array(msgPool::duplicated(_("Name")));
137           msg_dialog::displayChecks($msgs);
138         }else{
140           // Remove old entry if filter was renamed
141           if($orig_name != "" && isset($this->filters[$orig_name])){
142             unset($this->filters[$orig_name]);
143           }
144           
145           // Now append the new filter object.
146           $this->filters[$new_name] = $this->dialog->save();
147           $this->dialog = NULL;
148           $this->filterWidget->setListData($this->filters, $this->convertFilterList());
149           $this->filterWidget->update();
150         }
151       }
152     }
154     // Act on edit requests 
155     $this->filterWidget->save_object();
156     $action = $this->filterWidget->getAction();
157     if($action['action'] == 'edit' && count($action['targets']) == 1){
158       $key= $this->filterWidget->getKey($action['targets'][0]);
159       if(isset($this->filters[$key])){
160         $this->dialog=new userFilterEditor($this->filters[$key], $this->availableCategories);
161       }
162     }
164     // Act on new requests
165     if(isset($_POST['addFilter'])){
166       $this->dialog=new userFilterEditor(array(), $this->availableCategories);
167     }
169     // Act on remove requests 
170     $action = $this->filterWidget->getAction();
171     if($action['action'] == 'delete' && count($action['targets']) == 1){
172       $key= $this->filterWidget->getKey($action['targets'][0]);
173       if(isset($this->filters[$key])){
174         unset($this->filters[$key]);
175         $this->filterWidget->update();
176       }
177     }
179     // Display edit dialog
180     if($this->dialog instanceOf userFilterEditor){
181       $this->dialog->save_object();
182       return($this->dialog->execute());
183     }
185     $smarty = get_smarty();
186     $smarty->assign("list", $this->filterWidget->render());
187     return($smarty->fetch(get_template_path('userFilter.tpl', FALSE)));
188   }
191   /*! \brief    Returns user defined filter for a given list of categories,
192    *             if no categories were specified all enabled filters will be returned.
193    */
194   static function getFilter($category=array())
195   {
196     global $config;
197     $ldap=$config->get_ldap_link();
198     $ui = get_userinfo();
199     $ldap->cd($config->current['BASE']);
200     $ldap->search("(&(objectClass=gosaProperties)(gosaUserDefinedFilter=*))",array('gosaUserDefinedFilter'));
201     $filter = array();
202     while($attrs = $ldap->fetch()){
203       for($i=0; $i < $attrs['gosaUserDefinedFilter']['count']; $i++){
204         $tmp = userFilter::explodeFilterString($attrs['gosaUserDefinedFilter'][$i]);
205         if(!isset($tmp['name'])) continue;
206           
207         // Remove line breaks from the filter, which may were added for better reading. 
208         $c = preg_split('/\n/',$tmp['filter']);
209         foreach($c as $key => $str) $c[$key] = trim($str);
210         $tmp['filter'] = implode($c);
211  
212         // The filter is visible if it is shared or if is one of our own creations.
213         //  ... and enabled.
214         $visible = in_array('enable', $tmp['flags']) && 
215           ($attrs['dn'] == $ui->dn || in_array('share', $tmp['flags']));
216           
217         // Add filter if it matches the category list
218         if($visible && (count($category) == 0 || array_intersect($category, $tmp['categories']))){ 
219           $filter[$tmp['name']] = $tmp;
220         }
221       }
222     }
223     return($filter);
224   }
227   /*! \brief    Write user-filter modifications back to the ldap.  
228    */
229   function save()
230   {
231     // Build up new list of filters 
232     $attrs = array();
233     foreach($this->filters as $filter){
234       $tmp = implode(',', $filter['categories']).";";
235       $tmp.= $filter['name'].";";
236       $tmp.= base64_encode($filter['description']).";";
237       $tmp.= base64_encode($filter['filter']).";";
238       $tmp.= implode(',', $filter['flags']);
239       $attrs[] = $tmp;
240     }
241     $this->gosaUserDefinedFilter = $attrs;
243     plugin::save();
245     $ldap = $this->config->get_ldap_link();
246     $ldap->cd($this->dn);
247     $ldap->modify($this->attrs);
248     
249     new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
251     if (!$ldap->success()){
252       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MODIFY, get_class()));
253     }
254   }  
256   
257   /*! \brief    Do not save any posted values here.
258    */
259   function save_object(){}
262 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
263 ?>