Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
6a488035 TO |
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_Contribute_Form_Contribution_OnBehalfOf { | |
36 | ||
37 | /** | |
38 | * Function to set variables up before form is built | |
39 | * | |
40 | * @return void | |
41 | * @access public | |
42 | */ | |
43 | static function preProcess(&$form) { | |
44 | $session = CRM_Core_Session::singleton(); | |
45 | $contactID = $session->get('userID'); | |
46 | ||
47 | $ufJoinParams = array( | |
48 | 'module' => 'onBehalf', | |
49 | 'entity_table' => 'civicrm_contribution_page', | |
50 | 'entity_id' => $form->_id, | |
51 | ); | |
52 | $profileId = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams); | |
53 | $form->_profileId = $profileId[0]; | |
54 | ||
55 | if (!$form->_profileId || | |
56 | !CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $form->_profileId, 'is_active') | |
57 | ) { | |
58 | CRM_Core_Error::fatal(ts('This contribution page has been configured for contribution on behalf of an organization and the selected onbehalf profile is either disabled or not found.')); | |
59 | } | |
60 | ||
61 | $requiredProfileFields = array('organization_name', 'email'); | |
62 | $validProfile = CRM_Core_BAO_UFGroup::checkValidProfile($form->_profileId, $requiredProfileFields); | |
63 | if (!$validProfile) { | |
64 | CRM_Core_Error::fatal(ts('This contribution page has been configured for contribution on behalf of an organization and the required fields of the selected onbehalf profile are disabled.')); | |
65 | } | |
66 | ||
67 | $form->assign('profileId', $form->_profileId); | |
68 | $form->assign('mode', $form->_mode); | |
69 | ||
70 | if ($contactID) { | |
71 | $form->_employers = CRM_Contact_BAO_Relationship::getPermissionedEmployer($contactID); | |
0130208a DS |
72 | |
73 | if (!empty($form->_membershipContactID) && $contactID != $form->_membershipContactID) { | |
74 | // renewal case - membership being renewed may or may not be for organization | |
75 | if (!empty($form->_employers) && array_key_exists($form->_membershipContactID, $form->_employers)) { | |
f5b34faf | 76 | // if _membershipContactID belongs to employers list, we can say: |
0130208a DS |
77 | $form->_relatedOrganizationFound = TRUE; |
78 | } | |
79 | } else if (!empty($form->_employers)) { | |
80 | // not a renewal case and _employers list is not empty | |
6a488035 | 81 | $form->_relatedOrganizationFound = TRUE; |
0130208a | 82 | } |
6a488035 | 83 | |
0130208a | 84 | if ($form->_relatedOrganizationFound) { |
6a488035 TO |
85 | $locDataURL = CRM_Utils_System::url('civicrm/ajax/permlocation', 'cid=', FALSE, NULL, FALSE); |
86 | $form->assign('locDataURL', $locDataURL); | |
87 | ||
88 | $dataURL = CRM_Utils_System::url('civicrm/ajax/employer', 'cid=' . $contactID, FALSE, NULL, FALSE); | |
89 | $form->assign('employerDataURL', $dataURL); | |
90 | } | |
91 | ||
92 | if ($form->_values['is_for_organization'] != 2) { | |
93 | $form->assign('relatedOrganizationFound', $form->_relatedOrganizationFound); | |
94 | } | |
95 | else { | |
96 | $form->assign('onBehalfRequired', $form->_onBehalfRequired); | |
97 | } | |
98 | ||
99 | if (count($form->_employers) == 1) { | |
100 | foreach ($form->_employers as $id => $value) { | |
101 | $form->_organizationName = $value['name']; | |
102 | $orgId = $id; | |
103 | } | |
104 | $form->assign('orgId', $orgId); | |
105 | $form->assign('organizationName', $form->_organizationName); | |
106 | } | |
107 | } | |
108 | } | |
109 | ||
110 | /** | |
111 | * Function to build form for related contacts / on behalf of organization. | |
112 | * | |
113 | * @param $form object invoking Object | |
114 | * @param $contactType string contact type | |
115 | * @param $title string fieldset title | |
116 | * | |
117 | * @static | |
118 | */ | |
119 | static function buildQuickForm(&$form) { | |
120 | $form->assign('fieldSetTitle', ts('Organization Details')); | |
121 | $form->assign('buildOnBehalfForm', TRUE); | |
122 | ||
123 | $session = CRM_Core_Session::singleton(); | |
124 | $contactID = $session->get('userID'); | |
125 | ||
126 | if ($contactID && count($form->_employers) >= 1) { | |
127 | $form->add('text', 'organization_id', ts('Select an existing related Organization OR enter a new one')); | |
128 | $form->add('hidden', 'onbehalfof_id', '', array('id' => 'onbehalfof_id')); | |
129 | ||
130 | $orgOptions = array(0 => ts('Select an existing organization'), | |
131 | 1 => ts('Enter a new organization'), | |
132 | ); | |
133 | ||
134 | $form->addRadio('org_option', ts('options'), $orgOptions); | |
135 | $form->setDefaults(array('org_option' => 0)); | |
136 | $form->add('checkbox', 'mode', ''); | |
137 | } | |
138 | ||
139 | $profileFields = CRM_Core_BAO_UFGroup::getFields($form->_profileId, FALSE, CRM_Core_Action::VIEW, NULL, | |
140 | NULL, FALSE, NULL, FALSE, NULL, | |
141 | CRM_Core_Permission::CREATE, NULL | |
142 | ); | |
143 | $fieldTypes = array('Contact', 'Organization'); | |
144 | $contactSubType = CRM_Contact_BAO_ContactType::subTypes('Organization'); | |
145 | $fieldTypes = array_merge($fieldTypes, $contactSubType); | |
146 | ||
147 | if (is_array($form->_membershipBlock) && !empty($form->_membershipBlock)) { | |
148 | $fieldTypes = array_merge($fieldTypes, array('Membership')); | |
149 | } | |
150 | else { | |
151 | $fieldTypes = array_merge($fieldTypes, array('Contribution')); | |
152 | } | |
153 | ||
154 | $stateCountryMap = array(); | |
f5b34faf | 155 | $location_type_id = null; |
6a488035 TO |
156 | foreach ($profileFields as $name => $field) { |
157 | if (in_array($field['field_type'], $fieldTypes)) { | |
158 | list($prefixName, $index) = CRM_Utils_System::explode('-', $name, 2); | |
159 | if (in_array($prefixName, array( | |
160 | 'state_province', 'country', 'county'))) { | |
161 | if (!array_key_exists($index, $stateCountryMap)) { | |
162 | $stateCountryMap[$index] = array(); | |
163 | } | |
164 | ||
165 | $stateCountryMap[$index][$prefixName] = 'onbehalf[' . $name . ']'; | |
f5b34faf | 166 | |
167 | if (count($form->_submitValues)) { | |
168 | $location_type_id = $field['location_type_id']; | |
169 | if(!empty($form->_submitValues['onbehalf']["country-{$location_type_id}"]) && | |
170 | $prefixName == "state_province") { | |
171 | $field['is_required'] = CRM_Core_Payment_Form::checkRequiredStateProvince($form, "country-{$location_type_id}", TRUE); | |
172 | } | |
173 | } | |
6a488035 TO |
174 | } |
175 | elseif (in_array($prefixName, array( | |
176 | 'organization_name', 'email')) && | |
177 | !CRM_Utils_Array::value('is_required', $field) | |
178 | ) { | |
179 | $field['is_required'] = 1; | |
180 | } | |
181 | ||
182 | CRM_Core_BAO_UFGroup::buildProfile($form, $field, NULL, NULL, FALSE, TRUE); | |
183 | } | |
184 | } | |
185 | ||
186 | if (!empty($stateCountryMap)) { | |
187 | CRM_Core_BAO_Address::addStateCountryMap($stateCountryMap); | |
188 | ||
189 | // now fix all state country selectors | |
190 | CRM_Core_BAO_Address::fixAllStateSelects($form, CRM_Core_DAO::$_nullArray); | |
191 | } | |
192 | ||
193 | $form->assign('onBehalfOfFields', $profileFields); | |
194 | $form->addElement('hidden', 'hidden_onbehalf_profile', 1); | |
195 | } | |
196 | } | |
197 |