Code

Fixed some errors
[gosa.git] / include / sieve / class_sieveElement_Vacation.inc
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   = "";
11   var $addresses= array();
12   var $object_id= -1;
13   var $Expert   = FALSE;
15   function sieve_vacation($data,$object_id)
16   {
18     $this->object_id = $object_id;
19     /* Usage:   vacation [":days" number] [":subject" string]
20        [":from" string] [":addresses" string-list]
21        [":mime"] [":handle" string] <reason: string> */
23     /* Not all attribute types are supported by the sieve class right now */
24     $known_attrs = array(":days",":subject",":from",":mime",":handle");
26     /* skip if empty */
27     if(($data == NULL) || !is_array($data)) return;
29     /* Walk through elements */
30     for($i = 0 ; $i < count($data['ELEMENTS']) ; $i ++){
32       /* get current element */
33       $node = $data['ELEMENTS'][$i];
35       /* Check if tag is in the specified list of attributes */
36       if($node['class'] == "tag" && in_array($node['text'],$known_attrs)){
38         $var = preg_replace("/\:/","",$node['text']);
39         $this->$var = $data['ELEMENTS'][$i+1]['text'];
40         $i ++;
41       }
43       /* Check for addresses */
44       if($node['class'] == "tag" && $node['text'] == ":addresses") {
45         $this->addresses = array();
46         $i ++;
48         /* Multiple or single address given */
49         if($data['ELEMENTS'][$i]['class'] == "left-bracket"){
50           while($data['ELEMENTS'][$i]['class'] != "right-bracket" && ($i < count($data['ELEMENTS']))){
51             $i ++;
52             if($data['ELEMENTS'][$i]['class'] == "quoted-string"){
53               $this->addresses[] = preg_replace("/\"/i","",$data['ELEMENTS'][$i]['text']);
54             }
55           }
56         }else{
57                $this->addresses[] = $data['ELEMENTS'][$i]['text'] ;
58         }
59       }
61       /* Add the vacation message */
62       if(in_array($node['class'],array("quoted-string","multi-line"))){
64         $tmp = sieve_get_strings($data['ELEMENTS'],$i);
65         $strs= $tmp['STRINGS'];
66  
67         $data = ""; 
68         foreach($strs as $str){
69           $data .= $str;
70         }
71         $this->reason = $data;//preg_replace("/\"/","",$data);
72       }
73     }
74   }
76   function get_sieve_script_part()
77   {
78     $str = "vacation ";
79     if($this->days){
80       $str.= ":days ".$this->days;
81     }
83     if(count($this->addresses)){
84       $str .= ":addresses ".sieve_create_strings($this->addresses);
85       if($this->subject){
86         $str.= ":subject ".sieve_create_strings($this->subject);
87       }
88     }
89     if($this->mime){
90       $str.= ":mime ".sieve_create_strings($this->mime);
91     }
92     $str .= "\n ".sieve_create_strings($this->reason);
93     return($str." ; \n");
94   }
96   function save_object()
97   {
98     /* Get release date */
99     if(isset($_POST['vacation_release_'.$this->object_id])){
100       $this->days = $_POST['vacation_release_'.$this->object_id];
101     }
103     /* Check if we want to toggle the expert mode */
104     if(isset($_POST['Toggle_Expert_'.$this->object_id])){
105       $this->Expert = !$this->Expert;
106     }
108     /* Get release date */
109     if(isset($_POST['vacation_receiver_'.$this->object_id])){
110       $vr = stripslashes ($_POST['vacation_receiver_'.$this->object_id]);
111       $tmp = array();
112       $tmp2 = split(",",$vr);
113       foreach($tmp2 as $val){
114         $ad = trim(preg_replace("/\"/","",$val));
115         if(!empty($ad)){
116           $tmp[] = "\"".$ad."\"";
117         }
118       }
119       $this->addresses = $tmp;
120     }
122     /* Get reason */
123     if(isset($_POST['vacation_reason_'.$this->object_id])){
124       $vr = stripslashes ($_POST['vacation_reason_'.$this->object_id]);
125       $this->reason = "\"".trim(preg_replace("/\"/","",$vr))."\"";
126     }
127   }
129   function check()
130   {
131     $msgs = array();
132     $err = FALSE;
133     foreach($this->addresses as $addr){
134       if(!is_email(preg_replace("/\"/","",$addr))){
135         $err = true;
136       }
137     }
138     if($err){
139       $msgs[] = _("Alternative sender addresse must be valid email addresses.");
140     }
141     return($msgs);
142   }
144   function execute()
145   {
146     $Addresses = "";
147     foreach($this->addresses as $key){
148       $Addresses .= $key.", ";
149     }
150     $Addresses = preg_replace("/,$/","",trim($Addresses));
152     $smarty = get_smarty();
153     $smarty->assign("LastError",$this->check());
154     $smarty->assign("LastErrorCnt",count($this->check()));
155     $smarty->assign("Reason",$this->reason);
156     $smarty->assign("Addresses",$Addresses);
157     $smarty->assign("Subject",$this->subject);
158     $smarty->assign("Days",$this->days);
159     $smarty->assign("ID",$this->object_id);
160     $smarty->assign("Expert",$this->Expert);
162     $object_container = $smarty->fetch(get_template_path("templates/object_container.tpl",TRUE,dirname(__FILE__)));
163     $object= $smarty->fetch(get_template_path("templates/element_vacation.tpl",TRUE,dirname(__FILE__)));
164     $str = preg_replace("/%%OBJECT_CONTENT%%/",$object,$object_container);
165     return($str);
166   }
169 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
170 ?>