Merge pull request #20834 from colemanw/fixUnusedVars
[civicrm-core.git] / CRM / Grant / 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 * Class for grant form task actions.
14 * FIXME: This needs refactoring to properly inherit from CRM_Core_Form_Task and share more functions.
15 */
16 class CRM_Grant_Form_Task extends CRM_Core_Form_Task {
17
18 /**
19 * The array that holds all the grant ids.
20 *
21 * @var array
22 */
23 protected $_grantIds;
24
25 /**
26 * Build all the data structures needed to build the form.
27 *
28 * @param
29 *
30 * @return void
31 */
32 public function preProcess() {
33 self::preProcessCommon($this);
34 }
35
36 /**
37 * @param \CRM_Core_Form_Task $form
38 *
39 * @throws \CRM_Core_Exception
40 */
41 public static function preProcessCommon(&$form) {
42 $form->_grantIds = [];
43
44 $values = $form->getSearchFormValues();
45
46 $form->_task = $values['task'];
47 $tasks = CRM_Grant_Task::tasks();
48 if (!array_key_exists($form->_task, $tasks)) {
49 CRM_Core_Error::statusBounce(ts('You do not have permission to access this page.'));
50 }
51
52 $ids = $form->getSelectedIDs($values);
53
54 if (!$ids) {
55 $queryParams = $form->get('queryParams');
56 $sortOrder = NULL;
57 if ($form->get(CRM_Utils_Sort::SORT_ORDER)) {
58 $sortOrder = $form->get(CRM_Utils_Sort::SORT_ORDER);
59 }
60 $query = new CRM_Contact_BAO_Query($queryParams, NULL, NULL, FALSE, FALSE,
61 CRM_Contact_BAO_Query::MODE_GRANT
62 );
63 $query->_distinctComponentClause = ' civicrm_grant.id';
64 $query->_groupByComponentClause = ' GROUP BY civicrm_grant.id ';
65 $result = $query->searchQuery(0, 0, $sortOrder);
66 while ($result->fetch()) {
67 $ids[] = $result->grant_id;
68 }
69 }
70
71 if (!empty($ids)) {
72 $form->_componentClause = ' civicrm_grant.id IN ( ' . implode(',', $ids) . ' ) ';
73 $form->assign('totalSelectedGrants', count($ids));
74 }
75
76 $form->_grantIds = $form->_componentIds = $ids;
77
78 $form->setNextUrl('grant');
79 }
80
81 /**
82 * Given the grant id, compute the contact id
83 * since its used for things like send email
84 */
85 public function setContactIDs() {
86 $this->_contactIds = CRM_Core_DAO::getContactIDsFromComponent($this->_grantIds,
87 'civicrm_grant'
88 );
89 }
90
91 /**
92 * Simple shell that derived classes can call to add buttons to.
93 * the form with a customized title for the main Submit
94 *
95 * @param string $title
96 * Title of the main button.
97 * @param string $nextType
98 * @param string $backType
99 *
100 * @param bool $submitOnce
101 */
102 public function addDefaultButtons($title, $nextType = 'next', $backType = 'back', $submitOnce = FALSE) {
103 $this->addButtons([
104 [
105 'type' => $nextType,
106 'name' => $title,
107 'isDefault' => TRUE,
108 ],
109 [
110 'type' => $backType,
111 'name' => ts('Cancel'),
112 ],
113 ]);
114 }
115
116 }