3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
35 class CRM_Contribute_Form_Contribution_OnBehalfOf
{
38 * Function to set variables up before form is built
45 static function preProcess(&$form) {
46 $session = CRM_Core_Session
::singleton();
47 $contactID = $form->_contactID
;
49 $ufJoinParams = array(
50 'module' => 'onBehalf',
51 'entity_table' => 'civicrm_contribution_page',
52 'entity_id' => $form->_id
,
54 $profileId = CRM_Core_BAO_UFJoin
::getUFGroupIds($ufJoinParams);
55 $form->_profileId
= $profileId[0];
57 if (!$form->_profileId ||
58 !CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', $form->_profileId
, 'is_active')
60 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.'));
63 $requiredProfileFields = array('organization_name', 'email');
64 $validProfile = CRM_Core_BAO_UFGroup
::checkValidProfile($form->_profileId
, $requiredProfileFields);
66 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.'));
69 $form->assign('profileId', $form->_profileId
);
70 $form->assign('mode', $form->_mode
);
73 $form->_employers
= CRM_Contact_BAO_Relationship
::getPermissionedEmployer($contactID);
75 if (!empty($form->_membershipContactID
) && $contactID != $form->_membershipContactID
) {
76 // renewal case - membership being renewed may or may not be for organization
77 if (!empty($form->_employers
) && array_key_exists($form->_membershipContactID
, $form->_employers
)) {
78 // if _membershipContactID belongs to employers list, we can say:
79 $form->_relatedOrganizationFound
= TRUE;
81 } else if (!empty($form->_employers
)) {
82 // not a renewal case and _employers list is not empty
83 $form->_relatedOrganizationFound
= TRUE;
86 if ($form->_relatedOrganizationFound
) {
87 // Related org url - pass checksum if needed
88 $args = array('cid' => '');
89 if (!empty($_GET['cs'])) {
91 'uid' => $form->_contactID
,
96 $locDataURL = CRM_Utils_System
::url('civicrm/ajax/permlocation', $args, FALSE, NULL, FALSE);
97 $form->assign('locDataURL', $locDataURL);
100 if ($form->_values
['is_for_organization'] != 2) {
101 $form->assign('relatedOrganizationFound', $form->_relatedOrganizationFound
);
104 $form->assign('onBehalfRequired', $form->_onBehalfRequired
);
107 if (count($form->_employers
) == 1) {
108 foreach ($form->_employers
as $id => $value) {
109 $form->_organizationName
= $value['name'];
112 $form->assign('orgId', $orgId);
113 $form->assign('organizationName', $form->_organizationName
);
119 * Function to build form for related contacts / on behalf of organization.
121 * @param $form object invoking Object
123 * @internal param string $contactType contact type
124 * @internal param string $title fieldset title
128 static function buildQuickForm(&$form) {
129 $form->assign('fieldSetTitle', ts('Organization Details'));
130 $form->assign('buildOnBehalfForm', TRUE);
132 $contactID = $form->_contactID
;
134 if ($contactID && count($form->_employers
) >= 1) {
135 $form->add('text', 'organization_id', ts('Select an existing related Organization OR enter a new one'));
137 $form->add('select', 'onbehalfof_id', '', CRM_Utils_Array
::collect('name', $form->_employers
));
140 0 => ts('Select an existing organization'),
141 1 => ts('Enter a new organization'),
144 $form->addRadio('org_option', ts('options'), $orgOptions);
145 $form->setDefaults(array('org_option' => 0));
146 $form->add('checkbox', 'mode', '');
149 $profileFields = CRM_Core_BAO_UFGroup
::getFields($form->_profileId
, FALSE, CRM_Core_Action
::VIEW
, NULL,
150 NULL, FALSE, NULL, FALSE, NULL,
151 CRM_Core_Permission
::CREATE
, NULL
153 $fieldTypes = array('Contact', 'Organization');
154 $contactSubType = CRM_Contact_BAO_ContactType
::subTypes('Organization');
155 $fieldTypes = array_merge($fieldTypes, $contactSubType);
157 if (is_array($form->_membershipBlock
) && !empty($form->_membershipBlock
)) {
158 $fieldTypes = array_merge($fieldTypes, array('Membership'));
161 $fieldTypes = array_merge($fieldTypes, array('Contribution'));
164 $stateCountryMap = array();
165 foreach ($profileFields as $name => $field) {
166 if (in_array($field['field_type'], $fieldTypes)) {
167 list($prefixName, $index) = CRM_Utils_System
::explode('-', $name, 2);
168 if (in_array($prefixName, array(
169 'state_province', 'country', 'county'))) {
170 if (!array_key_exists($index, $stateCountryMap)) {
171 $stateCountryMap[$index] = array();
174 $stateCountryMap[$index][$prefixName] = 'onbehalf[' . $name . ']';
176 if (count($form->_submitValues
)) {
177 $locationTypeId = $field['location_type_id'];
178 if (!empty($form->_submitValues
['onbehalf']["country-{$locationTypeId}"]) &&
179 $prefixName == "state_province") {
180 $field['is_required'] = CRM_Core_Payment_Form
::checkRequiredStateProvince($form, "country-{$locationTypeId}", TRUE);
184 elseif (in_array($prefixName, array(
185 'organization_name', 'email')) && empty($field['is_required'])) {
186 $field['is_required'] = 1;
189 CRM_Core_BAO_UFGroup
::buildProfile($form, $field, NULL, NULL, FALSE, TRUE);
193 if (!empty($stateCountryMap)) {
194 CRM_Core_BAO_Address
::addStateCountryMap($stateCountryMap);
196 // now fix all state country selectors
197 CRM_Core_BAO_Address
::fixAllStateSelects($form, CRM_Core_DAO
::$_nullArray);
200 $form->assign('onBehalfOfFields', $profileFields);
201 $form->addElement('hidden', 'hidden_onbehalf_profile', 1);