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