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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
21 * This class generates form components for Tell A Friend Form For End User
24 class CRM_Friend_Form
extends CRM_Core_Form
{
27 * Constants for number of friend contacts.
32 * The id of the entity that we are processing.
39 * Tell a friend id in db.
46 * The table name of the entity that we are processing.
50 protected $_entityTable;
52 protected $_campaignId;
59 protected $_contactID;
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);
65 $pcomponent = CRM_Utils_Request
::retrieve('pcomponent', 'String', $this, TRUE);
67 if (in_array($pcomponent, [
72 $params = ['id' => $this->_entityId
];
73 CRM_Core_DAO
::commonRetrieve('CRM_Contribute_DAO_ContributionPage',
74 $params, $values, ['title', 'campaign_id', 'is_share']
76 $this->_title
= CRM_Utils_Array
::value('title', $values);
77 $this->_campaignId
= CRM_Utils_Array
::value('campaign_id', $values);
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');
82 $this->_title
= CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Event', $this->_entityId
, 'title');
85 $isShare = CRM_Utils_Array
::value('is_share', $values);
87 // Tell Form.tpl whether to include SocialNetwork.tpl for social media sharing
88 $this->assign('isShare', $isShare);
90 elseif ($pcomponent == 'pcp') {
91 $this->_pcpBlockId
= CRM_Utils_Request
::retrieve('blockId', 'Positive', $this, TRUE);
94 $params = ['id' => $this->_pcpBlockId
];
95 CRM_Core_DAO
::commonRetrieve('CRM_PCP_DAO_PCPBlock',
96 $params, $values, ['is_tellfriend_enabled', 'tellfriend_limit']
99 if (empty($values['is_tellfriend_enabled'])) {
100 CRM_Core_Error
::fatal(ts('Tell Friend is disable for this Personal Campaign Page'));
103 $this->_mailLimit
= $values['tellfriend_limit'];
104 $this->_entityTable
= 'civicrm_pcp';
110 INNER JOIN civicrm_contribution_page contrib ON ( pcp.page_id = contrib.id AND pcp.page_type = "contribute" )
112 $pcp = CRM_Core_DAO
::executeQuery($sql, [1 => [$this->_entityId
, 'Positive']]);
113 while ($pcp->fetch()) {
114 $this->_title
= $pcp->title
;
115 $this->_campaignId
= $pcp->campaign_id
;
118 $this->assign('pcpTitle', $this->_title
);
121 CRM_Core_Error
::fatal(ts('page argument missing or invalid'));
123 $this->assign('context', $pcomponent);
125 $session = CRM_Core_Session
::singleton();
126 $this->_contactID
= $session->get('userID');
127 if (!$this->_contactID
) {
128 $this->_contactID
= $session->get('transaction.userID');
131 if (!$this->_contactID
) {
132 CRM_Core_Error
::fatal(ts('Could not get the contact ID'));
135 // we do not want to display recently viewed items, so turn off
136 $this->assign('displayRecent', FALSE);
140 * Set default values for the form.
145 public function setDefaultValues() {
148 $defaults['entity_id'] = $this->_entityId
;
149 $defaults['entity_table'] = $this->_entityTable
;
151 CRM_Friend_BAO_Friend
::getValues($defaults);
152 CRM_Utils_System
::setTitle(CRM_Utils_Array
::value('title', $defaults));
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
);
159 list($fromName, $fromEmail) = CRM_Contact_BAO_Contact
::getContactDetails($this->_contactID
);
161 $defaults['from_name'] = $fromName;
162 $defaults['from_email'] = $fromEmail;
168 * Build the form object.
172 public function buildQuickForm() {
173 $this->applyFilter('__ALL__', 'trim');
175 $name = &$this->add('text',
178 CRM_Core_DAO
::getAttribute('CRM_Contact_DAO_Contact', 'first_name')
182 $email = &$this->add('text',
185 CRM_Core_DAO
::getAttribute('CRM_Core_DAO_Email', 'email'),
190 $this->add('wysiwyg', 'suggested_message', ts('Your Message'), CRM_Core_DAO
::getAttribute('CRM_Friend_DAO_Friend', 'suggested_message'));
192 $mailLimit = self
::NUM_OPTION
;
193 if ($this->_entityTable
== 'civicrm_pcp') {
194 $mailLimit = $this->_mailLimit
;
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');
207 'name' => ts('Send Your Message'),
208 'spacing' => ' ',
213 'name' => ts('Cancel'),
217 $this->addFormRule(['CRM_Friend_Form', 'formRule']);
223 * @param array $fields
226 * mixed true or array of errors
228 public static function formRule($fields) {
233 foreach ($fields['friend'] as $key => $val) {
234 if (trim($val['first_name']) ||
trim($val['last_name']) ||
trim($val['email'])) {
237 if (!trim($val['first_name'])) {
238 $errors["friend[{$key}][first_name]"] = ts('Please enter your friend\'s first name.');
241 if (!trim($val['last_name'])) {
242 $errors["friend[{$key}][last_name]"] = ts('Please enter your friend\'s last name.');
245 if (!trim($val['email'])) {
246 $errors["friend[{$key}][email]"] = ts('Please enter your friend\'s email address.');
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.");
255 return empty($errors) ?
TRUE : $errors;
259 * Process the form submission.
264 public function postProcess() {
265 // get the submitted form values.
266 $formValues = $this->controller
->exportValues($this->_name
);
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
;
275 CRM_Friend_BAO_Friend
::create($formValues);
277 $this->assign('status', 'thankyou');
280 $defaults['entity_id'] = $this->_entityId
;
281 $defaults['entity_table'] = $this->_entityTable
;
283 CRM_Friend_BAO_Friend
::getValues($defaults);
284 if ($this->_entityTable
== 'civicrm_pcp') {
285 $defaults['thankyou_text'] = $defaults['thankyou_title'] = ts('Thank you for your support');
286 $defaults['thankyou_text'] = ts('Thanks for supporting this campaign by spreading the word to your friends.');
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'])) {
292 $linkTextUrl = CRM_Utils_System
::url('civicrm/contribute/campaign',
293 "action=add&reset=1&pageId={$defaults['entity_id']}&component=contribute",
297 $this->assign('linkTextUrl', $linkTextUrl);
298 $this->assign('linkText', $linkText);
301 elseif ($this->_entityTable
== 'civicrm_event') {
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';
304 if ($linkText = CRM_PCP_BAO_PCP
::getPcpBlockStatus($defaults['entity_id'], $defaults['entity_table'])) {
305 $linkTextUrl = CRM_Utils_System
::url('civicrm/contribute/campaign',
306 "action=add&reset=1&pageId={$defaults['entity_id']}&component=event",
309 $this->assign('linkTextUrl', $linkTextUrl);
310 $this->assign('linkText', $linkText);
314 CRM_Utils_System
::setTitle($defaults['thankyou_title']);
315 $this->assign('thankYouText', $defaults['thankyou_text']);