Code

Updated filter automatic indent
[gosa.git] / gosa-core / include / class_userFilterEditor.inc
1 <?php
3 class userFilterEditor extends plugin 
4 {
5   public $pathTitle= "Edit";
7   // The original entry else array(), allows us to perform existence checks.
8   public $entry = array();
10   // The values
11   public $name = "";
12   public $description = "";
13   public $parent = "";
14   public $selectedCategories = array();
15   public $share = FALSE;
16   public $enabled = TRUE;
17   public $filter = "(objectClass=*)";
19   // The list of all categories mangaged by the current filter object.
20   // Used in the grop-down box.
21   public $availableCategories = array();
22   public $fixedFilters = array();
23   public $orig_name = "";
26   /*! \brief    Instantiate the filter editing dialog. 
27    *            Parses the filter info into editable data.
28    */
29   function __construct($entry, $categories, $fixedFilters)
30   {
31     $this->availableCategories = $categories;
32     $this->fixedFilters = $fixedFilters;
33     if($entry){
34       $this->entry = $entry;
35       $this->parent = $entry['parent'];
36       $this->name = $entry['name'];
37       $this->description = $entry['description'];
38       $this->filter = userFilterEditor::_autoIndentFilter($entry['filter'], "  ");
39       $this->selectedCategories = $entry['categories'];
40       $this->share = in_array("share",$entry['flags']);
41       $this->enable = in_array("enable",$entry['flags']);
42     }
43     $this->orig_name = $this->name;
44   }
47   /*! \brief    Automatic indent indentation for filters.
48    */
49   static function _autoIndentFilter($str, $indent = " ")
50   {
51     // Remove line breaks and escaped brackets 
52     $str = preg_replace('/[\t ]*\n[\t ]*/', "", $str);
53     $str = preg_replace('/\\\\\\(/', "::OPEN::", $str);
54     $str = preg_replace('/\\\\\\)/', "::CLOSE::", $str);
55    
56     // Add a line break infront of every bracket 
57     $str = preg_replace('/\\(/', "\n(", $str);
58     $str = preg_replace('/\\)/', ")\n", $str);
60     // Split by linebreaks
61     $lines = preg_split("/\n/", $str);
62     $str = "";
63     $i = 0;
64  
65     // Walk trough search blocks 
66     foreach($lines as $line){
67       $line = trim($line);
68       if(empty($line)) continue;
70       // Go back one level in indentation  
71       if(!preg_match("/\\(.*\\)/", $line) && preg_match('/\\)$/', $line)){
72         $i --;
73       }
75       $str.= "\n";
76       $str = str_pad($str,strlen($str)+$i, $indent); 
77       $str.= $line;
79       // Go one level deeper in indentation 
80       if(!preg_match("/\\(.*\\)/", $line) && preg_match('/^\\(/', $line)){
81         $i ++;
82       }
83     }
84     $str = preg_replace('/::OPEN::/', '\(', $str);
85     $str = preg_replace('/::CLOSE::/', '\)', $str);
86     return($str);
87   }
90   /*! \brief    Retunrs the filters original name 
91    *  @param    The original name of the filter (if none was given 
92    *             an empty string is returned)
93    */
94   function getOriginalName()
95   {
96     return($this->orig_name);
97   }
100   /*! \brief    Retunrs the filters name.
101    *  @param    The name of the filter
102    */
103   function getCurrentName()
104   {
105     return($this->name);
106   }
109   /*! \brief    Generates the <HTML> content, to edit the filter settings.
110    *  @return   String  HTML form.
111    */
112   function execute()
113   {
114     plugin::execute();
115     $smarty = get_smarty();
116     $smarty->assign('parent', $this->parent);
117     $smarty->assign('name', htmlentities($this->name,ENT_COMPAT,'UTF-8'));
118     $smarty->assign('filter', htmlentities($this->filter,ENT_COMPAT,'UTF-8'));
119     $smarty->assign('share', $this->share);
120     $smarty->assign('enable', $this->enabled);
121     $smarty->assign('description', htmlentities($this->description,ENT_COMPAT,'UTF-8'));
122     $smarty->assign('selectedCategories', $this->selectedCategories);
123     $smarty->assign('availableCategories', $this->availableCategories);
124     $smarty->assign('fixedFilters', $this->fixedFilters);
125     return($smarty->fetch(get_template_path('userFilterEditor.tpl', FALSE)));
126   }
129   /*! \brief    Keep values entered in the input form of the dialog. (POST/GET)
130    */
131   function save_object()
132   {
133     if(isset($_POST['userFilterEditor'])){
135       // Get posted strings
136       foreach(array('name','description', 'parent') as $attr){
137         if(isset($_POST[$attr])){
138           $this->$attr = get_post($attr);
139         }
140       }
142       // Filter needs special handling, it may contain charactes like < and >
143       //  wich are stipped out by get_post() && validate()
144       if(isset($_POST['filter'])){
145         $f = mb_convert_encoding($_POST['filter'], 'UTF-8');
146         if(get_magic_quotes_gpc()){
147           $f = stripcslashes($f);
148         }
149         $this->filter = $f;
150       }
152       // Get posted flags 
153       $this->share = isset($_POST['shareFilter']);
154       $this->enable = isset($_POST['enableFilter']);
156       // Get additional category  
157       if(isset($_POST['addCategory'])){
158         if(isset($_POST['manualCategory']) && !empty($_POST['manualCategory'])){
159           $this->selectedCategories[] = get_post('manualCategory');
160         }elseif(isset($_POST['availableCategory']) && !empty($_POST['availableCategory'])){
161           $this->selectedCategories[] = get_post('availableCategory');
162         }
163       }
165       // Remove categories
166       if(isset($_POST['delCategory']) && isset($_POST['usedCategory'])){
167         foreach($_POST['usedCategory'] as $cat){
168           if(isset($this->selectedCategories[$cat])) unset($this->selectedCategories[$cat]);
169         }
170       }
171     }
172   }
174   
175   /*! \brief    Validate user input 
176    *  @return   Array   An Array containing potential error messages
177    */
178   function check()
179   {
180     $msgs = plugin::check();
181   
182     // Check if the name is given
183     if(empty($this->name)){
184       $msgs[] = msgPool::required(_("Name"));
185     }elseif(preg_match("/[^a-z0-9]/i", $this->name)){
186       
187       // Check for a valid name, no special chars here - in particular no ; 
188       $msgs[] = msgPool::invalid(_("Name"), $this->name,"/[a-z0-9]/i");
189     }  
191     // Description is a must value.
192     if(empty($this->description)){
193       $msgs[] = msgPool::required(_("Description"));
194     }
196     // Count the number of opening and closing brackets - exclude escaped ones.
197     $f = preg_replace('/\\\\[\(\)]/',"",$this->filter);
198     $o = substr_count($f, '(');
199     $c = substr_count($f, ')');
200     if($o != $c){
201       $msgs[] = sprintf(_("Please check your filter. You have '%s' opening and '%s' closing brackets!"), $o, $c);
202     }
204     return($msgs);
205   }
208   /*! \brief    Transforms the entered values into a filter object (array) which is useable
209    *             for the userFilter overview dialog.
210    *  @return   Returns transformed filter data.
211    */
212   function save()
213   {
214     $ret= array();
215     $ret['parent'] = $this->parent;
216     $ret['name'] = $this->name;
217     $ret['description'] = $this->description;
218     $ret['categories'] = $this->selectedCategories;
219     $ret['filter'] = $this->filter;
220     $ret['flags'] = array();
221     if($this->share){
222       $ret['flags'][] = "share";
223     }
224     if($this->enable){
225       $ret['flags'][] = "enable";
226     }
227     return($ret);
228   }
231 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
232 ?>