Merge remote-tracking branch 'upstream/4.6' into 4.6-master-2015-08-12-15-44-44
[civicrm-core.git] / CRM / Activity / Form / Search.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
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 |
c73475ea 12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
6a488035
TO
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 |
c73475ea
WA
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * Files required
38 */
39
40/**
41 * This file is for activity search
42 */
3efb5b86 43class CRM_Activity_Form_Search extends CRM_Core_Form_Search {
6a488035 44
6a488035 45 /**
fe482240 46 * The params that are sent to the query.
6a488035
TO
47 *
48 * @var array
6a488035
TO
49 */
50 protected $_queryParams;
51
6a488035 52 /**
fe482240 53 * Are we restricting ourselves to a single contact.
6a488035 54 *
6a488035
TO
55 * @var boolean
56 */
57 protected $_single = FALSE;
58
59 /**
fe482240 60 * Are we restricting ourselves to a single contact.
6a488035 61 *
6a488035
TO
62 * @var boolean
63 */
64 protected $_limit = NULL;
65
6a488035 66 /**
fe482240 67 * Prefix for the controller.
6a488035
TO
68 */
69 protected $_prefix = "activity_";
70
6a488035 71 /**
fe482240 72 * The saved search ID retrieved from the GET vars.
6a488035
TO
73 *
74 * @var int
6a488035
TO
75 */
76 protected $_ssID;
77
78 /**
fe482240 79 * Processing needed for buildForm and later.
6a488035
TO
80 *
81 * @return void
6a488035 82 */
00be9182 83 public function preProcess() {
6a488035
TO
84 $this->set('searchFormName', 'Search');
85
c490a46a 86 // set the button names
6a488035 87 $this->_searchButtonName = $this->getButtonName('refresh');
6a488035
TO
88 $this->_actionButtonName = $this->getButtonName('next', 'action');
89
90 $this->_done = FALSE;
91 $this->defaults = array();
92
c490a46a
CW
93 // we allow the controller to set force/reset externally, useful when we are being
94 // driven by the wizard framework
353ffa53
TO
95 $this->_reset = CRM_Utils_Request::retrieve('reset', 'Boolean', CRM_Core_DAO::$_nullObject);
96 $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean', $this, FALSE);
97 $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive', $this);
6a488035
TO
98 $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'search');
99
100 $this->assign("context", $this->_context);
101
102 // get user submitted values
103 // get it from controller only if form has been submitted, else preProcess has set this
104 if (!empty($_POST) && !$this->controller->isModal()) {
105 $this->_formValues = $this->controller->exportValues($this->_name);
106 }
107 else {
108 $this->_formValues = $this->get('formValues');
109 }
110
111 if (empty($this->_formValues)) {
112 if (isset($this->_ssID)) {
113 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
114 }
115 }
116
117 if ($this->_force) {
118 $this->postProcess();
119 $this->set('force', 0);
120 }
121
122 $sortID = NULL;
123 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
124 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
125 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
126 );
127 }
128
129 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
130 $selector = new CRM_Activity_Selector_Search($this->_queryParams,
131 $this->_action,
132 NULL,
133 $this->_single,
134 $this->_limit,
135 $this->_context
136 );
137 $prefix = NULL;
138 if ($this->_context == 'user') {
139 $prefix = $this->_prefix;
140 }
141
142 $this->assign("{$prefix}limit", $this->_limit);
143 $this->assign("{$prefix}single", $this->_single);
144
145 $controller = new CRM_Core_Selector_Controller($selector,
146 $this->get(CRM_Utils_Pager::PAGE_ID),
147 $sortID,
148 CRM_Core_Action::VIEW,
149 $this,
150 CRM_Core_Selector_Controller::TRANSFER,
151 $prefix
152 );
153 $controller->setEmbedded(TRUE);
154 $controller->moveFromSessionToTemplate();
155
156 $this->assign('summary', $this->get('summary'));
157 }
158
159 /**
fe482240 160 * Build the form object.
6a488035 161 *
6a488035
TO
162 *
163 * @return void
164 */
00be9182 165 public function buildQuickForm() {
3efb5b86 166 parent::buildQuickForm();
18d2d5e2 167 $this->addElement('text', 'sort_name', ts('Name or Email'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name'));
6a488035
TO
168
169 CRM_Activity_BAO_Query::buildSearchForm($this);
170
6a488035
TO
171 $rows = $this->get('rows');
172 if (is_array($rows)) {
173 if (!$this->_single) {
8d36b801 174 $this->addRowSelectors($rows);
6a488035
TO
175 }
176
6a488035
TO
177 $permission = CRM_Core_Permission::getPermission();
178
34197a55 179 $this->addTaskMenu(CRM_Activity_Task::permissionedTaskTitles($permission));
6a488035
TO
180 }
181
6a488035
TO
182 }
183
184 /**
185 * The post processing of the form gets done here.
186 *
187 * Key things done during post processing are
188 * - check for reset or next request. if present, skip post procesing.
189 * - now check if user requested running a saved search, if so, then
190 * the form values associated with the saved search are used for searching.
191 * - if user has done a submit with new values the regular post submissing is
192 * done.
2e2605fe 193 *
6a488035
TO
194 * The processing consists of using a Selector / Controller framework for getting the
195 * search results.
6a488035 196 */
00be9182 197 public function postProcess() {
6a488035
TO
198 if ($this->_done) {
199 return;
200 }
201
202 $this->_done = TRUE;
203
204 if (!empty($_POST)) {
205 $this->_formValues = $this->controller->exportValues($this->_name);
9ab34172 206 foreach (array('activity_type_id', 'status_id', 'activity_subject') as $element) {
207 $value = CRM_Utils_Array::value($element, $this->_formValues);
208 if ($value) {
209 if (is_array($value)) {
210 if ($element == 'status_id') {
211 unset($this->_formValues[$element]);
212 $element = 'activity_' . $element;
213 }
214 $this->_formValues[$element] = array('IN' => $value);
215 }
216 else {
217 $this->_formValues[$element] = array('LIKE' => "%$value%");
218 }
219 }
220 }
6a488035
TO
221 }
222
223 $this->fixFormValues();
224
225 if (isset($this->_ssID) && empty($_POST)) {
226 // if we are editing / running a saved search and the form has not been posted
227 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
228 }
6a488035 229
d43b88cc
CW
230 // We don't show test records in summaries or dashboards
231 if (empty($this->_formValues['activity_test']) && $this->_force) {
6a488035
TO
232 $this->_formValues["activity_test"] = 0;
233 }
28c666be 234
c94d39fd 235 CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
6a488035
TO
236
237 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
238
239 $this->set('formValues', $this->_formValues);
240 $this->set('queryParams', $this->_queryParams);
241
242 $buttonName = $this->controller->getButtonName();
e341bbee 243 if ($buttonName == $this->_actionButtonName) {
6a488035
TO
244 // check actionName and if next, then do not repeat a search, since we are going to the next page
245 // hack, make sure we reset the task values
95ea77ec 246 $stateMachine = $this->controller->getStateMachine();
6a488035
TO
247 $formName = $stateMachine->getTaskFormName();
248 $this->controller->resetPage($formName);
249 return;
250 }
251
252 $sortID = NULL;
253 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
254 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
255 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
256 );
257 }
258
259 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
260
261 $selector = new CRM_Activity_Selector_Search($this->_queryParams,
262 $this->_action,
263 NULL,
264 $this->_single,
265 $this->_limit,
266 $this->_context
267 );
268 $selector->setKey($this->controller->_key);
269
270 $prefix = NULL;
271 if ($this->_context == 'basic' || $this->_context == 'user') {
272 $prefix = $this->_prefix;
273 }
274
275 $controller = new CRM_Core_Selector_Controller($selector,
276 $this->get(CRM_Utils_Pager::PAGE_ID),
277 $sortID,
278 CRM_Core_Action::VIEW,
279 $this,
280 CRM_Core_Selector_Controller::SESSION,
281 $prefix
282 );
283 $controller->setEmbedded(TRUE);
284 $query = &$selector->getQuery();
285
286 if ($this->_context == 'user') {
287 $query->setSkipPermission(TRUE);
288 }
289 $controller->run();
290 }
291
00be9182 292 public function fixFormValues() {
6a488035
TO
293 if (!$this->_force) {
294 return;
295 }
0370ad20 296
6a488035
TO
297 $status = CRM_Utils_Request::retrieve('status', 'String', $this);
298 if ($status) {
97b002d7
CW
299 $this->_formValues['activity_status_id'] = $status;
300 $this->_defaults['activity_status_id'] = $status;
6a488035
TO
301 }
302
300c3253
KJ
303 $survey = CRM_Utils_Request::retrieve('survey', 'Positive', CRM_Core_DAO::$_nullObject);
304
6a488035 305 if ($survey) {
9231464f
KJ
306 $this->_formValues['activity_survey_id'] = $this->_defaults['activity_survey_id'] = $survey;
307 $sid = CRM_Utils_Array::value('activity_survey_id', $this->_formValues);
308 $activity_type_id = CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Survey', $sid, 'activity_type_id');
309
c56c5190 310 // since checkbox are replaced by multiple select option
311 $this->_formValues['activity_type_id'] = $activity_type_id;
312 $this->_defaults['activity_type_id'] = $activity_type_id;
6a488035
TO
313 }
314 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
315
316 if ($cid) {
317 $cid = CRM_Utils_Type::escape($cid, 'Integer');
318 if ($cid > 0) {
319 $this->_formValues['contact_id'] = $cid;
320
321 $activity_role = CRM_Utils_Request::retrieve('activity_role', 'Positive', $this);
322
323 if ($activity_role) {
324 $this->_formValues['activity_role'] = $activity_role;
325 }
326 else {
6a488035
TO
327 $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid, 'sort_name');
328 }
329 // also assign individual mode to the template
330 $this->_single = TRUE;
331 }
332 }
8ef12e64 333
4e636a74 334 // Added for membership search
8ef12e64 335
4e636a74
AH
336 $signupType = CRM_Utils_Request::retrieve('signupType', 'Positive',
337 CRM_Core_DAO::$_nullObject
338 );
8ef12e64 339
4e636a74
AH
340 if ($signupType) {
341 //$this->_formValues['activity_type_id'] = array();
2e2acd1f 342 $this->_formValues['activity_role'] = 1;
2e2acd1f 343 $this->_defaults['activity_role'] = 1;
4e636a74 344 $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, FALSE, FALSE, 'name');
0370ad20
AH
345
346 $renew = CRM_Utils_Array::key('Membership Renewal', $activityTypes);
347 $signup = CRM_Utils_Array::key('Membership Signup', $activityTypes);
8ef12e64 348
4e636a74
AH
349 switch ($signupType) {
350 case 3: // signups and renewals
351 $this->_formValues['activity_type_id'][$renew] = 1;
352 $this->_defaults['activity_type_id'][$renew] = 1;
353 case 1: // signups only
354 $this->_formValues['activity_type_id'][$signup] = 1;
355 $this->_defaults['activity_type_id'][$signup] = 1;
356 break;
8ef12e64 357
4e636a74
AH
358 case 2: // renewals only
359 $this->_formValues['activity_type_id'][$renew] = 1;
360 $this->_defaults['activity_type_id'][$renew] = 1;
361 break;
362 }
363 }
8ef12e64 364
0370ad20 365 $dateLow = CRM_Utils_Request::retrieve('dateLow', 'String',
4e636a74
AH
366 CRM_Core_DAO::$_nullObject
367 );
8ef12e64 368
4e636a74 369 if ($dateLow) {
0370ad20
AH
370 $dateLow = date('m/d/Y', strtotime($dateLow));
371 $this->_formValues['activity_date_relative'] = 0;
372 $this->_defaults['activity_date_relative'] = 0;
4e636a74
AH
373 $this->_formValues['activity_date_low'] = $dateLow;
374 $this->_defaults['activity_date_low'] = $dateLow;
375 }
8ef12e64 376
0370ad20 377 $dateHigh = CRM_Utils_Request::retrieve('dateHigh', 'String',
4e636a74
AH
378 CRM_Core_DAO::$_nullObject
379 );
8ef12e64 380
4e636a74
AH
381 if ($dateHigh) {
382 // Activity date time assumes midnight at the beginning of the date
383 // This sets it to almost midnight at the end of the date
32864ccf 384 /* if ($dateHigh <= 99999999) {
e70a7fc0 385 $dateHigh = 1000000 * $dateHigh + 235959;
0370ad20
AH
386 } */
387 $dateHigh = date('m/d/Y', strtotime($dateHigh));
388 $this->_formValues['activity_date_relative'] = 0;
389 $this->_defaults['activity_date_relative'] = 0;
4e636a74
AH
390 $this->_formValues['activity_date_high'] = $dateHigh;
391 $this->_defaults['activity_date_high'] = $dateHigh;
8ef12e64 392 }
9231464f
KJ
393
394 if (!empty($this->_defaults)) {
395 $this->setDefaults($this->_defaults);
396 }
6a488035
TO
397 }
398
ffd93213
EM
399 /**
400 * @return null
401 */
00be9182 402 public function getFormValues() {
6a488035
TO
403 return NULL;
404 }
405
406 /**
407 * Return a descriptive name for the page, used in wizard header
408 *
409 * @return string
6a488035
TO
410 */
411 public function getTitle() {
412 return ts('Find Activities');
413 }
96025800 414
6a488035 415}