| 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 | class CRM_Contact_Form_ProfileContact { |
| 36 | |
| 37 | protected $_mode; |
| 38 | |
| 39 | /** |
| 40 | * Function to set variables up before form is built |
| 41 | * |
| 42 | * @param $form |
| 43 | * |
| 44 | * @return void |
| 45 | * @access public |
| 46 | */ |
| 47 | static function preProcess(&$form) { |
| 48 | $session = CRM_Core_Session::singleton(); |
| 49 | $contactID = $session->get('userID'); |
| 50 | |
| 51 | $ufJoinParams = array( |
| 52 | 'module' => 'soft_credit', |
| 53 | 'entity_table' => 'civicrm_contribution_page', |
| 54 | 'entity_id' => $form->_id, |
| 55 | ); |
| 56 | $profileId = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams); |
| 57 | $form->_honoreeProfileId = $profileId[0]; |
| 58 | |
| 59 | if (!$form->_honoreeProfileId || |
| 60 | !CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $form->_honoreeProfileId, 'is_active') |
| 61 | ) { |
| 62 | 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.')); |
| 63 | } |
| 64 | |
| 65 | $profileContactType = CRM_Core_BAO_UFGroup::getContactType($form->_honoreeProfileId); |
| 66 | $requiredProfileFields = array( |
| 67 | 'Individual' => array('first_name', 'last_name'), |
| 68 | 'Organization' => array('organization_name', 'email'), |
| 69 | 'Household' => array('household_name', 'email') |
| 70 | ); |
| 71 | $validProfile = CRM_Core_BAO_UFGroup::checkValidProfile($form->_honoreeProfileId, $requiredProfileFields[$profileContactType]); |
| 72 | if (!$validProfile) { |
| 73 | 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.')); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Function to build form for honoree contact / on behalf of organization. |
| 79 | * |
| 80 | * @param $form object invoking Object |
| 81 | * |
| 82 | * @internal param string $contactType contact type |
| 83 | * @internal param string $title fieldset title |
| 84 | * |
| 85 | * @static |
| 86 | */ |
| 87 | static function buildQuickForm(&$form) { |
| 88 | $ufGroup = new CRM_Core_DAO_UFGroup(); |
| 89 | $ufGroup->id = $form->_honoreeProfileId; |
| 90 | if (!$ufGroup->find(TRUE)) { |
| 91 | CRM_Core_Error::fatal(ts('Chosen honoree profile for this contribution is disabled')); |
| 92 | } |
| 93 | |
| 94 | $prefix = 'honor'; |
| 95 | $honoreeProfileFields = CRM_Core_BAO_UFGroup::getFields($form->_honoreeProfileId, FALSE, NULL, |
| 96 | NULL, NULL, |
| 97 | FALSE, NULL, |
| 98 | TRUE, NULL, |
| 99 | CRM_Core_Permission::CREATE |
| 100 | ); |
| 101 | $form->addElement('hidden', 'honoree_profile_id', $form->_honoreeProfileId); |
| 102 | $form->assign('honoreeProfileFields', $honoreeProfileFields); |
| 103 | |
| 104 | // add the form elements |
| 105 | foreach ($honoreeProfileFields as $name => $field) { |
| 106 | // If soft credit type is not chosen then make omit requiredness from honoree profile fields |
| 107 | if (count($form->_submitValues) && empty($form->_submitValues['soft_credit_type_id']) && !empty($field['is_required'])) { |
| 108 | $field['is_required'] = FALSE; |
| 109 | } |
| 110 | CRM_Core_BAO_UFGroup::buildProfile($form, $field, CRM_Profile_Form::MODE_CREATE, NULL, FALSE, FALSE, NULL, $prefix); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * @param $form |
| 116 | */ |
| 117 | static function postProcess($form) { |
| 118 | $params = $form->_params; |
| 119 | if (!empty($form->_honor_block_is_active) && !empty($params['soft_credit_type_id'])) { |
| 120 | $honorId = null; |
| 121 | |
| 122 | //check if there is any duplicate contact |
| 123 | $profileContactType = CRM_Core_BAO_UFGroup::getContactType($params['honoree_profile_id']); |
| 124 | $dedupeParams = CRM_Dedupe_Finder::formatParams($params['honor'], $profileContactType); |
| 125 | $dedupeParams['check_permission'] = FALSE; |
| 126 | $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, $profileContactType); |
| 127 | if(count($ids)) { |
| 128 | $honorId = CRM_Utils_Array::value(0, $ids); |
| 129 | } |
| 130 | |
| 131 | $honorId = CRM_Contact_BAO_Contact::createProfileContact( |
| 132 | $params['honor'], CRM_Core_DAO::$_nullArray, |
| 133 | $honorId, NULL, |
| 134 | $params['honoree_profile_id'] |
| 135 | ); |
| 136 | $softParams = array(); |
| 137 | $softParams['contribution_id'] = $form->_contributionID; |
| 138 | $softParams['contact_id'] = $honorId; |
| 139 | $softParams['soft_credit_type_id'] = $params['soft_credit_type_id']; |
| 140 | $contribution = new CRM_Contribute_DAO_Contribution(); |
| 141 | $contribution->id = $form->_contributionID; |
| 142 | $contribution->find(); |
| 143 | while ($contribution->fetch()) { |
| 144 | $softParams['currency'] = $contribution->currency; |
| 145 | $softParams['amount'] = $contribution->total_amount; |
| 146 | } |
| 147 | CRM_Contribute_BAO_ContributionSoft::add($softParams); |
| 148 | |
| 149 | if (CRM_Utils_Array::value('is_email_receipt', $form->_values)) { |
| 150 | $form->_values['honor'] = array( |
| 151 | 'soft_credit_type' => CRM_Utils_Array::value( |
| 152 | $params['soft_credit_type_id'], |
| 153 | CRM_Core_OptionGroup::values("soft_credit_type") |
| 154 | ), |
| 155 | 'honor_id' => $honorId, |
| 156 | 'honor_profile_id' => $params['honoree_profile_id'], |
| 157 | 'honor_profile_values' => $params['honor'] |
| 158 | ); |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |