Merge pull request #4882 from colemanw/CRM-15789
[civicrm-core.git] / CRM / Friend / Form.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * This class generates form components for Tell A Friend Form For End User
38 *
39 */
40 class CRM_Friend_Form extends CRM_Core_Form {
41
42 /**
43 * Constants for number of friend contacts
44 */
45 const NUM_OPTION = 3;
46
47 /**
48 * The id of the entity that we are proceessing
49 *
50 * @var int
51 */
52 protected $_entityId;
53
54 /**
55 * The table name of the entity that we are proceessing
56 *
57 * @var string
58 */
59 protected $_entityTable;
60
61 protected $_campaignId;
62
63 /**
64 * The contact ID
65 *
66 * @var int
67 */
68 protected $_contactID;
69
70 public function preProcess() {
71 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this);
72 $this->_entityId = CRM_Utils_Request::retrieve('eid', 'Positive', $this, TRUE);
73
74 $pcomponent = CRM_Utils_Request::retrieve('pcomponent', 'String', $this, TRUE);
75
76 if (in_array($pcomponent, array(
77 'contribute', 'event'))) {
78 $values = array();
79 $params = array('id' => $this->_entityId);
80 CRM_Core_DAO::commonRetrieve('CRM_Contribute_DAO_ContributionPage',
81 $params, $values, array('title', 'campaign_id', 'is_share')
82 );
83 $this->_title = CRM_Utils_Array::value('title', $values);
84 $this->_campaignId = CRM_Utils_Array::value('campaign_id', $values);
85 $this->_entityTable = 'civicrm_contribution_page';
86 if ($pcomponent == 'event') {
87 $this->_entityTable = 'civicrm_event';
88 $isShare = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $this->_entityId, 'is_share');
89 }
90 else {
91 $isShare = CRM_Utils_Array::value('is_share', $values);
92 }
93 // Tell Form.tpl whether to include SocialNetwork.tpl for social media sharing
94 $this->assign('isShare', $isShare);
95 }
96 elseif ($pcomponent == 'pcp') {
97 $this->_pcpBlockId = CRM_Utils_Request::retrieve('blockId', 'Positive', $this, TRUE);
98
99 $values = array();
100 $params = array('id' => $this->_pcpBlockId);
101 CRM_Core_DAO::commonRetrieve('CRM_PCP_DAO_PCPBlock',
102 $params, $values, array('is_tellfriend_enabled', 'tellfriend_limit')
103 );
104
105 if (empty($values['is_tellfriend_enabled'])) {
106 CRM_Core_Error::fatal(ts('Tell Friend is disable for this Personal Campaign Page'));
107 }
108
109 $this->_mailLimit = $values['tellfriend_limit'];
110 $this->_entityTable = 'civicrm_pcp';
111
112 $sql = '
113 SELECT pcp.title,
114 contrib.campaign_id
115 FROM civicrm_pcp pcp
116 INNER JOIN civicrm_contribution_page contrib ON ( pcp.page_id = contrib.id AND pcp.page_type = "contribute" )
117 WHERE pcp.id = %1';
118 $pcp = CRM_Core_DAO::executeQuery($sql, array(1 => array($this->_entityId, 'Positive')));
119 while ($pcp->fetch()) {
120 $this->_title = $pcp->title;
121 $this->_campaignId = $pcp->campaign_id;
122 $pcp->free();
123 }
124
125 $this->assign('pcpTitle', $this->_title);
126 }
127 else {
128 CRM_Core_Error::fatal(ts('page argument missing or invalid'));
129 }
130 $this->assign('context', $pcomponent);
131
132 $session = CRM_Core_Session::singleton();
133 $this->_contactID = $session->get('userID');
134 if (!$this->_contactID) {
135 $this->_contactID = $session->get('transaction.userID');
136 }
137
138 if (!$this->_contactID) {
139 CRM_Core_Error::fatal(ts('Could not get the contact ID'));
140 }
141
142 // we do not want to display recently viewed items, so turn off
143 $this->assign('displayRecent', FALSE);
144 }
145
146 /**
147 * Set default values for the form.
148 *
149 *
150 * @return void
151 */
152 public function setDefaultValues() {
153 $defaults = array();
154
155 $defaults['entity_id'] = $this->_entityId;
156 $defaults['entity_table'] = $this->_entityTable;
157
158 CRM_Friend_BAO_Friend::getValues($defaults);
159 CRM_Utils_System::setTitle(CRM_Utils_Array::value('title', $defaults));
160
161 $this->assign('title', CRM_Utils_Array::value('title', $defaults));
162 $this->assign('intro', CRM_Utils_Array::value('intro', $defaults));
163 $this->assign('message', CRM_Utils_Array::value('suggested_message', $defaults));
164 $this->assign('entityID', $this->_entityId);
165
166 list($fromName, $fromEmail) = CRM_Contact_BAO_Contact::getContactDetails($this->_contactID);
167
168 $defaults['from_name'] = $fromName;
169 $defaults['from_email'] = $fromEmail;
170
171 return $defaults;
172 }
173
174 /**
175 * Build the form object
176 *
177 * @return void
178 */
179 public function buildQuickForm() {
180 // Details of User
181 $name = &$this->add('text',
182 'from_name',
183 ts('From'),
184 CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'first_name')
185 );
186 $name->freeze();
187
188 $email = &$this->add('text',
189 'from_email',
190 ts('Your Email'),
191 CRM_Core_DAO::getAttribute('CRM_Core_DAO_Email', 'email'),
192 TRUE
193 );
194 $email->freeze();
195
196 $this->addWysiwyg('suggested_message', ts('Your Message'), CRM_Core_DAO::getAttribute('CRM_Friend_DAO_Friend', 'suggested_message'));
197 $friend = array();
198 $mailLimit = self::NUM_OPTION;
199 if ($this->_entityTable == 'civicrm_pcp') {
200 $mailLimit = $this->_mailLimit;
201 }
202 $this->assign('mailLimit', $mailLimit + 1);
203 for ($i = 1; $i <= $mailLimit; $i++) {
204 $this->add('text', "friend[$i][first_name]", ts("Friend's First Name"));
205 $this->add('text', "friend[$i][last_name]", ts("Friend's Last Name"));
206 $this->add('text', "friend[$i][email]", ts("Friend's Email"));
207 $this->addRule("friend[$i][email]", ts('The format of this email address is not valid.'), 'email');
208 }
209
210 $this->addButtons(array(
211 array(
212 'type' => 'submit',
213 'name' => ts('Send Your Message'),
214 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
215 'isDefault' => TRUE,
216 ),
217 array(
218 'type' => 'cancel',
219 'name' => ts('Cancel'),
220 ),
221 )
222 );
223
224 $this->addFormRule(array('CRM_Friend_Form', 'formRule'));
225 }
226
227 /**
228 * Validation
229 *
230 * @param array $fields
231 *
232 * @return mixed true or array of errors
233 * @static
234 */
235 public static function formRule($fields) {
236
237 $errors = array();
238
239 $valid = FALSE;
240 foreach ($fields['friend'] as $key => $val) {
241 if (trim($val['first_name']) || trim($val['last_name']) || trim($val['email'])) {
242 $valid = TRUE;
243
244 if (!trim($val['first_name'])) {
245 $errors["friend[{$key}][first_name]"] = ts('Please enter your friend\'s first name.');
246 }
247
248 if (!trim($val['last_name'])) {
249 $errors["friend[{$key}][last_name]"] = ts('Please enter your friend\'s last name.');
250 }
251
252 if (!trim($val['email'])) {
253 $errors["friend[{$key}][email]"] = ts('Please enter your friend\'s email address.');
254 }
255 }
256 }
257
258 if (!$valid) {
259 $errors['friend[1][first_name]'] = ts("Please enter at least one friend's information, or click Cancel if you don't want to send emails at this time.");
260 }
261
262 return empty($errors) ? TRUE : $errors;
263 }
264
265 /**
266 * Process the form submission
267 *
268 *
269 * @return void
270 */
271 public function postProcess() {
272 // get the submitted form values.
273 $formValues = $this->controller->exportValues($this->_name);
274
275 $formValues['entity_id'] = $this->_entityId;
276 $formValues['entity_table'] = $this->_entityTable;
277 $formValues['source_contact_id'] = $this->_contactID;
278 $formValues['is_test'] = $this->_action ? 1 : 0;
279 $formValues['title'] = $this->_title;
280 $formValues['campaign_id'] = $this->_campaignId;
281
282 CRM_Friend_BAO_Friend::create($formValues);
283
284 $this->assign('status', 'thankyou');
285 $defaults = array();
286
287 $defaults['entity_id'] = $this->_entityId;
288 $defaults['entity_table'] = $this->_entityTable;
289
290 CRM_Friend_BAO_Friend::getValues($defaults);
291 if ($this->_entityTable == 'civicrm_pcp') {
292 $defaults['thankyou_text'] = $defaults['thankyou_title'] = ts('Thank you for your support');
293 $defaults['thankyou_text'] = ts('Thanks for supporting this campaign by spreading the word to your friends.');
294 }
295 elseif ($this->_entityTable == 'civicrm_contribution_page') {
296 // If this is tell a friend after contributing, give donor link to create their own fundraising page
297 if ($linkText = CRM_PCP_BAO_PCP::getPcpBlockStatus($defaults['entity_id'], $defaults['entity_table'])) {
298
299 $linkTextUrl = CRM_Utils_System::url('civicrm/contribute/campaign',
300 "action=add&reset=1&pageId={$defaults['entity_id']}&component=contribute",
301 FALSE, NULL, TRUE,
302 TRUE
303 );
304 $this->assign('linkTextUrl', $linkTextUrl);
305 $this->assign('linkText', $linkText);
306 }
307 }
308 elseif ($this->_entityTable == 'civicrm_event') {
309 // If this is tell a friend after registering for an event, give donor link to create their own fundraising page
310 require_once 'CRM/PCP/BAO/PCP.php';
311 if ($linkText = CRM_PCP_BAO_PCP::getPcpBlockStatus($defaults['entity_id'], $defaults['entity_table'])) {
312 $linkTextUrl = CRM_Utils_System::url('civicrm/contribute/campaign',
313 "action=add&reset=1&pageId={$defaults['entity_id']}&component=event",
314 FALSE, NULL, TRUE,
315 TRUE);
316 $this->assign('linkTextUrl', $linkTextUrl);
317 $this->assign('linkText', $linkText);
318 }
319 }
320
321 CRM_Utils_System::setTitle($defaults['thankyou_title']);
322 $this->assign('thankYouText', $defaults['thankyou_text']);
323 }
324 }