Indentation, whitespace and wrapping.
[civicrm-core.git] / CRM / Contribute / Page / ContributionPage.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * Create a page for displaying Contribute Pages
38 * Contribute Pages are pages that are used to display
39 * contributions of different types. Pages consist
40 * of many customizable sections which can be
41 * accessed.
42 *
43 * This page provides a top level browse view
44 * of all the contribution pages in the system.
45 *
46 */
47 class CRM_Contribute_Page_ContributionPage extends CRM_Core_Page {
48
49 /**
50 * The action links that we need to display for the browse screen
51 *
52 * @var array
53 */
54 private static $_actionLinks;
55 private static $_contributionLinks;
56 private static $_configureActionLinks;
57 private static $_onlineContributionLinks;
58
59 // @todo Unused private field can be safely removed.
60 private static $_links = NULL;
61
62 /**
63 * @var CRM_Utils_Pager
64 */
65 protected $_pager = NULL;
66
67 /**
68 * @var string
69 */
70 protected $_sortByCharacter;
71
72 /**
73 * Get the action links for this page.
74 *
75 * @return array
76 */
77 function &actionLinks() {
78 // check if variable _actionsLinks is populated
79 if (!isset(self::$_actionLinks)) {
80 // helper variable for nicer formatting
81 $deleteExtra = ts('Are you sure you want to delete this Contribution page?');
82 $copyExtra = ts('Are you sure you want to make a copy of this Contribution page?');
83
84 self::$_actionLinks = array(
85 CRM_Core_Action::COPY => array(
86 'name' => ts('Make a Copy'),
87 'url' => CRM_Utils_System::currentPath(),
88 'qs' => 'action=copy&gid=%%id%%',
89 'title' => ts('Make a Copy of CiviCRM Contribution Page'),
90 'extra' => 'onclick = "return confirm(\'' . $copyExtra . '\');"',
91 ),
92 CRM_Core_Action::DISABLE => array(
93 'name' => ts('Disable'),
94 'title' => ts('Disable'),
95 'ref' => 'crm-enable-disable',
96 ),
97 CRM_Core_Action::ENABLE => array(
98 'name' => ts('Enable'),
99 'ref' => 'crm-enable-disable',
100 'title' => ts('Enable'),
101 ),
102 CRM_Core_Action::DELETE => array(
103 'name' => ts('Delete'),
104 'url' => CRM_Utils_System::currentPath(),
105 'qs' => 'action=delete&reset=1&id=%%id%%',
106 'title' => ts('Delete Custom Field'),
107 'extra' => 'onclick = "return confirm(\'' . $deleteExtra . '\');"',
108 ),
109 );
110 }
111 return self::$_actionLinks;
112 }
113
114 /**
115 * Get the configure action links for this page.
116 *
117 * @return array
118 */
119 public function &configureActionLinks() {
120 // check if variable _actionsLinks is populated
121 if (!isset(self::$_configureActionLinks)) {
122 $urlString = 'civicrm/admin/contribute/';
123 $urlParams = 'reset=1&action=update&id=%%id%%';
124
125 self::$_configureActionLinks = array(
126 CRM_Core_Action::ADD => array(
127 'name' => ts('Title and Settings'),
128 'title' => ts('Title and Settings'),
129 'url' => $urlString . 'settings',
130 'qs' => $urlParams,
131 'uniqueName' => 'settings',
132 ),
133 CRM_Core_Action::UPDATE => array(
134 'name' => ts('Contribution Amounts'),
135 'title' => ts('Contribution Amounts'),
136 'url' => $urlString . 'amount',
137 'qs' => $urlParams,
138 'uniqueName' => 'amount',
139 ),
140 CRM_Core_Action::VIEW => array(
141 'name' => ts('Membership Settings'),
142 'title' => ts('Membership Settings'),
143 'url' => $urlString . 'membership',
144 'qs' => $urlParams,
145 'uniqueName' => 'membership',
146 ),
147 CRM_Core_Action::EXPORT => array(
148 'name' => ts('Thank-you and Receipting'),
149 'title' => ts('Thank-you and Receipting'),
150 'url' => $urlString . 'thankyou',
151 'qs' => $urlParams,
152 'uniqueName' => 'thankyou',
153 ),
154 CRM_Core_Action::BASIC => array(
155 'name' => ts('Tell a Friend'),
156 'title' => ts('Tell a Friend'),
157 'url' => $urlString . 'friend',
158 'qs' => $urlParams,
159 'uniqueName' => 'friend',
160 ),
161 CRM_Core_Action::PROFILE => array(
162 'name' => ts('Include Profiles'),
163 'title' => ts('Include Profiles'),
164 'url' => $urlString . 'custom',
165 'qs' => $urlParams,
166 'uniqueName' => 'custom',
167 ),
168 CRM_Core_Action::MAP => array(
169 'name' => ts('Contribution Widget'),
170 'title' => ts('Contribution Widget'),
171 'url' => $urlString . 'widget',
172 'qs' => $urlParams,
173 'uniqueName' => 'widget',
174 ),
175 CRM_Core_Action::FOLLOWUP => array(
176 'name' => ts('Premiums'),
177 'title' => ts('Premiums'),
178 'url' => $urlString . 'premium',
179 'qs' => $urlParams,
180 'uniqueName' => 'premium',
181 ),
182 CRM_Core_Action::ADVANCED => array(
183 'name' => ts('Personal Campaign Pages'),
184 'title' => ts('Personal Campaign Pages'),
185 'url' => $urlString . 'pcp',
186 'qs' => $urlParams,
187 'uniqueName' => 'pcp',
188 ),
189 );
190 }
191
192 return self::$_configureActionLinks;
193 }
194
195 /**
196 * Get the online contribution links.
197 *
198 * @return array
199 */
200 public function &onlineContributionLinks() {
201 if (!isset(self::$_onlineContributionLinks)) {
202 $urlString = 'civicrm/contribute/transact';
203 $urlParams = 'reset=1&id=%%id%%';
204 self::$_onlineContributionLinks = array(
205 CRM_Core_Action::RENEW => array(
206 'name' => ts('Live Page'),
207 'title' => ts('Live Page'),
208 'url' => $urlString,
209 'qs' => $urlParams,
210 'fe' => TRUE,
211 'uniqueName' => 'live_page',
212 ),
213 CRM_Core_Action::PREVIEW => array(
214 'name' => ts('Test-drive'),
215 'title' => ts('Test-drive'),
216 'url' => $urlString,
217 'qs' => $urlParams . '&action=preview',
218 'uniqueName' => 'test_drive',
219 ),
220 );
221 }
222
223 return self::$_onlineContributionLinks;
224 }
225
226 /**
227 * Get the contributions links.
228 *
229 * @return array
230 */
231 public function &contributionLinks() {
232 if (!isset(self::$_contributionLinks)) {
233 //get contribution dates.
234 $dates = CRM_Contribute_BAO_Contribution::getContributionDates();
235 $now = $dates['now'];
236 $yearDate = $dates['yearDate'];
237 $monthDate = $dates['monthDate'];
238 $yearNow = $yearDate + 10000;
239
240 $urlString = 'civicrm/contribute/search';
241 $urlParams = 'reset=1&pid=%%id%%&force=1&test=0';
242
243 self::$_contributionLinks = array(
244 CRM_Core_Action::DETACH => array(
245 'name' => ts('Current Month-To-Date'),
246 'title' => ts('Current Month-To-Date'),
247 'url' => $urlString,
248 'qs' => "{$urlParams}&start={$monthDate}&end={$now}",
249 'uniqueName' => 'current_month_to_date',
250 ),
251 CRM_Core_Action::REVERT => array(
252 'name' => ts('Fiscal Year-To-Date'),
253 'title' => ts('Fiscal Year-To-Date'),
254 'url' => $urlString,
255 'qs' => "{$urlParams}&start={$yearDate}&end={$yearNow}",
256 'uniqueName' => 'fiscal_year_to_date',
257 ),
258 CRM_Core_Action::BROWSE => array(
259 'name' => ts('Cumulative'),
260 'title' => ts('Cumulative'),
261 'url' => $urlString,
262 'qs' => "{$urlParams}&start=&end=$now",
263 'uniqueName' => 'cumulative',
264 ),
265 );
266 }
267
268 return self::$_contributionLinks;
269 }
270
271 /**
272 * Run the page.
273 *
274 * This method is called after the page is created. It checks for the
275 * type of action and executes that action.
276 * Finally it calls the parent's run method.
277 *
278 * @return mixed
279 */
280 public function run() {
281 // get the requested action
282 $action = CRM_Utils_Request::retrieve('action', 'String',
283 // default to 'browse'
284 $this, FALSE, 'browse'
285 );
286
287 // assign vars to templates
288 $this->assign('action', $action);
289 $id = CRM_Utils_Request::retrieve('id', 'Positive',
290 $this, FALSE, 0
291 );
292
293 // set breadcrumb to append to 2nd layer pages
294 $breadCrumb = array(array('title' => ts('Manage Contribution Pages'),
295 'url' => CRM_Utils_System::url(CRM_Utils_System::currentPath(),
296 'reset=1'
297 ),
298 ));
299
300 // what action to take ?
301 if ($action & CRM_Core_Action::ADD) {
302 $session = CRM_Core_Session::singleton();
303 $session->pushUserContext(CRM_Utils_System::url(CRM_Utils_System::currentPath(),
304 'action=browse&reset=1'
305 ));
306
307 $controller = new CRM_Contribute_Controller_ContributionPage(NULL, $action);
308 CRM_Utils_System::setTitle(ts('Manage Contribution Page'));
309 CRM_Utils_System::appendBreadCrumb($breadCrumb);
310 return $controller->run();
311 }
312 elseif ($action & CRM_Core_Action::UPDATE) {
313 $config = CRM_Core_Config::singleton();
314
315 // assign vars to templates
316 $this->assign('id', $id);
317 $this->assign('title', CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $id, 'title'));
318 $this->assign('is_active', CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $id, 'is_active'));
319 if (in_array('CiviMember', $config->enableComponents)) {
320 $this->assign('CiviMember', TRUE);
321 }
322 }
323 elseif ($action & CRM_Core_Action::COPY) {
324 // @todo Unused local variable can be safely removed.
325 $session = CRM_Core_Session::singleton();
326 CRM_Core_Session::setStatus(ts('A copy of the contribution page has been created'), ts('Successfully Copied'), 'success');
327 $this->copy();
328 }
329 elseif ($action & CRM_Core_Action::DELETE) {
330 CRM_Utils_System::appendBreadCrumb($breadCrumb);
331
332 $session = CRM_Core_Session::singleton();
333 $session->pushUserContext(CRM_Utils_System::url(CRM_Utils_System::currentPath(),
334 'reset=1&action=browse'
335 ));
336
337 $id = CRM_Utils_Request::retrieve('id', 'Positive',
338 $this, FALSE, 0
339 );
340 $query = "
341 SELECT ccp.title
342 FROM civicrm_contribution_page ccp
343 JOIN civicrm_pcp cp ON ccp.id = cp.page_id
344 WHERE cp.page_id = {$id}
345 AND cp.page_type = 'contribute'
346 ";
347
348 if ($pageTitle = CRM_Core_DAO::singleValueQuery($query)) {
349 CRM_Core_Session::setStatus(ts('The \'%1\' cannot be deleted! You must Delete all Personal Campaign Page(s) related with this contribution page prior to deleting the page.', array(1 => $pageTitle)), ts('Deletion Error'), 'error');
350
351 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/contribute', 'reset=1'));
352 }
353
354 $controller = new CRM_Core_Controller_Simple('CRM_Contribute_Form_ContributionPage_Delete',
355 'Delete Contribution Page',
356 CRM_Core_Action::DELETE
357 );
358 $controller->set('id', $id);
359 $controller->process();
360 return $controller->run();
361 }
362 else {
363 // finally browse the contribution pages
364 $this->browse();
365
366 CRM_Utils_System::setTitle(ts('Manage Contribution Pages'));
367 }
368
369 return parent::run();
370 }
371
372 /**
373 * This function is to make a copy of a contribution page, including
374 * all the fields in the page
375 *
376 * @return void
377 */
378 public function copy() {
379 $gid = CRM_Utils_Request::retrieve('gid', 'Positive',
380 $this, TRUE, 0, 'GET'
381 );
382
383 CRM_Contribute_BAO_ContributionPage::copy($gid);
384
385 CRM_Utils_System::redirect(CRM_Utils_System::url(CRM_Utils_System::currentPath(), 'reset=1'));
386 }
387
388 /**
389 * Browse all contribution pages
390 *
391 * @param mixed $action
392 * Unused parameter.
393 * @static
394 */
395 public function browse($action = NULL) {
396 $this->_sortByCharacter = CRM_Utils_Request::retrieve('sortByCharacter',
397 'String',
398 $this
399 );
400 // @todo Unused local variable can be safely removed.
401 $createdId = CRM_Utils_Request::retrieve('cid', 'Positive',
402 $this, FALSE, 0
403 );
404
405 if ($this->_sortByCharacter == 1 ||
406 !empty($_POST)
407 ) {
408 $this->_sortByCharacter = '';
409 $this->set('sortByCharacter', '');
410 }
411
412 $this->search();
413
414 $params = array();
415
416 $whereClause = $this->whereClause($params, FALSE);
417 $this->pagerAToZ($whereClause, $params);
418
419 $params = array();
420 $whereClause = $this->whereClause($params, TRUE);
421 $this->pager($whereClause, $params);
422
423 list($offset, $rowCount) = $this->_pager->getOffsetAndRowCount();
424
425 //check for delete CRM-4418
426 $allowToDelete = CRM_Core_Permission::check('delete in CiviContribute');
427
428 $query = "
429 SELECT id
430 FROM civicrm_contribution_page
431 WHERE $whereClause
432 LIMIT $offset, $rowCount";
433 $contribPage = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Contribute_DAO_ContributionPage');
434 $contribPageIds = array();
435 while ($contribPage->fetch()) {
436 $contribPageIds[$contribPage->id] = $contribPage->id;
437 }
438 //get all section info.
439 $contriPageSectionInfo = CRM_Contribute_BAO_ContributionPage::getSectionInfo($contribPageIds);
440
441 $query = "
442 SELECT *
443 FROM civicrm_contribution_page
444 WHERE $whereClause
445 ORDER BY title asc
446 LIMIT $offset, $rowCount";
447
448 $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Contribute_DAO_ContributionPage');
449
450 //get all campaigns.
451 $allCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
452
453 //get configure actions links.
454 $configureActionLinks = self::configureActionLinks();
455
456 while ($dao->fetch()) {
457 $contribution[$dao->id] = array();
458 CRM_Core_DAO::storeValues($dao, $contribution[$dao->id]);
459
460 // form all action links
461 $action = array_sum(array_keys($this->actionLinks()));
462
463 //add configure actions links.
464 $action += array_sum(array_keys($configureActionLinks));
465
466 //add online contribution links.
467 $action += array_sum(array_keys(self::onlineContributionLinks()));
468
469 //add contribution search links.
470 $action += array_sum(array_keys(self::contributionLinks()));
471
472 if ($dao->is_active) {
473 $action -= (int)CRM_Core_Action::ENABLE;
474 }
475 else {
476 $action -= (int)CRM_Core_Action::DISABLE;
477 }
478
479 //CRM-4418
480 if (!$allowToDelete) {
481 $action -= (int)CRM_Core_Action::DELETE;
482 }
483
484 //build the configure links.
485 $sectionsInfo = CRM_Utils_Array::value($dao->id, $contriPageSectionInfo, array());
486 $contribution[$dao->id]['configureActionLinks'] = CRM_Core_Action::formLink(self::formatConfigureLinks($sectionsInfo),
487 $action,
488 array('id' => $dao->id),
489 ts('Configure'),
490 TRUE,
491 'contributionpage.configure.actions',
492 'ContributionPage',
493 $dao->id
494 );
495
496 //build the contributions links.
497 $contribution[$dao->id]['contributionLinks'] = CRM_Core_Action::formLink(self::contributionLinks(),
498 $action,
499 array('id' => $dao->id),
500 ts('Contributions'),
501 TRUE,
502 'contributionpage.contributions.search',
503 'ContributionPage',
504 $dao->id
505 );
506
507 //build the online contribution links.
508 $contribution[$dao->id]['onlineContributionLinks'] = CRM_Core_Action::formLink(self::onlineContributionLinks(),
509 $action,
510 array('id' => $dao->id),
511 ts('Links'),
512 TRUE,
513 'contributionpage.online.links',
514 'ContributionPage',
515 $dao->id
516 );
517
518 //build the normal action links.
519 $contribution[$dao->id]['action'] = CRM_Core_Action::formLink(self::actionLinks(),
520 $action,
521 array('id' => $dao->id),
522 ts('more'),
523 TRUE,
524 'contributionpage.action.links',
525 'ContributionPage',
526 $dao->id
527 );
528
529 //show campaigns on selector.
530 $contribution[$dao->id]['campaign'] = CRM_Utils_Array::value($dao->campaign_id, $allCampaigns);
531 }
532
533 if (isset($contribution)) {
534 $this->assign('rows', $contribution);
535 }
536 }
537
538 public function search() {
539 if (isset($this->_action) &
540 (CRM_Core_Action::ADD |
541 CRM_Core_Action::UPDATE |
542 CRM_Core_Action::DELETE
543 )
544 ) {
545 return;
546 }
547
548 $form = new CRM_Core_Controller_Simple('CRM_Contribute_Form_SearchContribution',
549 ts('Search Contribution'),
550 CRM_Core_Action::ADD
551 );
552 $form->setEmbedded(TRUE);
553 $form->setParent($this);
554 $form->process();
555 $form->run();
556 }
557
558 /**
559 * @param array $params
560 * @param bool $sortBy
561 *
562 * @return int|string
563 */
564 public function whereClause(&$params, $sortBy = TRUE) {
565 // @todo Unused local variable can be safely removed.
566 $values = $clauses = array();
567 $title = $this->get('title');
568 $createdId = $this->get('cid');
569
570 if ($createdId) {
571 $clauses[] = "(created_id = {$createdId})";
572 }
573
574 if ($title) {
575 $clauses[] = "title LIKE %1";
576 if (strpos($title, '%') !== FALSE) {
577 $params[1] = array(trim($title), 'String', FALSE);
578 }
579 else {
580 $params[1] = array(trim($title), 'String', TRUE);
581 }
582 }
583
584 $value = $this->get( 'financial_type_id' );
585 $val = array();
586 if ($value) {
587 if (is_array($value)) {
588 foreach ($value as $k => $v) {
589 if ($v) {
590 $val[$k] = $k;
591 }
592 }
593 $type = implode(',', $val);
594 }
595 // @todo Variable 'type' might not have been defined.
596 $clauses[] = "financial_type_id IN ({$type})";
597 }
598
599 if ($sortBy && $this->_sortByCharacter !== NULL) {
600 $clauses[] = "title LIKE '" . strtolower(CRM_Core_DAO::escapeWildCardString($this->_sortByCharacter)) . "%'";
601 }
602
603 // @todo Fatal typo: $campainIds vs $campaignIds.
604 $campainIds = $this->get('campaign_id');
605 if (!CRM_Utils_System::isNull($campainIds)) {
606 if (!is_array($campainIds)) {
607 // @todo Undefined variable $campaignIds, due to fatal typo above.
608 $campaignIds = array($campaignIds);
609 }
610 $clauses[] = '( campaign_id IN ( ' . implode(' , ', array_values($campainIds)) . ' ) )';
611 }
612
613 if (empty($clauses)) {
614 // Let template know if user has run a search or not
615 $this->assign('isSearch', 0);
616 return 1;
617 }
618 else {
619 $this->assign('isSearch', 1);
620 }
621
622 return implode(' AND ', $clauses);
623 }
624
625 /**
626 * @param $whereClause
627 * @param array $whereParams
628 */
629 public function pager($whereClause, $whereParams) {
630
631 $params['status'] = ts('Contribution %%StatusMessage%%');
632 $params['csvString'] = NULL;
633 $params['buttonTop'] = 'PagerTopButton';
634 $params['buttonBottom'] = 'PagerBottomButton';
635 $params['rowCount'] = $this->get(CRM_Utils_Pager::PAGE_ROWCOUNT);
636 if (!$params['rowCount']) {
637 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
638 }
639
640 $query = "
641 SELECT count(id)
642 FROM civicrm_contribution_page
643 WHERE $whereClause";
644
645 $params['total'] = CRM_Core_DAO::singleValueQuery($query, $whereParams);
646
647 $this->_pager = new CRM_Utils_Pager($params);
648 $this->assign_by_ref('pager', $this->_pager);
649 }
650
651 /**
652 * @param $whereClause
653 * @param array $whereParams
654 */
655 public function pagerAtoZ($whereClause, $whereParams) {
656
657 $query = "
658 SELECT DISTINCT UPPER(LEFT(title, 1)) as sort_name
659 FROM civicrm_contribution_page
660 WHERE $whereClause
661 ORDER BY LEFT(title, 1)
662 ";
663 $dao = CRM_Core_DAO::executeQuery($query, $whereParams);
664
665 $aToZBar = CRM_Utils_PagerAToZ::getAToZBar($dao, $this->_sortByCharacter, TRUE);
666 $this->assign('aToZ', $aToZBar);
667 }
668
669 /**
670 * @param array $sectionsInfo
671 *
672 * @return array
673 */
674 public function formatConfigureLinks($sectionsInfo) {
675 // build the formatted configure links.
676 $formattedConfLinks = self::configureActionLinks();
677 foreach ($formattedConfLinks as $act => & $link) {
678 $sectionName = CRM_Utils_Array::value('uniqueName', $link);
679 if (!$sectionName) {
680 continue;
681 }
682
683 // @todo Unused local variable can be safely removed.
684 $classes = array();
685 if (isset($link['class'])) {
686 $classes = $link['class'];
687 }
688
689 if (empty($sectionsInfo[$sectionName])) {
690 $classes = array();
691 if (isset($link['class'])) {
692 $classes = $link['class'];
693 }
694 $link['class'] = array_merge($classes, array('disabled'));
695 }
696 }
697
698 return $formattedConfLinks;
699 }
700 }