Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
6a488035 TO |
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 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Contribute_Form_Contribution_OnBehalfOf { | |
36 | ||
37 | /** | |
fe482240 | 38 | * Set variables up before form is built. |
6a488035 | 39 | * |
c490a46a | 40 | * @param CRM_Core_Form $form |
dd244018 | 41 | * |
6a488035 | 42 | * @return void |
6a488035 | 43 | */ |
00be9182 | 44 | public static function preProcess(&$form) { |
6a488035 | 45 | $session = CRM_Core_Session::singleton(); |
0e5e0c2e | 46 | $contactID = $form->_contactID; |
6a488035 TO |
47 | |
48 | $ufJoinParams = array( | |
49 | 'module' => 'onBehalf', | |
50 | 'entity_table' => 'civicrm_contribution_page', | |
51 | 'entity_id' => $form->_id, | |
52 | ); | |
53 | $profileId = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams); | |
54 | $form->_profileId = $profileId[0]; | |
55 | ||
56 | if (!$form->_profileId || | |
57 | !CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $form->_profileId, 'is_active') | |
58 | ) { | |
59 | 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.')); | |
60 | } | |
61 | ||
62 | $requiredProfileFields = array('organization_name', 'email'); | |
63 | $validProfile = CRM_Core_BAO_UFGroup::checkValidProfile($form->_profileId, $requiredProfileFields); | |
64 | if (!$validProfile) { | |
65 | 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.')); | |
66 | } | |
67 | ||
68 | $form->assign('profileId', $form->_profileId); | |
69 | $form->assign('mode', $form->_mode); | |
70 | ||
71 | if ($contactID) { | |
72 | $form->_employers = CRM_Contact_BAO_Relationship::getPermissionedEmployer($contactID); | |
0130208a DS |
73 | |
74 | if (!empty($form->_membershipContactID) && $contactID != $form->_membershipContactID) { | |
75 | // renewal case - membership being renewed may or may not be for organization | |
76 | if (!empty($form->_employers) && array_key_exists($form->_membershipContactID, $form->_employers)) { | |
f5b34faf | 77 | // if _membershipContactID belongs to employers list, we can say: |
0130208a DS |
78 | $form->_relatedOrganizationFound = TRUE; |
79 | } | |
0db6c3e1 | 80 | } |
4c9b6178 | 81 | elseif (!empty($form->_employers)) { |
0130208a | 82 | // not a renewal case and _employers list is not empty |
6a488035 | 83 | $form->_relatedOrganizationFound = TRUE; |
0130208a | 84 | } |
6a488035 | 85 | |
0130208a | 86 | if ($form->_relatedOrganizationFound) { |
3719c6dd CW |
87 | // Related org url - pass checksum if needed |
88 | $args = array('cid' => ''); | |
89 | if (!empty($_GET['cs'])) { | |
90 | $args = array( | |
91 | 'uid' => $form->_contactID, | |
92 | 'cs' => $_GET['cs'], | |
93 | 'cid' => '', | |
94 | ); | |
95 | } | |
96 | $locDataURL = CRM_Utils_System::url('civicrm/ajax/permlocation', $args, FALSE, NULL, FALSE); | |
6a488035 | 97 | $form->assign('locDataURL', $locDataURL); |
8d7965a3 | 98 | |
99 | if (!empty($form->_submitValues['onbehalf'])) { | |
c8fa0a31 | 100 | if (!empty($form->_submitValues['onbehalfof_id'])) { |
101 | $form->assign('submittedOnBehalf', $form->_submitValues['onbehalfof_id']); | |
102 | } | |
8d7965a3 | 103 | $form->assign('submittedOnBehalfInfo', json_encode($form->_submitValues['onbehalf'])); |
104 | } | |
6a488035 TO |
105 | } |
106 | ||
107 | if ($form->_values['is_for_organization'] != 2) { | |
108 | $form->assign('relatedOrganizationFound', $form->_relatedOrganizationFound); | |
109 | } | |
110 | else { | |
111 | $form->assign('onBehalfRequired', $form->_onBehalfRequired); | |
112 | } | |
113 | ||
114 | if (count($form->_employers) == 1) { | |
115 | foreach ($form->_employers as $id => $value) { | |
116 | $form->_organizationName = $value['name']; | |
117 | $orgId = $id; | |
118 | } | |
119 | $form->assign('orgId', $orgId); | |
120 | $form->assign('organizationName', $form->_organizationName); | |
121 | } | |
122 | } | |
123 | } | |
124 | ||
125 | /** | |
100fef9d | 126 | * Build form for related contacts / on behalf of organization. |
6a488035 | 127 | * |
c490a46a | 128 | * @param CRM_Core_Form $form |
6a488035 | 129 | * |
6a488035 | 130 | */ |
00be9182 | 131 | public static function buildQuickForm(&$form) { |
6a488035 TO |
132 | $form->assign('fieldSetTitle', ts('Organization Details')); |
133 | $form->assign('buildOnBehalfForm', TRUE); | |
134 | ||
0e5e0c2e | 135 | $contactID = $form->_contactID; |
6a488035 TO |
136 | |
137 | if ($contactID && count($form->_employers) >= 1) { | |
138 | $form->add('text', 'organization_id', ts('Select an existing related Organization OR enter a new one')); | |
6a488035 | 139 | |
202df408 CW |
140 | $form->add('select', 'onbehalfof_id', '', CRM_Utils_Array::collect('name', $form->_employers)); |
141 | ||
142 | $orgOptions = array( | |
143 | 0 => ts('Select an existing organization'), | |
6a488035 TO |
144 | 1 => ts('Enter a new organization'), |
145 | ); | |
146 | ||
147 | $form->addRadio('org_option', ts('options'), $orgOptions); | |
148 | $form->setDefaults(array('org_option' => 0)); | |
149 | $form->add('checkbox', 'mode', ''); | |
150 | } | |
151 | ||
152 | $profileFields = CRM_Core_BAO_UFGroup::getFields($form->_profileId, FALSE, CRM_Core_Action::VIEW, NULL, | |
153 | NULL, FALSE, NULL, FALSE, NULL, | |
154 | CRM_Core_Permission::CREATE, NULL | |
155 | ); | |
353ffa53 | 156 | $fieldTypes = array('Contact', 'Organization'); |
6a488035 | 157 | $contactSubType = CRM_Contact_BAO_ContactType::subTypes('Organization'); |
353ffa53 | 158 | $fieldTypes = array_merge($fieldTypes, $contactSubType); |
6a488035 TO |
159 | |
160 | if (is_array($form->_membershipBlock) && !empty($form->_membershipBlock)) { | |
161 | $fieldTypes = array_merge($fieldTypes, array('Membership')); | |
162 | } | |
163 | else { | |
164 | $fieldTypes = array_merge($fieldTypes, array('Contribution')); | |
165 | } | |
166 | ||
6a488035 TO |
167 | foreach ($profileFields as $name => $field) { |
168 | if (in_array($field['field_type'], $fieldTypes)) { | |
169 | list($prefixName, $index) = CRM_Utils_System::explode('-', $name, 2); | |
bc999cd1 | 170 | if (in_array($prefixName, array('organization_name', 'email')) && empty($field['is_required'])) { |
6a488035 TO |
171 | $field['is_required'] = 1; |
172 | } | |
173 | ||
7269e3ed | 174 | CRM_Core_BAO_UFGroup::buildProfile($form, $field, NULL, NULL, FALSE, 'onbehalf'); |
6a488035 TO |
175 | } |
176 | } | |
177 | ||
6a488035 TO |
178 | $form->assign('onBehalfOfFields', $profileFields); |
179 | $form->addElement('hidden', 'hidden_onbehalf_profile', 1); | |
180 | } | |
96025800 | 181 | |
6a488035 | 182 | } |