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