Skip to content

Commit

Permalink
Merge pull request #2372 from kurund/CRM-13981
Browse files Browse the repository at this point in the history
CRM-13981, Migrate "In Honor of" to Soft Credits
kurund committed Jan 17, 2014

Verified

This commit was signed with the committer’s verified signature.
nlohmann Niels Lohmann
2 parents a055fd3 + 13e4fbd commit 6a7f5c0
Showing 43 changed files with 792 additions and 966 deletions.
149 changes: 149 additions & 0 deletions CRM/Contact/Form/ProfileContact.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.4 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2013 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2013
* $Id$
*
*/
class CRM_Contact_Form_ProfileContact {

protected $_mode;

/**
* Function to set variables up before form is built
*
* @return void
* @access public
*/
static function preProcess(&$form) {
$session = CRM_Core_Session::singleton();
$contactID = $session->get('userID');

$ufJoinParams = array(
'module' => 'soft_credit',
'entity_table' => 'civicrm_contribution_page',
'entity_id' => $form->_id,
);
$profileId = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams);
$form->_honoreeProfileId = $profileId[0];

if (!$form->_honoreeProfileId ||
!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $form->_honoreeProfileId, 'is_active')
) {
CRM_Core_Error::fatal(ts('This contribution page has been configured for contribution on behalf of honoree and the selected honoree profile is either disabled or not found.'));
}

$profileContactType = CRM_Core_BAO_UFGroup::getContactType($form->_honoreeProfileId);
$requiredProfileFields = array(
'Individual' => array('first_name', 'last_name'),
'Organization' => array('organization_name', 'email'),
'Household' => array('household_name', 'email')
);
$validProfile = CRM_Core_BAO_UFGroup::checkValidProfile($form->_honoreeProfileId, $requiredProfileFields[$profileContactType]);
if (!$validProfile) {
CRM_Core_Error::fatal(ts('This contribution page has been configured for contribution on behalf of honoree and the required fields of the selected honoree profile are disabled or doesn\'t exist.'));
}
}

/**
* Function to build form for honoree contact / on behalf of organization.
*
* @param $form object invoking Object
* @param $contactType string contact type
* @param $title string fieldset title
*
* @static
*/
static function buildQuickForm(&$form) {
$ufGroup = new CRM_Core_DAO_UFGroup();
$ufGroup->id = $form->_honoreeProfileId;
if (!$ufGroup->find(TRUE)) {
CRM_Core_Error::fatal(ts('Chosen honoree profile is for this contribution is disabled'));
}

$prefix = 'honor';
$honoreeProfileFields = CRM_Core_BAO_UFGroup::getFields($form->_honoreeProfileId, FALSE, NULL,
NULL, NULL,
FALSE, NULL,
TRUE, NULL,
CRM_Core_Permission::CREATE
);
$form->addElement('hidden', 'honoree_profile_id', $form->_honoreeProfileId);
$form->assign('honoreeProfileFields', $honoreeProfileFields);

// add the form elements
foreach ($honoreeProfileFields as $name => $field) {
// If soft credit type is not chosen then make omit requiredness from honoree profile fields
if (count($form->_submitValues) &&
!CRM_Utils_Array::value('soft_credit_type_id', $form->_submitValues) &&
CRM_Utils_Array::value('is_required', $field)
) {
$field['is_required'] = FALSE;
}
CRM_Core_BAO_UFGroup::buildProfile($form, $field, CRM_Profile_Form::MODE_CREATE, NULL, FALSE, FALSE, NULL, $prefix);
}
}

static function postProcess($form) {
$params = $form->_params;
$values = $form->_values;
if ($form->get('honor_block_is_active') && CRM_Utils_Array::value('soft_credit_type_id', $params)) {
$honorId = null;

//check if there is any duplicate contact
$profileContactType = CRM_Core_BAO_UFGroup::getContactType($params['honoree_profile_id']);
$dedupeParams = CRM_Dedupe_Finder::formatParams($params['honor'], $profileContactType);
$dedupeParams['check_permission'] = FALSE;
$ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, $profileContactType);
if(count($ids)) {
$honorId = CRM_Utils_Array::value(0, $ids);
}

$honorId = CRM_Contact_BAO_Contact::createProfileContact(
$params['honor'], CRM_Core_DAO::$_nullArray,
$honorId, NULL,
$params['honoree_profile_id']
);
$softParams = array();
$softParams['contribution_id'] = $form->_contributionID;
$softParams['contact_id'] = $honorId;
$softParams['soft_credit_type_id'] = $params['soft_credit_type_id'];
$contribution = new CRM_Contribute_DAO_Contribution();
$contribution->id = $form->_contributionID;
$contribution->find();
while ($contribution->fetch()) {
$softParams['currency'] = $contribution->currency;
$softParams['amount'] = $contribution->total_amount;
}
CRM_Contribute_BAO_ContributionSoft::add($softParams);
}
}
}

