Commit | Line | Data |
---|---|---|
133e2c99 | 1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
133e2c99 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
133e2c99 | 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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
133e2c99 | 27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
133e2c99 | 32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Contact_Form_ProfileContact { | |
36 | ||
37 | protected $_mode; | |
38 | ||
39 | /** | |
fe482240 | 40 | * Set variables up before form is built. |
133e2c99 | 41 | * |
c490a46a | 42 | * @param CRM_Core_Form $form |
77b97be7 | 43 | * |
133e2c99 | 44 | * @return void |
133e2c99 | 45 | */ |
00be9182 | 46 | public static function preProcess(&$form) { |
133e2c99 | 47 | $session = CRM_Core_Session::singleton(); |
48 | $contactID = $session->get('userID'); | |
49 | ||
50 | $ufJoinParams = array( | |
51 | 'module' => 'soft_credit', | |
52 | 'entity_table' => 'civicrm_contribution_page', | |
53 | 'entity_id' => $form->_id, | |
54 | ); | |
55 | $profileId = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams); | |
56 | $form->_honoreeProfileId = $profileId[0]; | |
57 | ||
58 | if (!$form->_honoreeProfileId || | |
59 | !CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $form->_honoreeProfileId, 'is_active') | |
60 | ) { | |
61 | 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.')); | |
62 | } | |
63 | ||
64 | $profileContactType = CRM_Core_BAO_UFGroup::getContactType($form->_honoreeProfileId); | |
65 | $requiredProfileFields = array( | |
66 | 'Individual' => array('first_name', 'last_name'), | |
67 | 'Organization' => array('organization_name', 'email'), | |
21dfd5f5 | 68 | 'Household' => array('household_name', 'email'), |
133e2c99 | 69 | ); |
70 | $validProfile = CRM_Core_BAO_UFGroup::checkValidProfile($form->_honoreeProfileId, $requiredProfileFields[$profileContactType]); | |
71 | if (!$validProfile) { | |
72 | 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.')); | |
73 | } | |
74 | } | |
75 | ||
76 | /** | |
100fef9d | 77 | * Build form for honoree contact / on behalf of organization. |
133e2c99 | 78 | * |
c490a46a | 79 | * @param CRM_Core_Form $form |
133e2c99 | 80 | * |
133e2c99 | 81 | */ |
00be9182 | 82 | public static function buildQuickForm(&$form) { |
133e2c99 | 83 | $ufGroup = new CRM_Core_DAO_UFGroup(); |
84 | $ufGroup->id = $form->_honoreeProfileId; | |
85 | if (!$ufGroup->find(TRUE)) { | |
8381af80 | 86 | CRM_Core_Error::fatal(ts('Chosen honoree profile for this contribution is disabled')); |
133e2c99 | 87 | } |
88 | ||
89 | $prefix = 'honor'; | |
90 | $honoreeProfileFields = CRM_Core_BAO_UFGroup::getFields($form->_honoreeProfileId, FALSE, NULL, | |
353ffa53 TO |
91 | NULL, NULL, |
92 | FALSE, NULL, | |
93 | TRUE, NULL, | |
94 | CRM_Core_Permission::CREATE | |
95 | ); | |
133e2c99 | 96 | $form->addElement('hidden', 'honoree_profile_id', $form->_honoreeProfileId); |
97 | $form->assign('honoreeProfileFields', $honoreeProfileFields); | |
98 | ||
99 | // add the form elements | |
100 | foreach ($honoreeProfileFields as $name => $field) { | |
101 | // If soft credit type is not chosen then make omit requiredness from honoree profile fields | |
8cc574cf | 102 | if (count($form->_submitValues) && empty($form->_submitValues['soft_credit_type_id']) && !empty($field['is_required'])) { |
133e2c99 | 103 | $field['is_required'] = FALSE; |
104 | } | |
105 | CRM_Core_BAO_UFGroup::buildProfile($form, $field, CRM_Profile_Form::MODE_CREATE, NULL, FALSE, FALSE, NULL, $prefix); | |
106 | } | |
107 | } | |
108 | ||
86538308 EM |
109 | /** |
110 | * @param $form | |
111 | */ | |
00be9182 | 112 | public static function postProcess($form) { |
133e2c99 | 113 | $params = $form->_params; |
016a5f08 | 114 | if (!empty($form->_honor_block_is_active) && !empty($params['soft_credit_type_id'])) { |
e60f24eb | 115 | $honorId = NULL; |
133e2c99 | 116 | |
117 | //check if there is any duplicate contact | |
118 | $profileContactType = CRM_Core_BAO_UFGroup::getContactType($params['honoree_profile_id']); | |
133e2c99 | 119 | $dedupeParams = CRM_Dedupe_Finder::formatParams($params['honor'], $profileContactType); |
120 | $dedupeParams['check_permission'] = FALSE; | |
121 | $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, $profileContactType); | |
22e263ad | 122 | if (count($ids)) { |
133e2c99 | 123 | $honorId = CRM_Utils_Array::value(0, $ids); |
124 | } | |
125 | ||
126 | $honorId = CRM_Contact_BAO_Contact::createProfileContact( | |
8381af80 | 127 | $params['honor'], CRM_Core_DAO::$_nullArray, |
128 | $honorId, NULL, | |
129 | $params['honoree_profile_id'] | |
133e2c99 | 130 | ); |
131 | $softParams = array(); | |
132 | $softParams['contribution_id'] = $form->_contributionID; | |
133 | $softParams['contact_id'] = $honorId; | |
134 | $softParams['soft_credit_type_id'] = $params['soft_credit_type_id']; | |
135 | $contribution = new CRM_Contribute_DAO_Contribution(); | |
136 | $contribution->id = $form->_contributionID; | |
137 | $contribution->find(); | |
138 | while ($contribution->fetch()) { | |
139 | $softParams['currency'] = $contribution->currency; | |
140 | $softParams['amount'] = $contribution->total_amount; | |
141 | } | |
142 | CRM_Contribute_BAO_ContributionSoft::add($softParams); | |
1421174e | 143 | |
144 | if (CRM_Utils_Array::value('is_email_receipt', $form->_values)) { | |
145 | $form->_values['honor'] = array( | |
146 | 'soft_credit_type' => CRM_Utils_Array::value( | |
147 | $params['soft_credit_type_id'], | |
148 | CRM_Core_OptionGroup::values("soft_credit_type") | |
149 | ), | |
150 | 'honor_id' => $honorId, | |
151 | 'honor_profile_id' => $params['honoree_profile_id'], | |
21dfd5f5 | 152 | 'honor_profile_values' => $params['honor'], |
1421174e | 153 | ); |
154 | } | |
133e2c99 | 155 | } |
156 | } | |
96025800 | 157 | |
133e2c99 | 158 | } |