3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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; function __construct() {
142 parent
::__construct();
143 // this property used by civicrm_fb module and if true, forces thank you email to be sent
144 // for users signing in via Facebook connect; also sets Fb email to check against
145 $this->forceEmailConfirmed
['flag'] = FALSE;
146 $this->forceEmailConfirmed
['email'] = '';
149 function getContactID() {
150 $tempID = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
152 // force to ignore the authenticated user
153 if ($tempID === '0') {
157 //check if this is a checksum authentication
158 $userChecksum = CRM_Utils_Request
::retrieve('cs', 'String', $this);
160 //check for anonymous user.
161 $validUser = CRM_Contact_BAO_Contact_Utils
::validChecksum($tempID, $userChecksum);
167 // check if the user is registered and we have a contact ID
168 $session = CRM_Core_Session
::singleton();
169 return $session->get('userID');
172 public function preProcess() {
173 $this->bao
= new CRM_Campaign_BAO_Petition();
174 $this->_mode
= self
::MODE_CREATE
;
177 $this->_surveyId
= CRM_Utils_Request
::retrieve('sid', 'Positive', $this);
180 if (!$this->_surveyId
) {
181 CRM_Core_Error
::fatal('Petition id is not valid. (it needs a "sid" in the url).');
184 //check petition is valid and active
185 $params['id'] = $this->_surveyId
;
186 $this->petition
= array();
187 CRM_Campaign_BAO_Survey
::retrieve($params, $this->petition
);
188 if (empty($this->petition
)) {
189 CRM_Core_Error
::fatal('Petition doesn\'t exist.');
191 if ($this->petition
['is_active'] == 0) {
192 CRM_Core_Error
::fatal('Petition is no longer active.');
195 //get userID from session
196 $session = CRM_Core_Session
::singleton();
198 //get the contact id for this user if logged in
199 $this->_contactId
= $this->getContactId();
200 if (isset($this->_contactId
)) {
201 $this->_loggedIn
= TRUE;
204 // add the custom contact and activity profile fields to the signature form
206 $ufJoinParams = array(
207 'entity_id' => $this->_surveyId
,
208 'entity_table' => 'civicrm_survey',
209 'module' => 'CiviCampaign',
213 $this->_contactProfileId
= CRM_Core_BAO_UFJoin
::findUFGroupId($ufJoinParams);
214 if ($this->_contactProfileId
) {
215 $this->_contactProfileFields
= CRM_Core_BAO_UFGroup
::getFields($this->_contactProfileId
, FALSE, CRM_Core_Action
::ADD
);
217 if (!isset($this->_contactProfileFields
['email-Primary'])) {
218 CRM_Core_Error
::fatal('The contact profile needs to contain the primary email address field');
222 $ufJoinParams['weight'] = 1;
223 $this->_activityProfileId
= CRM_Core_BAO_UFJoin
::findUFGroupId($ufJoinParams);
225 if ($this->_activityProfileId
) {
226 $this->_activityProfileFields
= CRM_Core_BAO_UFGroup
::getFields($this->_activityProfileId
, FALSE, CRM_Core_Action
::ADD
);
229 $this->setDefaultValues();
230 CRM_Utils_System
::setTitle($this->petition
['title']);
234 * This function sets the default values for the form.
240 function setDefaultValues() {
241 $this->_defaults
= array();
242 if ($this->_contactId
) {
243 CRM_Core_BAO_UFGroup
::setProfileDefaults($this->_contactId
, $this->_contactProfileFields
, $this->_defaults
, TRUE);
244 if ($this->_activityProfileId
) {
245 CRM_Core_BAO_UFGroup
::setProfileDefaults($this->_contactId
, $this->_activityProfileFields
, $this->_defaults
, TRUE);
249 //set custom field defaults
251 foreach ($this->_contactProfileFields
as $name => $field) {
252 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($name)) {
253 $htmlType = $field['html_type'];
255 if (!isset($this->_defaults
[$name])) {
256 CRM_Core_BAO_CustomField
::setProfileDefaults($customFieldID,
266 if ($this->_activityProfileFields
) {
267 foreach ($this->_activityProfileFields
as $name => $field) {
268 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($name)) {
269 $htmlType = $field['html_type'];
271 if (!isset($this->_defaults
[$name])) {
272 CRM_Core_BAO_CustomField
::setProfileDefaults($customFieldID,
283 $this->setDefaults($this->_defaults
);
286 public function buildQuickForm() {
287 $this->assign('survey_id', $this->_surveyId
);
288 $this->assign('petitionTitle', $this->petition
['title']);
289 if (isset($_COOKIE['signed_' . $this->_surveyId
])) {
290 if (isset($_COOKIE['confirmed_' . $this->_surveyId
])) {
291 $this->assign('duplicate', "confirmed");
294 $this->assign('duplicate', "unconfirmed");
299 $this->applyFilter('__ALL__', 'trim');
301 $this->buildCustom($this->_contactProfileId
, 'petitionContactProfile');
302 if ($this->_activityProfileId
) {
303 $this->buildCustom($this->_activityProfileId
, 'petitionActivityProfile');
306 $this->addButtons(array(
309 'name' => ts('Sign the Petition'),
317 * This function is used to add the rules (mainly global rules) for form.
318 * All local rules are added near the element
325 static function formRule($fields, $files, $errors) {
328 return empty($errors) ?
TRUE : $errors;
332 * Form submission of petition signature
338 public function postProcess() {
339 $tag_name = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::CAMPAIGN_PREFERENCES_NAME
,
345 // Check if contact 'email confirmed' tag exists, else create one
346 // This should be in the petition module initialise code to create a default tag for this
347 $tag_params['name'] = $tag_name;
348 $tag_params['version'] = 3;
349 $tag = civicrm_api('tag', 'get', $tag_params);
350 if ($tag['count'] == 0) {
352 $tag_params['description'] = $tag_name;
353 $tag_params['is_reserved'] = 1;
354 $tag_params['used_for'] = 'civicrm_contact';
355 $tag = civicrm_api('tag', 'create', $tag_params);
357 $this->_tagId
= $tag['id'];
360 // export the field values to be used for saving the profile form
361 $params = $this->controller
->exportValues($this->_name
);
363 $session = CRM_Core_Session
::singleton();
365 $params['last_modified_id'] = $session->get('userID');
366 $params['last_modified_date'] = date('YmdHis');
368 if ($this->_action
& CRM_Core_Action
::ADD
) {
369 $params['created_id'] = $session->get('userID');
370 $params['created_date'] = date('YmdHis');
373 if (isset($this->_surveyId
)) {
374 $params['sid'] = $this->_surveyId
;
377 if (isset($this->_contactId
)) {
378 $params['contactId'] = $this->_contactId
;
381 // if logged in user, skip dedupe
382 if ($this->_loggedIn
) {
383 $ids[0] = $this->_contactId
;
386 // dupeCheck - check if contact record already exists
387 // code modified from api/v2/Contact.php-function civicrm_contact_check_params()
388 $params['contact_type'] = $this->_ctype
;
389 //TODO - current dedupe finds soft deleted contacts - adding param is_deleted not working
390 // ignore soft deleted contacts
391 //$params['is_deleted'] = 0;
392 $dedupeParams = CRM_Dedupe_Finder
::formatParams($params, $params['contact_type']);
393 $dedupeParams['check_permission'] = '';
395 //dupesByParams($params, $ctype, $level = 'Unsupervised', $except = array())
396 $ids = CRM_Dedupe_Finder
::dupesByParams($dedupeParams, $params['contact_type']);
399 $petition_params['id'] = $this->_surveyId
;
401 CRM_Campaign_BAO_Survey
::retrieve($petition_params, $petition);
403 switch (count($ids)) {
405 //no matching contacts - create a new contact
406 // Add a source for this new contact
407 $params['source'] = ts('Petition Signature') . ' ' . $this->petition
['title'];
409 if ($this->petition
['bypass_confirm']) {
410 // send thank you email directly, bypassing confirmation
411 $this->_sendEmailMode
= self
::EMAIL_THANK
;
412 // Set status for signature activity to completed
413 $params['statusId'] = 2;
416 $this->_sendEmailMode
= self
::EMAIL_CONFIRM
;
418 // Set status for signature activity to scheduled until email is verified
419 $params['statusId'] = 1;
424 $this->_contactId
= $params['contactId'] = $ids[0];
426 // check if user has already signed this petition - redirects to Thank You if true
427 $this->redirectIfSigned($params);
429 if ($this->petition
['bypass_confirm']) {
430 // send thank you email directly, bypassing confirmation
431 $this->_sendEmailMode
= self
::EMAIL_THANK
;
432 // Set status for signature activity to completed
433 $params['statusId'] = 2;
437 // dedupe matched single contact, check for 'unconfirmed' tag
439 $tag = new CRM_Core_DAO_EntityTag();
440 $tag->entity_id
= $this->_contactId
;
441 $tag->tag_id
= $this->_tagId
;
443 if (!($tag->find())) {
444 // send thank you email directly, the user is known and validated
445 $this->_sendEmailMode
= self
::EMAIL_THANK
;
446 // Set status for signature activity to completed
447 $params['statusId'] = 2;
450 // send email verification email
451 $this->_sendEmailMode
= self
::EMAIL_CONFIRM
;
452 // Set status for signature activity to scheduled until email is verified
453 $params['statusId'] = 1;
459 // more than 1 matching contact
460 // for time being, take the first matching contact (not sure that's the best strategy, but better than creating another duplicate)
461 $this->_contactId
= $params['contactId'] = $ids[0];
463 // check if user has already signed this petition - redirects to Thank You if true
464 $this->redirectIfSigned($params);
466 if ($this->petition
['bypass_confirm']) {
467 // send thank you email directly, bypassing confirmation
468 $this->_sendEmailMode
= self
::EMAIL_THANK
;
469 // Set status for signature activity to completed
470 $params['statusId'] = 2;
475 $tag = new CRM_Core_DAO_EntityTag();
476 $tag->entity_id
= $this->_contactId
;
477 $tag->tag_id
= $this->_tagId
;
479 if (!($tag->find())) {
480 // send thank you email
481 $this->_sendEmailMode
= self
::EMAIL_THANK
;
482 // Set status for signature activity to completed
483 $params['statusId'] = 2;
486 // send email verification email
487 $this->_sendEmailMode
= self
::EMAIL_CONFIRM
;
488 // Set status for signature activity to scheduled until email is verified
489 $params['statusId'] = 1;
497 $transaction = new CRM_Core_Transaction();
499 $addToGroupID = isset($this->_addToGroupID
) ?
$this->_addToGroupID
: NULL;
500 $this->_contactId
= CRM_Contact_BAO_Contact
::createProfileContact($params, $this->_contactProfileFields
,
501 $this->_contactId
, $addToGroupID,
502 $this->_contactProfileId
, $this->_ctype
,
506 // get additional custom activity profile field data
507 // to save with new signature activity record
508 $surveyInfo = $this->bao
->getSurveyInfo($this->_surveyId
);
509 $customActivityFields = CRM_Core_BAO_CustomField
::getFields('Activity', FALSE, FALSE,
510 $surveyInfo['activity_type_id']
512 $customActivityFields = CRM_Utils_Array
::crmArrayMerge($customActivityFields,
513 CRM_Core_BAO_CustomField
::getFields('Activity', FALSE, FALSE,
518 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess($params,
519 $customActivityFields,
524 // create the signature activity record
525 $params['contactId'] = $this->_contactId
;
526 $params['activity_campaign_id'] = $this->petition
['campaign_id'];
527 $result = $this->bao
->createSignature($params);
529 // send thank you or email verification emails
531 // if logged in using Facebook connect and email on form matches Fb email,
532 // no need for email confirmation, send thank you email
533 if ($this->forceEmailConfirmed
['flag'] &&
534 ($this->forceEmailConfirmed
['email'] == $params['email-Primary'])
536 $this->_sendEmailMode
= self
::EMAIL_THANK
;
539 switch ($this->_sendEmailMode
) {
540 case self
::EMAIL_THANK
:
541 // mark the signature activity as completed and set confirmed cookie
542 $this->bao
->confirmSignature($result->id
, $this->_contactId
, $this->_surveyId
);
545 case self
::EMAIL_CONFIRM
:
546 // set 'Unconfirmed' tag for this new contact
549 $tag_params['contact_id'] = $this->_contactId
;
550 $tag_params['tag_id'] = $this->_tagId
;
551 $tag_value = civicrm_api('entity_tag', 'create', $tag_params);
557 $params['activityId'] = $result->id
;
558 $params['tagId'] = $this->_tagId
;
559 $this->bao
->sendEmail($params, $this->_sendEmailMode
);
561 $transaction->commit();
564 // call the hook before we redirect
565 $this->postProcessHook();
567 // set the template to thank you
569 CRM_Utils_System
::url(
570 'civicrm/petition/thankyou',
571 'pid=' . $this->_surveyId
. '&id=' . $this->_sendEmailMode
. '&reset=1'
573 CRM_Utils_System
::redirect($url);
578 * Function to build the petition profile form
583 function buildCustom($id, $name, $viewOnly = FALSE) {
586 $session = CRM_Core_Session
::singleton();
587 $this->assign("petition", $this->petition
);
588 //$contactID = $this->_contactId;
590 $this->assign('contact_id', $this->_contactId
);
593 // TODO: contactID is never set (commented above)
595 if (CRM_Core_BAO_UFGroup
::filterUFGroups($id, $contactID)) {
596 $fields = CRM_Core_BAO_UFGroup
::getFields($id, FALSE, CRM_Core_Action
::ADD
);
600 $fields = CRM_Core_BAO_UFGroup
::getFields($id, FALSE, CRM_Core_Action
::ADD
);
605 // unset any email-* fields since we already collect it, CRM-2888
606 foreach ( array_keys( $fields ) as $fieldName ) {
607 if ( substr( $fieldName, 0, 6 ) == 'email-' ) {
608 unset( $fields[$fieldName] );
614 $this->assign($name, $fields);
617 foreach ($fields as $key => $field) {
619 isset($field['data_type']) &&
620 $field['data_type'] == 'File' ||
($viewOnly && $field['name'] == 'image_URL')
622 // ignore file upload fields
627 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, CRM_Profile_Form
::MODE_CREATE
, $contactID, TRUE);
628 $this->_fields
[$key] = $field;
629 if ($field['add_captcha']) {
637 $captcha = CRM_Utils_ReCAPTCHA
::singleton();
638 $captcha->add($this);
639 $this->assign("isCaptcha", TRUE);
645 function getTemplateFileName() {
646 if (isset($this->thankyou
)) {
647 return ('CRM/Campaign/Page/Petition/ThankYou.tpl');
650 return parent
::getTemplateFileName();
653 // check if user has already signed this petition
654 function redirectIfSigned($params) {
655 $signature = $this->bao
->checkSignature($this->_surveyId
, $this->_contactId
);
656 //TODO: error case when more than one signature found for this petition and this contact
657 if (!empty($signature) && (count($signature) == 1)) {
658 $signature_id = array_keys($signature);
659 switch ($signature[$signature_id[0]]['status_id']) {
661 //status is scheduled - email is unconfirmed
662 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/petition/thankyou', 'pid=' . $this->_surveyId
. '&id=4&reset=1'));
666 //status is completed
667 $this->bao
->sendEmail($params, 1);
668 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/petition/thankyou', 'pid=' . $this->_surveyId
. '&id=5&reset=1'));