3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
37 * This class generates form components for processing a petition signature
40 class CRM_Campaign_Form_Petition_Signature
extends CRM_Core_Form
{
41 CONST EMAIL_THANK
= 1, EMAIL_CONFIRM
= 2, MODE_CREATE
= 4;
46 * The id of the contact associated with this signature
54 * Is this a logged in user
58 protected $_loggedIn = FALSE;
63 * @var string ("Individual"/"Household"/"Organization"). Never been tested for something else than Individual
65 protected $_ctype = 'Individual';
68 * The contact profile id attached with this petition
72 protected $_contactProfileId;
75 * The contact profile fields used for this petition
79 public $_contactProfileFields;
82 * The activity profile id attached with this petition
86 protected $_activityProfileId;
89 * The activity profile fields used for this petition
93 public $_activityProfileFields;
96 * The id of the survey (petition) we are proceessing
104 * The tag id used to set against contacts with unconfirmed email
111 * Values to use for custom profiles
119 * The params submitted by the form
127 * Which email send mode do we use
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
135 * send a confirmation request email
137 protected $_sendEmailMode;
139 protected $_image_URL;
141 protected $_defaults = NULL;
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'] = '';
157 function getContactID() {
158 $tempID = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
160 // force to ignore the authenticated user
161 if ($tempID === '0') {
165 //check if this is a checksum authentication
166 $userChecksum = CRM_Utils_Request
::retrieve('cs', 'String', $this);
168 //check for anonymous user.
169 $validUser = CRM_Contact_BAO_Contact_Utils
::validChecksum($tempID, $userChecksum);
175 // check if the user is registered and we have a contact ID
176 $session = CRM_Core_Session
::singleton();
177 return $session->get('userID');
180 public function preProcess() {
181 $this->bao
= new CRM_Campaign_BAO_Petition();
182 $this->_mode
= self
::MODE_CREATE
;
185 $this->_surveyId
= CRM_Utils_Request
::retrieve('sid', 'Positive', $this);
188 if (!$this->_surveyId
) {
189 CRM_Core_Error
::fatal('Petition id is not valid. (it needs a "sid" in the url).');
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.');
199 if ($this->petition
['is_active'] == 0) {
200 CRM_Core_Error
::fatal('Petition is no longer active.');
203 //get userID from session
204 $session = CRM_Core_Session
::singleton();
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;
212 // add the custom contact and activity profile fields to the signature form
214 $ufJoinParams = array(
215 'entity_id' => $this->_surveyId
,
216 'entity_table' => 'civicrm_survey',
217 'module' => 'CiviCampaign',
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
);
225 if (!isset($this->_contactProfileFields
['email-Primary'])) {
226 CRM_Core_Error
::fatal('The contact profile needs to contain the primary email address field');
230 $ufJoinParams['weight'] = 1;
231 $this->_activityProfileId
= CRM_Core_BAO_UFJoin
::findUFGroupId($ufJoinParams);
233 if ($this->_activityProfileId
) {
234 $this->_activityProfileFields
= CRM_Core_BAO_UFGroup
::getFields($this->_activityProfileId
, FALSE, CRM_Core_Action
::ADD
);
237 $this->setDefaultValues();
238 CRM_Utils_System
::setTitle($this->petition
['title']);
242 * Set default values for the form.
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);
257 //set custom field defaults
259 foreach ($this->_contactProfileFields
as $name => $field) {
260 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($name)) {
261 $htmlType = $field['html_type'];
263 if (!isset($this->_defaults
[$name])) {
264 CRM_Core_BAO_CustomField
::setProfileDefaults($customFieldID,
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'];
279 if (!isset($this->_defaults
[$name])) {
280 CRM_Core_BAO_CustomField
::setProfileDefaults($customFieldID,
291 $this->setDefaults($this->_defaults
);
294 public function buildQuickForm() {
295 $this->assign('survey_id', $this->_surveyId
);
296 $this->assign('petitionTitle', $this->petition
['title']);
297 if (isset($_COOKIE['signed_' . $this->_surveyId
])) {
298 if (isset($_COOKIE['confirmed_' . $this->_surveyId
])) {
299 $this->assign('duplicate', "confirmed");
302 $this->assign('duplicate', "unconfirmed");
307 $this->applyFilter('__ALL__', 'trim');
309 $this->buildCustom($this->_contactProfileId
, 'petitionContactProfile');
310 if ($this->_activityProfileId
) {
311 $this->buildCustom($this->_activityProfileId
, 'petitionActivityProfile');
314 $this->addButtons(array(
317 'name' => ts('Sign the Petition'),
325 * This function is used to add the rules (mainly global rules) for form.
326 * All local rules are added near the element
337 static function formRule($fields, $files, $errors) {
340 return empty($errors) ?
TRUE : $errors;
344 * Form submission of petition signature
350 public function postProcess() {
351 $tag_name = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::CAMPAIGN_PREFERENCES_NAME
,
357 // Check if contact 'email confirmed' tag exists, else create one
358 // This should be in the petition module initialise code to create a default tag for this
359 $tag_params['name'] = $tag_name;
360 $tag_params['version'] = 3;
361 $tag = civicrm_api('tag', 'get', $tag_params);
362 if ($tag['count'] == 0) {
364 $tag_params['description'] = $tag_name;
365 $tag_params['is_reserved'] = 1;
366 $tag_params['used_for'] = 'civicrm_contact';
367 $tag = civicrm_api('tag', 'create', $tag_params);
369 $this->_tagId
= $tag['id'];
372 // export the field values to be used for saving the profile form
373 $params = $this->controller
->exportValues($this->_name
);
375 $session = CRM_Core_Session
::singleton();
377 $params['last_modified_id'] = $session->get('userID');
378 $params['last_modified_date'] = date('YmdHis');
380 if ($this->_action
& CRM_Core_Action
::ADD
) {
381 $params['created_id'] = $session->get('userID');
382 $params['created_date'] = date('YmdHis');
385 if (isset($this->_surveyId
)) {
386 $params['sid'] = $this->_surveyId
;
389 if (isset($this->_contactId
)) {
390 $params['contactId'] = $this->_contactId
;
393 // if logged in user, skip dedupe
394 if ($this->_loggedIn
) {
395 $ids[0] = $this->_contactId
;
398 // dupeCheck - check if contact record already exists
399 // code modified from api/v2/Contact.php-function civicrm_contact_check_params()
400 $params['contact_type'] = $this->_ctype
;
401 //TODO - current dedupe finds soft deleted contacts - adding param is_deleted not working
402 // ignore soft deleted contacts
403 //$params['is_deleted'] = 0;
404 $dedupeParams = CRM_Dedupe_Finder
::formatParams($params, $params['contact_type']);
405 $dedupeParams['check_permission'] = '';
407 //dupesByParams($params, $ctype, $level = 'Unsupervised', $except = array())
408 $ids = CRM_Dedupe_Finder
::dupesByParams($dedupeParams, $params['contact_type']);
411 $petition_params['id'] = $this->_surveyId
;
413 CRM_Campaign_BAO_Survey
::retrieve($petition_params, $petition);
415 switch (count($ids)) {
417 //no matching contacts - create a new contact
418 // Add a source for this new contact
419 $params['source'] = ts('Petition Signature') . ' ' . $this->petition
['title'];
421 if ($this->petition
['bypass_confirm']) {
422 // send thank you email directly, bypassing confirmation
423 $this->_sendEmailMode
= self
::EMAIL_THANK
;
424 // Set status for signature activity to completed
425 $params['statusId'] = 2;
428 $this->_sendEmailMode
= self
::EMAIL_CONFIRM
;
430 // Set status for signature activity to scheduled until email is verified
431 $params['statusId'] = 1;
436 $this->_contactId
= $params['contactId'] = $ids[0];
438 // check if user has already signed this petition - redirects to Thank You if true
439 $this->redirectIfSigned($params);
441 if ($this->petition
['bypass_confirm']) {
442 // send thank you email directly, bypassing confirmation
443 $this->_sendEmailMode
= self
::EMAIL_THANK
;
444 // Set status for signature activity to completed
445 $params['statusId'] = 2;
449 // dedupe matched single contact, check for 'unconfirmed' tag
451 $tag = new CRM_Core_DAO_EntityTag();
452 $tag->entity_id
= $this->_contactId
;
453 $tag->tag_id
= $this->_tagId
;
455 if (!($tag->find())) {
456 // send thank you email directly, the user is known and validated
457 $this->_sendEmailMode
= self
::EMAIL_THANK
;
458 // Set status for signature activity to completed
459 $params['statusId'] = 2;
462 // send email verification email
463 $this->_sendEmailMode
= self
::EMAIL_CONFIRM
;
464 // Set status for signature activity to scheduled until email is verified
465 $params['statusId'] = 1;
471 // more than 1 matching contact
472 // for time being, take the first matching contact (not sure that's the best strategy, but better than creating another duplicate)
473 $this->_contactId
= $params['contactId'] = $ids[0];
475 // check if user has already signed this petition - redirects to Thank You if true
476 $this->redirectIfSigned($params);
478 if ($this->petition
['bypass_confirm']) {
479 // send thank you email directly, bypassing confirmation
480 $this->_sendEmailMode
= self
::EMAIL_THANK
;
481 // Set status for signature activity to completed
482 $params['statusId'] = 2;
487 $tag = new CRM_Core_DAO_EntityTag();
488 $tag->entity_id
= $this->_contactId
;
489 $tag->tag_id
= $this->_tagId
;
491 if (!($tag->find())) {
492 // send thank you email
493 $this->_sendEmailMode
= self
::EMAIL_THANK
;
494 // Set status for signature activity to completed
495 $params['statusId'] = 2;
498 // send email verification email
499 $this->_sendEmailMode
= self
::EMAIL_CONFIRM
;
500 // Set status for signature activity to scheduled until email is verified
501 $params['statusId'] = 1;
509 $transaction = new CRM_Core_Transaction();
511 $addToGroupID = isset($this->_addToGroupID
) ?
$this->_addToGroupID
: NULL;
512 $this->_contactId
= CRM_Contact_BAO_Contact
::createProfileContact($params, $this->_contactProfileFields
,
513 $this->_contactId
, $addToGroupID,
514 $this->_contactProfileId
, $this->_ctype
,
518 // get additional custom activity profile field data
519 // to save with new signature activity record
520 $surveyInfo = $this->bao
->getSurveyInfo($this->_surveyId
);
521 $customActivityFields = CRM_Core_BAO_CustomField
::getFields('Activity', FALSE, FALSE,
522 $surveyInfo['activity_type_id']
524 $customActivityFields = CRM_Utils_Array
::crmArrayMerge($customActivityFields,
525 CRM_Core_BAO_CustomField
::getFields('Activity', FALSE, FALSE,
530 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess($params,
531 $customActivityFields,
536 // create the signature activity record
537 $params['contactId'] = $this->_contactId
;
538 $params['activity_campaign_id'] = CRM_Utils_Array
::value('campaign_id', $this->petition
);
539 $result = $this->bao
->createSignature($params);
541 // send thank you or email verification emails
543 // if logged in using Facebook connect and email on form matches Fb email,
544 // no need for email confirmation, send thank you email
545 if ($this->forceEmailConfirmed
['flag'] &&
546 ($this->forceEmailConfirmed
['email'] == $params['email-Primary'])
548 $this->_sendEmailMode
= self
::EMAIL_THANK
;
551 switch ($this->_sendEmailMode
) {
552 case self
::EMAIL_THANK
:
553 // mark the signature activity as completed and set confirmed cookie
554 $this->bao
->confirmSignature($result->id
, $this->_contactId
, $this->_surveyId
);
557 case self
::EMAIL_CONFIRM
:
558 // set 'Unconfirmed' tag for this new contact
561 $tag_params['contact_id'] = $this->_contactId
;
562 $tag_params['tag_id'] = $this->_tagId
;
563 $tag_params['version'] = 3;
564 $tag_value = civicrm_api('entity_tag', 'create', $tag_params);
570 $params['activityId'] = $result->id
;
571 $params['tagId'] = $this->_tagId
;
573 $transaction->commit();
575 $this->bao
->sendEmail($params, $this->_sendEmailMode
);
578 // call the hook before we redirect
579 $this->postProcessHook();
581 // set the template to thank you
583 CRM_Utils_System
::url(
584 'civicrm/petition/thankyou',
585 'pid=' . $this->_surveyId
. '&id=' . $this->_sendEmailMode
. '&reset=1'
587 CRM_Utils_System
::redirect($url);
592 * Build the petition profile form
595 * @param string $name
596 * @param bool $viewOnly
601 function buildCustom($id, $name, $viewOnly = FALSE) {
603 $session = CRM_Core_Session
::singleton();
604 $this->assign("petition", $this->petition
);
605 //$contactID = $this->_contactId;
607 $this->assign('contact_id', $this->_contactId
);
610 // TODO: contactID is never set (commented above)
612 if (CRM_Core_BAO_UFGroup
::filterUFGroups($id, $contactID)) {
613 $fields = CRM_Core_BAO_UFGroup
::getFields($id, FALSE, CRM_Core_Action
::ADD
);
617 $fields = CRM_Core_BAO_UFGroup
::getFields($id, FALSE, CRM_Core_Action
::ADD
);
622 // unset any email-* fields since we already collect it, CRM-2888
623 foreach ( array_keys( $fields ) as $fieldName ) {
624 if ( substr( $fieldName, 0, 6 ) == 'email-' ) {
625 unset( $fields[$fieldName] );
631 $this->assign($name, $fields);
634 foreach ($fields as $key => $field) {
636 isset($field['data_type']) &&
637 $field['data_type'] == 'File' ||
($viewOnly && $field['name'] == 'image_URL')
639 // ignore file upload fields
643 // if state or country in the profile, create map
644 list($prefixName, $index) = CRM_Utils_System
::explode('-', $key, 2);
646 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, CRM_Profile_Form
::MODE_CREATE
, $contactID, TRUE);
647 $this->_fields
[$key] = $field;
648 // CRM-11316 Is ReCAPTCHA enabled for this profile AND is this an anonymous visitor
649 if ($field['add_captcha'] && !$this->_contactId
) {
654 if ($addCaptcha && !$viewOnly) {
655 $captcha = CRM_Utils_ReCAPTCHA
::singleton();
656 $captcha->add($this);
657 $this->assign("isCaptcha", TRUE);
666 function getTemplateFileName() {
667 if (isset($this->thankyou
)) {
668 return ('CRM/Campaign/Page/Petition/ThankYou.tpl');
671 return parent
::getTemplateFileName();
674 // check if user has already signed this petition
676 * @param array $params
678 function redirectIfSigned($params) {
679 $signature = $this->bao
->checkSignature($this->_surveyId
, $this->_contactId
);
680 //TODO: error case when more than one signature found for this petition and this contact
681 if (!empty($signature) && (count($signature) == 1)) {
682 $signature_id = array_keys($signature);
683 switch ($signature[$signature_id[0]]['status_id']) {
685 //status is scheduled - email is unconfirmed
686 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/petition/thankyou', 'pid=' . $this->_surveyId
. '&id=4&reset=1'));
690 //status is completed
691 $this->bao
->sendEmail($params, 1);
692 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/petition/thankyou', 'pid=' . $this->_surveyId
. '&id=5&reset=1'));