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