Code

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