3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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 * Build the form object
46 public function buildQuickForm() {
48 // Register 'contact_1' model
50 $entities[] = array('entity_name' => 'contact_1', 'entity_type' => 'IndividualModel');
51 $allowCoreTypes = array_merge(array('Contact', 'Individual'), CRM_Contact_BAO_ContactType
::subTypes('Individual'));
52 $allowSubTypes = array();
54 // Register 'contribution_1'
55 $financialTypeId = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_ContributionPage', $this->_id
, 'financial_type_id');
56 $allowCoreTypes[] = 'Contribution';
58 $allowSubTypes['ContributionType'] = array($financialTypeId);
59 $entities[] = array('entity_name' => 'contribution_1', 'entity_type' => 'ContributionModel', 'entity_sub_type' => '*');
61 // If applicable, register 'membership_1'
62 $member = CRM_Member_BAO_Membership
::getMembershipBlock($this->_id
);
63 if ($member && $member['is_active']) {
65 $entities[] = array('entity_name' => 'membership_1', 'entity_type' => 'MembershipModel', 'entity_sub_type' => '*');
66 $allowCoreTypes[] = 'Membership';
67 $allowSubTypes['MembershipType'] = explode(',', $member['membership_types']);
70 $this->addProfileSelector('custom_pre_id', ts('Include Profile') . '<br />' . ts('(top of page)'), $allowCoreTypes, $allowSubTypes, $entities, TRUE);
71 $this->addProfileSelector('custom_post_id', ts('Include Profile') . '<br />' . ts('(bottom of page)'), $allowCoreTypes, $allowSubTypes, $entities, TRUE);
73 $this->addFormRule(array('CRM_Contribute_Form_ContributionPage_Custom', 'formRule'), $this->_id
);
75 parent
::buildQuickForm();
79 * Set default values for the form. Note that in edit/view mode
80 * the default values are retrieved from the database
85 public 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') . " ($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) : '';
112 public function postProcess() {
113 // get the submitted form values.
114 $params = $this->controller
->exportValues($this->_name
);
116 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
117 $params['id'] = $this->_id
;
120 $transaction = new CRM_Core_Transaction();
122 // also update uf join table
123 $ufJoinParams = array(
125 'module' => 'CiviContribute',
126 'entity_table' => 'civicrm_contribution_page',
127 'entity_id' => $this->_id
,
130 // first delete all past entries
131 CRM_Core_BAO_UFJoin
::deleteAll($ufJoinParams);
133 if (!empty($params['custom_pre_id'])) {
134 $ufJoinParams['weight'] = 1;
135 $ufJoinParams['uf_group_id'] = $params['custom_pre_id'];
136 CRM_Core_BAO_UFJoin
::create($ufJoinParams);
139 unset($ufJoinParams['id']);
141 if (!empty($params['custom_post_id'])) {
142 $ufJoinParams['weight'] = 2;
143 $ufJoinParams['uf_group_id'] = $params['custom_post_id'];
144 CRM_Core_BAO_UFJoin
::create($ufJoinParams);
147 $transaction->commit();
148 parent
::endPostProcess();
152 * Return a descriptive name for the page, used in wizard header
156 public function getTitle() {
157 return ts('Include Profiles');
163 * @param array $fields
164 * The input form values.
167 * @param int $contributionPageId
169 * @return true if no errors, else array of errors
172 public static function formRule($fields, $files, $contributionPageId) {
174 $preProfileType = $postProfileType = NULL;
175 // for membership profile make sure Membership section is enabled
176 // get membership section for this contribution page
177 $dao = new CRM_Member_DAO_MembershipBlock();
178 $dao->entity_table
= 'civicrm_contribution_page';
179 $dao->entity_id
= $contributionPageId;
181 $membershipEnable = FALSE;
183 if ($dao->find(TRUE) && $dao->is_active
) {
184 $membershipEnable = TRUE;
187 if ($fields['custom_pre_id']) {
188 $preProfileType = CRM_Core_BAO_UFField
::getProfileType($fields['custom_pre_id']);
191 if ($fields['custom_post_id']) {
192 $postProfileType = CRM_Core_BAO_UFField
::getProfileType($fields['custom_post_id']);
195 $errorMsg = ts('You must enable the Membership Block for this Contribution Page if you want to include a Profile with Membership fields.');
197 if (($preProfileType == 'Membership') && !$membershipEnable) {
198 $errors['custom_pre_id'] = $errorMsg;
201 if (($postProfileType == 'Membership') && !$membershipEnable) {
202 $errors['custom_post_id'] = $errorMsg;
205 $behalf = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_ContributionPage', $contributionPageId, 'is_for_organization');
206 if ($fields['custom_pre_id']) {
207 $errorMsg = ts('You should move the membership related fields in the "On Behalf" profile for this Contribution Page');
208 if ($preProfileType == 'Membership' && $behalf) {
209 $errors['custom_pre_id'] = isset($errors['custom_pre_id']) ?
$errors['custom_pre_id'] . $errorMsg : $errorMsg;
213 if ($fields['custom_post_id']) {
214 $errorMsg = ts('You should move the membership related fields in the "On Behalf" profile for this Contribution Page');
215 if ($postProfileType == 'Membership' && $behalf) {
216 $errors['custom_post_id'] = isset($errors['custom_post_id']) ?
$errors['custom_post_id'] . $errorMsg : $errorMsg;
219 return empty($errors) ?
TRUE : $errors;