3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 +--------------------------------------------------------------------+
30 * @copyright CiviCRM LLC (c) 2004-2015
36 * Form to process actions on the group aspect of Custom Data.
38 class CRM_Contribute_Form_ContributionPage_Custom
extends CRM_Contribute_Form_ContributionPage
{
41 * Build the form object.
45 public function buildQuickForm() {
47 // Register 'contact_1' model
49 $entities[] = array('entity_name' => 'contact_1', 'entity_type' => 'IndividualModel');
50 $allowCoreTypes = array_merge(array('Contact', 'Individual'), CRM_Contact_BAO_ContactType
::subTypes('Individual'));
51 $allowSubTypes = array();
53 // Register 'contribution_1'
54 $financialTypeId = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_ContributionPage', $this->_id
, 'financial_type_id');
55 $allowCoreTypes[] = 'Contribution';
57 $allowSubTypes['ContributionType'] = array($financialTypeId);
59 'entity_name' => 'contribution_1',
60 'entity_type' => 'ContributionModel',
61 'entity_sub_type' => '*',
64 // If applicable, register 'membership_1'
65 $member = CRM_Member_BAO_Membership
::getMembershipBlock($this->_id
);
66 if ($member && $member['is_active']) {
69 'entity_name' => 'membership_1',
70 'entity_type' => 'MembershipModel',
71 'entity_sub_type' => '*',
73 $allowCoreTypes[] = 'Membership';
74 $allowSubTypes['MembershipType'] = explode(',', $member['membership_types']);
77 $this->addProfileSelector('custom_pre_id', ts('Include Profile') . '<br />' . ts('(top of page)'), $allowCoreTypes, $allowSubTypes, $entities, TRUE);
78 $this->addProfileSelector('custom_post_id', ts('Include Profile') . '<br />' . ts('(bottom of page)'), $allowCoreTypes, $allowSubTypes, $entities, TRUE);
80 $this->addFormRule(array('CRM_Contribute_Form_ContributionPage_Custom', 'formRule'), $this->_id
);
82 parent
::buildQuickForm();
86 * Set default values for the form. Note that in edit/view mode
87 * the default values are retrieved from the database
92 public function setDefaultValues() {
93 $defaults = parent
::setDefaultValues();
96 $title = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_ContributionPage', $this->_id
, 'title');
97 CRM_Utils_System
::setTitle(ts('Include Profiles') . " ($title)");
100 $ufJoinParams = array(
101 'module' => 'CiviContribute',
102 'entity_table' => 'civicrm_contribution_page',
103 'entity_id' => $this->_id
,
105 list($defaults['custom_pre_id'],
106 $second) = CRM_Core_BAO_UFJoin
::getUFGroupIds($ufJoinParams);
107 $defaults['custom_post_id'] = $second ?
array_shift($second) : '';
117 public function postProcess() {
118 // get the submitted form values.
119 $params = $this->controller
->exportValues($this->_name
);
121 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
122 $params['id'] = $this->_id
;
125 $transaction = new CRM_Core_Transaction();
127 // also update uf join table
128 $ufJoinParams = array(
130 'module' => 'CiviContribute',
131 'entity_table' => 'civicrm_contribution_page',
132 'entity_id' => $this->_id
,
135 // first delete all past entries
136 CRM_Core_BAO_UFJoin
::deleteAll($ufJoinParams);
138 if (!empty($params['custom_pre_id'])) {
139 $ufJoinParams['weight'] = 1;
140 $ufJoinParams['uf_group_id'] = $params['custom_pre_id'];
141 CRM_Core_BAO_UFJoin
::create($ufJoinParams);
144 unset($ufJoinParams['id']);
146 if (!empty($params['custom_post_id'])) {
147 $ufJoinParams['weight'] = 2;
148 $ufJoinParams['uf_group_id'] = $params['custom_post_id'];
149 CRM_Core_BAO_UFJoin
::create($ufJoinParams);
152 $transaction->commit();
153 parent
::endPostProcess();
157 * Return a descriptive name for the page, used in wizard header
161 public function getTitle() {
162 return ts('Include Profiles');
168 * @param array $fields
169 * The input form values.
172 * @param int $contributionPageId
175 * true if no errors, else array of errors
177 public static function formRule($fields, $files, $contributionPageId) {
179 $preProfileType = $postProfileType = NULL;
180 // for membership profile make sure Membership section is enabled
181 // get membership section for this contribution page
182 $dao = new CRM_Member_DAO_MembershipBlock();
183 $dao->entity_table
= 'civicrm_contribution_page';
184 $dao->entity_id
= $contributionPageId;
186 $membershipEnable = FALSE;
188 if ($dao->find(TRUE) && $dao->is_active
) {
189 $membershipEnable = TRUE;
192 if ($fields['custom_pre_id']) {
193 $preProfileType = CRM_Core_BAO_UFField
::getProfileType($fields['custom_pre_id']);
196 if ($fields['custom_post_id']) {
197 $postProfileType = CRM_Core_BAO_UFField
::getProfileType($fields['custom_post_id']);
200 $errorMsg = ts('You must enable the Membership Block for this Contribution Page if you want to include a Profile with Membership fields.');
202 if (($preProfileType == 'Membership') && !$membershipEnable) {
203 $errors['custom_pre_id'] = $errorMsg;
206 if (($postProfileType == 'Membership') && !$membershipEnable) {
207 $errors['custom_post_id'] = $errorMsg;
210 $behalf = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_ContributionPage', $contributionPageId, 'is_for_organization');
211 if ($fields['custom_pre_id']) {
212 $errorMsg = ts('You should move the membership related fields in the "On Behalf" profile for this Contribution Page');
213 if ($preProfileType == 'Membership' && $behalf) {
214 $errors['custom_pre_id'] = isset($errors['custom_pre_id']) ?
$errors['custom_pre_id'] . $errorMsg : $errorMsg;
218 if ($fields['custom_post_id']) {
219 $errorMsg = ts('You should move the membership related fields in the "On Behalf" profile for this Contribution Page');
220 if ($postProfileType == 'Membership' && $behalf) {
221 $errors['custom_post_id'] = isset($errors['custom_post_id']) ?
$errors['custom_post_id'] . $errorMsg : $errorMsg;
224 return empty($errors) ?
TRUE : $errors;