Merge pull request #5059 from totten/master-validation
[civicrm-core.git] / CRM / Activity / Form / Search.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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. |
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 +--------------------------------------------------------------------+
25 */
26
27 /**
28 *
29 * @package CRM
30 * @copyright CiviCRM LLC (c) 2004-2014
31 * $Id$
32 *
33 */
34
35 /**
36 * Files required
37 */
38
39 /**
40 * This file is for activity search
41 */
42 class CRM_Activity_Form_Search extends CRM_Core_Form_Search {
43
44 /**
45 * The params that are sent to the query
46 *
47 * @var array
48 */
49 protected $_queryParams;
50
51 /**
52 * Are we restricting ourselves to a single contact
53 *
54 * @var boolean
55 */
56 protected $_single = FALSE;
57
58 /**
59 * Are we restricting ourselves to a single contact
60 *
61 * @var boolean
62 */
63 protected $_limit = NULL;
64
65 /**
66 * Prefix for the controller
67 */
68 protected $_prefix = "activity_";
69
70 protected $_defaults;
71
72 /**
73 * The saved search ID retrieved from the GET vars
74 *
75 * @var int
76 */
77 protected $_ssID;
78
79 /**
80 * Processing needed for buildForm and later
81 *
82 * @return void
83 */
84 public function preProcess() {
85 $this->set('searchFormName', 'Search');
86
87 // set the button names
88 $this->_searchButtonName = $this->getButtonName('refresh');
89 $this->_actionButtonName = $this->getButtonName('next', 'action');
90
91 $this->_done = FALSE;
92 $this->defaults = array();
93
94 // we allow the controller to set force/reset externally, useful when we are being
95 // driven by the wizard framework
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);
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 /**
161 * Build the form object
162 *
163 *
164 * @return void
165 */
166 public function buildQuickForm() {
167 parent::buildQuickForm();
168 $this->addElement('text', 'sort_name', ts('Name or Email'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name'));
169
170 CRM_Activity_BAO_Query::buildSearchForm($this);
171
172 $rows = $this->get('rows');
173 if (is_array($rows)) {
174 if (!$this->_single) {
175 $this->addRowSelectors($rows);
176 }
177
178 $permission = CRM_Core_Permission::getPermission();
179
180 $this->addTaskMenu(CRM_Activity_Task::permissionedTaskTitles($permission));
181 }
182
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.
194 * The processing consists of using a Selector / Controller framework for getting the
195 * search results.
196 *
197 * @param
198 *
199 * @return void
200 */
201 public function postProcess() {
202 if ($this->_done) {
203 return;
204 }
205
206 $this->_done = TRUE;
207
208 if (!empty($_POST)) {
209 $this->_formValues = $this->controller->exportValues($this->_name);
210 foreach (array('activity_type_id', 'status_id', 'activity_subject') as $element) {
211 $value = CRM_Utils_Array::value($element, $this->_formValues);
212 if ($value) {
213 if (is_array($value)) {
214 if ($element == 'status_id') {
215 unset($this->_formValues[$element]);
216 $element = 'activity_' . $element;
217 }
218 $this->_formValues[$element] = array('IN' => $value);
219 }
220 else {
221 $this->_formValues[$element] = array('LIKE' => "%$value%");
222 }
223 }
224 }
225 }
226
227 $this->fixFormValues();
228
229 if (isset($this->_ssID) && empty($_POST)) {
230 // if we are editing / running a saved search and the form has not been posted
231 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
232 }
233
234 // We don't show test records in summaries or dashboards
235 if (empty($this->_formValues['activity_test']) && $this->_force) {
236 $this->_formValues["activity_test"] = 0;
237 }
238
239 CRM_Core_BAO_CustomValue::fixFieldValueOfTypeMemo($this->_formValues);
240
241 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
242
243 $this->set('formValues', $this->_formValues);
244 $this->set('queryParams', $this->_queryParams);
245
246 $buttonName = $this->controller->getButtonName();
247 if ($buttonName == $this->_actionButtonName) {
248 // check actionName and if next, then do not repeat a search, since we are going to the next page
249 // hack, make sure we reset the task values
250 $stateMachine = $this->controller->getStateMachine();
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_Activity_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 == 'basic' || $this->_context == 'user') {
276 $prefix = $this->_prefix;
277 }
278
279 $controller = new CRM_Core_Selector_Controller($selector,
280 $this->get(CRM_Utils_Pager::PAGE_ID),
281 $sortID,
282 CRM_Core_Action::VIEW,
283 $this,
284 CRM_Core_Selector_Controller::SESSION,
285 $prefix
286 );
287 $controller->setEmbedded(TRUE);
288 $query = &$selector->getQuery();
289
290 if ($this->_context == 'user') {
291 $query->setSkipPermission(TRUE);
292 }
293 $controller->run();
294 }
295
296 public function fixFormValues() {
297 if (!$this->_force) {
298 return;
299 }
300
301 $status = CRM_Utils_Request::retrieve('status', 'String', $this);
302 if ($status) {
303 $this->_formValues['activity_status'] = $status;
304 $this->_defaults['activity_status'] = $status;
305 }
306
307 $survey = CRM_Utils_Request::retrieve('survey', 'Positive', CRM_Core_DAO::$_nullObject);
308
309 if ($survey) {
310 $this->_formValues['activity_survey_id'] = $this->_defaults['activity_survey_id'] = $survey;
311 $sid = CRM_Utils_Array::value('activity_survey_id', $this->_formValues);
312 $activity_type_id = CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Survey', $sid, 'activity_type_id');
313
314 $this->_formValues['activity_type_id'][$activity_type_id] = 1;
315 $this->_defaults['activity_type_id'][$activity_type_id] = 1;
316 }
317 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
318
319 if ($cid) {
320 $cid = CRM_Utils_Type::escape($cid, 'Integer');
321 if ($cid > 0) {
322 $this->_formValues['contact_id'] = $cid;
323
324 $activity_role = CRM_Utils_Request::retrieve('activity_role', 'Positive', $this);
325
326 if ($activity_role) {
327 $this->_formValues['activity_role'] = $activity_role;
328 }
329 else {
330 $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid, 'sort_name');
331 }
332 // also assign individual mode to the template
333 $this->_single = TRUE;
334 }
335 }
336
337 // Added for membership search
338
339 $signupType = CRM_Utils_Request::retrieve('signupType', 'Positive',
340 CRM_Core_DAO::$_nullObject
341 );
342
343 if ($signupType) {
344 //$this->_formValues['activity_type_id'] = array();
345 $this->_formValues['activity_role'] = 1;
346 $this->_defaults['activity_role'] = 1;
347 $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, FALSE, FALSE, 'name');
348
349 $renew = CRM_Utils_Array::key('Membership Renewal', $activityTypes);
350 $signup = CRM_Utils_Array::key('Membership Signup', $activityTypes);
351
352 switch ($signupType) {
353 case 3: // signups and renewals
354 $this->_formValues['activity_type_id'][$renew] = 1;
355 $this->_defaults['activity_type_id'][$renew] = 1;
356 case 1: // signups only
357 $this->_formValues['activity_type_id'][$signup] = 1;
358 $this->_defaults['activity_type_id'][$signup] = 1;
359 break;
360
361 case 2: // renewals only
362 $this->_formValues['activity_type_id'][$renew] = 1;
363 $this->_defaults['activity_type_id'][$renew] = 1;
364 break;
365 }
366 }
367
368 $dateLow = CRM_Utils_Request::retrieve('dateLow', 'String',
369 CRM_Core_DAO::$_nullObject
370 );
371
372 if ($dateLow) {
373 $dateLow = date('m/d/Y', strtotime($dateLow));
374 $this->_formValues['activity_date_relative'] = 0;
375 $this->_defaults['activity_date_relative'] = 0;
376 $this->_formValues['activity_date_low'] = $dateLow;
377 $this->_defaults['activity_date_low'] = $dateLow;
378 }
379
380 $dateHigh = CRM_Utils_Request::retrieve('dateHigh', 'String',
381 CRM_Core_DAO::$_nullObject
382 );
383
384 if ($dateHigh) {
385 // Activity date time assumes midnight at the beginning of the date
386 // This sets it to almost midnight at the end of the date
387 /* if ($dateHigh <= 99999999) {
388 $dateHigh = 1000000 * $dateHigh + 235959;
389 } */
390 $dateHigh = date('m/d/Y', strtotime($dateHigh));
391 $this->_formValues['activity_date_relative'] = 0;
392 $this->_defaults['activity_date_relative'] = 0;
393 $this->_formValues['activity_date_high'] = $dateHigh;
394 $this->_defaults['activity_date_high'] = $dateHigh;
395 }
396
397 if (!empty($this->_defaults)) {
398 $this->setDefaults($this->_defaults);
399 }
400 }
401
402 /**
403 * @return null
404 */
405 public function getFormValues() {
406 return NULL;
407 }
408
409 /**
410 * Return a descriptive name for the page, used in wizard header
411 *
412 * @return string
413 */
414 public function getTitle() {
415 return ts('Find Activities');
416 }
417
418 }