Merge pull request #16410 from civicrm/5.22
[civicrm-core.git] / CRM / Pledge / Form / Task.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * Class for pledge form task actions.
20 * FIXME: This needs refactoring to properly inherit from CRM_Core_Form_Task and share more functions.
21 */
22 class CRM_Pledge_Form_Task extends CRM_Core_Form_Task {
23
24 /**
25 * The array that holds all the pledge ids.
26 *
27 * @var array
28 */
29 protected $_pledgeIds;
30
31 /**
32 * Build all the data structures needed to build the form.
33 */
34 public function preProcess() {
35 self::preProcessCommon($this);
36 }
37
38 /**
39 * Common pre-processing.
40 *
41 * @param CRM_Core_Form $form
42 */
43 public static function preProcessCommon(&$form) {
44 $form->_pledgeIds = [];
45
46 $values = $form->controller->exportValues('Search');
47
48 $form->_task = $values['task'];
49 $pledgeTasks = CRM_Pledge_Task::tasks();
50 $form->assign('taskName', $pledgeTasks[$form->_task]);
51
52 $ids = [];
53 if ($values['radio_ts'] == 'ts_sel') {
54 foreach ($values as $name => $value) {
55 if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
56 $ids[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN);
57 }
58 }
59 }
60 else {
61 $queryParams = $form->get('queryParams');
62 $sortOrder = NULL;
63 if ($form->get(CRM_Utils_Sort::SORT_ORDER)) {
64 $sortOrder = $form->get(CRM_Utils_Sort::SORT_ORDER);
65 }
66 $query = new CRM_Contact_BAO_Query($queryParams, NULL, NULL, FALSE, FALSE,
67 CRM_Contact_BAO_Query::MODE_PLEDGE
68 );
69 $query->_distinctComponentClause = ' civicrm_pledge.id';
70 $query->_groupByComponentClause = ' GROUP BY civicrm_pledge.id ';
71
72 $result = $query->searchQuery(0, 0, $sortOrder);
73 while ($result->fetch()) {
74 $ids[] = $result->pledge_id;
75 }
76 }
77
78 if (!empty($ids)) {
79 $form->_componentClause = ' civicrm_pledge.id IN ( ' . implode(',', $ids) . ' ) ';
80 $form->assign('totalSelectedPledges', count($ids));
81 }
82
83 $form->_pledgeIds = $form->_componentIds = $ids;
84
85 // set the context for redirection for any task actions
86 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $form);
87 $urlParams = 'force=1';
88 if (CRM_Utils_Rule::qfKey($qfKey)) {
89 $urlParams .= "&qfKey=$qfKey";
90 }
91
92 $session = CRM_Core_Session::singleton();
93 $session->replaceUserContext(CRM_Utils_System::url('civicrm/pledge/search', $urlParams));
94 }
95
96 /**
97 * Given the signer id, compute the contact id
98 * since its used for things like send email
99 */
100 public function setContactIDs() {
101 $this->_contactIds = CRM_Core_DAO::getContactIDsFromComponent($this->_pledgeIds,
102 'civicrm_pledge'
103 );
104 }
105
106 /**
107 * Simple shell that derived classes can call to add buttons to
108 * the form with a customized title for the main Submit
109 *
110 * @param string $title
111 * Title of the main button.
112 * @param string $nextType
113 * @param string $backType
114 * @param bool $submitOnce
115 */
116 public function addDefaultButtons($title, $nextType = 'next', $backType = 'back', $submitOnce = FALSE) {
117 $this->addButtons([
118 [
119 'type' => $nextType,
120 'name' => $title,
121 'isDefault' => TRUE,
122 ],
123 [
124 'type' => $backType,
125 'name' => ts('Cancel'),
126 ],
127 ]);
128 }
129
130 }