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 for($i = 0 ; $i < count($data['ELEMENTS']) ; $i ++){
35 /* get current element */
36 $node = $data['ELEMENTS'][$i];
38 /* Check if tag is in the specified list of attributes */
39 if($node['class'] == "tag" && in_array($node['text'],$known_attrs)){
41 $var = preg_replace("/\:/","",$node['text']);
42 $this->$var = $data['ELEMENTS'][$i+1]['text'];
43 $i ++;
44 }
46 /* Check for addresses */
47 if($node['class'] == "tag" && $node['text'] == ":addresses") {
48 $this->addresses = array();
49 $i ++;
51 /* Multiple or single address given */
52 if($data['ELEMENTS'][$i]['class'] == "left-bracket"){
53 while($data['ELEMENTS'][$i]['class'] != "right-bracket" && ($i < count($data['ELEMENTS']))){
54 $i ++;
55 if($data['ELEMENTS'][$i]['class'] == "quoted-string"){
56 $this->addresses[] = preg_replace("/\"/i","",$data['ELEMENTS'][$i]['text']);
57 }
58 }
59 }else{
60 $this->addresses[] = preg_replace("/\"/i","",$data['ELEMENTS'][$i]['text']);
61 }
62 }
64 /* Add the vacation message */
65 if(in_array($node['class'],array("quoted-string","multi-line"))){
67 $tmp = sieve_get_strings($data['ELEMENTS'],$i);
68 $strs= $tmp['STRINGS'];
70 $data = "";
71 foreach($strs as $str){
72 $data .= $str;
73 }
74 $this->reason = $data;
75 }
76 }
77 }
79 function get_sieve_script_part()
80 {
81 $str = "vacation ";
82 if($this->days){
83 $str.= ":days ".$this->days;
84 }
86 if(count($this->addresses)){
87 $str .= ":addresses ".sieve_create_strings($this->addresses);
88 if($this->subject){
89 $str.= ":subject ".sieve_create_strings($this->subject);
90 }
91 }
92 if($this->mime){
93 $str.= ":mime ".sieve_create_strings($this->mime);
94 }
96 /* Append reason and ensure that this will be
97 * handled as multiline text element
98 * by adding a "\n" new line
99 */
100 $str .= "\n ".sieve_create_strings($this->reason."\n");
101 return($str." ; \n");
102 }
104 function save_object()
105 {
106 /* Get release date */
107 if(isset($_POST['vacation_release_'.$this->object_id])){
108 $this->days = stripslashes($_POST['vacation_release_'.$this->object_id]);
109 }
111 /* Check if we want to toggle the expert mode */
112 if(isset($_POST['Toggle_Expert_'.$this->object_id])){
113 $this->Expert = !$this->Expert;
114 }
116 /* Get release date */
117 if(isset($_POST['vacation_receiver_'.$this->object_id])){
118 $vr = stripslashes ($_POST['vacation_receiver_'.$this->object_id]);
119 $tmp = array();
120 $tmp2 = split(",",$vr);
121 foreach($tmp2 as $val){
122 $ad = trim($val);
123 if(!empty($ad)){
124 $tmp[] = $ad;
125 }
126 }
127 $this->addresses = $tmp;
128 }
130 /* Get reason */
131 if(isset($_POST['vacation_reason_'.$this->object_id])){
132 $vr = stripslashes ($_POST['vacation_reason_'.$this->object_id]);
133 $this->reason = trim($vr);
134 }
135 }
137 function check()
138 {
139 $msgs = array();
140 $err = FALSE;
141 foreach($this->addresses as $addr){
142 if(!is_email($addr)){
143 $err = true;
144 }
145 }
146 if($err){
147 $msgs[] = _("Alternative sender addresse must be valid email addresses.");
148 }
149 return($msgs);
150 }
152 function execute()
153 {
154 $Addresses = "";
155 foreach($this->addresses as $key){
156 $Addresses .= $key.", ";
157 }
158 $Addresses = preg_replace("/,$/","",trim($Addresses));
160 $smarty = get_smarty();
161 $smarty->assign("LastError",$this->check());
162 $smarty->assign("LastErrorCnt",count($this->check()));
163 $smarty->assign("Reason",$this->reason);
164 $smarty->assign("Addresses",$Addresses);
165 $smarty->assign("Subject",$this->subject);
166 $smarty->assign("Days",$this->days);
167 $smarty->assign("ID",$this->object_id);
168 $smarty->assign("Expert",$this->Expert);
170 $object_container = $smarty->fetch(get_template_path("templates/object_container.tpl",TRUE,dirname(__FILE__)));
171 $object= $smarty->fetch(get_template_path("templates/element_vacation.tpl",TRUE,dirname(__FILE__)));
172 $str = preg_replace("/%%OBJECT_CONTENT%%/",addcslashes($object,"\\"),$object_container);
173 return($str);
174 }
175 }
177 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
178 ?>