Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
07f8d162 | 35 | * Contribution Page form. |
6a488035 TO |
36 | */ |
37 | class CRM_Contribute_Form_ContributionPage extends CRM_Core_Form { | |
38 | ||
39 | /** | |
fe482240 | 40 | * The page id saved to the session for an update. |
6a488035 TO |
41 | * |
42 | * @var int | |
6a488035 TO |
43 | */ |
44 | protected $_id; | |
45 | ||
46 | /** | |
fe482240 | 47 | * The pledgeBlock id saved to the session for an update. |
6a488035 TO |
48 | * |
49 | * @var int | |
6a488035 TO |
50 | */ |
51 | protected $_pledgeBlockID; | |
52 | ||
53 | /** | |
100fef9d | 54 | * Are we in single form mode or wizard mode? |
6a488035 TO |
55 | * |
56 | * @var boolean | |
6a488035 TO |
57 | */ |
58 | protected $_single; | |
59 | ||
60 | /** | |
100fef9d | 61 | * Is this the first page? |
6a488035 TO |
62 | * |
63 | * @var boolean | |
6a488035 TO |
64 | */ |
65 | protected $_first = FALSE; | |
66 | ||
5fe87df6 N |
67 | /** |
68 | * Is this the last page? | |
69 | * | |
70 | * @var boolean | |
71 | */ | |
72 | protected $_last = FALSE; | |
73 | ||
6a488035 | 74 | /** |
100fef9d | 75 | * Store price set id. |
6a488035 TO |
76 | * |
77 | * @var int | |
6a488035 TO |
78 | */ |
79 | protected $_priceSetID = NULL; | |
80 | ||
81 | protected $_values; | |
d5965a37 | 82 | |
6e62b28c TM |
83 | /** |
84 | * Explicitly declare the entity api name. | |
85 | */ | |
86 | public function getDefaultEntity() { | |
87 | return 'Contribution'; | |
88 | } | |
6a488035 TO |
89 | |
90 | /** | |
fe482240 | 91 | * Set variables up before form is built. |
6a488035 TO |
92 | */ |
93 | public function preProcess() { | |
94 | // current contribution page id | |
95 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', | |
96 | $this, FALSE, NULL, 'REQUEST' | |
97 | ); | |
98 | $this->assign('contributionPageID', $this->_id); | |
99 | ||
100 | // get the requested action | |
101 | $this->_action = CRM_Utils_Request::retrieve('action', 'String', | |
102 | // default to 'browse' | |
103 | $this, FALSE, 'browse' | |
104 | ); | |
105 | ||
106 | // setting title and 3rd level breadcrumb for html page if contrib page exists | |
107 | if ($this->_id) { | |
108 | $title = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $this->_id, 'title'); | |
109 | ||
110 | if ($this->_action == CRM_Core_Action::UPDATE) { | |
111 | $this->_single = TRUE; | |
112 | } | |
113 | } | |
114 | ||
e4fa553b | 115 | // CRM-16776 - show edit/copy/create buttons on Profiles Tab if user has required permission. |
116 | if (CRM_Core_Permission::check('administer CiviCRM')) { | |
117 | $this->assign('perm', TRUE); | |
118 | } | |
6a488035 TO |
119 | // set up tabs |
120 | CRM_Contribute_Form_ContributionPage_TabHeader::build($this); | |
121 | ||
122 | if ($this->_action == CRM_Core_Action::UPDATE) { | |
123 | CRM_Utils_System::setTitle(ts('Configure Page - %1', array(1 => $title))); | |
124 | } | |
125 | elseif ($this->_action == CRM_Core_Action::VIEW) { | |
126 | CRM_Utils_System::setTitle(ts('Preview Page - %1', array(1 => $title))); | |
127 | } | |
128 | elseif ($this->_action == CRM_Core_Action::DELETE) { | |
129 | CRM_Utils_System::setTitle(ts('Delete Page - %1', array(1 => $title))); | |
130 | } | |
131 | ||
132 | //cache values. | |
133 | $this->_values = $this->get('values'); | |
134 | if (!is_array($this->_values)) { | |
135 | $this->_values = array(); | |
136 | if (isset($this->_id) && $this->_id) { | |
137 | $params = array('id' => $this->_id); | |
138 | CRM_Core_DAO::commonRetrieve('CRM_Contribute_DAO_ContributionPage', $params, $this->_values); | |
139 | } | |
140 | $this->set('values', $this->_values); | |
141 | } | |
c50f55b5 CW |
142 | |
143 | // Preload libraries required by the "Profiles" tab | |
133e2c99 | 144 | $schemas = array('IndividualModel', 'OrganizationModel', 'ContributionModel'); |
c50f55b5 CW |
145 | if (in_array('CiviMember', CRM_Core_Config::singleton()->enableComponents)) { |
146 | $schemas[] = 'MembershipModel'; | |
147 | } | |
148 | CRM_UF_Page_ProfileEditor::registerProfileScripts(); | |
149 | CRM_UF_Page_ProfileEditor::registerSchemas($schemas); | |
6a488035 TO |
150 | } |
151 | ||
152 | /** | |
fe482240 | 153 | * Build the form object. |
6a488035 TO |
154 | */ |
155 | public function buildQuickForm() { | |
156 | $this->applyFilter('__ALL__', 'trim'); | |
157 | ||
158 | $session = CRM_Core_Session::singleton(); | |
159 | $this->_cancelURL = CRM_Utils_Array::value('cancelURL', $_POST); | |
160 | ||
161 | if (!$this->_cancelURL) { | |
162 | $this->_cancelURL = CRM_Utils_System::url('civicrm/admin/contribute', 'reset=1'); | |
163 | } | |
164 | ||
165 | if ($this->_cancelURL) { | |
166 | $this->addElement('hidden', 'cancelURL', $this->_cancelURL); | |
167 | } | |
168 | ||
6a488035 | 169 | if ($this->_single) { |
5fe87df6 N |
170 | $buttons = array( |
171 | array( | |
172 | 'type' => 'next', | |
173 | 'name' => ts('Save'), | |
174 | 'spacing' => ' ', | |
175 | 'isDefault' => TRUE, | |
176 | ), | |
177 | array( | |
178 | 'type' => 'upload', | |
179 | 'name' => ts('Save and Done'), | |
180 | 'spacing' => ' ', | |
181 | 'subName' => 'done', | |
182 | ), | |
6a488035 | 183 | ); |
5fe87df6 N |
184 | if (!$this->_last) { |
185 | $buttons[] = array( | |
186 | 'type' => 'submit', | |
187 | 'name' => ts('Save and Next'), | |
188 | 'spacing' => ' ', | |
189 | 'subName' => 'savenext', | |
190 | ); | |
191 | } | |
192 | $buttons[] = array( | |
193 | 'type' => 'cancel', | |
194 | 'name' => ts('Cancel'), | |
195 | ); | |
196 | $this->addButtons($buttons); | |
6a488035 TO |
197 | } |
198 | else { | |
199 | $buttons = array(); | |
200 | if (!$this->_first) { | |
201 | $buttons[] = array( | |
202 | 'type' => 'back', | |
f212d37d | 203 | 'name' => ts('Previous'), |
6a488035 TO |
204 | 'spacing' => ' ', |
205 | ); | |
206 | } | |
207 | $buttons[] = array( | |
208 | 'type' => 'next', | |
f212d37d | 209 | 'name' => ts('Continue'), |
6a488035 TO |
210 | 'spacing' => ' ', |
211 | 'isDefault' => TRUE, | |
212 | ); | |
213 | $buttons[] = array( | |
214 | 'type' => 'cancel', | |
215 | 'name' => ts('Cancel'), | |
216 | ); | |
217 | ||
218 | $this->addButtons($buttons); | |
219 | } | |
220 | ||
221 | $session->replaceUserContext($this->_cancelURL); | |
222 | // views are implemented as frozen form | |
223 | if ($this->_action & CRM_Core_Action::VIEW) { | |
224 | $this->freeze(); | |
225 | $this->addElement('button', 'done', ts('Done'), array('onclick' => "location.href='civicrm/admin/custom/group?reset=1&action=browse'")); | |
226 | } | |
133e2c99 | 227 | |
1ebebaab AH |
228 | // don't show option for contribution amounts section if membership price set |
229 | // this flag is sent to template | |
133e2c99 | 230 | |
1ebebaab AH |
231 | $membershipBlock = new CRM_Member_DAO_MembershipBlock(); |
232 | $membershipBlock->entity_table = 'civicrm_contribution_page'; | |
233 | $membershipBlock->entity_id = $this->_id; | |
234 | $membershipBlock->is_active = 1; | |
235 | $hasMembershipBlk = FALSE; | |
133e2c99 | 236 | if ($membershipBlock->find(TRUE) && |
1ebebaab AH |
237 | ($setID = CRM_Price_BAO_PriceSet::getFor('civicrm_contribution_page', $this->_id, NULL, 1)) |
238 | ) { | |
239 | $extends = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $setID, 'extends'); | |
240 | if ($extends && $extends == CRM_Core_Component::getComponentID('CiviMember')) { | |
241 | $hasMembershipBlk = TRUE; | |
242 | } | |
243 | } | |
244 | // set value in DOM that membership price set exists | |
245 | CRM_Core_Resources::singleton()->addSetting(array('memberPriceset' => $hasMembershipBlk)); | |
6a488035 TO |
246 | } |
247 | ||
248 | /** | |
c490a46a | 249 | * Set default values for the form. Note that in edit/view mode |
6a488035 TO |
250 | * the default values are retrieved from the database |
251 | * | |
6a488035 | 252 | * |
a6c01b45 CW |
253 | * @return array |
254 | * defaults | |
6a488035 | 255 | */ |
00be9182 | 256 | public function setDefaultValues() { |
6a488035 TO |
257 | //some child classes calling setdefaults directly w/o preprocess. |
258 | $this->_values = $this->get('values'); | |
259 | if (!is_array($this->_values)) { | |
260 | $this->_values = array(); | |
261 | if (isset($this->_id) && $this->_id) { | |
262 | $params = array('id' => $this->_id); | |
263 | CRM_Core_DAO::commonRetrieve('CRM_Contribute_DAO_ContributionPage', $params, $this->_values); | |
264 | } | |
265 | $this->set('values', $this->_values); | |
266 | } | |
267 | $defaults = $this->_values; | |
268 | ||
269 | $config = CRM_Core_Config::singleton(); | |
270 | if (isset($this->_id)) { | |
271 | ||
272 | //set defaults for pledgeBlock values. | |
273 | $pledgeBlockParams = array( | |
274 | 'entity_id' => $this->_id, | |
275 | 'entity_table' => ts('civicrm_contribution_page'), | |
276 | ); | |
277 | $pledgeBlockDefaults = array(); | |
278 | CRM_Pledge_BAO_PledgeBlock::retrieve($pledgeBlockParams, $pledgeBlockDefaults); | |
279 | if ($this->_pledgeBlockID = CRM_Utils_Array::value('id', $pledgeBlockDefaults)) { | |
280 | $defaults['is_pledge_active'] = TRUE; | |
281 | } | |
282 | $pledgeBlock = array( | |
353ffa53 TO |
283 | 'is_pledge_interval', |
284 | 'max_reminders', | |
285 | 'initial_reminder_day', | |
286 | 'additional_reminder_day', | |
6a488035 TO |
287 | ); |
288 | foreach ($pledgeBlock as $key) { | |
289 | $defaults[$key] = CRM_Utils_Array::value($key, $pledgeBlockDefaults); | |
290 | } | |
a7488080 | 291 | if (!empty($pledgeBlockDefaults['pledge_frequency_unit'])) { |
6a488035 | 292 | $defaults['pledge_frequency_unit'] = array_fill_keys(explode(CRM_Core_DAO::VALUE_SEPARATOR, |
353ffa53 TO |
293 | $pledgeBlockDefaults['pledge_frequency_unit'] |
294 | ), '1'); | |
6a488035 TO |
295 | } |
296 | ||
297 | // fix the display of the monetary value, CRM-4038 | |
298 | if (isset($defaults['goal_amount'])) { | |
299 | $defaults['goal_amount'] = CRM_Utils_Money::format($defaults['goal_amount'], NULL, '%a'); | |
300 | } | |
301 | ||
302 | // get price set of type contributions | |
303 | //this is the value for stored in db if price set extends contribution | |
304 | $usedFor = 2; | |
9da8dc8c | 305 | $this->_priceSetID = CRM_Price_BAO_PriceSet::getFor('civicrm_contribution_page', $this->_id, $usedFor, 1); |
6a488035 TO |
306 | if ($this->_priceSetID) { |
307 | $defaults['price_set_id'] = $this->_priceSetID; | |
308 | } | |
309 | ||
a7488080 | 310 | if (!empty($defaults['end_date'])) { |
6a488035 TO |
311 | list($defaults['end_date'], $defaults['end_date_time']) = CRM_Utils_Date::setDateDefaults($defaults['end_date']); |
312 | } | |
313 | ||
a7488080 | 314 | if (!empty($defaults['start_date'])) { |
6a488035 TO |
315 | list($defaults['start_date'], $defaults['start_date_time']) = CRM_Utils_Date::setDateDefaults($defaults['start_date']); |
316 | } | |
317 | } | |
318 | else { | |
319 | $defaults['is_active'] = 1; | |
320 | // set current date as start date | |
321 | list($defaults['start_date'], $defaults['start_date_time']) = CRM_Utils_Date::setDateDefaults(); | |
322 | } | |
323 | ||
324 | if (!isset($defaults['for_organization'])) { | |
325 | $defaults['for_organization'] = ts('I am contributing on behalf of an organization.'); | |
326 | } | |
327 | ||
a7488080 | 328 | if (!empty($defaults['recur_frequency_unit'])) { |
6a488035 | 329 | $defaults['recur_frequency_unit'] = array_fill_keys(explode(CRM_Core_DAO::VALUE_SEPARATOR, |
353ffa53 TO |
330 | $defaults['recur_frequency_unit'] |
331 | ), '1'); | |
6a488035 TO |
332 | } |
333 | else { | |
334 | # CRM 10860 | |
335 | $defaults['recur_frequency_unit'] = array('month' => 1); | |
336 | } | |
337 | ||
a7488080 | 338 | if (!empty($defaults['is_for_organization'])) { |
6a488035 TO |
339 | $defaults['is_organization'] = 1; |
340 | } | |
341 | else { | |
342 | $defaults['is_for_organization'] = 1; | |
343 | } | |
344 | ||
345 | // confirm page starts out enabled | |
346 | if (!isset($defaults['is_confirm_enabled'])) { | |
347 | $defaults['is_confirm_enabled'] = 1; | |
348 | } | |
349 | ||
350 | return $defaults; | |
351 | } | |
352 | ||
353 | /** | |
fe482240 | 354 | * Process the form. |
6a488035 TO |
355 | */ |
356 | public function postProcess() { | |
357 | $pageId = $this->get('id'); | |
358 | //page is newly created. | |
359 | if ($pageId && !$this->_id) { | |
360 | $session = CRM_Core_Session::singleton(); | |
361 | $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/contribute', 'reset=1')); | |
362 | } | |
363 | } | |
364 | ||
00be9182 | 365 | public function endPostProcess() { |
6a488035 TO |
366 | // make submit buttons keep the current working tab opened, or save and next tab |
367 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
368 | $className = CRM_Utils_String::getClassName($this->_name); | |
369 | ||
370 | //retrieve list of pages from StateMachine and find next page | |
371 | //this is quite painful because StateMachine is full of protected variables | |
372 | //so we have to retrieve all pages, find current page, and then retrieve next | |
373 | $stateMachine = new CRM_Contribute_StateMachine_ContributionPage($this); | |
353ffa53 TO |
374 | $states = $stateMachine->getStates(); |
375 | $statesList = array_keys($states); | |
376 | $currKey = array_search($className, $statesList); | |
377 | $nextPage = (array_key_exists($currKey + 1, $statesList)) ? $statesList[$currKey + 1] : ''; | |
6a488035 TO |
378 | |
379 | //unfortunately, some classes don't map to subpage names, so we alter the exceptions | |
380 | ||
381 | switch ($className) { | |
382 | case 'Contribute': | |
353ffa53 TO |
383 | $attributes = $this->getVar('_attributes'); |
384 | $subPage = strtolower(basename(CRM_Utils_Array::value('action', $attributes))); | |
874c9be7 | 385 | $subPageName = ucfirst($subPage); |
6a488035 TO |
386 | if ($subPage == 'friend') { |
387 | $nextPage = 'custom'; | |
388 | } | |
389 | else { | |
390 | $nextPage = 'settings'; | |
391 | } | |
392 | break; | |
393 | ||
394 | case 'MembershipBlock': | |
353ffa53 | 395 | $subPage = 'membership'; |
6a488035 | 396 | $subPageName = 'MembershipBlock'; |
353ffa53 | 397 | $nextPage = 'thankyou'; |
6a488035 TO |
398 | break; |
399 | ||
400 | default: | |
353ffa53 | 401 | $subPage = strtolower($className); |
6a488035 | 402 | $subPageName = $className; |
353ffa53 | 403 | $nextPage = strtolower($nextPage); |
6a488035 TO |
404 | |
405 | if ($subPage == 'amount') { | |
406 | $nextPage = 'membership'; | |
407 | } | |
408 | elseif ($subPage == 'thankyou') { | |
409 | $nextPage = 'friend'; | |
410 | } | |
411 | break; | |
412 | } | |
413 | ||
414 | CRM_Core_Session::setStatus(ts("'%1' information has been saved.", | |
353ffa53 TO |
415 | array(1 => $subPageName) |
416 | ), ts('Saved'), 'success'); | |
6a488035 TO |
417 | |
418 | $this->postProcessHook(); | |
419 | ||
420 | if ($this->controller->getButtonName('submit') == "_qf_{$className}_next") { | |
421 | CRM_Utils_System::redirect(CRM_Utils_System::url("civicrm/admin/contribute/{$subPage}", | |
353ffa53 TO |
422 | "action=update&reset=1&id={$this->_id}" |
423 | )); | |
6a488035 TO |
424 | } |
425 | elseif ($this->controller->getButtonName('submit') == "_qf_{$className}_submit_savenext") { | |
426 | if ($nextPage) { | |
427 | CRM_Utils_System::redirect(CRM_Utils_System::url("civicrm/admin/contribute/{$nextPage}", | |
353ffa53 TO |
428 | "action=update&reset=1&id={$this->_id}" |
429 | )); | |
6a488035 TO |
430 | } |
431 | else { | |
432 | CRM_Utils_System::redirect(CRM_Utils_System::url("civicrm/admin/contribute", | |
353ffa53 TO |
433 | "reset=1" |
434 | )); | |
6a488035 TO |
435 | } |
436 | } | |
437 | else { | |
438 | CRM_Utils_System::redirect(CRM_Utils_System::url("civicrm/admin/contribute", 'reset=1')); | |
439 | } | |
440 | } | |
441 | } | |
442 | ||
186c9c17 | 443 | /** |
fe482240 | 444 | * Use the form name to create the tpl file name. |
186c9c17 EM |
445 | * |
446 | * @return string | |
186c9c17 EM |
447 | */ |
448 | /** | |
449 | * @return string | |
450 | */ | |
00be9182 | 451 | public function getTemplateFileName() { |
cc4f2812 | 452 | if ($this->controller->getPrint() || $this->getVar('_id') <= 0 || |
6a488035 TO |
453 | ($this->_action & CRM_Core_Action::DELETE) || |
454 | (CRM_Utils_String::getClassName($this->_name) == 'AddProduct') | |
455 | ) { | |
456 | return parent::getTemplateFileName(); | |
457 | } | |
458 | else { | |
459 | // hack lets suppress the form rendering for now | |
460 | self::$_template->assign('isForm', FALSE); | |
461 | return 'CRM/Contribute/Form/ContributionPage/Tab.tpl'; | |
462 | } | |
463 | } | |
96025800 | 464 | |
6a488035 | 465 | } |