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