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
37 * form to process actions on the group aspect of Custom Data
39 class CRM_Contribute_Form_ContributionPage_Custom
extends CRM_Contribute_Form_ContributionPage
{
42 * Function to actually build the form
47 public function buildQuickForm() {
49 // Register 'contact_1' model
51 $entities[] = array('entity_name' => 'contact_1', 'entity_type' => 'IndividualModel');
52 $allowCoreTypes = array_merge(array('Contact', 'Individual'), CRM_Contact_BAO_ContactType
::subTypes('Individual'));
53 $allowSubTypes = array();
55 // Register 'contribution_1'
56 $financialTypeId = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_ContributionPage', $this->_id
, 'financial_type_id');
57 $allowCoreTypes[] = 'Contribution';
58 // $allowSubTypes['ContributionType'] = array($financialTypeId);
59 $entities[] = array('entity_name' => 'contribution_1', 'entity_type' => 'ContributionModel', 'entity_sub_type' => $financialTypeId);
61 // If applicable, register 'membership_1'
62 $member = CRM_Member_BAO_Membership
::getMembershipBlock($this->_id
);
63 if ($member && $member['is_active']) {
64 $entities[] = array('entity_name' => 'membership_1', 'entity_type' => 'MembershipModel', 'entity_sub_type' => $member['membership_type_default']);
65 $allowCoreTypes[] = 'Membership';
66 $allowSubTypes['MembershipType'] = explode(',', $member['membership_types']);
69 $this->addProfileSelector('custom_pre_id', ts('Include Profile') . '<br />' . ts('(top of page)'), $allowCoreTypes, $allowSubTypes, $entities);
70 $this->addProfileSelector('custom_post_id', ts('Include Profile') . '<br />' . ts('(bottom of page)'), $allowCoreTypes, $allowSubTypes, $entities);
72 $this->addFormRule(array('CRM_Contribute_Form_ContributionPage_Custom', 'formRule'), $this->_id
);
74 parent
::buildQuickForm();
78 * This function sets the default values for the form. Note that in edit/view mode
79 * the default values are retrieved from the database
85 function setDefaultValues() {
86 $defaults = parent
::setDefaultValues();
89 $title = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_ContributionPage', $this->_id
, 'title');
90 CRM_Utils_System
::setTitle(ts('Include Profiles (%1)', array(1 => $title)));
94 $ufJoinParams = array(
95 'module' => 'CiviContribute',
96 'entity_table' => 'civicrm_contribution_page',
97 'entity_id' => $this->_id
,
99 list($defaults['custom_pre_id'],
100 $second) = CRM_Core_BAO_UFJoin
::getUFGroupIds($ufJoinParams);
101 $defaults['custom_post_id'] = $second ?
array_shift($second) : '';
113 public function postProcess() {
114 // get the submitted form values.
115 $params = $this->controller
->exportValues($this->_name
);
117 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
118 $params['id'] = $this->_id
;
121 $transaction = new CRM_Core_Transaction();
123 // also update uf join table
124 $ufJoinParams = array(
126 'module' => 'CiviContribute',
127 'entity_table' => 'civicrm_contribution_page',
128 'entity_id' => $this->_id
,
131 // first delete all past entries
132 CRM_Core_BAO_UFJoin
::deleteAll($ufJoinParams);
134 if (!empty($params['custom_pre_id'])) {
135 $ufJoinParams['weight'] = 1;
136 $ufJoinParams['uf_group_id'] = $params['custom_pre_id'];
137 CRM_Core_BAO_UFJoin
::create($ufJoinParams);
140 unset($ufJoinParams['id']);
142 if (!empty($params['custom_post_id'])) {
143 $ufJoinParams['weight'] = 2;
144 $ufJoinParams['uf_group_id'] = $params['custom_post_id'];
145 CRM_Core_BAO_UFJoin
::create($ufJoinParams);
148 $transaction->commit();
149 parent
::endPostProcess();
153 * Return a descriptive name for the page, used in wizard header
158 public function getTitle() {
159 return ts('Include Profiles');
165 * @param array $fields the input form values
168 * @param $contributionPageId
170 * @return true if no errors, else array of errors
174 static function formRule($fields, $files, $contributionPageId) {
176 $preProfileType = $postProfileType = NULL;
177 // for membership profile make sure Membership section is enabled
178 // get membership section for this contribution page
179 $dao = new CRM_Member_DAO_MembershipBlock();
180 $dao->entity_table
= 'civicrm_contribution_page';
181 $dao->entity_id
= $contributionPageId;
183 $membershipEnable = FALSE;
185 if ($dao->find(TRUE) && $dao->is_active
) {
186 $membershipEnable = TRUE;
189 if ($fields['custom_pre_id']) {
190 $preProfileType = CRM_Core_BAO_UFField
::getProfileType($fields['custom_pre_id']);
193 if ($fields['custom_post_id']) {
194 $postProfileType = CRM_Core_BAO_UFField
::getProfileType($fields['custom_post_id']);
197 $errorMsg = ts('You must enable the Membership Block for this Contribution Page if you want to include a Profile with Membership fields.');
199 if (($preProfileType == 'Membership') && !$membershipEnable) {
200 $errors['custom_pre_id'] = $errorMsg;
203 if (($postProfileType == 'Membership') && !$membershipEnable) {
204 $errors['custom_post_id'] = $errorMsg;
207 $behalf = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_ContributionPage', $contributionPageId, 'is_for_organization');
208 if ($fields['custom_pre_id']) {
209 $errorMsg = ts('You should move the membership related fields in the "On Behalf" profile for this Contribution Page');
210 if ($preProfileType == 'Membership' && $behalf) {
211 $errors['custom_pre_id'] = isset($errors['custom_pre_id']) ?
$errors['custom_pre_id'] . $errorMsg : $errorMsg;
215 if ($fields['custom_post_id']) {
216 $errorMsg = ts('You should move the membership related fields in the "On Behalf" profile for this Contribution Page');
217 if ($postProfileType == 'Membership' && $behalf) {
218 $errors['custom_post_id'] = isset($errors['custom_post_id']) ?
$errors['custom_post_id'] . $errorMsg : $errorMsg;
221 return empty($errors) ?
TRUE : $errors;