Commit | Line | Data |
---|---|---|
888da08c MW |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
888da08c | 5 | +--------------------------------------------------------------------+ |
f299f7db | 6 | | Copyright CiviCRM LLC (c) 2004-2020 | |
888da08c MW |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * @package CRM | |
f299f7db | 30 | * @copyright CiviCRM LLC (c) 2004-2020 |
888da08c MW |
31 | */ |
32 | ||
33 | /** | |
34 | * This is a shared parent class for form task actions. | |
35 | */ | |
36 | abstract class CRM_Core_Form_Task extends CRM_Core_Form { | |
37 | ||
38 | /** | |
39 | * The task being performed | |
40 | * | |
41 | * @var int | |
42 | */ | |
43 | protected $_task; | |
44 | ||
45 | /** | |
46 | * The additional clause that we restrict the search with | |
47 | * | |
48 | * @var string | |
49 | */ | |
50 | protected $_componentClause = NULL; | |
51 | ||
52 | /** | |
53 | * The array that holds all the component ids | |
54 | * | |
55 | * @var array | |
56 | */ | |
57 | protected $_componentIds; | |
58 | ||
d5fd18f6 | 59 | /** |
60 | * @var int | |
61 | */ | |
62 | protected $queryMode; | |
63 | ||
888da08c MW |
64 | /** |
65 | * The array that holds all the case ids | |
66 | * | |
67 | * @var array | |
68 | */ | |
69 | public $_entityIds; | |
70 | ||
71 | /** | |
72 | * The array that holds all the contact ids | |
73 | * | |
74 | * @var array | |
75 | */ | |
76 | public $_contactIds; | |
77 | ||
fe841bdf MW |
78 | /** |
79 | * Must be set to entity table name (eg. civicrm_participant) by child class | |
80 | * | |
81 | * @var string | |
82 | */ | |
518fa0ee | 83 | public static $tableName = NULL; |
fe841bdf MW |
84 | |
85 | /** | |
86 | * Must be set to entity shortname (eg. event) | |
87 | * | |
88 | * @var string | |
89 | */ | |
518fa0ee | 90 | public static $entityShortname = NULL; |
888da08c MW |
91 | |
92 | /** | |
93 | * Build all the data structures needed to build the form. | |
6e3090fb MW |
94 | * |
95 | * @throws \CRM_Core_Exception | |
888da08c MW |
96 | */ |
97 | public function preProcess() { | |
6e3090fb MW |
98 | self::preProcessCommon($this); |
99 | } | |
100 | ||
101 | /** | |
102 | * Common pre-processing function. | |
103 | * | |
a0174743 | 104 | * @param CRM_Core_Form_Task $form |
6e3090fb MW |
105 | * |
106 | * @throws \CRM_Core_Exception | |
107 | */ | |
fe841bdf | 108 | public static function preProcessCommon(&$form) { |
be2fb01f | 109 | $form->_entityIds = []; |
888da08c | 110 | |
fe841bdf | 111 | $searchFormValues = $form->controller->exportValues($form->get('searchFormName')); |
888da08c | 112 | |
fe841bdf | 113 | $form->_task = $searchFormValues['task']; |
6e3090fb | 114 | $className = 'CRM_' . ucfirst($form::$entityShortname) . '_Task'; |
888da08c | 115 | $entityTasks = $className::tasks(); |
6e3090fb | 116 | $form->assign('taskName', $entityTasks[$form->_task]); |
888da08c | 117 | |
be2fb01f | 118 | $entityIds = []; |
fe841bdf MW |
119 | if ($searchFormValues['radio_ts'] == 'ts_sel') { |
120 | foreach ($searchFormValues as $name => $value) { | |
888da08c | 121 | if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) { |
fe841bdf | 122 | $entityIds[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN); |
888da08c MW |
123 | } |
124 | } | |
125 | } | |
126 | else { | |
6e3090fb | 127 | $queryParams = $form->get('queryParams'); |
888da08c | 128 | $sortOrder = NULL; |
6e3090fb MW |
129 | if ($form->get(CRM_Utils_Sort::SORT_ORDER)) { |
130 | $sortOrder = $form->get(CRM_Utils_Sort::SORT_ORDER); | |
888da08c MW |
131 | } |
132 | ||
a0174743 | 133 | $query = new CRM_Contact_BAO_Query($queryParams, NULL, NULL, FALSE, FALSE, $form->getQueryMode()); |
6e3090fb MW |
134 | $query->_distinctComponentClause = " ( " . $form::$tableName . ".id )"; |
135 | $query->_groupByComponentClause = " GROUP BY " . $form::$tableName . ".id "; | |
888da08c | 136 | $result = $query->searchQuery(0, 0, $sortOrder); |
6e3090fb | 137 | $selector = $form::$entityShortname . '_id'; |
888da08c | 138 | while ($result->fetch()) { |
fe841bdf | 139 | $entityIds[] = $result->$selector; |
888da08c MW |
140 | } |
141 | } | |
142 | ||
fe841bdf MW |
143 | if (!empty($entityIds)) { |
144 | $form->_componentClause = ' ' . $form::$tableName . '.id IN ( ' . implode(',', $entityIds) . ' ) '; | |
145 | $form->assign('totalSelected' . ucfirst($form::$entityShortname) . 's', count($entityIds)); | |
888da08c MW |
146 | } |
147 | ||
fe841bdf | 148 | $form->_entityIds = $form->_componentIds = $entityIds; |
888da08c | 149 | |
39fecc8a MW |
150 | // Some functions (eg. PDF letter tokens) rely on Ids being in specific fields rather than the generic $form->_entityIds |
151 | // So we set that specific field here (eg. for cases $form->_caseIds = $form->_entityIds). | |
152 | // FIXME: This is really to handle legacy code that should probably be updated to use $form->_entityIds | |
153 | $entitySpecificIdsName = '_' . $form::$entityShortname . 'Ids'; | |
154 | $form->$entitySpecificIdsName = $form->_entityIds; | |
155 | ||
888da08c | 156 | //set the context for redirection for any task actions |
6e3090fb | 157 | $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $form); |
888da08c MW |
158 | $urlParams = 'force=1'; |
159 | if (CRM_Utils_Rule::qfKey($qfKey)) { | |
160 | $urlParams .= "&qfKey=$qfKey"; | |
161 | } | |
162 | ||
163 | $session = CRM_Core_Session::singleton(); | |
6e3090fb | 164 | $searchFormName = strtolower($form->get('searchFormName')); |
888da08c | 165 | if ($searchFormName == 'search') { |
6e3090fb | 166 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/' . $form::$entityShortname . '/search', $urlParams)); |
888da08c MW |
167 | } |
168 | else { | |
169 | $session->replaceUserContext(CRM_Utils_System::url("civicrm/contact/search/$searchFormName", | |
170 | $urlParams | |
171 | )); | |
172 | } | |
173 | } | |
174 | ||
175 | /** | |
6e3090fb MW |
176 | * Given the entity id, compute the contact id since its used for things like send email |
177 | * For example, for cases we need to override this function as the table name is civicrm_case_contact | |
888da08c MW |
178 | */ |
179 | public function setContactIDs() { | |
9e7fec13 | 180 | $this->_contactIds = CRM_Core_DAO::getContactIDsFromComponent($this->_entityIds, |
888da08c MW |
181 | $this::$tableName |
182 | ); | |
183 | } | |
184 | ||
185 | /** | |
186 | * Simple shell that derived classes can call to add buttons to | |
187 | * the form with a customized title for the main Submit | |
188 | * | |
189 | * @param string $title | |
190 | * Title of the main button. | |
191 | * @param string $nextType | |
192 | * Button type for the form after processing. | |
193 | * @param string $backType | |
194 | * @param bool $submitOnce | |
195 | */ | |
196 | public function addDefaultButtons($title, $nextType = 'next', $backType = 'back', $submitOnce = FALSE) { | |
be2fb01f | 197 | $this->addButtons([ |
518fa0ee SL |
198 | [ |
199 | 'type' => $nextType, | |
200 | 'name' => $title, | |
201 | 'isDefault' => TRUE, | |
202 | ], | |
203 | [ | |
204 | 'type' => $backType, | |
205 | 'name' => ts('Cancel'), | |
206 | ], | |
207 | ]); | |
888da08c MW |
208 | } |
209 | ||
a0174743 MW |
210 | /** |
211 | * Get the query mode (eg. CRM_Core_BAO_Query::MODE_CASE) | |
212 | * Should be overridden by child classes in most cases | |
213 | * | |
214 | * @return int | |
215 | */ | |
216 | public function getQueryMode() { | |
d5fd18f6 | 217 | return $this->queryMode ?: CRM_Contact_BAO_Query::MODE_CONTACTS; |
a0174743 MW |
218 | } |
219 | ||
2e9051c7 D |
220 | /** |
221 | * Given the component id, compute the contact id | |
222 | * since it's used for things like send email. | |
223 | * | |
224 | * @todo At the moment this duplicates a similar function in CRM_Core_DAO | |
225 | * because right now only the case component is using this. Since the | |
226 | * default $orderBy is '' which is what the original does, others should be | |
227 | * easily convertable as NFC. | |
228 | * @todo The passed in variables should be class member variables. Shouldn't | |
229 | * need to have passed in vars. | |
230 | * | |
231 | * @param $componentIDs | |
232 | * @param string $tableName | |
233 | * @param string $idField | |
234 | * | |
235 | * @return array | |
236 | */ | |
237 | public function getContactIDsFromComponent($componentIDs, $tableName, $idField = 'id') { | |
238 | $contactIDs = []; | |
239 | ||
240 | if (empty($componentIDs)) { | |
241 | return $contactIDs; | |
242 | } | |
243 | ||
244 | $orderBy = $this->orderBy(); | |
245 | ||
246 | $IDs = implode(',', $componentIDs); | |
247 | $query = " | |
248 | SELECT contact_id | |
249 | FROM $tableName | |
250 | WHERE $idField IN ( $IDs ) $orderBy | |
251 | "; | |
252 | ||
253 | $dao = CRM_Core_DAO::executeQuery($query); | |
254 | while ($dao->fetch()) { | |
255 | $contactIDs[] = $dao->contact_id; | |
256 | } | |
257 | return $contactIDs; | |
258 | } | |
259 | ||
260 | /** | |
261 | * Default ordering for getContactIDsFromComponent. Subclasses can override. | |
262 | * | |
263 | * @return string | |
264 | * SQL fragment. Either return '' or a valid order clause including the | |
265 | * words "ORDER BY", e.g. "ORDER BY `{$this->idField}`" | |
266 | */ | |
267 | public function orderBy() { | |
268 | return ''; | |
269 | } | |
270 | ||
888da08c | 271 | } |