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';
59 $allowSubTypes['ContributionType'] = array($financialTypeId);
60 $entities[] = array('entity_name' => 'contribution_1', 'entity_type' => 'ContributionModel', 'entity_sub_type' => '*');
62 // If applicable, register 'membership_1'
63 $member = CRM_Member_BAO_Membership
::getMembershipBlock($this->_id
);
64 if ($member && $member['is_active']) {
66 $entities[] = array('entity_name' => 'membership_1', 'entity_type' => 'MembershipModel', 'entity_sub_type' => '*');
67 $allowCoreTypes[] = 'Membership';
68 $allowSubTypes['MembershipType'] = explode(',', $member['membership_types']);
71 $this->addProfileSelector('custom_pre_id', ts('Include Profile') . '<br />' . ts('(top of page)'), $allowCoreTypes, $allowSubTypes, $entities, TRUE);
72 $this->addProfileSelector('custom_post_id', ts('Include Profile') . '<br />' . ts('(bottom of page)'), $allowCoreTypes, $allowSubTypes, $entities, TRUE);
74 $this->addFormRule(array('CRM_Contribute_Form_ContributionPage_Custom', 'formRule'), $this->_id
);
76 parent
::buildQuickForm();
80 * This function sets the default values for the form. Note that in edit/view mode
81 * the default values are retrieved from the database
87 function setDefaultValues() {
88 $defaults = parent
::setDefaultValues();
91 $title = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_ContributionPage', $this->_id
, 'title');
92 CRM_Utils_System
::setTitle(ts('Include Profiles (%1)', array(1 => $title)));
96 $ufJoinParams = array(
97 'module' => 'CiviContribute',
98 'entity_table' => 'civicrm_contribution_page',
99 'entity_id' => $this->_id
,
101 list($defaults['custom_pre_id'],
102 $second) = CRM_Core_BAO_UFJoin
::getUFGroupIds($ufJoinParams);
103 $defaults['custom_post_id'] = $second ?
array_shift($second) : '';
115 public function postProcess() {
116 // get the submitted form values.
117 $params = $this->controller
->exportValues($this->_name
);
119 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
120 $params['id'] = $this->_id
;
123 $transaction = new CRM_Core_Transaction();
125 // also update uf join table
126 $ufJoinParams = array(
128 'module' => 'CiviContribute',
129 'entity_table' => 'civicrm_contribution_page',
130 'entity_id' => $this->_id
,
133 // first delete all past entries
134 CRM_Core_BAO_UFJoin
::deleteAll($ufJoinParams);
136 if (!empty($params['custom_pre_id'])) {
137 $ufJoinParams['weight'] = 1;
138 $ufJoinParams['uf_group_id'] = $params['custom_pre_id'];
139 CRM_Core_BAO_UFJoin
::create($ufJoinParams);
142 unset($ufJoinParams['id']);
144 if (!empty($params['custom_post_id'])) {
145 $ufJoinParams['weight'] = 2;
146 $ufJoinParams['uf_group_id'] = $params['custom_post_id'];
147 CRM_Core_BAO_UFJoin
::create($ufJoinParams);
150 $transaction->commit();
151 parent
::endPostProcess();
155 * Return a descriptive name for the page, used in wizard header
160 public function getTitle() {
161 return ts('Include Profiles');
167 * @param array $fields the input form values
170 * @param $contributionPageId
172 * @return true if no errors, else array of errors
176 static function formRule($fields, $files, $contributionPageId) {
178 $preProfileType = $postProfileType = NULL;
179 // for membership profile make sure Membership section is enabled
180 // get membership section for this contribution page
181 $dao = new CRM_Member_DAO_MembershipBlock();
182 $dao->entity_table
= 'civicrm_contribution_page';
183 $dao->entity_id
= $contributionPageId;
185 $membershipEnable = FALSE;
187 if ($dao->find(TRUE) && $dao->is_active
) {
188 $membershipEnable = TRUE;
191 if ($fields['custom_pre_id']) {
192 $preProfileType = CRM_Core_BAO_UFField
::getProfileType($fields['custom_pre_id']);
195 if ($fields['custom_post_id']) {
196 $postProfileType = CRM_Core_BAO_UFField
::getProfileType($fields['custom_post_id']);
199 $errorMsg = ts('You must enable the Membership Block for this Contribution Page if you want to include a Profile with Membership fields.');
201 if (($preProfileType == 'Membership') && !$membershipEnable) {
202 $errors['custom_pre_id'] = $errorMsg;
205 if (($postProfileType == 'Membership') && !$membershipEnable) {
206 $errors['custom_post_id'] = $errorMsg;
209 $behalf = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_ContributionPage', $contributionPageId, 'is_for_organization');
210 if ($fields['custom_pre_id']) {
211 $errorMsg = ts('You should move the membership related fields in the "On Behalf" profile for this Contribution Page');
212 if ($preProfileType == 'Membership' && $behalf) {
213 $errors['custom_pre_id'] = isset($errors['custom_pre_id']) ?
$errors['custom_pre_id'] . $errorMsg : $errorMsg;
217 if ($fields['custom_post_id']) {
218 $errorMsg = ts('You should move the membership related fields in the "On Behalf" profile for this Contribution Page');
219 if ($postProfileType == 'Membership' && $behalf) {
220 $errors['custom_post_id'] = isset($errors['custom_post_id']) ?
$errors['custom_post_id'] . $errorMsg : $errorMsg;
223 return empty($errors) ?
TRUE : $errors;