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