CMS  Version 3.9
email_recipients_field_renderer.inc
Go to the documentation of this file.
1 <?php
7 /**************************************************************
8 
9  Copyright (c) 2010 Sonjara, Inc
10 
11  Permission is hereby granted, free of charge, to any person
12  obtaining a copy of this software and associated documentation
13  files (the "Software"), to deal in the Software without
14  restriction, including without limitation the rights to use,
15  copy, modify, merge, publish, distribute, sublicense, and/or sell
16  copies of the Software, and to permit persons to whom the
17  Software is furnished to do so, subject to the following
18  conditions:
19 
20  The above copyright notice and this permission notice shall be
21  included in all copies or substantial portions of the Software.
22 
23  Except as contained in this notice, the name(s) of the above
24  copyright holders shall not be used in advertising or otherwise
25  to promote the sale, use or other dealings in this Software
26  without prior written authorization.
27 
28  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
30  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
32  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
33  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
34  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
35  OTHER DEALINGS IN THE SOFTWARE.
36 
37 *****************************************************************/
38 
39 Fakoli::usingFile("framework/field_renderers/text_field_renderer.inc");
40 
41 /*
42  * For forms that use a field "recipients" for entry of
43  * multiple email addresses. If the user enters inconsistent
44  * delimiters between email addresses, change to consistent ","
45  * on preprocess.
46  */
47 class EmailListFieldRenderer extends TextFieldRenderer
48 {
49  var $removeDuplicates = false;
50 
51  function EmailListFieldRenderer(&$form, $field, $label = "")
52  {
53  parent::TextFieldRenderer($form);
54 
55  $label = ($label) ? $label : prettify($field);
56 
57  if ($form->data->hasField($field))
58  {
59  $form->override($field, $label, $this);
60  }
61  else
62  {
63  $form->add($this, $field);
64  $form->overrides[$field]['label'] = $label;
65  }
66  }
67 
68  function addValidatorsToForm($field, $required = false)
69  {
70  $label = isset($this->label) ? $this->label : $this->parent->prettifyFieldName($field);
71 
72  $this->parent->validator->add(new EmailListValidator($field, $label));
73 
74  if($required)
75  {
76  $this->parent->validator->add(new RequiredValidator($field, $label));
77  }
78  }
79 
80 
81  function renderField($field)
82  {
83  $recipients = $this->parent->data->get($field);
84  $recipients = preg_replace("/,\s*/", ", ", $recipients);
85  $this->parent->data->set($field, $recipients);
86 
87  parent::renderField($field);
88  }
89 
90  function preProcess($field = "")
91  {
92  if(!$field) $field = "recipients";
93 
94  $recipients = EmailListFieldRenderer::cleanup($this->parent->data->get($field));
95 
96  if($this->removeDuplicates)
97  {
98  $recipients = implode(",", array_unique(explode(",", $recipients)));
99  }
100 
101  $this->parent->data->set($field, $recipients);
102  }
103 
110  static function cleanup($recipients)
111  {
112  $patterns = array("/,\s+/", "/;\s+/");
113  $replacements = array(",", ",");
114  $recipients = preg_replace($patterns, $replacements, $recipients);
115  $recipients = rtrim($recipients, ",;");
116 
117  return $recipients;
118  }
119 }
120 
121 
128 class EmailRecipientScrollBoxFieldRenderer extends FieldRenderer
129 {
130  var $cssClass = 'email_recipient_scrollbox';
131  var $field;
136  var $deleteLabel = "Clear All";
137 
139  {
140  $this->recipients = $recipients;
141  $this->field = $field;
142  $this->nameField = $nameField;
143 
144  parent::FieldRenderer($form);
145 
146  $label = ($label) ? $label : prettify($field);
147 
148  if ($form->data->hasField($field))
149  {
150  $form->override($field, $label, $this);
151  }
152  else
153  {
154  $form->add($this, $field);
155  $form->overrides[$field]['label'] = $label;
156  }
157 
158  $this->delete_fn = $delete_fn;
159  $this->delete_all_fn = $delete_all_fn;
160  $this->colspan = 2;
161  }
162 
163  function renderField($field)
164  {
165  $this->_startField($field);
166 
168 
169  echo "<div id='email_recipients'>\n";
170 
171  if($this->delete_all_fn)
172  {
173  $pk = $this->parent->data->getPrimaryKey();
174  $id = $this->parent->data->$pk;
175  echo "<a class='button' href='' onclick=\"{$this->delete_all_fn}($id); return false;\">{$this->deleteLabel}</a>\n";
176  }
177  echo "<div id='{$this->parent->id}_{$field}' class='{$this->cssClass}' name='$field'>\n";
178 
179  echo $this->formatRecipients($recipients);
180 
181  echo "</div>\n";
182  echo "</div>\n";
183 
184  $this->_endField($field);
185  }
186 
187  function addValidatorsToForm($field, $required = false)
188  {
189  $label = isset($this->label) ? $this->label : $this->parent->prettifyFieldName($field);
190 
191  $this->parent->validator->add(new EmailListValidator($field, $label));
192 
193  if($required)
194  {
195  $this->parent->validator->add(new RequiredValidator($field, $label));
196  }
197  }
198 
199 
201  {
202  $this->delete_fn = "";
203  $this->delete_all_fn = "";
204  $this->renderField($field);
205  }
206 
208  {
209  if(!count($recipients)) return "";
210 
211  foreach($recipients as $recipient)
212  {
213  $html .= $this->formatOneRecipient($recipient);
214  }
215 
216  return $html;
217  }
218 
219 
228  function formatOneRecipient($recipient)
229  {
230  $pk = $recipient->getPrimaryKey();
232  if($this->delete_fn)
233  {
234  $removeLink = "<a class='remove_recipient' href='#' onclick='{$this->delete_fn}({{$pk}}); return false;'>x</a>";
235  }
236 
237  return $recipient->format("<div id='{$pk}_{{$pk}}' class='email_recipient'>{$nameField}{$removeLink}</div>");
238  }
239 }?>
$form
addValidatorsToForm($field, $required=false)
EmailListFieldRenderer(&$form, $field, $label="")
static cleanup($recipients)
Given a string of emails, clean up the list, removing any ";" or extra spaces, or trailing punctuatio...
Email recipient list validator.
Displays email recipients in mail client type of bubble inside a scrollbox.
$delete_all_fn
js function to delete all recipients
formatOneRecipient($recipient)
Construct a list member using the id and value, such as id: "user_id_7" value "Sam Jones".
EmailRecipientScrollBoxFieldRenderer(&$form, $field, $label="", $recipients, $nameField, $delete_fn="", $delete_all_fn)
$delete_fn
js function to delete selected recipient using "x"
static usingFile()
Uses the specified framework file(s) from the framework directory.
Definition: core.inc:369
$topicList colspan
Definition: group_form.inc:54
$email recipients
Definition: mail_to.inc:53
$openData field