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