Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
form_action_send_email.inc
1 <?php
18 require_once SQ_PACKAGES_PATH.'/cms/form/form_action/form_action.inc';
19 require_once SQ_ATTRIBUTES_PATH.'/email_format/email_format.inc';
20 
21 require_once SQ_FUDGE_PATH.'/general/general.inc';
22 
36 class Form_Action_Send_Email extends Form_Action
37 {
38 
39 
44  function __construct()
45  {
46  parent::__construct();
47 
48  }//end constructor
49 
50 
56  public static function paintInlineInterface(Form $form, $settings, Backend_Outputter $o, $prefix)
57  {
58  if (!isset($settings['email_format'])) {
59  $email_settings = serialize(Array());
60  } else {
61  $email_settings = $settings['email_format'];
62  }
63 
64  // Set up email format
65  $email_format = new Asset_Attribute_Email_Format(0, $email_settings);
66  $xml = simplexml_load_string('<email_format with_asset_finder="1" show_inc_attachments="1" />');
67  $email_format->setEditParams($xml);
68 
69  $email_format->paint($prefix);
70 
71  }//end paintInlineInterface()
72 
73 
79  public static function processInlineInterface(Form $form, &$settings, Backend_Outputter $o, $prefix)
80  {
81  if (!isset($settings['email_format'])) {
82  $email_settings = serialize(Array());
83  } else {
84  $email_settings = $settings['email_format'];
85  }
86 
87  // Set up email format
88  $email_format = new Asset_Attribute_Email_Format(0, $email_settings);
89  $xml = simplexml_load_string('<email_format with_asset_finder="1" show_inc_attachments="1" />');
90  $email_format->setEditParams($xml);
91 
92  $email_format->name = 'current action\'s settings';
93  $email_format->process($prefix);
94 
95  $settings['email_format'] = $email_format->value;
96 
97  return $email_format->validateValue($email_format->value);
98 
99  }//end processInlineInterface()
100 
101 
107  public static function paintSummary(Form $form, $settings, Backend_Outputter $o, $prefix)
108  {
109  if (!isset($settings['email_format'])) {
110  $email_settings = Array();
111  } else {
112  $email_settings = unserialize($settings['email_format']);
113  }
114  self::_fillEmailFormatDefaults($email_settings);
115 
116  ?><table class="no-borders">
117  <colgroup>
118  <col width="80" />
119  <col/>
120  </colgroup>
121  <tbody>
122  <tr>
123  <td class="sq-backend-table-cell" style="vertical-align: top"><p><strong>To:</strong></p></td>
124  <td class="sq-backend-table-cell" style="vertical-align: top"><p><?php
125  $i = 0;
126  $to_array = Array();
127  $all_keywords = TRUE;
128 
129  // Get manually-entered "to" settings.
130  for (; ($i < 6) && !empty($email_settings['to']); $i++) {
131  $this_email = array_shift($email_settings['to']);
132  $to_array[] = htmlspecialchars($this_email);
133  if (!preg_match('/%.*%/', $this_email)) {
134  $all_keywords = FALSE;
135  }
136  }
137 
138  // Now the users (by definition, these are not based on keywords).
139  // Continue $i's count from previous loop.
140  for (; ($i < 6) && !empty($email_settings['to_assetids']); $i++) {
141  $to_array[] = get_asset_tag_line(array_shift($email_settings['to_assetids']));
142  $all_keywords = FALSE;
143  }
144 
145  if ($i > 0) {
146  echo implode(', ', $to_array);
147  $extra_recipients = count($email_settings['to']) + count($email_settings['to_assetids']);
148  if ($extra_recipients > 0) {
149  ?>, <em>plus <?php echo $extra_recipients ?> more recipients</em><?php
150  }
151  if ($all_keywords) {
152  ?><br/><em>Note: All of your recipients are based on the results of keywords (for instance, submitted responses). If these are left blank in a submission, this email may not send.</em><?php
153  }
154  } else {
155  ?><span class="sq-backend-warning">No recipients specified.</span> This email cannot send without at least one recipient.<?php
156  }
157  ?></p></td>
158  </tr>
159  <tr>
160  <td class="sq-backend-table-cell" style="vertical-align: top"><p><strong>From:</strong></p></td>
161  <td class="sq-backend-table-cell" style="vertical-align: top"><p><?php
162  if (!empty($email_settings['from'])) {
163  echo htmlspecialchars($email_settings['from']);
164  } else {
165  ?><span class="sq-backend-warning">No "From:" address specified.</span> The system's default email address (<?php echo SQ_CONF_DEFAULT_EMAIL ?>) will be used.<?php
166  }
167  ?></p></td>
168  </tr>
169  <tr>
170  <td class="sq-backend-table-cell" style="vertical-align: top"><p><strong>Subject:</strong></p></td>
171  <td class="sq-backend-table-cell" style="vertical-align: top"><p><?php
172  if (!empty($email_settings['subject'])) {
173  echo htmlspecialchars($email_settings['subject']);
174  } else {
175  ?><span class="sq-backend-warning">No subject specified.</span> Although an email can send without a subject, it is recommended you add one.<?php
176  } ?></p></td>
177  </tr>
178  <tr>
179  <td class="sq-backend-table-cell" style="vertical-align: top"><p><strong>HTML Body:</strong></p></td>
180  <td class="sq-backend-table-cell" style="vertical-align: top"><p><?php
181  if (!empty($email_settings['html_format'])) {
182  echo ellipsisize(strip_tags(preg_replace('/\\n/', ' ', $email_settings['html_format'])), 512);
183  } else {
184  ?><span class="sq-backend-warning">The HTML body is empty.</span><?php
185  } ?></p></td>
186  </tr>
187  <tr>
188  <td class="sq-backend-table-cell" style="vertical-align: top"><p><strong>Text Body:</strong></p></td>
189  <td class="sq-backend-table-cell" style="vertical-align: top"><p><?php
190  if (!empty($email_settings['text_format'])) {
191  echo ellipsisize(preg_replace('/\\n/', ' ', $email_settings['text_format']), 512);
192  } else {
193  ?><span class="sq-backend-warning">The text-only body is empty.</span><?php
194  } ?></p></td>
195  </tr>
196  <tr>
197  <td class="sq-backend-table-cell" style="vertical-align: top"><p><strong>Uploads:</strong></p></td>
198  <td class="sq-backend-table-cell" style="vertical-align: top"><p>File uploads are <strong><?php echo $email_settings['inc_attachments'] ? 'attached' : 'not attached' ?></strong>.</p></td>
199  </tr>
200  </tbody>
201  </table>
202  <?php
203 
204  }//end paintSummary()
205 
206 
211  public static function execute(Form $form, $settings)
212  {
213  $email_settings = $settings['email_format'];
214 
215  // Set up email format and let it be the final judge
216  $email_format = new Asset_Attribute_Email_Format(0, $email_settings);
217  $xml = simplexml_load_string('<email_format with_asset_finder="1" show_inc_attachments="1" />');
218  $email_format->setEditParams($xml);
219 
220  $form->sendEmail($email_format);
221  return TRUE;
222 
223  }//end execute()
224 
225 
230  public static function isValid(Form $form, $settings)
231  {
232  if (!isset($settings['email_format'])) {
233  $email_settings = Array();
234  } else {
235  $email_settings = unserialize($settings['email_format']);
236  }
237  self::_fillEmailFormatDefaults($email_settings);
238 
239  // No-one to send it to!
240  if (empty($email_settings['to']) && empty($email_settings['to_assetids'])) {
241  return FALSE;
242  }
243 
244  // Nothing in either body!
245  if (empty($email_settings['text_format']) && empty($email_settings['html_format'])) {
246  return FALSE;
247  }
248 
249  // Set up email format and let it be the final judge
250  $email_format = new Asset_Attribute_Email_Format(0, serialize($email_settings));
251  $xml = simplexml_load_string('<email_format with_asset_finder="1" show_inc_attachments="1" />');
252  $email_format->setEditParams($xml);
253 
254  return $email_format->validateValue($email_settings);
255 
256  }//end isValid()
257 
258 
266  private static function _fillEmailFormatDefaults(&$email_format) {
267  if (!isset($email_format['to'])) {
268  $email_format['to'] = Array();
269  }
270 
271  if (!isset($email_format['to_assetids'])) {
272  $email_format['to_assetids'] = Array();
273  }
274 
275  if (!isset($email_format['from'])) {
276  $email_format['from'] = '';
277  }
278 
279  if (!isset($email_format['subject'])) {
280  $email_format['subject'] = '';
281  }
282 
283  if (!isset($email_format['html_format'])) {
284  $email_format['html_format'] = '';
285  }
286 
287  if (!isset($email_format['text_format'])) {
288  $email_format['text_format'] = '';
289  }
290 
291  if (!isset($email_format['inc_attachments'])) {
292  $email_format['inc_attachments'] = Array();
293  }
294 
295  if (!isset($email_format['attachments'])) {
296  $email_format['attachments'] = Array();
297  }
298 
299 
300  }//end _fillDefaultValues()
301 
302 
303 }//end class
304 
305 ?>