Merge pull request #18891 from civicrm/5.31
[civicrm-core.git] / CRM / Pledge / Form / Task.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
31aaf096
MW
19 * Class for pledge form task actions.
20 * FIXME: This needs refactoring to properly inherit from CRM_Core_Form_Task and share more functions.
6a488035 21 */
31aaf096 22class CRM_Pledge_Form_Task extends CRM_Core_Form_Task {
6a488035
TO
23
24 /**
fe482240 25 * The array that holds all the pledge ids.
6a488035
TO
26 *
27 * @var array
28 */
29 protected $_pledgeIds;
30
31 /**
fe482240 32 * Build all the data structures needed to build the form.
6a488035 33 */
00be9182 34 public function preProcess() {
6a488035
TO
35 self::preProcessCommon($this);
36 }
37
ffd93213 38 /**
167bcb5f 39 * Common pre-processing.
40 *
188bf7c3 41 * @param CRM_Pledge_Form_Task $form
ffd93213 42 */
2b089ce1 43 public static function preProcessCommon(&$form) {
be2fb01f 44 $form->_pledgeIds = [];
6a488035
TO
45
46 $values = $form->controller->exportValues('Search');
47
48 $form->_task = $values['task'];
6a488035 49
be2fb01f 50 $ids = [];
6a488035
TO
51 if ($values['radio_ts'] == 'ts_sel') {
52 foreach ($values as $name => $value) {
53 if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
54 $ids[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN);
55 }
56 }
57 }
58 else {
59 $queryParams = $form->get('queryParams');
098201d8 60 $sortOrder = NULL;
353ffa53 61 if ($form->get(CRM_Utils_Sort::SORT_ORDER)) {
481a74f4 62 $sortOrder = $form->get(CRM_Utils_Sort::SORT_ORDER);
6a488035
TO
63 }
64 $query = new CRM_Contact_BAO_Query($queryParams, NULL, NULL, FALSE, FALSE,
65 CRM_Contact_BAO_Query::MODE_PLEDGE
66 );
67 $query->_distinctComponentClause = ' civicrm_pledge.id';
68 $query->_groupByComponentClause = ' GROUP BY civicrm_pledge.id ';
69
70 $result = $query->searchQuery(0, 0, $sortOrder);
71 while ($result->fetch()) {
72 $ids[] = $result->pledge_id;
73 }
74 }
75
76 if (!empty($ids)) {
77 $form->_componentClause = ' civicrm_pledge.id IN ( ' . implode(',', $ids) . ' ) ';
78 $form->assign('totalSelectedPledges', count($ids));
79 }
80
81 $form->_pledgeIds = $form->_componentIds = $ids;
188bf7c3 82 $form->setNextUrl('pledge');
6a488035
TO
83 }
84
85 /**
86 * Given the signer id, compute the contact id
87 * since its used for things like send email
88 */
89 public function setContactIDs() {
ae18f7d8 90 $this->_contactIds = CRM_Core_DAO::getContactIDsFromComponent($this->_pledgeIds,
6a488035
TO
91 'civicrm_pledge'
92 );
93 }
94
95 /**
100fef9d 96 * Simple shell that derived classes can call to add buttons to
6a488035
TO
97 * the form with a customized title for the main Submit
98 *
3a1617b6
TO
99 * @param string $title
100 * Title of the main button.
dd244018
EM
101 * @param string $nextType
102 * @param string $backType
103 * @param bool $submitOnce
6a488035 104 */
00be9182 105 public function addDefaultButtons($title, $nextType = 'next', $backType = 'back', $submitOnce = FALSE) {
be2fb01f
CW
106 $this->addButtons([
107 [
6a488035
TO
108 'type' => $nextType,
109 'name' => $title,
110 'isDefault' => TRUE,
be2fb01f
CW
111 ],
112 [
6a488035
TO
113 'type' => $backType,
114 'name' => ts('Cancel'),
be2fb01f 115 ],
c86d4e7c 116 ]);
6a488035 117 }
96025800 118
6a488035 119}