Code

Updated filter handling
[gosa.git] / gosa-plugins / groupware / personal / groupware / class_filterManager.inc
1 <?php
3 class filterManager extends plugin{
5     var $filterListing   = NULL;
6     var $filterEditor = NULL;
8     function __construct($config,$parent,$rules)
9     {
10         plugin::plugin($config);
11         
12         $this->parent = &$parent;
14         $this->filter = $rules;
15         $this->filterListing= new sortableListing();
16         $this->filterListing->setDeleteable(true);
17         $this->filterListing->setEditable(true);
18         $this->filterListing->setColspecs(array('*'));
19         $this->filterListing->setWidth("100%");
20         $this->filterListing->setHeight("150px;");
21         $this->filterListing->setAcl($this->parent->getacl('mailFilter'));
22     }
24     function execute()
25     {
26         // Display filter editor while a filter rule is edited
27         if($this->filterEditor instanceOf filterEditor){
28             $this->filterEditor->save_object();
29             return($this->filterEditor->execute());
30         }
31         
33         $smarty = get_smarty();
34         $data = $lData = array();
35         foreach($this->filter as $key => $filter){
36             $data[$key] = $filter;
38             switch($filter['STATUS']){
39                 case 'NEW' : $img = image('images/lists/edit.png[new]');break;
40                 case 'MODIFIED' : $img = image('images/lists/edit.png');break;
41                 case 'EXISTS' : $img = image('images/lists/edit.png');break;
42                 default : $img = "";
43             }
45             $lData[$key] = array('data' => array($img,$filter['NAME'], $filter['DESC']));
46         }
47         $this->filterListing->setListData($data,$lData);
48         $this->filterListing->update();
52         $smarty->assign('list', $this->filterListing->render());
54         return($smarty->fetch(get_template_path('filterManager.tpl',TRUE,dirname(__FILE__))));
55     }
57     function save_object()
58     {
59         $this->filterListing->save_object();
60         $action = $this->filterListing->getAction();
62         // Remove filter was requested.
63         if($action['action'] == 'delete'){
64             $key = $action['targets'][0];
65             $key = $this->filterListing->getKey($key);
66             if(isset($this->filter[$key])){
67                 unset($this->filter[$key]);
68                 $this->filter = array_values($this->filter);
69             }
70         }
72         // Edit filter was requested.
73         if($action['action'] == 'edit'){
74             $key = $action['targets'][0];
75             $key = $this->filterListing->getKey($key);
76             if(isset($this->filter[$key])){
77                 $filter = $this->filter[$key];
78                 $this->filterEditor = new filterEditor($this->config,$this->parent, $filter);
79                 $this->currentFilter = $key;
80             }
81         }
83         // Add new filter
84         if(isset($_POST['addFilter'])){
85             $filter =   array (
86                     'STATUS' => 'NEW',
87                     'TYPE' => 'AND',
88                     'NAME' => _('name'),
89                     'DESC' => _('description'),
90                     'CONDITIONS' => array  (
91                         array('FIELD' => 'from',
92                             'COMPARATOR' => 'equals',
93                             'MATCH' => 'example@domain.com'),
94                         array('FIELD' => 'subject',
95                             'COMPARATOR' => 'contains',
96                             'MATCH' => _('advertising')),
97                         ),
98                     'ACTION' => array (
99                         array('ACTION'=>'MARK',
100                             'VALUE' => 'SPAM'),
101                         array('ACTION'=>'MOVE',
102                             'VALUE' => '')
103                         )
104                     );
105             $this->filterEditor = new filterEditor($this->config,$this->parent, $filter);
106         }
108         // Close filter editor 
109         if(isset($_POST['filterEditor_cancel']) && $this->filterEditor instanceOf filterEditor){
110             $this->currentFilter = NULL;
111             $this->filterEditor = NULL;
112         }
114         // Save filter modifications and close the dialog
115         if(isset($_POST['filterEditor_ok']) && $this->filterEditor instanceOf filterEditor){
116             $this->filterEditor->save_object();
117             $msgs = $this->filterEditor->check();
118             if(count($msgs)){
119                 msg_dialog::displayChecks($msgs);
120             }else{
121                 $filter = $this->filterEditor->save();
122                 if($filter['STATUS'] == 'NEW'){
123                     $this->filter[] = $filter;
124                 }else{
125                     if($filter['STATUS'] != 'NEW') $filter['STATUS'] = 'MODIFIED';
126                     $this->filter[$this->currentFilter] = $filter;
127                 }
128                 $this->filterEditor = NULL;
129                 $this->currentFilter = NULL;
130             }
131         }
132     }
135     function save()
136     {
137         return($this->filter);
138     }
141 ?>