fix duplicate menu
[civicrm-core.git] / CRM / Friend / Form.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * This class generates form components for Tell A Friend Form For End User
14 *
15 */
16 class CRM_Friend_Form extends CRM_Core_Form {
17
18 /**
19 * Constants for number of friend contacts.
20 */
21 const NUM_OPTION = 3;
22
23 /**
24 * The id of the entity that we are processing.
25 *
26 * @var int
27 */
28 protected $_entityId;
29
30 /**
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.
39 *
40 * @var string
41 */
42 protected $_entityTable;
43
44 protected $_campaignId;
45
46 /**
47 * The contact ID.
48 *
49 * @var int
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
59 if (in_array($pcomponent, [
60 'contribute',
61 'event',
62 ])) {
63 $values = [];
64 $params = ['id' => $this->_entityId];
65 CRM_Core_DAO::commonRetrieve('CRM_Contribute_DAO_ContributionPage',
66 $params, $values, ['title', 'campaign_id', 'is_share']
67 );
68 $this->_title = $values['title'] ?? NULL;
69 $this->_campaignId = $values['campaign_id'] ?? NULL;
70 $this->_entityTable = 'civicrm_contribution_page';
71 if ($pcomponent === 'event') {
72 $this->_entityTable = 'civicrm_event';
73 $isShare = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $this->_entityId, 'is_share');
74 $this->_title = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $this->_entityId, 'title');
75 }
76 else {
77 $isShare = $values['is_share'] ?? NULL;
78 }
79 // Tell Form.tpl whether to include SocialNetwork.tpl for social media sharing
80 $this->assign('isShare', $isShare);
81 }
82 elseif ($pcomponent === 'pcp') {
83 $this->_pcpBlockId = CRM_Utils_Request::retrieve('blockId', 'Positive', $this, TRUE);
84
85 $values = [];
86 $params = ['id' => $this->_pcpBlockId];
87 CRM_Core_DAO::commonRetrieve('CRM_PCP_DAO_PCPBlock',
88 $params, $values, ['is_tellfriend_enabled', 'tellfriend_limit']
89 );
90
91 if (empty($values['is_tellfriend_enabled'])) {
92 CRM_Core_Error::statusBounce(ts('Tell Friend is disable for this Personal Campaign Page'));
93 }
94
95 $this->_mailLimit = $values['tellfriend_limit'];
96 $this->_entityTable = 'civicrm_pcp';
97
98 $sql = '
99 SELECT pcp.title,
100 contrib.campaign_id
101 FROM civicrm_pcp pcp
102 INNER JOIN civicrm_contribution_page contrib ON ( pcp.page_id = contrib.id AND pcp.page_type = "contribute" )
103 WHERE pcp.id = %1';
104 $pcp = CRM_Core_DAO::executeQuery($sql, [1 => [$this->_entityId, 'Positive']]);
105 while ($pcp->fetch()) {
106 $this->_title = $pcp->title;
107 $this->_campaignId = $pcp->campaign_id;
108 }
109
110 $this->assign('pcpTitle', $this->_title);
111 }
112 else {
113 CRM_Core_Error::statusBounce(ts('page argument missing or invalid'));
114 }
115 $this->assign('context', $pcomponent);
116
117 $this->_contactID = CRM_Core_Session::getLoggedInContactID();
118 if (!$this->_contactID) {
119 $this->_contactID = CRM_Core_Session::singleton()->get('transaction.userID');
120 }
121
122 if (!$this->_contactID) {
123 CRM_Core_Error::statusBounce(ts('Could not get the contact ID'));
124 }
125
126 // we do not want to display recently viewed items, so turn off
127 $this->assign('displayRecent', FALSE);
128 }
129
130 /**
131 * Set default values for the form.
132 *
133 *
134 * @return array
135 */
136 public function setDefaultValues() {
137 $defaults = [];
138
139 $defaults['entity_id'] = $this->_entityId;
140 $defaults['entity_table'] = $this->_entityTable;
141
142 CRM_Friend_BAO_Friend::getValues($defaults);
143 $this->setTitle(CRM_Utils_Array::value('title', $defaults));
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 /**
159 * Build the form object.
160 *
161 * @return void
162 */
163 public function buildQuickForm() {
164 $this->applyFilter('__ALL__', 'trim');
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
181 $this->add('wysiwyg', 'suggested_message', ts('Your Message'), CRM_Core_DAO::getAttribute('CRM_Friend_DAO_Friend', 'suggested_message'));
182 $friend = [];
183 $mailLimit = self::NUM_OPTION;
184 if ($this->_entityTable === 'civicrm_pcp') {
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
195 $this->addButtons([
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 ]);
207
208 $this->addFormRule(['CRM_Friend_Form', 'formRule']);
209 }
210
211 /**
212 * Validation.
213 *
214 * @param array $fields
215 *
216 * @return bool|array
217 * mixed true or array of errors
218 */
219 public static function formRule($fields) {
220
221 $errors = [];
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 /**
250 * Process the form submission.
251 *
252 * @return void
253 * @throws \CRM_Core_Exception
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');
269 $defaults = [];
270
271 $defaults['entity_id'] = $this->_entityId;
272 $defaults['entity_table'] = $this->_entityTable;
273
274 CRM_Friend_BAO_Friend::getValues($defaults);
275 if ($this->_entityTable === 'civicrm_pcp') {
276 $defaults['thankyou_text'] = $defaults['thankyou_title'] = ts('Thank you for your support');
277 $defaults['thankyou_text'] = ts('Thanks for supporting this campaign by spreading the word to your friends.');
278 }
279 elseif ($this->_entityTable === 'civicrm_contribution_page') {
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 }
291 }
292 elseif ($this->_entityTable === 'civicrm_event') {
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';
295 if ($linkText = CRM_PCP_BAO_PCP::getPcpBlockStatus($defaults['entity_id'], $defaults['entity_table'])) {
296 $linkTextUrl = CRM_Utils_System::url('civicrm/contribute/campaign',
297 "action=add&reset=1&pageId={$defaults['entity_id']}&component=event",
298 FALSE, NULL, TRUE,
299 TRUE);
300 $this->assign('linkTextUrl', $linkTextUrl);
301 $this->assign('linkText', $linkText);
302 }
303 }
304
305 $this->setTitle($defaults['thankyou_title']);
306 $this->assign('thankYouText', $defaults['thankyou_text']);
307 }
308
309 }