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('OR' => _("or"), 'AND' => _("and"));
12 $this->actions = array(
13 "MOVE" => _("move mail to"),
14 "COPY" => _("copy mail to"),
15 "FORWARD" => _("forward message to"),
16 "MARK" => _("mark mail as "),
17 "DROP" => _("remove mail"),
18 "REPLY" => _("reply"));
20 $this->fields = array(
21 'subject' => _("Subject"),
22 'from'=> _("From"),
23 'body'=> _("Body"),
24 'date'=> _("Date"),
25 'priority'=> _("Priority"),
26 'status'=> _("Status"),
27 'to'=> _("To"),
28 'cc'=> _("CC"),
29 'age'=> _("Age"),
30 'size'=> _("Size"));
32 $this->comparators = array(
33 "is" => _("is"),
34 "is not" => _("is not"),
35 "equal" => _("is equal"),
36 "not equal" => _("is not equal"),
37 "is empty" => _("is empty"),
38 "is not empty" => _("is not empty"),
39 "contains" => _("contains"),
40 "contains not" => _("does not contain"),
41 "is in addressbook" => _("is in the addressbook"),
42 "is not in addressbook" => _("is not in the addressbook"),
43 "greater than" => _("is greater than"),
44 "less than" => _("smaller than"));
45 }
47 function execute()
48 {
49 $smarty = get_smarty();
50 $smarty->assign('NAME',set_post($this->filter['NAME']));
51 $smarty->assign('DESC',set_post($this->filter['DESC']));
52 $smarty->assign('filterStr', $this->renderFilter());
54 return($smarty->fetch(get_template_path('filterEditor.tpl',TRUE,dirname(__FILE__))));
55 }
57 function renderFilter()
58 {
59 $filter = $this->filter;
61 $cnt = count($filter['CONDITIONS']);
62 $str = "<h3>"._("Filter conditions")."</h3>";
63 $str .= _("Condition type")." \n<select name='cType' onChange='document.mainform.submit();'>";
64 foreach($this->types as $type => $desc){
65 $checked = ($type == $filter['TYPE'])? ' selected ' : '';
66 $str.= "\n<option {$checked} value=\"".set_post($type)."\">".set_post($desc)."</option>";
67 }
68 $str .= "\n</select>";
69 $str.= "<ul>";
70 foreach($filter['CONDITIONS'] as $id => $cond){
71 $str .= "<li>";
72 $str .= _("Check field")." ";
73 $str .= "\n<select name='cField_{$id}' onChange='document.mainform.submit();'>";
74 foreach($this->fields as $field => $desc){
75 $checked = ($field == $cond['FIELD'])? ' selected ' : '';
76 $str.= "\n<option {$checked} value=\"".set_post($field)."\">".set_post($desc)."</option>";
77 }
78 $str .= "\n</select>";
79 $str .= " "._("if it")." ";
80 $str .= "\n<select name='cComparator_{$id}' onChange='document.mainform.submit();'>";
81 foreach($this->comparators as $comparator => $desc){
82 $checked = ($comparator == $cond['COMPARATOR'])? ' selected ' : '';
83 $str.= "\n<option {$checked} value=\"".set_post($comparator)."\">".set_post($desc)."</option>";
84 }
85 $str .= "\n</select>";
86 if(!in_array($cond['COMPARATOR'], array('is in addressbook','is not in addressbook','is empty','is not empty'))){
87 $str .= "<input type='text' name='cMatch_{$id}' value=\"".set_post($cond['MATCH'])."\">";
88 }
89 $cnt --;
90 $str .= "</li>";
91 if($cnt) $str .= $this->types[$filter['TYPE']];
92 }
93 $str .= "</ul>";
94 $str .= "<button name='addCondition'>".msgPool::addButton()."</button> ";
95 if(count($filter['CONDITIONS']) >= 2){
96 $str .= "<button name='removeCondition'>".msgPool::delButton()."</button> ";
97 }
99 $str .= "<hr>";
100 $str .= "<h3>"._("Filter actions")."</h3>";
102 $str.= "<ul>";
103 foreach($filter['ACTION'] as $id => $action){
104 $str .= "<li>";
105 $str .= "\n<select name='cAction_{$id}' onChange='document.mainform.submit();'>";
106 foreach($this->actions as $act => $desc){
107 $checked = ($act == $action['ACTION'])? ' selected ' : '';
108 $str.= "\n<option {$checked} value=\"".set_post($act)."\">".set_post($desc)."</option>";
109 }
110 $str .= "</select>";
111 if(!in_array($action['ACTION'], array('DROP'))){
112 $str .= "<input type='text' name='cValue_{$id}' value=\"".set_post($action['VALUE'])."\">";
113 }
114 $str .= "</li>";
115 }
116 $str.= "</ul>";
118 $str .= "<button name='addAction'>".msgPool::addButton()."</button> ";
119 if(count($filter['ACTION']) >= 2){
120 $str .= "<button name='removeAction'>".msgPool::delButton()."</button> ";
121 }
123 return($str);
124 }
127 function save_object()
128 {
130 $this->filter['TYPE'] = (isset($_POST['cType']))? get_post('cType'): $this->filter['TYPE'];
132 foreach($this->filter['CONDITIONS'] as $id => $value){
133 foreach(array(
134 'cField_' => 'FIELD',
135 'cComparator_'=>'COMPARATOR',
136 'cMatch_'=>'MATCH') as $post => $name){
137 if(isset($_POST[$post.$id])){
138 $this->filter['CONDITIONS'][$id][$name] = get_post($post.$id);
139 }
140 }
141 }
142 foreach($this->filter['ACTION'] as $id => $value){
143 if(isset($_POST['cAction_'.$id])){
144 $this->filter['ACTION'][$id]['ACTION'] = get_post('cAction_'.$id);
145 }
146 if(isset($_POST['cValue_'.$id])){
147 $this->filter['ACTION'][$id]['VALUE'] = get_post('cValue_'.$id);
148 }
149 }
151 // Add / remove conditions
152 if(isset($_POST['removeCondition']) && count($this->filter['CONDITIONS']) >= 2){
153 array_pop($this->filter['CONDITIONS']);
154 }
155 if(isset($_POST['addCondition'])){
156 $this->filter['CONDITIONS'][] = array('FIELD' => key($this->fields),'COMPARATOR' => key($this->comparators), 'MATCH' => '');
157 }
159 // Add / remove actions
160 if(isset($_POST['removeAction']) && count($this->filter['ACTION']) >= 2){
161 array_pop($this->filter['ACTION']);
162 }
163 if(isset($_POST['addAction'])){
164 $this->filter['ACTION'][] = array('ACTION' => 'MOVE', 'VALUE' => '');
165 }
166 }
167 }
168 ?>