CRM-16980 - Only show relevant contacts when creating case role
[civicrm-core.git] / CRM / Case / Form / Search.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
6a488035
TO
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 +--------------------------------------------------------------------+
e70a7fc0 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 */
33
34/**
df371444 35 * This file is for Case search.
6a488035 36 */
3efb5b86 37class CRM_Case_Form_Search extends CRM_Core_Form_Search {
6a488035 38
6a488035 39 /**
100fef9d 40 * The params that are sent to the query
6a488035
TO
41 *
42 * @var array
6a488035
TO
43 */
44 protected $_queryParams;
45
6a488035 46 /**
100fef9d 47 * Are we restricting ourselves to a single contact
6a488035 48 *
6a488035
TO
49 * @var boolean
50 */
51 protected $_single = FALSE;
52
53 /**
100fef9d 54 * Are we restricting ourselves to a single contact
6a488035 55 *
6a488035
TO
56 * @var boolean
57 */
58 protected $_limit = NULL;
59
6a488035 60 /**
100fef9d 61 * Prefix for the controller
6a488035
TO
62 */
63 protected $_prefix = 'case_';
64
6a488035 65 /**
fe482240 66 * Processing needed for buildForm and later.
6a488035 67 */
00be9182 68 public function preProcess() {
6a488035
TO
69 $this->set('searchFormName', 'Search');
70
71 //check for civicase access.
72 if (!CRM_Case_BAO_Case::accessCiviCase()) {
73 CRM_Core_Error::fatal(ts('You are not authorized to access this page.'));
74 }
75
76 //validate case configuration.
77 $configured = CRM_Case_BAO_Case::isCaseConfigured();
78 $this->assign('notConfigured', !$configured['configured']);
79 if (!$configured['configured']) {
80 return;
81 }
82
83 /**
84 * set the button names
85 */
86 $this->_searchButtonName = $this->getButtonName('refresh');
6a488035
TO
87 $this->_actionButtonName = $this->getButtonName('next', 'action');
88
89 $this->_done = FALSE;
90 $this->defaults = array();
91
92 /*
e70a7fc0
TO
93 * we allow the controller to set force/reset externally, useful when we are being
94 * driven by the wizard framework
95 */
6a488035 96
353ffa53
TO
97 $this->_reset = CRM_Utils_Request::retrieve('reset', 'Boolean', CRM_Core_DAO::$_nullObject);
98 $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean', $this, FALSE);
99 $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive', $this);
6a488035
TO
100 $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'search');
101
102 $this->assign('context', $this->_context);
103
104 // get user submitted values
105 // get it from controller only if form has been submitted, else preProcess has set this
106 if (!empty($_POST) && !$this->controller->isModal()) {
107 $this->_formValues = $this->controller->exportValues($this->_name);
108 }
109 else {
110 $this->_formValues = $this->get('formValues');
111 }
112
113 if (empty($this->_formValues)) {
114 if (isset($this->_ssID)) {
115 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
116 }
117 }
118
119 if ($this->_force) {
120 $this->postProcess();
121 $this->set('force', 0);
122 }
123
124 $sortID = NULL;
125 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
126 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
127 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
128 );
129 }
130
6a488035
TO
131 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
132 $selector = new CRM_Case_Selector_Search($this->_queryParams,
133 $this->_action,
134 NULL,
135 $this->_single,
136 $this->_limit,
137 $this->_context
138 );
139
140 $prefix = NULL;
141 if ($this->_context == 'user') {
142 $prefix = $this->_prefix;
143 }
144
145 $this->assign("{$prefix}limit", $this->_limit);
146 $this->assign("{$prefix}single", $this->_single);
147
148 $controller = new CRM_Core_Selector_Controller($selector,
149 $this->get(CRM_Utils_Pager::PAGE_ID),
150 $sortID,
151 CRM_Core_Action::VIEW,
152 $this,
153 CRM_Core_Selector_Controller::TRANSFER,
154 $prefix
155 );
156 $controller->setEmbedded(TRUE);
157 $controller->moveFromSessionToTemplate();
158
159 $this->assign('summary', $this->get('summary'));
160 }
161
162 /**
fe482240 163 * Build the form object.
6a488035 164 */
00be9182 165 public function buildQuickForm() {
3efb5b86 166 parent::buildQuickForm();
6a488035
TO
167 $this->addElement('text',
168 'sort_name',
169 ts('Client Name or Email'),
170 CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name')
171 );
172
173 CRM_Case_BAO_Query::buildSearchForm($this);
174
6a488035
TO
175 $rows = $this->get('rows');
176 if (is_array($rows)) {
6a488035 177 if (!$this->_single) {
8d36b801 178 $this->addRowSelectors($rows);
6a488035
TO
179 }
180
6a488035
TO
181 $permission = CRM_Core_Permission::getPermission();
182
34197a55 183 $tasks = CRM_Case_Task::permissionedTaskTitles($permission);
6a488035 184
a7488080 185 if (!empty($this->_formValues['case_deleted'])) {
6a488035
TO
186 unset($tasks[1]);
187 }
188 else {
189 unset($tasks[4]);
190 }
191
34197a55 192 $this->addTaskMenu($tasks);
6a488035
TO
193 }
194
6a488035
TO
195 }
196
197 /**
198 * The post processing of the form gets done here.
199 *
200 * Key things done during post processing are
201 * - check for reset or next request. if present, skip post procesing.
202 * - now check if user requested running a saved search, if so, then
203 * the form values associated with the saved search are used for searching.
204 * - if user has done a submit with new values the regular post submissing is
205 * done.
206 * The processing consists of using a Selector / Controller framework for getting the
207 * search results.
6a488035 208 */
00be9182 209 public function postProcess() {
6a488035
TO
210 if ($this->_done) {
211 return;
212 }
213
214 $this->_done = TRUE;
215 $this->_formValues = $this->controller->exportValues($this->_name);
216 $this->fixFormValues();
217
218 if (isset($this->_ssID) && empty($_POST)) {
219 // if we are editing / running a saved search and the form has not been posted
220 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
221 }
222
223 //search for civicase
224 if (!$this->_force) {
225 if (array_key_exists('case_owner', $this->_formValues) && !$this->_formValues['case_owner']) {
226 $this->_formValues['case_owner'] = 0;
227 }
228 }
229
230 //only fetch own cases.
231 if (!CRM_Core_Permission::check('access all cases and activities')) {
232 $this->_formValues['case_owner'] = 2;
233 }
234
a7488080 235 if (empty($this->_formValues['case_deleted'])) {
6a488035
TO
236 $this->_formValues['case_deleted'] = 0;
237 }
c94d39fd 238 CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
6a488035
TO
239
240 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
241
242 $this->set('formValues', $this->_formValues);
243 $this->set('queryParams', $this->_queryParams);
244
245 $buttonName = $this->controller->getButtonName();
e341bbee 246 if ($buttonName == $this->_actionButtonName) {
6a488035
TO
247 // check actionName and if next, then do not repeat a search, since we are going to the next page
248
249 // hack, make sure we reset the task values
95ea77ec 250 $stateMachine = $this->controller->getStateMachine();
6a488035
TO
251 $formName = $stateMachine->getTaskFormName();
252 $this->controller->resetPage($formName);
253 return;
254 }
255
256 $sortID = NULL;
257 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
258 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
259 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
260 );
261 }
262
263 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
264
265 $selector = new CRM_Case_Selector_Search($this->_queryParams,
266 $this->_action,
267 NULL,
268 $this->_single,
269 $this->_limit,
270 $this->_context
271 );
272 $selector->setKey($this->controller->_key);
273
274 $prefix = NULL;
275 if ($this->_context == 'user') {
276 $prefix = $this->_prefix;
277 }
278
279 $this->assign("{$prefix}limit", $this->_limit);
280 $this->assign("{$prefix}single", $this->_single);
281
282 $controller = new CRM_Core_Selector_Controller($selector,
283 $this->get(CRM_Utils_Pager::PAGE_ID),
284 $sortID,
285 CRM_Core_Action::VIEW,
286 $this,
287 CRM_Core_Selector_Controller::SESSION,
288 $prefix
289 );
290 $controller->setEmbedded(TRUE);
291
292 $query = &$selector->getQuery();
293 if ($this->_context == 'user') {
294 $query->setSkipPermission(TRUE);
295 }
296 $controller->run();
297 }
298
299 /**
df371444 300 * Add the rules (mainly global rules) for form.
301 *
6a488035
TO
302 * All local rules are added near the element
303 *
6a488035
TO
304 * @see valid_date
305 */
00be9182 306 public function addRules() {
6a488035
TO
307 $this->addFormRule(array('CRM_Case_Form_Search', 'formRule'));
308 }
309
310 /**
fe482240 311 * Global validation rules for the form.
6a488035 312 *
64bd5a0e
TO
313 * @param array $fields
314 * Posted values of the form.
2a6da8d7 315 *
df371444 316 * @return array|bool
6a488035 317 */
00be9182 318 public static function formRule($fields) {
6a488035
TO
319 $errors = array();
320
321 if (!empty($errors)) {
322 return $errors;
323 }
324
325 return TRUE;
326 }
327
328 /**
fe482240 329 * Set the default form values.
6a488035 330 *
6a488035 331 *
a6c01b45
CW
332 * @return array
333 * the default array reference
6a488035 334 */
00be9182 335 public function setDefaultValues() {
6a488035
TO
336 $defaults = array();
337 $defaults = $this->_formValues;
338 return $defaults;
339 }
340
00be9182 341 public function fixFormValues() {
6a488035
TO
342 if (!$this->_force) {
343 return;
344 }
345
346 $caseStatus = CRM_Utils_Request::retrieve('status', 'Positive',
347 CRM_Core_DAO::$_nullObject
348 );
349 if ($caseStatus) {
350 $this->_formValues['case_status_id'] = $caseStatus;
351 $this->_defaults['case_status_id'] = $caseStatus;
352 }
353 $caseType = CRM_Utils_Request::retrieve('type', 'Positive',
354 CRM_Core_DAO::$_nullObject
355 );
356 if ($caseType) {
357 $this->_formValues['case_type_id'][$caseType] = 1;
358 $this->_defaults['case_type_id'][$caseType] = 1;
359 }
360
361 $caseFromDate = CRM_Utils_Request::retrieve('pstart', 'Date',
362 CRM_Core_DAO::$_nullObject
363 );
364 if ($caseFromDate) {
365 list($date) = CRM_Utils_Date::setDateDefaults($caseFromDate);
366 $this->_formValues['case_start_date_low'] = $date;
367 $this->_defaults['case_start_date_low'] = $date;
368 }
369
370 $caseToDate = CRM_Utils_Request::retrieve('pend', 'Date',
371 CRM_Core_DAO::$_nullObject
372 );
373 if ($caseToDate) {
374 list($date) = CRM_Utils_Date::setDateDefaults($caseToDate);
375 $this->_formValues['case_start_date_high'] = $date;
376 $this->_defaults['case_start_date_high'] = $date;
377 }
378
379 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
380 if ($cid) {
381 $cid = CRM_Utils_Type::escape($cid, 'Integer');
382 if ($cid > 0) {
383 $this->_formValues['contact_id'] = $cid;
384 list($display, $image) = CRM_Contact_BAO_Contact::getDisplayAndImage($cid);
385 $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid,
386 'sort_name'
387 );
388 // also assign individual mode to the template
389 $this->_single = TRUE;
390 }
391 }
392 else {
e547f744 393 // First, if "all" is stored in the session, default to all cases, otherwise default to no selection.
6a488035
TO
394 $session = CRM_Core_Session::singleton();
395 if (CRM_Utils_Request::retrieve('all', 'Positive', $session)) {
396 $this->_formValues['case_owner'] = 1;
397 $this->_defaults['case_owner'] = 1;
398 }
399 else {
400 $this->_formValues['case_owner'] = 0;
401 $this->_defaults['case_owner'] = 0;
402 }
403
404 // Now if case_owner is set in the url/post, use that instead.
405 $caseOwner = CRM_Utils_Request::retrieve('case_owner', 'Positive',
406 CRM_Core_DAO::$_nullObject
407 );
408 if ($caseOwner) {
409 $this->_formValues['case_owner'] = $caseOwner;
410 $this->_defaults['case_owner'] = $caseOwner;
411 }
412 }
413 }
414
4c6ce474
EM
415 /**
416 * @return null
417 */
00be9182 418 public function getFormValues() {
6a488035
TO
419 return NULL;
420 }
421
422 /**
423 * Return a descriptive name for the page, used in wizard header
424 *
425 * @return string
6a488035
TO
426 */
427 public function getTitle() {
428 return ts('Find Cases');
429 }
96025800 430
6a488035 431}