571c25433db1a017236e9c8e89eb46902b6d153d
[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 $form
38 */
39 public static function preProcessCommon(&$form) {
40 $form->_grantIds = [];
41
42 $values = $form->controller->exportValues($form->get('searchFormName'));
43
44 $form->_task = $values['task'];
45 $tasks = CRM_Grant_Task::tasks();
46 if (!array_key_exists($form->_task, $tasks)) {
47 CRM_Core_Error::statusBounce(ts('You do not have permission to access this page.'));
48 }
49
50 $ids = [];
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');
60 $sortOrder = NULL;
61 if ($form->get(CRM_Utils_Sort::SORT_ORDER)) {
62 $sortOrder = $form->get(CRM_Utils_Sort::SORT_ORDER);
63 }
64 $query = new CRM_Contact_BAO_Query($queryParams, NULL, NULL, FALSE, FALSE,
65 CRM_Contact_BAO_Query::MODE_GRANT
66 );
67 $query->_distinctComponentClause = ' civicrm_grant.id';
68 $query->_groupByComponentClause = ' GROUP BY civicrm_grant.id ';
69 $result = $query->searchQuery(0, 0, $sortOrder);
70 while ($result->fetch()) {
71 $ids[] = $result->grant_id;
72 }
73 }
74
75 if (!empty($ids)) {
76 $form->_componentClause = ' civicrm_grant.id IN ( ' . implode(',', $ids) . ' ) ';
77 $form->assign('totalSelectedGrants', count($ids));
78 }
79
80 $form->_grantIds = $form->_componentIds = $ids;
81
82 //set the context for redirection for any task actions
83 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $form);
84 $urlParams = 'force=1';
85 if (CRM_Utils_Rule::qfKey($qfKey)) {
86 $urlParams .= "&qfKey=$qfKey";
87 }
88
89 $session = CRM_Core_Session::singleton();
90 $session->replaceUserContext(CRM_Utils_System::url('civicrm/grant/search', $urlParams));
91 }
92
93 /**
94 * Given the grant id, compute the contact id
95 * since its used for things like send email
96 */
97 public function setContactIDs() {
98 $this->_contactIds = CRM_Core_DAO::getContactIDsFromComponent($this->_grantIds,
99 'civicrm_grant'
100 );
101 }
102
103 /**
104 * Simple shell that derived classes can call to add buttons to.
105 * the form with a customized title for the main Submit
106 *
107 * @param string $title
108 * Title of the main button.
109 * @param string $nextType
110 * @param string $backType
111 *
112 * @param bool $submitOnce
113 */
114 public function addDefaultButtons($title, $nextType = 'next', $backType = 'back', $submitOnce = FALSE) {
115 $this->addButtons([
116 [
117 'type' => $nextType,
118 'name' => $title,
119 'isDefault' => TRUE,
120 ],
121 [
122 'type' => $backType,
123 'name' => ts('Cancel'),
124 ],
125 ]);
126 }
127
128 }