54 changes: 1 addition & 53 deletions CRM/Contribute/BAO/Contribution.php
Original file line number Diff line number Diff line change
@@ -342,10 +342,6 @@ static function &create(&$params, $ids = array()) {
foreach ($softParams as $softParam) {
$softParam['contribution_id'] = $contribution->id;
$softParam['currency'] = $contribution->currency;
$softParam['pcp_id'] = 'null';
$softParam['pcp_display_in_roll'] = 'null';
$softParam['pcp_roll_nickname'] = 'null';
$softParam['pcp_personal_note'] = 'NULL';
CRM_Contribute_BAO_ContributionSoft::add($softParam);
}
}
@@ -909,44 +905,6 @@ static function getCurrentandGoalAmount($pageID) {
}
}

/**
* Function to create is honor of
*
* @param array $params associated array of fields (by reference)
* @param int $honorId honor Id
* @param array $honorParams any params that should be send to the create function
*
* @return contact id
*/
static function createHonorContact(&$params, $honorId = NULL, $honorParams = array()) {
$honorParams = array_merge(
array(
'first_name' => $params['honor_first_name'],
'last_name' => $params['honor_last_name'],
'prefix_id' => $params['honor_prefix_id'],
'email-Primary' => $params['honor_email'],
),
$honorParams
);
if (!$honorId) {
$honorParams['email'] = $params['honor_email'];

$dedupeParams = CRM_Dedupe_Finder::formatParams($honorParams, 'Individual');
$dedupeParams['check_permission'] = FALSE;
$ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, 'Individual');

// if we find more than one contact, use the first one
$honorId = CRM_Utils_Array::value(0, $ids);
}

$contactID = CRM_Contact_BAO_Contact::createProfileContact(
$honorParams,
CRM_Core_DAO::$_nullArray,
$honorId
);
return $contactID;
}

