3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
13 * This class generates form components for Tell A Friend Form For End User
16 class CRM_Friend_Form
extends CRM_Core_Form
{
19 * Constants for number of friend contacts.
24 * The id of the entity that we are processing.
31 * Tell a friend id in db.
38 * The table name of the entity that we are processing.
42 protected $_entityTable;
44 protected $_campaignId;
51 protected $_contactID;
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);
57 $pcomponent = CRM_Utils_Request
::retrieve('pcomponent', 'String', $this, TRUE);
59 if (in_array($pcomponent, [
64 $params = ['id' => $this->_entityId
];
65 CRM_Core_DAO
::commonRetrieve('CRM_Contribute_DAO_ContributionPage',
66 $params, $values, ['title', 'campaign_id', 'is_share']
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');
77 $isShare = $values['is_share'] ??
NULL;
79 // Tell Form.tpl whether to include SocialNetwork.tpl for social media sharing
80 $this->assign('isShare', $isShare);
82 elseif ($pcomponent === 'pcp') {
83 $this->_pcpBlockId
= CRM_Utils_Request
::retrieve('blockId', 'Positive', $this, TRUE);
86 $params = ['id' => $this->_pcpBlockId
];
87 CRM_Core_DAO
::commonRetrieve('CRM_PCP_DAO_PCPBlock',
88 $params, $values, ['is_tellfriend_enabled', 'tellfriend_limit']
91 if (empty($values['is_tellfriend_enabled'])) {
92 CRM_Core_Error
::statusBounce(ts('Tell Friend is disable for this Personal Campaign Page'));
95 $this->_mailLimit
= $values['tellfriend_limit'];
96 $this->_entityTable
= 'civicrm_pcp';
102 INNER JOIN civicrm_contribution_page contrib ON ( pcp.page_id = contrib.id AND pcp.page_type = "contribute" )
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
;
110 $this->assign('pcpTitle', $this->_title
);
113 CRM_Core_Error
::statusBounce(ts('page argument missing or invalid'));
115 $this->assign('context', $pcomponent);
117 $this->_contactID
= CRM_Core_Session
::getLoggedInContactID();
118 if (!$this->_contactID
) {
119 $this->_contactID
= CRM_Core_Session
::singleton()->get('transaction.userID');
122 if (!$this->_contactID
) {
123 CRM_Core_Error
::statusBounce(ts('Could not get the contact ID'));
126 // we do not want to display recently viewed items, so turn off
127 $this->assign('displayRecent', FALSE);
131 * Set default values for the form.
136 public function setDefaultValues() {
139 $defaults['entity_id'] = $this->_entityId
;
140 $defaults['entity_table'] = $this->_entityTable
;
142 CRM_Friend_BAO_Friend
::getValues($defaults);
143 CRM_Utils_System
::setTitle(CRM_Utils_Array
::value('title', $defaults));
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
);
150 list($fromName, $fromEmail) = CRM_Contact_BAO_Contact
::getContactDetails($this->_contactID
);
152 $defaults['from_name'] = $fromName;
153 $defaults['from_email'] = $fromEmail;
159 * Build the form object.
163 public function buildQuickForm() {
164 $this->applyFilter('__ALL__', 'trim');
166 $name = &$this->add('text',
169 CRM_Core_DAO
::getAttribute('CRM_Contact_DAO_Contact', 'first_name')
173 $email = &$this->add('text',
176 CRM_Core_DAO
::getAttribute('CRM_Core_DAO_Email', 'email'),
181 $this->add('wysiwyg', 'suggested_message', ts('Your Message'), CRM_Core_DAO
::getAttribute('CRM_Friend_DAO_Friend', 'suggested_message'));
183 $mailLimit = self
::NUM_OPTION
;
184 if ($this->_entityTable
=== 'civicrm_pcp') {
185 $mailLimit = $this->_mailLimit
;
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');
198 'name' => ts('Send Your Message'),
199 'spacing' => ' ',
204 'name' => ts('Cancel'),
208 $this->addFormRule(['CRM_Friend_Form', 'formRule']);
214 * @param array $fields
217 * mixed true or array of errors
219 public static function formRule($fields) {
224 foreach ($fields['friend'] as $key => $val) {
225 if (trim($val['first_name']) ||
trim($val['last_name']) ||
trim($val['email'])) {
228 if (!trim($val['first_name'])) {
229 $errors["friend[{$key}][first_name]"] = ts('Please enter your friend\'s first name.');
232 if (!trim($val['last_name'])) {
233 $errors["friend[{$key}][last_name]"] = ts('Please enter your friend\'s last name.');
236 if (!trim($val['email'])) {
237 $errors["friend[{$key}][email]"] = ts('Please enter your friend\'s email address.');
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.");
246 return empty($errors) ?
TRUE : $errors;
250 * Process the form submission.
253 * @throws \CRM_Core_Exception
255 public function postProcess() {
256 // get the submitted form values.
257 $formValues = $this->controller
->exportValues($this->_name
);
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
;
266 CRM_Friend_BAO_Friend
::create($formValues);
268 $this->assign('status', 'thankyou');
271 $defaults['entity_id'] = $this->_entityId
;
272 $defaults['entity_table'] = $this->_entityTable
;
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.');
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'])) {
283 $linkTextUrl = CRM_Utils_System
::url('civicrm/contribute/campaign',
284 "action=add&reset=1&pageId={$defaults['entity_id']}&component=contribute",
288 $this->assign('linkTextUrl', $linkTextUrl);
289 $this->assign('linkText', $linkText);
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",
300 $this->assign('linkTextUrl', $linkTextUrl);
301 $this->assign('linkText', $linkText);
305 CRM_Utils_System
::setTitle($defaults['thankyou_title']);
306 $this->assign('thankYouText', $defaults['thankyou_text']);