Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
fa938177 | 6 | | Copyright CiviCRM LLC (c) 2004-2016 | |
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 | |
fa938177 | 31 | * @copyright CiviCRM LLC (c) 2004-2016 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
ce064e4f | 35 | * This class generates form components for processing a petition signature. |
6a488035 TO |
36 | */ |
37 | class CRM_Campaign_Form_Petition_Signature extends CRM_Core_Form { | |
7da04cde | 38 | const EMAIL_THANK = 1, EMAIL_CONFIRM = 2, MODE_CREATE = 4; |
6a488035 TO |
39 | |
40 | protected $_mode; | |
41 | ||
42 | /** | |
100fef9d | 43 | * The id of the contact associated with this signature |
6a488035 TO |
44 | * |
45 | * @var int | |
6a488035 TO |
46 | */ |
47 | public $_contactId; | |
48 | ||
49 | /** | |
50 | * Is this a logged in user | |
51 | * | |
52 | * @var int | |
53 | */ | |
54 | protected $_loggedIn = FALSE; | |
55 | ||
56 | /** | |
57 | * The contact type | |
58 | * | |
353ffa53 | 59 | * @var string ("Individual"/"Household"/"Organization"). Never been tested for something else than Individual |
6a488035 TO |
60 | */ |
61 | protected $_ctype = 'Individual'; | |
62 | ||
63 | /** | |
64 | * The contact profile id attached with this petition | |
65 | * | |
66 | * @var int | |
67 | */ | |
68 | protected $_contactProfileId; | |
69 | ||
70 | /** | |
100fef9d | 71 | * The contact profile fields used for this petition |
6a488035 TO |
72 | * |
73 | * @var array | |
74 | */ | |
75 | public $_contactProfileFields; | |
76 | ||
77 | /** | |
78 | * The activity profile id attached with this petition | |
79 | * | |
80 | * @var int | |
81 | */ | |
82 | protected $_activityProfileId; | |
83 | ||
84 | /** | |
100fef9d | 85 | * The activity profile fields used for this petition |
6a488035 TO |
86 | * |
87 | * @var array | |
88 | */ | |
89 | public $_activityProfileFields; | |
90 | ||
91 | /** | |
100fef9d | 92 | * The id of the survey (petition) we are proceessing |
6a488035 TO |
93 | * |
94 | * @var int | |
6a488035 TO |
95 | */ |
96 | public $_surveyId; | |
97 | ||
98 | /** | |
99 | * The tag id used to set against contacts with unconfirmed email | |
100 | * | |
101 | * @var int | |
102 | */ | |
103 | protected $_tagId; | |
104 | ||
105 | /** | |
100fef9d | 106 | * Values to use for custom profiles |
6a488035 TO |
107 | * |
108 | * @var array | |
6a488035 TO |
109 | */ |
110 | public $_values; | |
111 | ||
112 | /** | |
113 | * The params submitted by the form | |
114 | * | |
115 | * @var array | |
6a488035 TO |
116 | */ |
117 | protected $_params; | |
118 | ||
119 | /** | |
100fef9d | 120 | * Which email send mode do we use |
6a488035 TO |
121 | * |
122 | * @var int | |
123 | * EMAIL_THANK = 1, | |
124 | * connected user via login/pwd - thank you | |
125 | * or dedupe contact matched who doesn't have a tag CIVICRM_TAG_UNCONFIRMED - thank you | |
126 | * or login using fb connect - thank you + click to add msg to fb wall | |
127 | * EMAIL_CONFIRM = 2; | |
128 | * send a confirmation request email | |
129 | */ | |
130 | protected $_sendEmailMode; | |
131 | ||
132 | protected $_image_URL; | |
133 | ||
30c4e065 | 134 | /** |
30c4e065 | 135 | */ |
00be9182 | 136 | public function __construct() { |
6a488035 TO |
137 | parent::__construct(); |
138 | // this property used by civicrm_fb module and if true, forces thank you email to be sent | |
139 | // for users signing in via Facebook connect; also sets Fb email to check against | |
140 | $this->forceEmailConfirmed['flag'] = FALSE; | |
141 | $this->forceEmailConfirmed['email'] = ''; | |
142 | } | |
143 | ||
30c4e065 EM |
144 | /** |
145 | * @return mixed | |
146 | */ | |
00be9182 | 147 | public function getContactID() { |
6a488035 TO |
148 | $tempID = CRM_Utils_Request::retrieve('cid', 'Positive', $this); |
149 | ||
150 | // force to ignore the authenticated user | |
151 | if ($tempID === '0') { | |
152 | return $tempID; | |
153 | } | |
154 | ||
155 | //check if this is a checksum authentication | |
156 | $userChecksum = CRM_Utils_Request::retrieve('cs', 'String', $this); | |
157 | if ($userChecksum) { | |
158 | //check for anonymous user. | |
159 | $validUser = CRM_Contact_BAO_Contact_Utils::validChecksum($tempID, $userChecksum); | |
160 | if ($validUser) { | |
161 | return $tempID; | |
162 | } | |
163 | } | |
164 | ||
165 | // check if the user is registered and we have a contact ID | |
166 | $session = CRM_Core_Session::singleton(); | |
167 | return $session->get('userID'); | |
168 | } | |
169 | ||
170 | public function preProcess() { | |
171 | $this->bao = new CRM_Campaign_BAO_Petition(); | |
172 | $this->_mode = self::MODE_CREATE; | |
173 | ||
174 | //get the survey id | |
175 | $this->_surveyId = CRM_Utils_Request::retrieve('sid', 'Positive', $this); | |
176 | ||
177 | //some sanity checks | |
178 | if (!$this->_surveyId) { | |
179 | CRM_Core_Error::fatal('Petition id is not valid. (it needs a "sid" in the url).'); | |
180 | return; | |
181 | } | |
182 | //check petition is valid and active | |
183 | $params['id'] = $this->_surveyId; | |
184 | $this->petition = array(); | |
185 | CRM_Campaign_BAO_Survey::retrieve($params, $this->petition); | |
186 | if (empty($this->petition)) { | |
187 | CRM_Core_Error::fatal('Petition doesn\'t exist.'); | |
188 | } | |
189 | if ($this->petition['is_active'] == 0) { | |
190 | CRM_Core_Error::fatal('Petition is no longer active.'); | |
191 | } | |
192 | ||
193 | //get userID from session | |
194 | $session = CRM_Core_Session::singleton(); | |
195 | ||
196 | //get the contact id for this user if logged in | |
197 | $this->_contactId = $this->getContactId(); | |
198 | if (isset($this->_contactId)) { | |
199 | $this->_loggedIn = TRUE; | |
200 | } | |
201 | ||
202 | // add the custom contact and activity profile fields to the signature form | |
203 | ||
204 | $ufJoinParams = array( | |
205 | 'entity_id' => $this->_surveyId, | |
206 | 'entity_table' => 'civicrm_survey', | |
207 | 'module' => 'CiviCampaign', | |
208 | 'weight' => 2, | |
209 | ); | |
210 | ||
211 | $this->_contactProfileId = CRM_Core_BAO_UFJoin::findUFGroupId($ufJoinParams); | |
212 | if ($this->_contactProfileId) { | |
213 | $this->_contactProfileFields = CRM_Core_BAO_UFGroup::getFields($this->_contactProfileId, FALSE, CRM_Core_Action::ADD); | |
214 | } | |
215 | if (!isset($this->_contactProfileFields['email-Primary'])) { | |
216 | CRM_Core_Error::fatal('The contact profile needs to contain the primary email address field'); | |
217 | } | |
218 | ||
6a488035 TO |
219 | $ufJoinParams['weight'] = 1; |
220 | $this->_activityProfileId = CRM_Core_BAO_UFJoin::findUFGroupId($ufJoinParams); | |
221 | ||
222 | if ($this->_activityProfileId) { | |
223 | $this->_activityProfileFields = CRM_Core_BAO_UFGroup::getFields($this->_activityProfileId, FALSE, CRM_Core_Action::ADD); | |
224 | } | |
225 | ||
226 | $this->setDefaultValues(); | |
227 | CRM_Utils_System::setTitle($this->petition['title']); | |
228 | } | |
229 | ||
230 | /** | |
c490a46a | 231 | * Set default values for the form. |
6a488035 | 232 | */ |
00be9182 | 233 | public function setDefaultValues() { |
6a488035 TO |
234 | $this->_defaults = array(); |
235 | if ($this->_contactId) { | |
236 | CRM_Core_BAO_UFGroup::setProfileDefaults($this->_contactId, $this->_contactProfileFields, $this->_defaults, TRUE); | |
237 | if ($this->_activityProfileId) { | |
238 | CRM_Core_BAO_UFGroup::setProfileDefaults($this->_contactId, $this->_activityProfileFields, $this->_defaults, TRUE); | |
239 | } | |
240 | } | |
241 | ||
242 | //set custom field defaults | |
243 | ||
244 | foreach ($this->_contactProfileFields as $name => $field) { | |
245 | if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($name)) { | |
246 | $htmlType = $field['html_type']; | |
247 | ||
248 | if (!isset($this->_defaults[$name])) { | |
249 | CRM_Core_BAO_CustomField::setProfileDefaults($customFieldID, | |
250 | $name, | |
251 | $this->_defaults, | |
252 | $this->_contactId, | |
253 | $this->_mode | |
254 | ); | |
255 | } | |
256 | } | |
257 | } | |
258 | ||
259 | if ($this->_activityProfileFields) { | |
260 | foreach ($this->_activityProfileFields as $name => $field) { | |
261 | if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($name)) { | |
262 | $htmlType = $field['html_type']; | |
263 | ||
264 | if (!isset($this->_defaults[$name])) { | |
265 | CRM_Core_BAO_CustomField::setProfileDefaults($customFieldID, | |
266 | $name, | |
267 | $this->_defaults, | |
268 | $this->_contactId, | |
269 | $this->_mode | |
270 | ); | |
271 | } | |
272 | } | |
273 | } | |
274 | } | |
275 | ||
276 | $this->setDefaults($this->_defaults); | |
277 | } | |
278 | ||
279 | public function buildQuickForm() { | |
280 | $this->assign('survey_id', $this->_surveyId); | |
281 | $this->assign('petitionTitle', $this->petition['title']); | |
282 | if (isset($_COOKIE['signed_' . $this->_surveyId])) { | |
283 | if (isset($_COOKIE['confirmed_' . $this->_surveyId])) { | |
284 | $this->assign('duplicate', "confirmed"); | |
285 | } | |
286 | else { | |
287 | $this->assign('duplicate', "unconfirmed"); | |
288 | } | |
289 | return; | |
290 | } | |
291 | ||
292 | $this->applyFilter('__ALL__', 'trim'); | |
293 | ||
294 | $this->buildCustom($this->_contactProfileId, 'petitionContactProfile'); | |
295 | if ($this->_activityProfileId) { | |
296 | $this->buildCustom($this->_activityProfileId, 'petitionActivityProfile'); | |
297 | } | |
298 | // add buttons | |
299 | $this->addButtons(array( | |
300 | array( | |
b97e43f7 | 301 | 'type' => 'upload', |
6a488035 TO |
302 | 'name' => ts('Sign the Petition'), |
303 | 'isDefault' => TRUE, | |
304 | ), | |
305 | ) | |
306 | ); | |
307 | } | |
308 | ||
309 | /** | |
dc195289 | 310 | * add the rules (mainly global rules) for form. |
6a488035 TO |
311 | * All local rules are added near the element |
312 | * | |
da6b46f4 EM |
313 | * @param $fields |
314 | * @param $files | |
315 | * @param $errors | |
316 | * | |
6a488035 | 317 | * @see valid_date |
ce064e4f | 318 | * @return array|bool |
6a488035 | 319 | */ |
00be9182 | 320 | public static function formRule($fields, $files, $errors) { |
6a488035 TO |
321 | $errors = array(); |
322 | ||
323 | return empty($errors) ? TRUE : $errors; | |
324 | } | |
325 | ||
326 | /** | |
fe482240 | 327 | * Form submission of petition signature. |
6a488035 TO |
328 | */ |
329 | public function postProcess() { | |
aaffa79f | 330 | $tag_name = Civi::settings()->get('tag_unconfirmed'); |
6a488035 | 331 | |
6a488035 TO |
332 | if ($tag_name) { |
333 | // Check if contact 'email confirmed' tag exists, else create one | |
334 | // This should be in the petition module initialise code to create a default tag for this | |
335 | $tag_params['name'] = $tag_name; | |
336 | $tag_params['version'] = 3; | |
337 | $tag = civicrm_api('tag', 'get', $tag_params); | |
338 | if ($tag['count'] == 0) { | |
339 | //create tag | |
340 | $tag_params['description'] = $tag_name; | |
341 | $tag_params['is_reserved'] = 1; | |
342 | $tag_params['used_for'] = 'civicrm_contact'; | |
343 | $tag = civicrm_api('tag', 'create', $tag_params); | |
344 | } | |
345 | $this->_tagId = $tag['id']; | |
346 | } | |
347 | ||
348 | // export the field values to be used for saving the profile form | |
349 | $params = $this->controller->exportValues($this->_name); | |
350 | ||
351 | $session = CRM_Core_Session::singleton(); | |
352 | // format params | |
353 | $params['last_modified_id'] = $session->get('userID'); | |
354 | $params['last_modified_date'] = date('YmdHis'); | |
355 | ||
356 | if ($this->_action & CRM_Core_Action::ADD) { | |
357 | $params['created_id'] = $session->get('userID'); | |
358 | $params['created_date'] = date('YmdHis'); | |
359 | } | |
360 | ||
361 | if (isset($this->_surveyId)) { | |
362 | $params['sid'] = $this->_surveyId; | |
363 | } | |
364 | ||
365 | if (isset($this->_contactId)) { | |
366 | $params['contactId'] = $this->_contactId; | |
367 | } | |
368 | ||
369 | // if logged in user, skip dedupe | |
370 | if ($this->_loggedIn) { | |
371 | $ids[0] = $this->_contactId; | |
372 | } | |
373 | else { | |
374 | // dupeCheck - check if contact record already exists | |
375 | // code modified from api/v2/Contact.php-function civicrm_contact_check_params() | |
376 | $params['contact_type'] = $this->_ctype; | |
377 | //TODO - current dedupe finds soft deleted contacts - adding param is_deleted not working | |
378 | // ignore soft deleted contacts | |
379 | //$params['is_deleted'] = 0; | |
380 | $dedupeParams = CRM_Dedupe_Finder::formatParams($params, $params['contact_type']); | |
381 | $dedupeParams['check_permission'] = ''; | |
382 | ||
383 | //dupesByParams($params, $ctype, $level = 'Unsupervised', $except = array()) | |
384 | $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, $params['contact_type']); | |
385 | } | |
386 | ||
353ffa53 TO |
387 | $petition_params['id'] = $this->_surveyId; |
388 | $petition = array(); | |
389 | CRM_Campaign_BAO_Survey::retrieve($petition_params, $petition); | |
6a488035 TO |
390 | |
391 | switch (count($ids)) { | |
392 | case 0: | |
393 | //no matching contacts - create a new contact | |
394 | // Add a source for this new contact | |
395 | $params['source'] = ts('Petition Signature') . ' ' . $this->petition['title']; | |
396 | ||
397 | if ($this->petition['bypass_confirm']) { | |
398 | // send thank you email directly, bypassing confirmation | |
399 | $this->_sendEmailMode = self::EMAIL_THANK; | |
400 | // Set status for signature activity to completed | |
401 | $params['statusId'] = 2; | |
402 | } | |
403 | else { | |
353ffa53 | 404 | $this->_sendEmailMode = self::EMAIL_CONFIRM; |
6a488035 | 405 | |
353ffa53 TO |
406 | // Set status for signature activity to scheduled until email is verified |
407 | $params['statusId'] = 1; | |
6a488035 TO |
408 | } |
409 | break; | |
410 | ||
411 | case 1: | |
412 | $this->_contactId = $params['contactId'] = $ids[0]; | |
413 | ||
414 | // check if user has already signed this petition - redirects to Thank You if true | |
415 | $this->redirectIfSigned($params); | |
416 | ||
417 | if ($this->petition['bypass_confirm']) { | |
418 | // send thank you email directly, bypassing confirmation | |
419 | $this->_sendEmailMode = self::EMAIL_THANK; | |
420 | // Set status for signature activity to completed | |
421 | $params['statusId'] = 2; | |
422 | break; | |
423 | } | |
424 | ||
425 | // dedupe matched single contact, check for 'unconfirmed' tag | |
426 | if ($tag_name) { | |
353ffa53 | 427 | $tag = new CRM_Core_DAO_EntityTag(); |
6a488035 | 428 | $tag->entity_id = $this->_contactId; |
353ffa53 | 429 | $tag->tag_id = $this->_tagId; |
6a488035 TO |
430 | |
431 | if (!($tag->find())) { | |
432 | // send thank you email directly, the user is known and validated | |
433 | $this->_sendEmailMode = self::EMAIL_THANK; | |
434 | // Set status for signature activity to completed | |
435 | $params['statusId'] = 2; | |
436 | } | |
437 | else { | |
438 | // send email verification email | |
439 | $this->_sendEmailMode = self::EMAIL_CONFIRM; | |
440 | // Set status for signature activity to scheduled until email is verified | |
441 | $params['statusId'] = 1; | |
442 | } | |
443 | } | |
444 | break; | |
445 | ||
446 | default: | |
447 | // more than 1 matching contact | |
448 | // for time being, take the first matching contact (not sure that's the best strategy, but better than creating another duplicate) | |
449 | $this->_contactId = $params['contactId'] = $ids[0]; | |
450 | ||
451 | // check if user has already signed this petition - redirects to Thank You if true | |
452 | $this->redirectIfSigned($params); | |
453 | ||
454 | if ($this->petition['bypass_confirm']) { | |
455 | // send thank you email directly, bypassing confirmation | |
456 | $this->_sendEmailMode = self::EMAIL_THANK; | |
457 | // Set status for signature activity to completed | |
458 | $params['statusId'] = 2; | |
459 | break; | |
460 | } | |
461 | ||
462 | if ($tag_name) { | |
353ffa53 | 463 | $tag = new CRM_Core_DAO_EntityTag(); |
6a488035 | 464 | $tag->entity_id = $this->_contactId; |
353ffa53 | 465 | $tag->tag_id = $this->_tagId; |
6a488035 TO |
466 | |
467 | if (!($tag->find())) { | |
468 | // send thank you email | |
469 | $this->_sendEmailMode = self::EMAIL_THANK; | |
470 | // Set status for signature activity to completed | |
471 | $params['statusId'] = 2; | |
472 | } | |
473 | else { | |
474 | // send email verification email | |
475 | $this->_sendEmailMode = self::EMAIL_CONFIRM; | |
476 | // Set status for signature activity to scheduled until email is verified | |
477 | $params['statusId'] = 1; | |
478 | } | |
479 | } | |
480 | break; | |
481 | } | |
482 | ||
6a488035 TO |
483 | $transaction = new CRM_Core_Transaction(); |
484 | ||
32ad9152 | 485 | // CRM-17029 - get the add_to_group_id from the _contactProfileFields array. |
26016861 J |
486 | // There's a much more elegant solution with |
487 | // array_values($this->_contactProfileFields)[0] but it's PHP 5.4+ only. | |
488 | $slice = array_slice($this->_contactProfileFields, 0, 1); | |
489 | $firstField = array_shift($slice); | |
32ad9152 | 490 | $addToGroupID = isset($firstField['add_to_group_id']) ? $firstField['add_to_group_id'] : NULL; |
6a488035 TO |
491 | $this->_contactId = CRM_Contact_BAO_Contact::createProfileContact($params, $this->_contactProfileFields, |
492 | $this->_contactId, $addToGroupID, | |
493 | $this->_contactProfileId, $this->_ctype, | |
494 | TRUE | |
495 | ); | |
496 | ||
497 | // get additional custom activity profile field data | |
498 | // to save with new signature activity record | |
499 | $surveyInfo = $this->bao->getSurveyInfo($this->_surveyId); | |
500 | $customActivityFields = CRM_Core_BAO_CustomField::getFields('Activity', FALSE, FALSE, | |
501 | $surveyInfo['activity_type_id'] | |
502 | ); | |
503 | $customActivityFields = CRM_Utils_Array::crmArrayMerge($customActivityFields, | |
504 | CRM_Core_BAO_CustomField::getFields('Activity', FALSE, FALSE, | |
505 | NULL, NULL, TRUE | |
506 | ) | |
507 | ); | |
508 | ||
509 | $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, | |
6a488035 TO |
510 | NULL, |
511 | 'Activity' | |
512 | ); | |
513 | ||
514 | // create the signature activity record | |
515 | $params['contactId'] = $this->_contactId; | |
43427a24 | 516 | $params['activity_campaign_id'] = CRM_Utils_Array::value('campaign_id', $this->petition); |
6a488035 TO |
517 | $result = $this->bao->createSignature($params); |
518 | ||
519 | // send thank you or email verification emails | |
520 | ||
521 | // if logged in using Facebook connect and email on form matches Fb email, | |
522 | // no need for email confirmation, send thank you email | |
523 | if ($this->forceEmailConfirmed['flag'] && | |
524 | ($this->forceEmailConfirmed['email'] == $params['email-Primary']) | |
525 | ) { | |
526 | $this->_sendEmailMode = self::EMAIL_THANK; | |
527 | } | |
528 | ||
529 | switch ($this->_sendEmailMode) { | |
530 | case self::EMAIL_THANK: | |
531 | // mark the signature activity as completed and set confirmed cookie | |
532 | $this->bao->confirmSignature($result->id, $this->_contactId, $this->_surveyId); | |
533 | break; | |
534 | ||
535 | case self::EMAIL_CONFIRM: | |
536 | // set 'Unconfirmed' tag for this new contact | |
537 | if ($tag_name) { | |
538 | unset($tag_params); | |
539 | $tag_params['contact_id'] = $this->_contactId; | |
540 | $tag_params['tag_id'] = $this->_tagId; | |
83b0a3cc | 541 | $tag_params['version'] = 3; |
6a488035 TO |
542 | $tag_value = civicrm_api('entity_tag', 'create', $tag_params); |
543 | } | |
544 | break; | |
545 | } | |
546 | ||
547 | //send email | |
548 | $params['activityId'] = $result->id; | |
549 | $params['tagId'] = $this->_tagId; | |
6a488035 TO |
550 | |
551 | $transaction->commit(); | |
552 | ||
83b0a3cc | 553 | $this->bao->sendEmail($params, $this->_sendEmailMode); |
554 | ||
6a488035 TO |
555 | if ($result) { |
556 | // call the hook before we redirect | |
557 | $this->postProcessHook(); | |
558 | ||
559 | // set the template to thank you | |
8d7a9d07 CB |
560 | $url = CRM_Utils_System::url( |
561 | 'civicrm/petition/thankyou', | |
562 | 'pid=' . $this->_surveyId . '&id=' . $this->_sendEmailMode . '&reset=1' | |
563 | ); | |
6a488035 TO |
564 | CRM_Utils_System::redirect($url); |
565 | } | |
566 | } | |
567 | ||
568 | /** | |
fe482240 | 569 | * Build the petition profile form. |
6a488035 | 570 | * |
100fef9d CW |
571 | * @param int $id |
572 | * @param string $name | |
2a6da8d7 | 573 | * @param bool $viewOnly |
6a488035 | 574 | */ |
00be9182 | 575 | public function buildCustom($id, $name, $viewOnly = FALSE) { |
6a488035 TO |
576 | if ($id) { |
577 | $session = CRM_Core_Session::singleton(); | |
578 | $this->assign("petition", $this->petition); | |
579 | //$contactID = $this->_contactId; | |
580 | $contactID = NULL; | |
581 | $this->assign('contact_id', $this->_contactId); | |
582 | ||
583 | $fields = NULL; | |
584 | // TODO: contactID is never set (commented above) | |
585 | if ($contactID) { | |
586 | if (CRM_Core_BAO_UFGroup::filterUFGroups($id, $contactID)) { | |
587 | $fields = CRM_Core_BAO_UFGroup::getFields($id, FALSE, CRM_Core_Action::ADD); | |
588 | } | |
589 | } | |
590 | else { | |
591 | $fields = CRM_Core_BAO_UFGroup::getFields($id, FALSE, CRM_Core_Action::ADD); | |
592 | } | |
593 | ||
594 | if ($fields) { | |
6a488035 TO |
595 | $this->assign($name, $fields); |
596 | ||
597 | $addCaptcha = FALSE; | |
598 | foreach ($fields as $key => $field) { | |
599 | if ($viewOnly && | |
600 | isset($field['data_type']) && | |
601 | $field['data_type'] == 'File' || ($viewOnly && $field['name'] == 'image_URL') | |
602 | ) { | |
603 | // ignore file upload fields | |
604 | continue; | |
605 | } | |
2efcf0c2 | 606 | |
607 | // if state or country in the profile, create map | |
2c6b0601 | 608 | list($prefixName, $index) = CRM_Utils_System::explode('-', $key, 2); |
6a488035 TO |
609 | |
610 | CRM_Core_BAO_UFGroup::buildProfile($this, $field, CRM_Profile_Form::MODE_CREATE, $contactID, TRUE); | |
611 | $this->_fields[$key] = $field; | |
71fc6ea4 DG |
612 | // CRM-11316 Is ReCAPTCHA enabled for this profile AND is this an anonymous visitor |
613 | if ($field['add_captcha'] && !$this->_contactId) { | |
6a488035 TO |
614 | $addCaptcha = TRUE; |
615 | } | |
616 | } | |
617 | ||
71fc6ea4 | 618 | if ($addCaptcha && !$viewOnly) { |
6a488035 TO |
619 | $captcha = CRM_Utils_ReCAPTCHA::singleton(); |
620 | $captcha->add($this); | |
621 | $this->assign("isCaptcha", TRUE); | |
622 | } | |
623 | } | |
624 | } | |
625 | } | |
626 | ||
30c4e065 EM |
627 | /** |
628 | * @return string | |
629 | */ | |
00be9182 | 630 | public function getTemplateFileName() { |
6a488035 TO |
631 | if (isset($this->thankyou)) { |
632 | return ('CRM/Campaign/Page/Petition/ThankYou.tpl'); | |
633 | } | |
353ffa53 TO |
634 | else { |
635 | } | |
6a488035 TO |
636 | return parent::getTemplateFileName(); |
637 | } | |
638 | ||
30c4e065 | 639 | /** |
fe482240 | 640 | * check if user has already signed this petition. |
c490a46a | 641 | * @param array $params |
30c4e065 | 642 | */ |
00be9182 | 643 | public function redirectIfSigned($params) { |
6a488035 TO |
644 | $signature = $this->bao->checkSignature($this->_surveyId, $this->_contactId); |
645 | //TODO: error case when more than one signature found for this petition and this contact | |
646 | if (!empty($signature) && (count($signature) == 1)) { | |
647 | $signature_id = array_keys($signature); | |
648 | switch ($signature[$signature_id[0]]['status_id']) { | |
649 | case 1: | |
650 | //status is scheduled - email is unconfirmed | |
651 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/petition/thankyou', 'pid=' . $this->_surveyId . '&id=4&reset=1')); | |
652 | break; | |
653 | ||
654 | case 2: | |
655 | //status is completed | |
656 | $this->bao->sendEmail($params, 1); | |
657 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/petition/thankyou', 'pid=' . $this->_surveyId . '&id=5&reset=1')); | |
658 | break; | |
659 | } | |
660 | } | |
661 | } | |
96025800 | 662 | |
6a488035 | 663 | } |