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