root/trunk/patForms/Storage/Mail.php

Revision 241, 5.5 kB (checked in by schst, 3 years ago)

moved staticValues to the Storage base class, made some improvements to the mail storage

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2 /**
3  * patForms "storage" class for mails.
4  *
5  * This is no real storage class, but it follows the same
6  * API and can be used like a storage.
7  *
8  * It only sends an eMail containing all form data.
9  *
10  * $Id$
11  *
12  * @package        patForms
13  * @subpackage    Storage
14  */
15
16 /**
17  * Uses PEAR::Mail
18  */
19 require_once 'Mail.php';
20  
21 /**
22  * patForms "storage" class for mails.
23  *
24  * This is no real storage class, but it follows the same
25  * API and can be used like a storage.
26  *
27  * It only sends an eMail containing all form data.
28  *
29  * @package        patForms
30  * @subpackage    Storage
31  * @author        Stephan Schmidt <schst@php-tools.net>
32  * @license        LGPL, see license.txt for details
33  * @link        http://www.php-tools.net
34  */
35 class patForms_Storage_Mail extends patForms_Storage
36 {
37    /**
38     * Mail driver to use
39     *
40     * @access   private
41     * @var      string
42     */
43     var $_mailDriver = 'mail';
44
45    /**
46     * Parameters for the mail driver
47     *
48     * @access   private
49     * @var      string
50     */
51     var $_mailDriverParams = array();
52
53    /**
54     * Recipient(s) of the mail
55     *
56     * @access   private
57     * @var      array
58     */
59     var $_recipient = array();
60
61    /**
62     * Headers of the mail
63     *
64     * @access   private
65     * @var      array
66     */
67     var $_headers = array(
68                             'Subject'     => 'patForms auto-generated mail',
69                             'X-Mailed-By' => 'patForms_Storage_Mail $Revision$',
70                             'From'        => 'patForms_Storage_Mail'
71                         );
72
73    /**
74     * prepend text to the mail body
75     *
76     * @access   private
77     * @var      string
78     */
79     var $_prependText = null;
80
81    /**
82     * append text to the mail body
83     *
84     * @access   private
85     * @var      string
86     */
87     var $_appendText = null;
88     
89    /**
90     * Set the recipient of the mail
91     *
92     * @access   public
93     * @param    string|array
94     */
95     function setRecipient($recipient)
96     {
97         if (!is_array($recipient)) {
98             $recipient = array($recipient);
99         }
100         $this->_recipient = $recipient;
101     }
102
103    /**
104     * Set the headers of the mail
105     *
106     * @access   public
107     * @param    array
108     */
109     function setHeaders($headers)
110     {
111         $this->_headers = array_merge($this->_headers, $headers);
112     }
113
114    /**
115     * prepend text to the mail content
116     *
117     * @access   public
118     * @param    string
119     */
120     function prependText($text)
121     {
122         $this->_prependText = $text;
123     }
124
125    /**
126     * append text to the mail content
127     *
128     * @access   public
129     * @param    string
130     */
131     function appendText($text)
132     {
133         $this->_appendText = $text;
134     }
135
136    /**
137     * Set the mail driver to use
138     *
139     * @access   public
140     * @param    string      mail driver name
141     * @param    array       mail driver parameters
142     * @link     http://pear.php.net/manual/en/package.mail.mail.factory.php
143     */
144     function setMailDriver($driver, $params = array())
145     {
146         $this->_mailDriver = $driver;
147         $this->_mailDriverParams = $params;
148     }
149
150    /**
151     * Mail the form data
152     *
153     * @access    public
154     * @param    object patForms        patForms object that should be stored
155     * @return    boolean                true on success
156     */
157     function storeEntry(&$form)
158     {
159         $elements = $form->getElements();
160
161         // get all attributes
162         $fields = array();
163         foreach ($elements as $element) {
164             $value = $element->convertValueToHumanReadable($element->getValue());
165             $fields[$element->getAttribute('label')] = $value;
166         }
167         
168         $maxLength = max(array_map('strlen', array_keys($fields)));
169         
170         // prepend text
171         if (!empty($this->_prependText)) {
172             $mailBody = $this->insertValuesIntoText($this->_prependText, $form);
173         } else {
174             $mailBody = '';
175         }
176         
177         // insert form values into mailbody
178         foreach ($fields as $label => $value) {
179             $mailBody .= sprintf("%s : %s\n", str_pad($label, $maxLength), $value);
180         }
181
182         if (!empty($this->_appendText)) {
183             $mailBody .= $this->insertValuesIntoText($this->_appendText, $form);
184         }
185
186         $headers = array();
187         foreach ($this->_headers as $key => $value) {
188             $headers[$key] = $this->insertValuesIntoText($value, $form);
189         }
190         $mail = &Mail::factory($this->_mailDriver, $this->_mailDriverParams);
191         if (PEAR::isError($mail)) {
192             return patErrorManager::raiseError('patForms::Storage::Mail::001', $mail->getMessage());
193         }
194         $result = $mail->send($this->_recipient, $headers, $mailBody);
195         if (PEAR::isError($result)) {
196             return patErrorManager::raiseError('patForms::Storage::Mail::001', $result->getMessage());
197         }
198         return true;
199     }
200
201    /**
202     * insert the form values into any text
203     *
204     * @access   private
205     * @param    string
206     * @param    patForms
207     */
208     function insertValuesIntoText($text, $form)
209     {
210         $values = array_merge($this->_staticValues, $form->getValues());
211         foreach ($values as $name => $value) {
212                $text = str_replace('{'.strtoupper($name).'}', $value, $text);
213         }
214         return $text;
215     }
216     
217    /**
218     * validate the form
219     *
220     * @access   public
221     * @param    object  $form  The form
222     * @return   True or false, the validation result
223     */
224     function validateEntry(&$form)
225     {
226         return true;
227     }
228
229    /**
230     * get an entry
231     *
232     * This tries to find an entry in the storage container
233     * that matches the current data that has been set in the
234     * form and populates the form with the data of this
235     * entry
236     *
237     * @access    public
238     * @param    object patForms        patForms object that should be stored
239     * @return    boolean                true on success
240     */
241     function loadEntry( &$form )
242     {
243         return true;
244     }
245 }
246 ?>
Note: See TracBrowser for help on using the browser.