Code

9f39d6effe741127261915be8bc3f7bb98efd796
[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());    
56         $smarty->assign('acl', $this->parent->getacl('mailFilter'));    
58         return($smarty->fetch(get_template_path('filterEditor.tpl',TRUE,dirname(__FILE__))));
59     }
63     function check()
64     {
65         $msgs = array();
66         return($msgs);
67     }
70     function save()
71     {
72         // Just return the filter array we've modified
73         return($this->filter);
74     }
77     function renderFilter()
78     {
79         $filter = $this->filter;
81         $cnt = count($filter['CONDITIONS']);
82         $str = "<h3>"._("Filter conditions")."</h3>";
83         $str .= _("Condition type")."&nbsp;";
84         $str .="\n<select size='1' name='cType' onChange='document.mainform.submit();'>";
85         foreach($this->types as $type => $desc){
86             $checked = ($type == $filter['TYPE'])? ' selected ' : '';
87             $str.= "\n<option {$checked} value=\"".set_post($type)."\">".set_post($desc)."</option>";
88         }
89         $str .= "\n</select>";
90         $str.= "<ul>";
91         foreach($filter['CONDITIONS'] as $id => $cond){
92             $str .= "<li>";
93 #            $str .= _("Check field")."&nbsp;";
94             $str .= "\n<select size='1' style='width:100px;' name='cField_{$id}' onChange='document.mainform.submit();'>";
95             foreach($this->fields as $field => $desc){
96                 $checked = ($field == $cond['FIELD'])? ' selected ' : '';
97                 $str.= "\n<option {$checked} value=\"".set_post($field)."\">".set_post($desc)."</option>";
98             }
99             $str .= "\n</select>";
100 #            $str .= "&nbsp;"._("if it")."&nbsp;";
101             $str .= "\n<select size='1' style='width:200px;' name='cComparator_{$id}' onChange='document.mainform.submit();'>";
102             foreach($this->comparators as $comparator => $desc){
103                 $checked = ($comparator == $cond['COMPARATOR'])? ' selected ' : '';
104                 $str.= "\n<option {$checked} value=\"".set_post($comparator)."\">".set_post($desc)."</option>";
105             }
106             $str .= "\n</select>&nbsp;";
107             if(!in_array($cond['COMPARATOR'], array('is in addressbook','is not in addressbook','is empty','is not empty'))){
108                 $str .= "<input style='width:400px;' type='text' name='cMatch_{$id}' value=\"".set_post($cond['MATCH'])."\">";
109             }
110             if(count($filter['CONDITIONS']) >= 2){
111                 $str .= "<button name='removeCondition_{$id}'>".msgPool::delButton()."</button> ";
112             }
113             $cnt --;
114             $str .= "</li>";
115 #            if($cnt) $str .= $this->types[$filter['TYPE']];
116         } 
117         $str .= "</ul>";
118         $str .= "<button style='margin-left:40px;' name='addCondition'>".msgPool::addButton()."</button> ";
119        
120         $str .= "<hr>";
121         $str .= "<h3>"._("Filter actions")."</h3>";
123         $str.= "<ul>";
124         foreach($filter['ACTION'] as $id => $action){
125             $str .= "<li>";
126             $str .= "\n<select style='width:200px;' size='1' name='cAction_{$id}' onChange='document.mainform.submit();'>";
127             foreach($this->actions as $act => $desc){
128                 $checked = ($act == $action['ACTION'])? ' selected ' : '';
129                 $str.= "\n<option {$checked} value=\"".set_post($act)."\">".set_post($desc)."</option>";
130             }
131             $str .= "</select>&nbsp;";
132             if(!in_array($action['ACTION'], array('DROP'))){
133                 $str .= "<input style='width:500px;' type='text' name='cValue_{$id}' value=\"".set_post($action['VALUE'])."\">";
134             }
136             if(count($filter['ACTION']) >= 2){
137                 $str .= "<button name='removeAction_{$id}'>".msgPool::delButton()."</button>";
138             }
139             $str .= "</li>";
140         }
141         $str.= "</ul>";
142         $str .= "<button style='margin-left:40px;' name='addAction'>".msgPool::addButton()."</button> ";
144         return($str);
145     }
148     function save_object()
149     {
150         // Do nothing if the dialog wasn't submitted yet
151         if(!isset($_POST['filterEditorPosted'])) return;
153         // Get the filter confition type if posted
154         $this->filter['TYPE'] = (isset($_POST['cType']))? get_post('cType'): $this->filter['TYPE'];
156         // Get condition modifications
157         foreach($this->filter['CONDITIONS'] as $id => $value){
158             foreach(array('cField_' => 'FIELD',
159                         'cComparator_'=>'COMPARATOR',
160                         'cMatch_'=>'MATCH') as $post => $name){
161                 if(isset($_POST[$post.$id])){
162                     $this->filter['CONDITIONS'][$id][$name] = get_post($post.$id);
163                 }
164             }
165             if(isset($_POST['removeCondition_'.$id]) && count($this->filter['CONDITIONS']) >= 2){
166                 unset($this->filter['CONDITIONS'][$id]);
167             }
168         }
170         // Get Action modifications 
171         foreach($this->filter['ACTION'] as $id => $value){
172             foreach(array('cAction_' => 'ACTIOB','cValue_' => 'VALUE') as $post => $name){
173                 if(isset($_POST[$post.$id])){
174                     $this->filter['ACTION'][$id][$name] = get_post($post.$id);
175                 }
176             }
177             if(isset($_POST['removeAction_'.$id]) && count($this->filter['ACTION']) >= 2){
178                 unset($this->filter['ACTION'][$id]);
179             }
180         }
182         // Get NAME and DESC if posted
183         if(isset($_POST['NAME'])) $this->filter['NAME'] = get_post('NAME');
184         if(isset($_POST['DESC'])) $this->filter['DESC'] = get_post('DESC');
186         // Add conditions
187         if(isset($_POST['addCondition'])){
188             $this->filter['CONDITIONS'][] = array('FIELD' => key($this->fields),'COMPARATOR' => key($this->comparators), 'MATCH' => '');
189         }
190     
191         // Add actions
192         if(isset($_POST['addAction'])){
193             $this->filter['ACTION'][] = array('ACTION' => 'MOVE', 'VALUE' => '');
194         }
195     }
197 ?>