Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * form to process actions on the group aspect of Custom Data | |
38 | */ | |
39 | class CRM_Contribute_Form_ContributionPage_Custom extends CRM_Contribute_Form_ContributionPage { | |
40 | ||
41 | /** | |
42 | * Function to actually build the form | |
43 | * | |
44 | * @return void | |
45 | * @access public | |
46 | */ | |
47 | public function buildQuickForm() { | |
6a488035 | 48 | |
ede14745 | 49 | // Register 'contact_1' model |
c50f55b5 | 50 | $entities = array(); |
41fd8e26 | 51 | $entities[] = array('entity_name' => 'contact_1', 'entity_type' => 'IndividualModel'); |
c50f55b5 CW |
52 | $allowCoreTypes = array_merge(array('Contact', 'Individual'), CRM_Contact_BAO_ContactType::subTypes('Individual')); |
53 | $allowSubTypes = array(); | |
54 | ||
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); | |
60 | ||
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']); | |
6a488035 TO |
67 | } |
68 | ||
c50f55b5 CW |
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); | |
6a488035 TO |
71 | |
72 | $this->addFormRule(array('CRM_Contribute_Form_ContributionPage_Custom', 'formRule'), $this->_id); | |
73 | ||
74 | parent::buildQuickForm(); | |
75 | } | |
76 | ||
77 | /** | |
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 | |
80 | * | |
81 | * @access public | |
82 | * | |
83 | * @return void | |
84 | */ | |
85 | function setDefaultValues() { | |
86 | $defaults = parent::setDefaultValues(); | |
87 | ||
88 | if ($this->_id) { | |
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))); | |
91 | } | |
92 | ||
93 | ||
94 | $ufJoinParams = array( | |
95 | 'module' => 'CiviContribute', | |
96 | 'entity_table' => 'civicrm_contribution_page', | |
97 | 'entity_id' => $this->_id, | |
98 | ); | |
99 | list($defaults['custom_pre_id'], | |
c50f55b5 CW |
100 | $second) = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams); |
101 | $defaults['custom_post_id'] = $second ? array_shift($second) : ''; | |
102 | ||
6a488035 TO |
103 | |
104 | return $defaults; | |
105 | } | |
106 | ||
107 | /** | |
108 | * Process the form | |
109 | * | |
110 | * @return void | |
111 | * @access public | |
112 | */ | |
113 | public function postProcess() { | |
114 | // get the submitted form values. | |
115 | $params = $this->controller->exportValues($this->_name); | |
116 | ||
117 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
118 | $params['id'] = $this->_id; | |
119 | } | |
120 | ||
121 | $transaction = new CRM_Core_Transaction(); | |
122 | ||
123 | // also update uf join table | |
124 | $ufJoinParams = array( | |
125 | 'is_active' => 1, | |
126 | 'module' => 'CiviContribute', | |
127 | 'entity_table' => 'civicrm_contribution_page', | |
128 | 'entity_id' => $this->_id, | |
129 | ); | |
130 | ||
131 | // first delete all past entries | |
132 | CRM_Core_BAO_UFJoin::deleteAll($ufJoinParams); | |
133 | ||
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); | |
138 | } | |
139 | ||
140 | unset($ufJoinParams['id']); | |
141 | ||
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); | |
146 | } | |
147 | ||
148 | $transaction->commit(); | |
149 | parent::endPostProcess(); | |
150 | } | |
151 | ||
152 | /** | |
153 | * Return a descriptive name for the page, used in wizard header | |
154 | * | |
155 | * @return string | |
156 | * @access public | |
157 | */ | |
158 | public function getTitle() { | |
159 | return ts('Include Profiles'); | |
160 | } | |
161 | ||
162 | /** | |
163 | * global form rule | |
164 | * | |
da6b46f4 EM |
165 | * @param array $fields the input form values |
166 | * | |
167 | * @param $files | |
168 | * @param $contributionPageId | |
6a488035 TO |
169 | * |
170 | * @return true if no errors, else array of errors | |
171 | * @access public | |
172 | * @static | |
173 | */ | |
174 | static function formRule($fields, $files, $contributionPageId) { | |
175 | $errors = array(); | |
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; | |
182 | ||
183 | $membershipEnable = FALSE; | |
184 | ||
185 | if ($dao->find(TRUE) && $dao->is_active) { | |
186 | $membershipEnable = TRUE; | |
187 | } | |
188 | ||
189 | if ($fields['custom_pre_id']) { | |
190 | $preProfileType = CRM_Core_BAO_UFField::getProfileType($fields['custom_pre_id']); | |
191 | } | |
192 | ||
193 | if ($fields['custom_post_id']) { | |
194 | $postProfileType = CRM_Core_BAO_UFField::getProfileType($fields['custom_post_id']); | |
195 | } | |
196 | ||
197 | $errorMsg = ts('You must enable the Membership Block for this Contribution Page if you want to include a Profile with Membership fields.'); | |
198 | ||
199 | if (($preProfileType == 'Membership') && !$membershipEnable) { | |
200 | $errors['custom_pre_id'] = $errorMsg; | |
201 | } | |
202 | ||
203 | if (($postProfileType == 'Membership') && !$membershipEnable) { | |
204 | $errors['custom_post_id'] = $errorMsg; | |
205 | } | |
665e5ec7 | 206 | |
6a488035 TO |
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; | |
212 | } | |
213 | } | |
214 | ||
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; | |
219 | } | |
220 | } | |
221 | return empty($errors) ? TRUE : $errors; | |
222 | } | |
223 | } | |
224 |