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