1 <?php
3 class sieve_vacation
4 {
5 var $days = FALSE;
6 var $subject = FALSE;
7 var $from = "";
8 var $mime = "";
9 var $handle = "";
10 var $reason = "\"I am not available, currently.\"";
11 var $addresses= array();
12 var $object_id= -1;
13 var $Expert = FALSE;
14 var $parent = NULL;
16 function sieve_vacation($data,$object_id,$parent)
17 {
18 $this->parent = $parent;
19 $this->object_id = $object_id;
20 $this->parent->add_require("vacation");
22 /* Usage: vacation [":days" number] [":subject" string]
23 [":from" string] [":addresses" string-list]
24 [":mime"] [":handle" string] <reason: string> */
26 /* Not all attribute types are supported by the sieve class right now */
27 $known_attrs = array(":days",":subject",":from",":mime",":handle");
29 /* skip if empty */
30 if(($data === NULL) || !is_array($data)) return;
32 /* Walk through elements */
33 $p= count($data['ELEMENTS']);
34 for ($i= 0; $i < $p; $i++){
36 /* get current element */
37 $node = $data['ELEMENTS'][$i];
39 /* Check if tag is in the specified list of attributes */
40 if($node['class'] == "tag" && in_array($node['text'],$known_attrs)){
42 $var = preg_replace("/\:/","",$node['text']);
43 $this->$var = $data['ELEMENTS'][$i+1]['text'];
44 $i ++;
45 }
47 /* Check for addresses */
48 if($node['class'] == "tag" && $node['text'] == ":addresses") {
49 $this->addresses = array();
50 $i ++;
52 /* Multiple or single address given */
53 if($data['ELEMENTS'][$i]['class'] == "left-bracket"){
54 while($data['ELEMENTS'][$i]['class'] != "right-bracket" && ($i < count($data['ELEMENTS']))){
55 $i ++;
56 if($data['ELEMENTS'][$i]['class'] == "quoted-string"){
57 $this->addresses[] = preg_replace("/\"/i","",$data['ELEMENTS'][$i]['text']);
58 }
59 }
60 }else{
61 $this->addresses[] = preg_replace("/\"/i","",$data['ELEMENTS'][$i]['text']);
62 }
63 }
65 /* Add the vacation message */
66 if(in_array($node['class'],array("quoted-string","multi-line"))){
68 $tmp = sieve_get_strings($data['ELEMENTS'],$i);
69 $strs= $tmp['STRINGS'];
71 $data = "";
72 foreach($strs as $str){
73 $data .= $str;
74 }
75 $this->reason = $data;
76 }
77 }
78 }
80 function get_sieve_script_part()
81 {
82 $str = "vacation ";
83 if($this->days){
84 $str.= ":days ".$this->days;
85 }
87 if(count($this->addresses)){
88 $str .= ":addresses ".sieve_create_strings($this->addresses);
89 if($this->subject){
90 $str.= ":subject ".sieve_create_strings($this->subject);
91 }
92 }
93 if($this->mime){
94 $str.= ":mime ".sieve_create_strings($this->mime);
95 }
97 /* Append reason and ensure that this will be
98 * handled as multiline text element
99 * by adding a "\n" new line
100 */
101 $str .= "\n ".sieve_create_strings($this->reason."\n");
102 return($str." ; \n");
103 }
105 function save_object()
106 {
107 /* Get release date */
108 if(isset($_POST['vacation_release_'.$this->object_id])){
109 $this->days = stripslashes($_POST['vacation_release_'.$this->object_id]);
110 }
112 /* Check if we want to toggle the expert mode */
113 if(isset($_POST['Toggle_Expert_'.$this->object_id])){
114 $this->Expert = !$this->Expert;
115 }
117 /* Get release date */
118 if(isset($_POST['vacation_receiver_'.$this->object_id])){
119 $vr = stripslashes ($_POST['vacation_receiver_'.$this->object_id]);
120 $tmp = array();
121 $tmp2 = explode(",",$vr);
122 foreach($tmp2 as $val){
123 $ad = trim($val);
124 if(!empty($ad)){
125 $tmp[] = $ad;
126 }
127 }
128 $this->addresses = $tmp;
129 }
131 /* Get reason */
132 if(isset($_POST['vacation_reason_'.$this->object_id])){
133 $vr = stripslashes ($_POST['vacation_reason_'.$this->object_id]);
134 $this->reason = trim($vr);
135 }
136 }
138 function check()
139 {
140 $msgs = array();
141 $err = FALSE;
142 foreach($this->addresses as $addr){
143 if(!tests::is_email($addr)){
144 $err = true;
145 }
146 }
147 if($err){
148 $msgs[] = _("Alternative sender address must be a valid email addresses.");
149 }
150 return($msgs);
151 }
153 function execute()
154 {
155 $Addresses = "";
156 foreach($this->addresses as $key){
157 $Addresses .= $key.", ";
158 }
159 $Addresses = preg_replace("/,$/","",trim($Addresses));
161 $smarty = get_smarty();
162 $smarty->assign("LastError",$this->check());
163 $smarty->assign("LastErrorCnt",count($this->check()));
164 $smarty->assign("Reason",$this->reason);
165 $smarty->assign("Addresses",$Addresses);
166 $smarty->assign("Subject",$this->subject);
167 $smarty->assign("Days",$this->days);
168 $smarty->assign("ID",$this->object_id);
169 $smarty->assign("Expert",$this->Expert);
171 $object_container = $smarty->fetch(get_template_path("templates/object_container.tpl",TRUE,dirname(__FILE__)));
172 $object= $smarty->fetch(get_template_path("templates/element_vacation.tpl",TRUE,dirname(__FILE__)));
173 $str = preg_replace("/%%OBJECT_CONTENT%%/",addcslashes($object,"\\"),$object_container);
174 return($str);
175 }
176 }
178 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
179 ?>