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