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