3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
35 class CRM_Contribute_Form_Contribution_OnBehalfOf
{
38 * Function to set variables up before form is built
43 static function preProcess(&$form) {
44 $session = CRM_Core_Session
::singleton();
45 $contactID = $session->get('userID');
47 $ufJoinParams = array(
48 'module' => 'onBehalf',
49 'entity_table' => 'civicrm_contribution_page',
50 'entity_id' => $form->_id
,
52 $profileId = CRM_Core_BAO_UFJoin
::getUFGroupIds($ufJoinParams);
53 $form->_profileId
= $profileId[0];
55 if (!$form->_profileId ||
56 !CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', $form->_profileId
, 'is_active')
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.'));
61 $requiredProfileFields = array('organization_name', 'email');
62 $validProfile = CRM_Core_BAO_UFGroup
::checkValidProfile($form->_profileId
, $requiredProfileFields);
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.'));
67 $form->assign('profileId', $form->_profileId
);
68 $form->assign('mode', $form->_mode
);
71 $form->_employers
= CRM_Contact_BAO_Relationship
::getPermissionedEmployer($contactID);
72 if (!empty($form->_employers
)) {
73 $form->_relatedOrganizationFound
= TRUE;
75 $locDataURL = CRM_Utils_System
::url('civicrm/ajax/permlocation', 'cid=', FALSE, NULL, FALSE);
76 $form->assign('locDataURL', $locDataURL);
78 $dataURL = CRM_Utils_System
::url('civicrm/ajax/employer', 'cid=' . $contactID, FALSE, NULL, FALSE);
79 $form->assign('employerDataURL', $dataURL);
82 if ($form->_values
['is_for_organization'] != 2) {
83 $form->assign('relatedOrganizationFound', $form->_relatedOrganizationFound
);
86 $form->assign('onBehalfRequired', $form->_onBehalfRequired
);
89 if (count($form->_employers
) == 1) {
90 foreach ($form->_employers
as $id => $value) {
91 $form->_organizationName
= $value['name'];
94 $form->assign('orgId', $orgId);
95 $form->assign('organizationName', $form->_organizationName
);
101 * Function to build form for related contacts / on behalf of organization.
103 * @param $form object invoking Object
104 * @param $contactType string contact type
105 * @param $title string fieldset title
109 static function buildQuickForm(&$form) {
110 $form->assign('fieldSetTitle', ts('Organization Details'));
111 $form->assign('buildOnBehalfForm', TRUE);
113 $session = CRM_Core_Session
::singleton();
114 $contactID = $session->get('userID');
116 if ($contactID && count($form->_employers
) >= 1) {
117 $form->add('text', 'organization_id', ts('Select an existing related Organization OR enter a new one'));
118 $form->add('hidden', 'onbehalfof_id', '', array('id' => 'onbehalfof_id'));
120 $orgOptions = array(0 => ts('Select an existing organization'),
121 1 => ts('Enter a new organization'),
124 $form->addRadio('org_option', ts('options'), $orgOptions);
125 $form->setDefaults(array('org_option' => 0));
126 $form->add('checkbox', 'mode', '');
129 $profileFields = CRM_Core_BAO_UFGroup
::getFields($form->_profileId
, FALSE, CRM_Core_Action
::VIEW
, NULL,
130 NULL, FALSE, NULL, FALSE, NULL,
131 CRM_Core_Permission
::CREATE
, NULL
133 $fieldTypes = array('Contact', 'Organization');
134 $contactSubType = CRM_Contact_BAO_ContactType
::subTypes('Organization');
135 $fieldTypes = array_merge($fieldTypes, $contactSubType);
137 if (is_array($form->_membershipBlock
) && !empty($form->_membershipBlock
)) {
138 $fieldTypes = array_merge($fieldTypes, array('Membership'));
141 $fieldTypes = array_merge($fieldTypes, array('Contribution'));
144 $stateCountryMap = array();
145 foreach ($profileFields as $name => $field) {
146 if (in_array($field['field_type'], $fieldTypes)) {
147 list($prefixName, $index) = CRM_Utils_System
::explode('-', $name, 2);
148 if (in_array($prefixName, array(
149 'state_province', 'country', 'county'))) {
150 if (!array_key_exists($index, $stateCountryMap)) {
151 $stateCountryMap[$index] = array();
154 $stateCountryMap[$index][$prefixName] = 'onbehalf[' . $name . ']';
156 elseif (in_array($prefixName, array(
157 'organization_name', 'email')) &&
158 !CRM_Utils_Array
::value('is_required', $field)
160 $field['is_required'] = 1;
163 CRM_Core_BAO_UFGroup
::buildProfile($form, $field, NULL, NULL, FALSE, TRUE);
167 if (!empty($stateCountryMap)) {
168 CRM_Core_BAO_Address
::addStateCountryMap($stateCountryMap);
170 // now fix all state country selectors
171 CRM_Core_BAO_Address
::fixAllStateSelects($form, CRM_Core_DAO
::$_nullArray);
174 $form->assign('onBehalfOfFields', $profileFields);
175 $form->addElement('hidden', 'hidden_onbehalf_profile', 1);