Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
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 | /** | |
fe482240 | 48 | * The id of the entity that we are proceessing. |
6a488035 TO |
49 | * |
50 | * @var int | |
6a488035 TO |
51 | */ |
52 | protected $_entityId; | |
53 | ||
54 | /** | |
fe482240 | 55 | * The table name of the entity that we are proceessing. |
6a488035 TO |
56 | * |
57 | * @var string | |
6a488035 TO |
58 | */ |
59 | protected $_entityTable; | |
60 | ||
61 | protected $_campaignId; | |
62 | ||
63 | /** | |
fe482240 | 64 | * The contact ID. |
6a488035 TO |
65 | * |
66 | * @var int | |
6a488035 TO |
67 | */ |
68 | protected $_contactID; | |
69 | ||
70 | public function preProcess() { | |
71 | $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this); | |
72 | $this->_entityId = CRM_Utils_Request::retrieve('eid', 'Positive', $this, TRUE); | |
73 | ||
74 | $pcomponent = CRM_Utils_Request::retrieve('pcomponent', 'String', $this, TRUE); | |
75 | ||
76 | if (in_array($pcomponent, array( | |
353ffa53 | 77 | 'contribute', |
317fceb4 | 78 | 'event', |
353ffa53 | 79 | ))) { |
6a488035 TO |
80 | $values = array(); |
81 | $params = array('id' => $this->_entityId); | |
82 | CRM_Core_DAO::commonRetrieve('CRM_Contribute_DAO_ContributionPage', | |
83 | $params, $values, array('title', 'campaign_id', 'is_share') | |
84 | ); | |
353ffa53 TO |
85 | $this->_title = CRM_Utils_Array::value('title', $values); |
86 | $this->_campaignId = CRM_Utils_Array::value('campaign_id', $values); | |
6a488035 TO |
87 | $this->_entityTable = 'civicrm_contribution_page'; |
88 | if ($pcomponent == 'event') { | |
89 | $this->_entityTable = 'civicrm_event'; | |
90 | $isShare = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $this->_entityId, 'is_share'); | |
0db6c3e1 TO |
91 | } |
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 | /** | |
c490a46a | 149 | * Set default values for the form. |
6a488035 | 150 | * |
6a488035 | 151 | * |
355ba699 | 152 | * @return void |
6a488035 TO |
153 | */ |
154 | public function setDefaultValues() { | |
155 | $defaults = array(); | |
156 | ||
157 | $defaults['entity_id'] = $this->_entityId; | |
158 | $defaults['entity_table'] = $this->_entityTable; | |
159 | ||
160 | CRM_Friend_BAO_Friend::getValues($defaults); | |
161 | CRM_Utils_System::setTitle(CRM_Utils_Array::value('title', $defaults)); | |
162 | ||
163 | $this->assign('title', CRM_Utils_Array::value('title', $defaults)); | |
164 | $this->assign('intro', CRM_Utils_Array::value('intro', $defaults)); | |
165 | $this->assign('message', CRM_Utils_Array::value('suggested_message', $defaults)); | |
166 | $this->assign('entityID', $this->_entityId); | |
167 | ||
168 | list($fromName, $fromEmail) = CRM_Contact_BAO_Contact::getContactDetails($this->_contactID); | |
169 | ||
170 | $defaults['from_name'] = $fromName; | |
171 | $defaults['from_email'] = $fromEmail; | |
172 | ||
173 | return $defaults; | |
174 | } | |
175 | ||
176 | /** | |
fe482240 | 177 | * Build the form object. |
6a488035 | 178 | * |
355ba699 | 179 | * @return void |
6a488035 TO |
180 | */ |
181 | public function buildQuickForm() { | |
182 | // Details of User | |
183 | $name = &$this->add('text', | |
184 | 'from_name', | |
185 | ts('From'), | |
186 | CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'first_name') | |
187 | ); | |
188 | $name->freeze(); | |
189 | ||
190 | $email = &$this->add('text', | |
191 | 'from_email', | |
192 | ts('Your Email'), | |
193 | CRM_Core_DAO::getAttribute('CRM_Core_DAO_Email', 'email'), | |
194 | TRUE | |
195 | ); | |
196 | $email->freeze(); | |
197 | ||
5d51a2f9 | 198 | $this->add('wysiwyg', 'suggested_message', ts('Your Message'), CRM_Core_DAO::getAttribute('CRM_Friend_DAO_Friend', 'suggested_message')); |
6a488035 TO |
199 | $friend = array(); |
200 | $mailLimit = self::NUM_OPTION; | |
201 | if ($this->_entityTable == 'civicrm_pcp') { | |
202 | $mailLimit = $this->_mailLimit; | |
203 | } | |
204 | $this->assign('mailLimit', $mailLimit + 1); | |
205 | for ($i = 1; $i <= $mailLimit; $i++) { | |
206 | $this->add('text', "friend[$i][first_name]", ts("Friend's First Name")); | |
207 | $this->add('text', "friend[$i][last_name]", ts("Friend's Last Name")); | |
208 | $this->add('text', "friend[$i][email]", ts("Friend's Email")); | |
209 | $this->addRule("friend[$i][email]", ts('The format of this email address is not valid.'), 'email'); | |
210 | } | |
211 | ||
212 | $this->addButtons(array( | |
213 | array( | |
214 | 'type' => 'submit', | |
215 | 'name' => ts('Send Your Message'), | |
216 | 'spacing' => ' ', | |
217 | 'isDefault' => TRUE, | |
218 | ), | |
219 | array( | |
220 | 'type' => 'cancel', | |
221 | 'name' => ts('Cancel'), | |
222 | ), | |
223 | ) | |
224 | ); | |
225 | ||
226 | $this->addFormRule(array('CRM_Friend_Form', 'formRule')); | |
227 | } | |
228 | ||
229 | /** | |
fe482240 | 230 | * Validation. |
6a488035 | 231 | * |
c490a46a | 232 | * @param array $fields |
6a488035 | 233 | * |
72b3a70c CW |
234 | * @return bool|array |
235 | * mixed true or array of errors | |
6a488035 | 236 | */ |
00be9182 | 237 | public static function formRule($fields) { |
6a488035 TO |
238 | |
239 | $errors = array(); | |
240 | ||
241 | $valid = FALSE; | |
242 | foreach ($fields['friend'] as $key => $val) { | |
243 | if (trim($val['first_name']) || trim($val['last_name']) || trim($val['email'])) { | |
244 | $valid = TRUE; | |
245 | ||
246 | if (!trim($val['first_name'])) { | |
247 | $errors["friend[{$key}][first_name]"] = ts('Please enter your friend\'s first name.'); | |
248 | } | |
249 | ||
250 | if (!trim($val['last_name'])) { | |
251 | $errors["friend[{$key}][last_name]"] = ts('Please enter your friend\'s last name.'); | |
252 | } | |
253 | ||
254 | if (!trim($val['email'])) { | |
255 | $errors["friend[{$key}][email]"] = ts('Please enter your friend\'s email address.'); | |
256 | } | |
257 | } | |
258 | } | |
259 | ||
260 | if (!$valid) { | |
261 | $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."); | |
262 | } | |
263 | ||
264 | return empty($errors) ? TRUE : $errors; | |
265 | } | |
266 | ||
267 | /** | |
fe482240 | 268 | * Process the form submission. |
6a488035 | 269 | * |
6a488035 | 270 | * |
355ba699 | 271 | * @return void |
6a488035 TO |
272 | */ |
273 | public function postProcess() { | |
274 | // get the submitted form values. | |
275 | $formValues = $this->controller->exportValues($this->_name); | |
276 | ||
277 | $formValues['entity_id'] = $this->_entityId; | |
278 | $formValues['entity_table'] = $this->_entityTable; | |
279 | $formValues['source_contact_id'] = $this->_contactID; | |
280 | $formValues['is_test'] = $this->_action ? 1 : 0; | |
281 | $formValues['title'] = $this->_title; | |
282 | $formValues['campaign_id'] = $this->_campaignId; | |
283 | ||
284 | CRM_Friend_BAO_Friend::create($formValues); | |
285 | ||
286 | $this->assign('status', 'thankyou'); | |
287 | $defaults = array(); | |
288 | ||
289 | $defaults['entity_id'] = $this->_entityId; | |
290 | $defaults['entity_table'] = $this->_entityTable; | |
291 | ||
292 | CRM_Friend_BAO_Friend::getValues($defaults); | |
293 | if ($this->_entityTable == 'civicrm_pcp') { | |
2b3846c2 | 294 | $defaults['thankyou_text'] = $defaults['thankyou_title'] = ts('Thank you for your support'); |
6a488035 TO |
295 | $defaults['thankyou_text'] = ts('Thanks for supporting this campaign by spreading the word to your friends.'); |
296 | } | |
297 | elseif ($this->_entityTable == 'civicrm_contribution_page') { | |
298 | // If this is tell a friend after contributing, give donor link to create their own fundraising page | |
299 | if ($linkText = CRM_PCP_BAO_PCP::getPcpBlockStatus($defaults['entity_id'], $defaults['entity_table'])) { | |
300 | ||
301 | $linkTextUrl = CRM_Utils_System::url('civicrm/contribute/campaign', | |
302 | "action=add&reset=1&pageId={$defaults['entity_id']}&component=contribute", | |
303 | FALSE, NULL, TRUE, | |
304 | TRUE | |
305 | ); | |
306 | $this->assign('linkTextUrl', $linkTextUrl); | |
307 | $this->assign('linkText', $linkText); | |
308 | } | |
0db6c3e1 | 309 | } |
481a74f4 | 310 | elseif ($this->_entityTable == 'civicrm_event') { |
08ffe40c TO |
311 | // If this is tell a friend after registering for an event, give donor link to create their own fundraising page |
312 | require_once 'CRM/PCP/BAO/PCP.php'; | |
481a74f4 TO |
313 | if ($linkText = CRM_PCP_BAO_PCP::getPcpBlockStatus($defaults['entity_id'], $defaults['entity_table'])) { |
314 | $linkTextUrl = CRM_Utils_System::url('civicrm/contribute/campaign', | |
353ffa53 TO |
315 | "action=add&reset=1&pageId={$defaults['entity_id']}&component=event", |
316 | FALSE, NULL, TRUE, | |
317 | TRUE); | |
481a74f4 TO |
318 | $this->assign('linkTextUrl', $linkTextUrl); |
319 | $this->assign('linkText', $linkText); | |
08ffe40c | 320 | } |
6a488035 | 321 | } |
6a488035 TO |
322 | |
323 | CRM_Utils_System::setTitle($defaults['thankyou_title']); | |
324 | $this->assign('thankYouText', $defaults['thankyou_text']); | |
325 | } | |
96025800 | 326 | |
6a488035 | 327 | } |