3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2018
37 * This class generates form components for Tell A Friend Form For End User
40 class CRM_Friend_Form
extends CRM_Core_Form
{
43 * Constants for number of friend contacts.
48 * The id of the entity that we are processing.
55 * Tell a friend id in db.
62 * The table name of the entity that we are processing.
66 protected $_entityTable;
68 protected $_campaignId;
75 protected $_contactID;
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);
81 $pcomponent = CRM_Utils_Request
::retrieve('pcomponent', 'String', $this, TRUE);
83 if (in_array($pcomponent, array(
88 $params = array('id' => $this->_entityId
);
89 CRM_Core_DAO
::commonRetrieve('CRM_Contribute_DAO_ContributionPage',
90 $params, $values, array('title', 'campaign_id', 'is_share')
92 $this->_title
= CRM_Utils_Array
::value('title', $values);
93 $this->_campaignId
= CRM_Utils_Array
::value('campaign_id', $values);
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');
98 $this->_title
= CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Event', $this->_entityId
, 'title');
101 $isShare = CRM_Utils_Array
::value('is_share', $values);
103 // Tell Form.tpl whether to include SocialNetwork.tpl for social media sharing
104 $this->assign('isShare', $isShare);
106 elseif ($pcomponent == 'pcp') {
107 $this->_pcpBlockId
= CRM_Utils_Request
::retrieve('blockId', 'Positive', $this, TRUE);
110 $params = array('id' => $this->_pcpBlockId
);
111 CRM_Core_DAO
::commonRetrieve('CRM_PCP_DAO_PCPBlock',
112 $params, $values, array('is_tellfriend_enabled', 'tellfriend_limit')
115 if (empty($values['is_tellfriend_enabled'])) {
116 CRM_Core_Error
::fatal(ts('Tell Friend is disable for this Personal Campaign Page'));
119 $this->_mailLimit
= $values['tellfriend_limit'];
120 $this->_entityTable
= 'civicrm_pcp';
126 INNER JOIN civicrm_contribution_page contrib ON ( pcp.page_id = contrib.id AND pcp.page_type = "contribute" )
128 $pcp = CRM_Core_DAO
::executeQuery($sql, array(1 => array($this->_entityId
, 'Positive')));
129 while ($pcp->fetch()) {
130 $this->_title
= $pcp->title
;
131 $this->_campaignId
= $pcp->campaign_id
;
135 $this->assign('pcpTitle', $this->_title
);
138 CRM_Core_Error
::fatal(ts('page argument missing or invalid'));
140 $this->assign('context', $pcomponent);
142 $session = CRM_Core_Session
::singleton();
143 $this->_contactID
= $session->get('userID');
144 if (!$this->_contactID
) {
145 $this->_contactID
= $session->get('transaction.userID');
148 if (!$this->_contactID
) {
149 CRM_Core_Error
::fatal(ts('Could not get the contact ID'));
152 // we do not want to display recently viewed items, so turn off
153 $this->assign('displayRecent', FALSE);
157 * Set default values for the form.
162 public function setDefaultValues() {
165 $defaults['entity_id'] = $this->_entityId
;
166 $defaults['entity_table'] = $this->_entityTable
;
168 CRM_Friend_BAO_Friend
::getValues($defaults);
169 CRM_Utils_System
::setTitle(CRM_Utils_Array
::value('title', $defaults));
171 $this->assign('title', CRM_Utils_Array
::value('title', $defaults));
172 $this->assign('intro', CRM_Utils_Array
::value('intro', $defaults));
173 $this->assign('message', CRM_Utils_Array
::value('suggested_message', $defaults));
174 $this->assign('entityID', $this->_entityId
);
176 list($fromName, $fromEmail) = CRM_Contact_BAO_Contact
::getContactDetails($this->_contactID
);
178 $defaults['from_name'] = $fromName;
179 $defaults['from_email'] = $fromEmail;
185 * Build the form object.
189 public function buildQuickForm() {
190 $this->applyFilter('__ALL__', 'trim');
192 $name = &$this->add('text',
195 CRM_Core_DAO
::getAttribute('CRM_Contact_DAO_Contact', 'first_name')
199 $email = &$this->add('text',
202 CRM_Core_DAO
::getAttribute('CRM_Core_DAO_Email', 'email'),
207 $this->add('wysiwyg', 'suggested_message', ts('Your Message'), CRM_Core_DAO
::getAttribute('CRM_Friend_DAO_Friend', 'suggested_message'));
209 $mailLimit = self
::NUM_OPTION
;
210 if ($this->_entityTable
== 'civicrm_pcp') {
211 $mailLimit = $this->_mailLimit
;
213 $this->assign('mailLimit', $mailLimit +
1);
214 for ($i = 1; $i <= $mailLimit; $i++
) {
215 $this->add('text', "friend[$i][first_name]", ts("Friend's First Name"));
216 $this->add('text', "friend[$i][last_name]", ts("Friend's Last Name"));
217 $this->add('text', "friend[$i][email]", ts("Friend's Email"));
218 $this->addRule("friend[$i][email]", ts('The format of this email address is not valid.'), 'email');
221 $this->addButtons(array(
224 'name' => ts('Send Your Message'),
225 'spacing' => ' ',
230 'name' => ts('Cancel'),
235 $this->addFormRule(array('CRM_Friend_Form', 'formRule'));
241 * @param array $fields
244 * mixed true or array of errors
246 public static function formRule($fields) {
251 foreach ($fields['friend'] as $key => $val) {
252 if (trim($val['first_name']) ||
trim($val['last_name']) ||
trim($val['email'])) {
255 if (!trim($val['first_name'])) {
256 $errors["friend[{$key}][first_name]"] = ts('Please enter your friend\'s first name.');
259 if (!trim($val['last_name'])) {
260 $errors["friend[{$key}][last_name]"] = ts('Please enter your friend\'s last name.');
263 if (!trim($val['email'])) {
264 $errors["friend[{$key}][email]"] = ts('Please enter your friend\'s email address.');
270 $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.");
273 return empty($errors) ?
TRUE : $errors;
277 * Process the form submission.
282 public function postProcess() {
283 // get the submitted form values.
284 $formValues = $this->controller
->exportValues($this->_name
);
286 $formValues['entity_id'] = $this->_entityId
;
287 $formValues['entity_table'] = $this->_entityTable
;
288 $formValues['source_contact_id'] = $this->_contactID
;
289 $formValues['is_test'] = $this->_action ?
1 : 0;
290 $formValues['title'] = $this->_title
;
291 $formValues['campaign_id'] = $this->_campaignId
;
293 CRM_Friend_BAO_Friend
::create($formValues);
295 $this->assign('status', 'thankyou');
298 $defaults['entity_id'] = $this->_entityId
;
299 $defaults['entity_table'] = $this->_entityTable
;
301 CRM_Friend_BAO_Friend
::getValues($defaults);
302 if ($this->_entityTable
== 'civicrm_pcp') {
303 $defaults['thankyou_text'] = $defaults['thankyou_title'] = ts('Thank you for your support');
304 $defaults['thankyou_text'] = ts('Thanks for supporting this campaign by spreading the word to your friends.');
306 elseif ($this->_entityTable
== 'civicrm_contribution_page') {
307 // If this is tell a friend after contributing, give donor link to create their own fundraising page
308 if ($linkText = CRM_PCP_BAO_PCP
::getPcpBlockStatus($defaults['entity_id'], $defaults['entity_table'])) {
310 $linkTextUrl = CRM_Utils_System
::url('civicrm/contribute/campaign',
311 "action=add&reset=1&pageId={$defaults['entity_id']}&component=contribute",
315 $this->assign('linkTextUrl', $linkTextUrl);
316 $this->assign('linkText', $linkText);
319 elseif ($this->_entityTable
== 'civicrm_event') {
320 // If this is tell a friend after registering for an event, give donor link to create their own fundraising page
321 require_once 'CRM/PCP/BAO/PCP.php';
322 if ($linkText = CRM_PCP_BAO_PCP
::getPcpBlockStatus($defaults['entity_id'], $defaults['entity_table'])) {
323 $linkTextUrl = CRM_Utils_System
::url('civicrm/contribute/campaign',
324 "action=add&reset=1&pageId={$defaults['entity_id']}&component=event",
327 $this->assign('linkTextUrl', $linkTextUrl);
328 $this->assign('linkText', $linkText);
332 CRM_Utils_System
::setTitle($defaults['thankyou_title']);
333 $this->assign('thankYouText', $defaults['thankyou_text']);