Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
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 | * @return void | |
6a488035 TO |
94 | */ |
95 | public function preProcess() { | |
96 | // current contribution page id | |
97 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', | |
98 | $this, FALSE, NULL, 'REQUEST' | |
99 | ); | |
100 | $this->assign('contributionPageID', $this->_id); | |
101 | ||
102 | // get the requested action | |
103 | $this->_action = CRM_Utils_Request::retrieve('action', 'String', | |
104 | // default to 'browse' | |
105 | $this, FALSE, 'browse' | |
106 | ); | |
107 | ||
108 | // setting title and 3rd level breadcrumb for html page if contrib page exists | |
109 | if ($this->_id) { | |
110 | $title = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $this->_id, 'title'); | |
111 | ||
112 | if ($this->_action == CRM_Core_Action::UPDATE) { | |
113 | $this->_single = TRUE; | |
114 | } | |
115 | } | |
116 | ||
117 | // set up tabs | |
118 | CRM_Contribute_Form_ContributionPage_TabHeader::build($this); | |
119 | ||
120 | if ($this->_action == CRM_Core_Action::UPDATE) { | |
121 | CRM_Utils_System::setTitle(ts('Configure Page - %1', array(1 => $title))); | |
122 | } | |
123 | elseif ($this->_action == CRM_Core_Action::VIEW) { | |
124 | CRM_Utils_System::setTitle(ts('Preview Page - %1', array(1 => $title))); | |
125 | } | |
126 | elseif ($this->_action == CRM_Core_Action::DELETE) { | |
127 | CRM_Utils_System::setTitle(ts('Delete Page - %1', array(1 => $title))); | |
128 | } | |
129 | ||
130 | //cache values. | |
131 | $this->_values = $this->get('values'); | |
132 | if (!is_array($this->_values)) { | |
133 | $this->_values = array(); | |
134 | if (isset($this->_id) && $this->_id) { | |
135 | $params = array('id' => $this->_id); | |
136 | CRM_Core_DAO::commonRetrieve('CRM_Contribute_DAO_ContributionPage', $params, $this->_values); | |
137 | } | |
138 | $this->set('values', $this->_values); | |
139 | } | |
c50f55b5 CW |
140 | |
141 | // Preload libraries required by the "Profiles" tab | |
133e2c99 | 142 | $schemas = array('IndividualModel', 'OrganizationModel', 'ContributionModel'); |
c50f55b5 CW |
143 | if (in_array('CiviMember', CRM_Core_Config::singleton()->enableComponents)) { |
144 | $schemas[] = 'MembershipModel'; | |
145 | } | |
146 | CRM_UF_Page_ProfileEditor::registerProfileScripts(); | |
147 | CRM_UF_Page_ProfileEditor::registerSchemas($schemas); | |
6a488035 TO |
148 | } |
149 | ||
150 | /** | |
fe482240 | 151 | * Build the form object. |
6a488035 TO |
152 | * |
153 | * @return void | |
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 | * @return void | |
6a488035 TO |
357 | */ |
358 | public function postProcess() { | |
359 | $pageId = $this->get('id'); | |
360 | //page is newly created. | |
361 | if ($pageId && !$this->_id) { | |
362 | $session = CRM_Core_Session::singleton(); | |
363 | $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/contribute', 'reset=1')); | |
364 | } | |
365 | } | |
366 | ||
00be9182 | 367 | public function endPostProcess() { |
6a488035 TO |
368 | // make submit buttons keep the current working tab opened, or save and next tab |
369 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
370 | $className = CRM_Utils_String::getClassName($this->_name); | |
371 | ||
372 | //retrieve list of pages from StateMachine and find next page | |
373 | //this is quite painful because StateMachine is full of protected variables | |
374 | //so we have to retrieve all pages, find current page, and then retrieve next | |
375 | $stateMachine = new CRM_Contribute_StateMachine_ContributionPage($this); | |
353ffa53 TO |
376 | $states = $stateMachine->getStates(); |
377 | $statesList = array_keys($states); | |
378 | $currKey = array_search($className, $statesList); | |
379 | $nextPage = (array_key_exists($currKey + 1, $statesList)) ? $statesList[$currKey + 1] : ''; | |
6a488035 TO |
380 | |
381 | //unfortunately, some classes don't map to subpage names, so we alter the exceptions | |
382 | ||
383 | switch ($className) { | |
384 | case 'Contribute': | |
353ffa53 TO |
385 | $attributes = $this->getVar('_attributes'); |
386 | $subPage = strtolower(basename(CRM_Utils_Array::value('action', $attributes))); | |
874c9be7 | 387 | $subPageName = ucfirst($subPage); |
6a488035 TO |
388 | if ($subPage == 'friend') { |
389 | $nextPage = 'custom'; | |
390 | } | |
391 | else { | |
392 | $nextPage = 'settings'; | |
393 | } | |
394 | break; | |
395 | ||
396 | case 'MembershipBlock': | |
353ffa53 | 397 | $subPage = 'membership'; |
6a488035 | 398 | $subPageName = 'MembershipBlock'; |
353ffa53 | 399 | $nextPage = 'thankyou'; |
6a488035 TO |
400 | break; |
401 | ||
402 | default: | |
353ffa53 | 403 | $subPage = strtolower($className); |
6a488035 | 404 | $subPageName = $className; |
353ffa53 | 405 | $nextPage = strtolower($nextPage); |
6a488035 TO |
406 | |
407 | if ($subPage == 'amount') { | |
408 | $nextPage = 'membership'; | |
409 | } | |
410 | elseif ($subPage == 'thankyou') { | |
411 | $nextPage = 'friend'; | |
412 | } | |
413 | break; | |
414 | } | |
415 | ||
416 | CRM_Core_Session::setStatus(ts("'%1' information has been saved.", | |
353ffa53 TO |
417 | array(1 => $subPageName) |
418 | ), ts('Saved'), 'success'); | |
6a488035 TO |
419 | |
420 | $this->postProcessHook(); | |
421 | ||
422 | if ($this->controller->getButtonName('submit') == "_qf_{$className}_next") { | |
423 | CRM_Utils_System::redirect(CRM_Utils_System::url("civicrm/admin/contribute/{$subPage}", | |
353ffa53 TO |
424 | "action=update&reset=1&id={$this->_id}" |
425 | )); | |
6a488035 TO |
426 | } |
427 | elseif ($this->controller->getButtonName('submit') == "_qf_{$className}_submit_savenext") { | |
428 | if ($nextPage) { | |
429 | CRM_Utils_System::redirect(CRM_Utils_System::url("civicrm/admin/contribute/{$nextPage}", | |
353ffa53 TO |
430 | "action=update&reset=1&id={$this->_id}" |
431 | )); | |
6a488035 TO |
432 | } |
433 | else { | |
434 | CRM_Utils_System::redirect(CRM_Utils_System::url("civicrm/admin/contribute", | |
353ffa53 TO |
435 | "reset=1" |
436 | )); | |
6a488035 TO |
437 | } |
438 | } | |
439 | else { | |
440 | CRM_Utils_System::redirect(CRM_Utils_System::url("civicrm/admin/contribute", 'reset=1')); | |
441 | } | |
442 | } | |
443 | } | |
444 | ||
186c9c17 | 445 | /** |
fe482240 | 446 | * Use the form name to create the tpl file name. |
186c9c17 EM |
447 | * |
448 | * @return string | |
186c9c17 EM |
449 | */ |
450 | /** | |
451 | * @return string | |
452 | */ | |
00be9182 | 453 | public function getTemplateFileName() { |
cc4f2812 | 454 | if ($this->controller->getPrint() || $this->getVar('_id') <= 0 || |
6a488035 TO |
455 | ($this->_action & CRM_Core_Action::DELETE) || |
456 | (CRM_Utils_String::getClassName($this->_name) == 'AddProduct') | |
457 | ) { | |
458 | return parent::getTemplateFileName(); | |
459 | } | |
460 | else { | |
461 | // hack lets suppress the form rendering for now | |
462 | self::$_template->assign('isForm', FALSE); | |
463 | return 'CRM/Contribute/Form/ContributionPage/Tab.tpl'; | |
464 | } | |
465 | } | |
96025800 | 466 | |
6a488035 | 467 | } |