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