Code

Allow to modify filters
[gosa.git] / gosa-plugins / groupware / personal / groupware / class_filterEditor.inc
1 <?php
3 class filterEditor extends plugin
4 {
5     function __construct($config, $parent, $filter)
6     {
7         $this->config = &$config;
8         $this->parent = &$parent;
9         $this->filter = $filter;
11         $this->types = array(
12                 'OR' => _("At least one of the following conditions has to match"), 
13                 'AND' => _("All conditions have to match"));
15         $this->actions = array(
16                 "MOVE" => _("move mail to"),
17                 "COPY" => _("copy mail to"),
18                 "FORWARD" => _("forward message to"),
19                 "MARK" => _("mark mail as "),
20                 "DROP" => _("remove mail"),
21                 "REPLY" => _("reply"));
23         $this->fields = array(
24                 'subject' => _("Subject"),
25                 'from'=> _("From"),
26                 'body'=> _("Body"),
27                 'date'=> _("Date"),
28                 'priority'=> _("Priority"),
29                 'status'=> _("Status"),
30                 'to'=> _("To"),
31                 'cc'=> _("CC"),
32                 'age'=> _("Age"),
33                 'size'=> _("Size"));
35         $this->comparators = array(
36                 "is" => _("is"),
37                 "is not" => _("is not"), 
38                 "equal" => _("is equal"),       
39                 "not equal" => _("is not equal"),   
40                 "is empty" => _("is empty"),
41                 "is not empty" => _("is not empty"),
42                 "contains" => _("contains"),
43                 "contains not" => _("does not contain"),
44                 "is in addressbook" => _("is in the addressbook"),
45                 "is not in addressbook" => _("is not in the addressbook"),
46                 "greater than" => _("is greater than"),
47                 "less than" => _("smaller than"));
48     }
50     function execute()
51     {
52         $smarty = get_smarty();
53         $smarty->assign('NAME',set_post($this->filter['NAME']));    
54         $smarty->assign('DESC',set_post($this->filter['DESC']));    
55         $smarty->assign('filterStr', $this->renderFilter());    
57         return($smarty->fetch(get_template_path('filterEditor.tpl',TRUE,dirname(__FILE__))));
58     }
62     function check()
63     {
64         $msgs = array();
65         return($msgs);
66     }
69     function save()
70     {
71         // Just return the filter array we've modified
72         return($this->filter);
73     }
76     function renderFilter()
77     {
78         $filter = $this->filter;
80         $cnt = count($filter['CONDITIONS']);
81         $str = "<h3>"._("Filter conditions")."</h3>";
82         $str .= _("Condition type")."&nbsp;";
83         $str .="\n<select size='1' name='cType' onChange='document.mainform.submit();'>";
84         foreach($this->types as $type => $desc){
85             $checked = ($type == $filter['TYPE'])? ' selected ' : '';
86             $str.= "\n<option {$checked} value=\"".set_post($type)."\">".set_post($desc)."</option>";
87         }
88         $str .= "\n</select>";
89         $str.= "<ul>";
90         foreach($filter['CONDITIONS'] as $id => $cond){
91             $str .= "<li>";
92 #            $str .= _("Check field")."&nbsp;";
93             $str .= "\n<select size='1' style='width:100px;' name='cField_{$id}' onChange='document.mainform.submit();'>";
94             foreach($this->fields as $field => $desc){
95                 $checked = ($field == $cond['FIELD'])? ' selected ' : '';
96                 $str.= "\n<option {$checked} value=\"".set_post($field)."\">".set_post($desc)."</option>";
97             }
98             $str .= "\n</select>";
99 #            $str .= "&nbsp;"._("if it")."&nbsp;";
100             $str .= "\n<select size='1' style='width:200px;' name='cComparator_{$id}' onChange='document.mainform.submit();'>";
101             foreach($this->comparators as $comparator => $desc){
102                 $checked = ($comparator == $cond['COMPARATOR'])? ' selected ' : '';
103                 $str.= "\n<option {$checked} value=\"".set_post($comparator)."\">".set_post($desc)."</option>";
104             }
105             $str .= "\n</select>&nbsp;";
106             if(!in_array($cond['COMPARATOR'], array('is in addressbook','is not in addressbook','is empty','is not empty'))){
107                 $str .= "<input style='width:400px;' type='text' name='cMatch_{$id}' value=\"".set_post($cond['MATCH'])."\">";
108             }
109             $cnt --;
110             $str .= "</li>";
111 #            if($cnt) $str .= $this->types[$filter['TYPE']];
112         } 
113         $str .= "</ul>";
114         $str .= "<button style='margin-left:40px;' name='addCondition'>".msgPool::addButton()."</button> ";
115         if(count($filter['CONDITIONS']) >= 2){
116             $str .= "<button name='removeCondition'>".msgPool::delButton()."</button> ";
117         }
118        
119         $str .= "<hr>";
120         $str .= "<h3>"._("Filter actions")."</h3>";
122         $str.= "<ul>";
123         foreach($filter['ACTION'] as $id => $action){
124             $str .= "<li>";
125             $str .= "\n<select style='width:200px;' size='1' name='cAction_{$id}' onChange='document.mainform.submit();'>";
126             foreach($this->actions as $act => $desc){
127                 $checked = ($act == $action['ACTION'])? ' selected ' : '';
128                 $str.= "\n<option {$checked} value=\"".set_post($act)."\">".set_post($desc)."</option>";
129             }
130             $str .= "</select>&nbsp;";
131             if(!in_array($action['ACTION'], array('DROP'))){
132                 $str .= "<input style='width:500px;' type='text' name='cValue_{$id}' value=\"".set_post($action['VALUE'])."\">";
133             }
134             $str .= "</li>";
135         }
136         $str.= "</ul>";
137     
138         $str .= "<button style='margin-left:40px;' name='addAction'>".msgPool::addButton()."</button> ";
139         if(count($filter['ACTION']) >= 2){
140             $str .= "<button name='removeAction'>".msgPool::delButton()."</button> ";
141         }
143         return($str);
144     }
147     function save_object()
148     {
149         // Do nothing if the dialog wasn't submitted yet
150         if(!isset($_POST['filterEditorPosted'])) return;
152         // Get the filter confition type if posted
153         $this->filter['TYPE'] = (isset($_POST['cType']))? get_post('cType'): $this->filter['TYPE'];
155         // Get condition modifications
156         foreach($this->filter['CONDITIONS'] as $id => $value){
157             foreach(array('cField_' => 'FIELD',
158                         'cComparator_'=>'COMPARATOR',
159                         'cMatch_'=>'MATCH') as $post => $name){
160                 if(isset($_POST[$post.$id])){
161                     $this->filter['CONDITIONS'][$id][$name] = get_post($post.$id);
162                 }
163             }
164         }
166         // Get Action modifications 
167         foreach($this->filter['ACTION'] as $id => $value){
168             foreach(array('cAction_' => 'ACTIOB','cValue_' => 'VALUE') as $post => $name){
169                 if(isset($_POST[$post.$id])){
170                     $this->filter['ACTION'][$id][$name] = get_post($post.$id);
171                 }
172             }
173         }
175         // Get NAME and DESC if posted
176         if(isset($_POST['NAME'])) $this->filter['NAME'] = get_post('NAME');
177         if(isset($_POST['DESC'])) $this->filter['DESC'] = get_post('DESC');
179         // Add / remove conditions
180         if(isset($_POST['removeCondition']) && count($this->filter['CONDITIONS']) >= 2){
181             array_pop($this->filter['CONDITIONS']);
182         }
183         if(isset($_POST['addCondition'])){
184             $this->filter['CONDITIONS'][] = array('FIELD' => key($this->fields),'COMPARATOR' => key($this->comparators), 'MATCH' => '');
185         }
186     
187         // Add / remove actions
188         if(isset($_POST['removeAction']) && count($this->filter['ACTION']) >= 2){
189             array_pop($this->filter['ACTION']);
190         }
191         if(isset($_POST['addAction'])){
192             $this->filter['ACTION'][] = array('ACTION' => 'MOVE', 'VALUE' => '');
193         }
194     }
196 ?>