Code

Fixed encoding problems
[gosa.git] / gosa-core / include / class_userFilterEditor.inc
1 <?php
3 class userFilterEditor extends plugin 
4 {
5   // The original entry else array(), allows us to perform existence checks.
6   public $entry = array();
8   // The values
9   public $name = "";
10   public $description = "";
11   public $selectedCategories = array();
12   public $share = FALSE;
13   public $enabled = TRUE;
14   public $filter = "(objectClass=*)";
16   // The list of all categories mangaged by the current filter object.
17   // Used in the grop-down box.
18   public $availableCategories = array();
19   public $orig_name = "";
22   /*! \brief    Instantiate the filter editing dialog. 
23    *            Parses the filter info into editable data.
24    */
25   function __construct($entry, $categories)
26   {
27     $this->availableCategories = $categories;
28     if($entry){
29       $this->entry = $entry;
30       $this->name = $entry['name'];
31       $this->description = $entry['description'];
32       $this->filter = $entry['filter'];
33       $this->selectedCategories = $entry['categories'];
34       $this->share = in_array("share",$entry['flags']);
35       $this->enable = in_array("enable",$entry['flags']);
36     }
37     $this->orig_name = $this->name;
38   }
41   /*! \brief    Retunrs the filters original name 
42    *  @param    The original name of the filter (if none was given 
43    *             an empty string is returned)
44    */
45   function getOriginalName()
46   {
47     return($this->orig_name);
48   }
51   /*! \brief    Retunrs the filters name.
52    *  @param    The name of the filter
53    */
54   function getCurrentName()
55   {
56     return($this->name);
57   }
60   /*! \brief    Generates the <HTML> content, to edit the filter settings.
61    *  @return   String  HTML form.
62    */
63   function execute()
64   {
65     plugin::execute();
66     $smarty = get_smarty();
67     $smarty->assign('name', htmlentities($this->name,ENT_COMPAT,'UTF-8'));
68     $smarty->assign('filter', htmlentities($this->filter,ENT_COMPAT,'UTF-8'));
69     $smarty->assign('share', $this->share);
70     $smarty->assign('enable', $this->enabled);
71     $smarty->assign('description', htmlentities($this->description,ENT_COMPAT,'UTF-8'));
72     $smarty->assign('selectedCategories', $this->selectedCategories);
73     $smarty->assign('availableCategories', $this->availableCategories);
74     return($smarty->fetch(get_template_path('userFilterEditor.tpl', FALSE)));
75   }
78   /*! \brief    Keep values entered in the input form of the dialog. (POST/GET)
79    */
80   function save_object()
81   {
82     if(isset($_POST['userFilterEditor'])){
84       // Get posted strings
85       foreach(array('name','description') as $attr){
86         if(isset($_POST[$attr])){
87           $this->$attr = get_post($attr);
88         }
89       }
91       // Filter needs special handling, it may contain charactes like < and >
92       //  wich are stipped out by get_post() && validate()
93       if(isset($_POST['filter'])){
94         $f = $_POST['filter'];
95         if(get_magic_quotes_gpc()){
96           $f = stripcslashes($f);
97         }
98         $f = mb_convert_encoding($_POST['filter'], 'UTF-8');
99         $this->filter = $f;
100       }
102       // Get posted flags 
103       $this->share = isset($_POST['shareFilter']);
104       $this->enable = isset($_POST['enableFilter']);
106       // Get additional category  
107       if(isset($_POST['addCategory'])){
108         if(isset($_POST['manualCategory']) && !empty($_POST['manualCategory'])){
109           $this->selectedCategories[] = get_post('manualCategory');
110         }elseif(isset($_POST['availableCategory']) && !empty($_POST['availableCategory'])){
111           $this->selectedCategories[] = get_post('availableCategory');
112         }
113       }
115       // Remove categories
116       if(isset($_POST['delCategory']) && isset($_POST['usedCategory'])){
117         foreach($_POST['usedCategory'] as $cat){
118           if(isset($this->selectedCategories[$cat])) unset($this->selectedCategories[$cat]);
119         }
120       }
121     }
122   }
124   
125   /*! \brief    Validate user input 
126    *  @return   Array   An Array containing potential error messages
127    */
128   function check()
129   {
130     $msgs = plugin::check();
131   
132     // Check if the name is given
133     if(empty($this->name)){
134       $msgs[] = msgPool::required(_("Name"));
135     }elseif(preg_match("/[^a-z0-9\-_ ]/i", $this->name)){
136       
137       // Check for a valid name, no special chars here - in particular no ; 
138       $msgs[] = msgPool::invalid(_("Name"), $this->name,"/[a-z0-9\-_ ]/i");
139     }  
141     // Count the number of opening and closing brackets - exclude escaped ones.
142     $f = preg_replace('/\\\\[\(\)]/',"",$this->filter);
143     $o = substr_count($f, '(');
144     $c = substr_count($f, ')');
145     if($o != $c){
146       $msgs[] = sprintf(_("Please check your filter, you have '%s' opening and '%s' closing brackets!"), $o, $c);
147     }
149     return($msgs);
150   }
153   /*! \brief    Transforms the entered values into a filter object (array) which is useable
154    *             for the userFilter overview dialog.
155    *  @return   Returns transformed filter data.
156    */
157   function save()
158   {
159     $ret= array();
160     $ret['name'] = $this->name;
161     $ret['description'] = $this->description;
162     $ret['categories'] = $this->selectedCategories;
163     $ret['filter'] = $this->filter;
164     $ret['flags'] = array();
165     if($this->share){
166       $ret['flags'][] = "share";
167     }
168     if($this->enable){
169       $ret['flags'][] = "enable";
170     }
171     return($ret);
172   }
175 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
176 ?>