/**
* Function to get list of contribution In Honor of contact Ids
*
@@ -1697,7 +1655,7 @@ public static function getComponentDetails($contributionId) {
return $componentDetails;
}

static function contributionCount($contactId, $includeSoftCredit = TRUE, $includeHonoree = TRUE) {
static function contributionCount($contactId, $includeSoftCredit = TRUE) {
if (!$contactId) {
return 0;
}
@@ -1707,11 +1665,6 @@ static function contributionCount($contactId, $includeSoftCredit = TRUE, $includ
FROM civicrm_contribution contribution
WHERE contribution.is_test = 0 AND contribution.contact_id = {$contactId} ";

$contactHonoreeContributionsSQL = "
SELECT contribution.id
FROM civicrm_contribution contribution
WHERE contribution.is_test = 0 AND contribution.honor_contact_id = {$contactId} ";

$contactSoftCreditContributionsSQL = "
SELECT contribution.id
FROM civicrm_contribution contribution INNER JOIN civicrm_contribution_soft softContribution
@@ -1725,11 +1678,6 @@ static function contributionCount($contactId, $includeSoftCredit = TRUE, $includ
$query .= $contactSoftCreditContributionsSQL;
}

if ($includeHonoree) {
$query .= " UNION ";
$query .= $contactHonoreeContributionsSQL;
}

$query .= ") x";

return CRM_Core_DAO::singleValueQuery($query);
69 changes: 0 additions & 69 deletions CRM/Contribute/BAO/Query.php
Original file line number Diff line number Diff line change
@@ -127,38 +127,6 @@ static function select(&$query) {
$query->_tables['contribution_payment_instrument'] = 1;
}

// get honor contact name
if (CRM_Utils_Array::value('honor_contact_name', $query->_returnProperties)) {
$query->_select['contribution_honor_contact_name'] = "civicrm_contact_c.display_name as contribution_honor_contact_name";
$query->_element['contribution_honor_contact_name'] = 1;
$query->_tables['civicrm_contribution'] = 1;
$query->_tables['contribution_honor_contact_name'] = 1;
}

// get honor type label
if (CRM_Utils_Array::value('honor_type_label', $query->_returnProperties)) {
$query->_select['contribution_honor_type_label'] = "honor_type.label as contribution_honor_type_label";
$query->_element['contribution_honor_type_label'] = 1;
$query->_tables['civicrm_contribution'] = 1;
$query->_tables['contribution_honor_type_label'] = 1;
}

// get honor contact email
if (CRM_Utils_Array::value('honor_contact_email', $query->_returnProperties)) {
$query->_select['contribution_honor_contact_email'] = "honor_email.email as contribution_honor_contact_email";
$query->_element['contribution_honor_contact_email'] = 1;
$query->_tables['civicrm_contribution'] = 1;
$query->_tables['contribution_honor_contact_email'] = 1;
}

// get honor contact id
if (CRM_Utils_Array::value('honor_contact_id', $query->_returnProperties)) {
$query->_select['contribution_honor_contact_id'] = "civicrm_contribution.honor_contact_id as contribution_honor_contact_id";
$query->_element['contribution_honor_contact_id'] = 1;
$query->_tables['civicrm_contribution'] = 1;
}


if (CRM_Utils_Array::value('check_number', $query->_returnProperties)) {
$query->_select['contribution_check_number'] = "civicrm_contribution.check_number as contribution_check_number";
$query->_element['contribution_check_number'] = 1;
@@ -373,22 +341,6 @@ static function whereClauseSingle(&$values, &$query) {
$query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
return;

case 'contribution_in_honor_of':
$name = trim($value);
$newName = str_replace(',', " ", $name);
$pieces = explode(' ', $newName);
foreach ($pieces as $piece) {
$value = $strtolower(CRM_Core_DAO::escapeString(trim($piece)));
$value = "'%$value%'";
$sub[] = " ( contact_b.sort_name LIKE $value )";
}

$query->_where[$grouping][] = ' ( ' . implode(' OR ', $sub) . ' ) ';
$query->_qill[$grouping][] = ts('Honor name like - \'%1\'', array(1 => $name));
$query->_tables['civicrm_contact_b'] = $query->_whereTables['civicrm_contact_b'] = 1;
$query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
return;

case 'contribution_status':
case 'contribution_status_id':
if (is_array($value)) {
@@ -665,10 +617,6 @@ static function from($name, $mode, $side) {
AND option_group_payment_instrument.id = payment_instrument.option_group_id ) ";
break;

case 'civicrm_contact_b':
$from .= " $side JOIN civicrm_contact contact_b ON (civicrm_contribution.honor_contact_id = contact_b.id )";
break;

case 'contribution_status':
$from = " $side JOIN civicrm_option_group option_group_contribution_status ON (option_group_contribution_status.name = 'contribution_status')";
$from .= " $side JOIN civicrm_option_value contribution_status ON (civicrm_contribution.contribution_status_id = contribution_status.value
@@ -680,20 +628,6 @@ static function from($name, $mode, $side) {
civicrm_contribution.id = civicrm_note.entity_id )";
break;

case 'contribution_honor_contact_name':
$from .= " $side JOIN civicrm_contact civicrm_contact_c ON (civicrm_contribution.honor_contact_id = civicrm_contact_c.id )";
break;

case 'contribution_honor_contact_email':
$from .= " $side JOIN civicrm_email as honor_email ON (civicrm_contribution.honor_contact_id = honor_email.contact_id AND honor_email.is_primary = 1 )";
break;

case 'contribution_honor_type_label':
$from = " $side JOIN civicrm_option_group option_group_honor_type ON ( option_group_honor_type.name = 'honor_type')";
$from .= " $side JOIN civicrm_option_value honor_type ON (civicrm_contribution.honor_type_id = honor_type.value
AND option_group_honor_type.id = honor_type.option_group_id ) ";
break;

case 'contribution_membership':
$from = " $side JOIN civicrm_membership_payment ON civicrm_membership_payment.contribution_id = civicrm_contribution.id";
$from .= " $side JOIN civicrm_membership ON civicrm_membership_payment.membership_id = civicrm_membership.id ";
@@ -866,9 +800,6 @@ static function buildSearchForm(&$form) {
$form->addYesNo('contribution_thankyou_date_is_not_null', ts('Thank-you sent?'));
$form->addYesNo('contribution_receipt_date_is_not_null', ts('Receipt sent?'));

// Add fields for honor search
$form->addElement('text', 'contribution_in_honor_of', ts("In Honor Of"));

$form->addYesNo('contribution_pay_later', ts('Contribution is Pay Later?'));
$form->addYesNo('contribution_recurring', ts('Contribution is Recurring?'));

13 changes: 0 additions & 13 deletions CRM/Contribute/Form/AdditionalInfo.php
Original file line number Diff line number Diff line change
@@ -311,19 +311,6 @@ static function postProcessCommon(&$params, &$formatted, &$form) {
$params['receipt_date'] = $formatted['receipt_date'] = date('YmdHis');
}

if (CRM_Utils_Array::value('honor_type_id', $params)) {
if ($form->_honorID) {
$honorId = CRM_Contribute_BAO_Contribution::createHonorContact($params, $form->_honorID);
}
else {
$honorId = CRM_Contribute_BAO_Contribution::createHonorContact($params);
}
$formatted["honor_contact_id"] = $honorId;
}
else {
$formatted["honor_contact_id"] = 'null';
}

//special case to handle if all checkboxes are unchecked
$customFields = CRM_Core_BAO_CustomField::getFields('Contribution',
FALSE,
Loading

0 comments on commit 6a7f5c0

Please sign in to comment.