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;
143 function __construct() {
144 parent
::__construct();
145 // this property used by civicrm_fb module and if true, forces thank you email to be sent
146 // for users signing in via Facebook connect; also sets Fb email to check against
147 $this->forceEmailConfirmed
['flag'] = FALSE;
148 $this->forceEmailConfirmed
['email'] = '';
151 function getContactID() {
152 $tempID = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
154 // force to ignore the authenticated user
155 if ($tempID === '0') {
159 //check if this is a checksum authentication
160 $userChecksum = CRM_Utils_Request
::retrieve('cs', 'String', $this);
162 //check for anonymous user.
163 $validUser = CRM_Contact_BAO_Contact_Utils
::validChecksum($tempID, $userChecksum);
169 // check if the user is registered and we have a contact ID
170 $session = CRM_Core_Session
::singleton();
171 return $session->get('userID');
174 public function preProcess() {
175 $this->bao
= new CRM_Campaign_BAO_Petition();
176 $this->_mode
= self
::MODE_CREATE
;
179 $this->_surveyId
= CRM_Utils_Request
::retrieve('sid', 'Positive', $this);
182 if (!$this->_surveyId
) {
183 CRM_Core_Error
::fatal('Petition id is not valid. (it needs a "sid" in the url).');
186 //check petition is valid and active
187 $params['id'] = $this->_surveyId
;
188 $this->petition
= array();
189 CRM_Campaign_BAO_Survey
::retrieve($params, $this->petition
);
190 if (empty($this->petition
)) {
191 CRM_Core_Error
::fatal('Petition doesn\'t exist.');
193 if ($this->petition
['is_active'] == 0) {
194 CRM_Core_Error
::fatal('Petition is no longer active.');
197 //get userID from session
198 $session = CRM_Core_Session
::singleton();
200 //get the contact id for this user if logged in
201 $this->_contactId
= $this->getContactId();
202 if (isset($this->_contactId
)) {
203 $this->_loggedIn
= TRUE;
206 // add the custom contact and activity profile fields to the signature form
208 $ufJoinParams = array(
209 'entity_id' => $this->_surveyId
,
210 'entity_table' => 'civicrm_survey',
211 'module' => 'CiviCampaign',
215 $this->_contactProfileId
= CRM_Core_BAO_UFJoin
::findUFGroupId($ufJoinParams);
216 if ($this->_contactProfileId
) {
217 $this->_contactProfileFields
= CRM_Core_BAO_UFGroup
::getFields($this->_contactProfileId
, FALSE, CRM_Core_Action
::ADD
);
219 if (!isset($this->_contactProfileFields
['email-Primary'])) {
220 CRM_Core_Error
::fatal('The contact profile needs to contain the primary email address field');
224 $ufJoinParams['weight'] = 1;
225 $this->_activityProfileId
= CRM_Core_BAO_UFJoin
::findUFGroupId($ufJoinParams);
227 if ($this->_activityProfileId
) {
228 $this->_activityProfileFields
= CRM_Core_BAO_UFGroup
::getFields($this->_activityProfileId
, FALSE, CRM_Core_Action
::ADD
);
231 $this->setDefaultValues();
232 CRM_Utils_System
::setTitle($this->petition
['title']);
236 * This function sets the default values for the form.
242 function setDefaultValues() {
243 $this->_defaults
= array();
244 if ($this->_contactId
) {
245 CRM_Core_BAO_UFGroup
::setProfileDefaults($this->_contactId
, $this->_contactProfileFields
, $this->_defaults
, TRUE);
246 if ($this->_activityProfileId
) {
247 CRM_Core_BAO_UFGroup
::setProfileDefaults($this->_contactId
, $this->_activityProfileFields
, $this->_defaults
, TRUE);
251 //set custom field defaults
253 foreach ($this->_contactProfileFields
as $name => $field) {
254 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($name)) {
255 $htmlType = $field['html_type'];
257 if (!isset($this->_defaults
[$name])) {
258 CRM_Core_BAO_CustomField
::setProfileDefaults($customFieldID,
268 if ($this->_activityProfileFields
) {
269 foreach ($this->_activityProfileFields
as $name => $field) {
270 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($name)) {
271 $htmlType = $field['html_type'];
273 if (!isset($this->_defaults
[$name])) {
274 CRM_Core_BAO_CustomField
::setProfileDefaults($customFieldID,
285 $this->setDefaults($this->_defaults
);
287 // add in all state country selectors for enabled countries
288 CRM_Core_BAO_Address
::fixAllStateSelects($this, $this->_defaults
);
291 public function buildQuickForm() {
292 $this->assign('survey_id', $this->_surveyId
);
293 $this->assign('petitionTitle', $this->petition
['title']);
294 if (isset($_COOKIE['signed_' . $this->_surveyId
])) {
295 if (isset($_COOKIE['confirmed_' . $this->_surveyId
])) {
296 $this->assign('duplicate', "confirmed");
299 $this->assign('duplicate', "unconfirmed");
304 $this->applyFilter('__ALL__', 'trim');
306 $this->buildCustom($this->_contactProfileId
, 'petitionContactProfile');
307 if ($this->_activityProfileId
) {
308 $this->buildCustom($this->_activityProfileId
, 'petitionActivityProfile');
311 $this->addButtons(array(
314 'name' => ts('Sign the Petition'),
322 * This function is used to add the rules (mainly global rules) for form.
323 * All local rules are added near the element
334 static function formRule($fields, $files, $errors) {
337 return empty($errors) ?
TRUE : $errors;
341 * Form submission of petition signature
347 public function postProcess() {
348 $tag_name = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::CAMPAIGN_PREFERENCES_NAME
,
354 // Check if contact 'email confirmed' tag exists, else create one
355 // This should be in the petition module initialise code to create a default tag for this
356 $tag_params['name'] = $tag_name;
357 $tag_params['version'] = 3;
358 $tag = civicrm_api('tag', 'get', $tag_params);
359 if ($tag['count'] == 0) {
361 $tag_params['description'] = $tag_name;
362 $tag_params['is_reserved'] = 1;
363 $tag_params['used_for'] = 'civicrm_contact';
364 $tag = civicrm_api('tag', 'create', $tag_params);
366 $this->_tagId
= $tag['id'];
369 // export the field values to be used for saving the profile form
370 $params = $this->controller
->exportValues($this->_name
);
372 $session = CRM_Core_Session
::singleton();
374 $params['last_modified_id'] = $session->get('userID');
375 $params['last_modified_date'] = date('YmdHis');
377 if ($this->_action
& CRM_Core_Action
::ADD
) {
378 $params['created_id'] = $session->get('userID');
379 $params['created_date'] = date('YmdHis');
382 if (isset($this->_surveyId
)) {
383 $params['sid'] = $this->_surveyId
;
386 if (isset($this->_contactId
)) {
387 $params['contactId'] = $this->_contactId
;
390 // if logged in user, skip dedupe
391 if ($this->_loggedIn
) {
392 $ids[0] = $this->_contactId
;
395 // dupeCheck - check if contact record already exists
396 // code modified from api/v2/Contact.php-function civicrm_contact_check_params()
397 $params['contact_type'] = $this->_ctype
;
398 //TODO - current dedupe finds soft deleted contacts - adding param is_deleted not working
399 // ignore soft deleted contacts
400 //$params['is_deleted'] = 0;
401 $dedupeParams = CRM_Dedupe_Finder
::formatParams($params, $params['contact_type']);
402 $dedupeParams['check_permission'] = '';
404 //dupesByParams($params, $ctype, $level = 'Unsupervised', $except = array())
405 $ids = CRM_Dedupe_Finder
::dupesByParams($dedupeParams, $params['contact_type']);
408 $petition_params['id'] = $this->_surveyId
;
410 CRM_Campaign_BAO_Survey
::retrieve($petition_params, $petition);
412 switch (count($ids)) {
414 //no matching contacts - create a new contact
415 // Add a source for this new contact
416 $params['source'] = ts('Petition Signature') . ' ' . $this->petition
['title'];
418 if ($this->petition
['bypass_confirm']) {
419 // send thank you email directly, bypassing confirmation
420 $this->_sendEmailMode
= self
::EMAIL_THANK
;
421 // Set status for signature activity to completed
422 $params['statusId'] = 2;
425 $this->_sendEmailMode
= self
::EMAIL_CONFIRM
;
427 // Set status for signature activity to scheduled until email is verified
428 $params['statusId'] = 1;
433 $this->_contactId
= $params['contactId'] = $ids[0];
435 // check if user has already signed this petition - redirects to Thank You if true
436 $this->redirectIfSigned($params);
438 if ($this->petition
['bypass_confirm']) {
439 // send thank you email directly, bypassing confirmation
440 $this->_sendEmailMode
= self
::EMAIL_THANK
;
441 // Set status for signature activity to completed
442 $params['statusId'] = 2;
446 // dedupe matched single contact, check for 'unconfirmed' tag
448 $tag = new CRM_Core_DAO_EntityTag();
449 $tag->entity_id
= $this->_contactId
;
450 $tag->tag_id
= $this->_tagId
;
452 if (!($tag->find())) {
453 // send thank you email directly, the user is known and validated
454 $this->_sendEmailMode
= self
::EMAIL_THANK
;
455 // Set status for signature activity to completed
456 $params['statusId'] = 2;
459 // send email verification email
460 $this->_sendEmailMode
= self
::EMAIL_CONFIRM
;
461 // Set status for signature activity to scheduled until email is verified
462 $params['statusId'] = 1;
468 // more than 1 matching contact
469 // for time being, take the first matching contact (not sure that's the best strategy, but better than creating another duplicate)
470 $this->_contactId
= $params['contactId'] = $ids[0];
472 // check if user has already signed this petition - redirects to Thank You if true
473 $this->redirectIfSigned($params);
475 if ($this->petition
['bypass_confirm']) {
476 // send thank you email directly, bypassing confirmation
477 $this->_sendEmailMode
= self
::EMAIL_THANK
;
478 // Set status for signature activity to completed
479 $params['statusId'] = 2;
484 $tag = new CRM_Core_DAO_EntityTag();
485 $tag->entity_id
= $this->_contactId
;
486 $tag->tag_id
= $this->_tagId
;
488 if (!($tag->find())) {
489 // send thank you email
490 $this->_sendEmailMode
= self
::EMAIL_THANK
;
491 // Set status for signature activity to completed
492 $params['statusId'] = 2;
495 // send email verification email
496 $this->_sendEmailMode
= self
::EMAIL_CONFIRM
;
497 // Set status for signature activity to scheduled until email is verified
498 $params['statusId'] = 1;
506 $transaction = new CRM_Core_Transaction();
508 $addToGroupID = isset($this->_addToGroupID
) ?
$this->_addToGroupID
: NULL;
509 $this->_contactId
= CRM_Contact_BAO_Contact
::createProfileContact($params, $this->_contactProfileFields
,
510 $this->_contactId
, $addToGroupID,
511 $this->_contactProfileId
, $this->_ctype
,
515 // get additional custom activity profile field data
516 // to save with new signature activity record
517 $surveyInfo = $this->bao
->getSurveyInfo($this->_surveyId
);
518 $customActivityFields = CRM_Core_BAO_CustomField
::getFields('Activity', FALSE, FALSE,
519 $surveyInfo['activity_type_id']
521 $customActivityFields = CRM_Utils_Array
::crmArrayMerge($customActivityFields,
522 CRM_Core_BAO_CustomField
::getFields('Activity', FALSE, FALSE,
527 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess($params,
528 $customActivityFields,
533 // create the signature activity record
534 $params['contactId'] = $this->_contactId
;
535 $params['activity_campaign_id'] = CRM_Utils_Array
::value('campaign_id', $this->petition
);
536 $result = $this->bao
->createSignature($params);
538 // send thank you or email verification emails
540 // if logged in using Facebook connect and email on form matches Fb email,
541 // no need for email confirmation, send thank you email
542 if ($this->forceEmailConfirmed
['flag'] &&
543 ($this->forceEmailConfirmed
['email'] == $params['email-Primary'])
545 $this->_sendEmailMode
= self
::EMAIL_THANK
;
548 switch ($this->_sendEmailMode
) {
549 case self
::EMAIL_THANK
:
550 // mark the signature activity as completed and set confirmed cookie
551 $this->bao
->confirmSignature($result->id
, $this->_contactId
, $this->_surveyId
);
554 case self
::EMAIL_CONFIRM
:
555 // set 'Unconfirmed' tag for this new contact
558 $tag_params['contact_id'] = $this->_contactId
;
559 $tag_params['tag_id'] = $this->_tagId
;
560 $tag_params['version'] = 3;
561 $tag_value = civicrm_api('entity_tag', 'create', $tag_params);
567 $params['activityId'] = $result->id
;
568 $params['tagId'] = $this->_tagId
;
570 $transaction->commit();
572 $this->bao
->sendEmail($params, $this->_sendEmailMode
);
575 // call the hook before we redirect
576 $this->postProcessHook();
578 // set the template to thank you
580 CRM_Utils_System
::url(
581 'civicrm/petition/thankyou',
582 'pid=' . $this->_surveyId
. '&id=' . $this->_sendEmailMode
. '&reset=1'
584 CRM_Utils_System
::redirect($url);
589 * Function to build the petition profile form
593 * @param bool $viewOnly
598 function buildCustom($id, $name, $viewOnly = FALSE) {
600 // create state country map array to hold selectors
601 $stateCountryMap = array();
604 $session = CRM_Core_Session
::singleton();
605 $this->assign("petition", $this->petition
);
606 //$contactID = $this->_contactId;
608 $this->assign('contact_id', $this->_contactId
);
611 // TODO: contactID is never set (commented above)
613 if (CRM_Core_BAO_UFGroup
::filterUFGroups($id, $contactID)) {
614 $fields = CRM_Core_BAO_UFGroup
::getFields($id, FALSE, CRM_Core_Action
::ADD
);
618 $fields = CRM_Core_BAO_UFGroup
::getFields($id, FALSE, CRM_Core_Action
::ADD
);
623 // unset any email-* fields since we already collect it, CRM-2888
624 foreach ( array_keys( $fields ) as $fieldName ) {
625 if ( substr( $fieldName, 0, 6 ) == 'email-' ) {
626 unset( $fields[$fieldName] );
632 $this->assign($name, $fields);
635 foreach ($fields as $key => $field) {
637 isset($field['data_type']) &&
638 $field['data_type'] == 'File' ||
($viewOnly && $field['name'] == 'image_URL')
640 // ignore file upload fields
644 // if state or country in the profile, create map
645 list($prefixName, $index) = CRM_Utils_System
::explode('-', $key, 2);
646 if ($prefixName == 'state_province' ||
$prefixName == 'country' ||
$prefixName == 'county') {
647 if (!array_key_exists($index, $stateCountryMap)) {
648 $stateCountryMap[$index] = array();
650 $stateCountryMap[$index][$prefixName] = $key;
653 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, CRM_Profile_Form
::MODE_CREATE
, $contactID, TRUE);
654 $this->_fields
[$key] = $field;
655 // CRM-11316 Is ReCAPTCHA enabled for this profile AND is this an anonymous visitor
656 if ($field['add_captcha'] && !$this->_contactId
) {
661 // initialize the state country map
662 CRM_Core_BAO_Address
::addStateCountryMap($stateCountryMap);
664 if ($addCaptcha && !$viewOnly) {
665 $captcha = CRM_Utils_ReCAPTCHA
::singleton();
666 $captcha->add($this);
667 $this->assign("isCaptcha", TRUE);
673 function getTemplateFileName() {
674 if (isset($this->thankyou
)) {
675 return ('CRM/Campaign/Page/Petition/ThankYou.tpl');
678 return parent
::getTemplateFileName();
681 // check if user has already signed this petition
682 function redirectIfSigned($params) {
683 $signature = $this->bao
->checkSignature($this->_surveyId
, $this->_contactId
);
684 //TODO: error case when more than one signature found for this petition and this contact
685 if (!empty($signature) && (count($signature) == 1)) {
686 $signature_id = array_keys($signature);
687 switch ($signature[$signature_id[0]]['status_id']) {
689 //status is scheduled - email is unconfirmed
690 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/petition/thankyou', 'pid=' . $this->_surveyId
. '&id=4&reset=1'));
694 //status is completed
695 $this->bao
->sendEmail($params, 1);
696 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/petition/thankyou', 'pid=' . $this->_surveyId
. '&id=5&reset=1'));