CRM-17335 - Remove unnecessary use of CRM_Core_DAO::$_nullObject
[civicrm-core.git] / CRM / Member / Form / Search.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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-2016
32 */
33
34 /**
35 * Membership search.
36 *
37 * Class is a pane in advanced search and the membership search page.
38 */
39 class CRM_Member_Form_Search extends CRM_Core_Form_Search {
40
41 /**
42 * The params that are sent to the query.
43 *
44 * @var array
45 */
46 protected $_queryParams;
47
48 /**
49 * Are we restricting ourselves to a single contact.
50 *
51 * @var bool
52 */
53 protected $_single = FALSE;
54
55 /**
56 * Are we restricting ourselves to a single contact.
57 *
58 * @var bool
59 */
60 protected $_limit = NULL;
61
62 /**
63 * Prefix for the controller.
64 */
65 protected $_prefix = "member_";
66
67 /**
68 * Declare entity reference fields as they will need to be converted to using 'IN'.
69 *
70 * @var array
71 */
72 protected $entityReferenceFields = array('membership_type_id');
73
74 /**
75 * Processing needed for buildForm and later.
76 */
77 public function preProcess() {
78 $this->set('searchFormName', 'Search');
79
80 $this->_searchButtonName = $this->getButtonName('refresh');
81 $this->_actionButtonName = $this->getButtonName('next', 'action');
82
83 $this->_done = FALSE;
84
85 $this->defaults = array();
86
87 /*
88 * we allow the controller to set force/reset externally, useful when we are being
89 * driven by the wizard framework
90 */
91
92 $this->_reset = CRM_Utils_Request::retrieve('reset', 'Boolean');
93 $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean', $this, FALSE);
94 $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive', $this);
95 $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'search');
96
97 $this->assign("context", $this->_context);
98
99 // get user submitted values
100 // get it from controller only if form has been submitted, else preProcess has set this
101 if (!empty($_POST)) {
102 $this->_formValues = $this->controller->exportValues($this->_name);
103 }
104 else {
105 $this->_formValues = $this->get('formValues');
106 }
107
108 if ($this->_force) {
109 $this->postProcess();
110 $this->set('force', 0);
111 }
112
113 $sortID = NULL;
114 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
115 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
116 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
117 );
118 }
119
120 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, $this->entityReferenceFields);
121 $selector = new CRM_Member_Selector_Search($this->_queryParams,
122 $this->_action,
123 NULL,
124 $this->_single,
125 $this->_limit,
126 $this->_context
127 );
128 $prefix = NULL;
129 if ($this->_context == 'basic') {
130 $prefix = $this->_prefix;
131 }
132
133 $this->assign("{$prefix}limit", $this->_limit);
134 $this->assign("{$prefix}single", $this->_single);
135
136 $controller = new CRM_Core_Selector_Controller($selector,
137 $this->get(CRM_Utils_Pager::PAGE_ID),
138 $sortID,
139 CRM_Core_Action::VIEW,
140 $this,
141 CRM_Core_Selector_Controller::TRANSFER,
142 $prefix
143 );
144
145 $controller->setEmbedded(TRUE);
146 $controller->moveFromSessionToTemplate();
147
148 $this->assign('summary', $this->get('summary'));
149 }
150
151 /**
152 * Build the form object.
153 */
154 public function buildQuickForm() {
155 parent::buildQuickForm();
156 $this->addSortNameField();
157
158 CRM_Member_BAO_Query::buildSearchForm($this);
159
160 $rows = $this->get('rows');
161 if (is_array($rows)) {
162 if (!$this->_single) {
163 $this->addRowSelectors($rows);
164 }
165
166 $permission = CRM_Core_Permission::getPermission();
167
168 $this->addTaskMenu(CRM_Member_Task::permissionedTaskTitles($permission));
169 }
170
171 }
172
173 /**
174 * Get the label for the sortName field if email searching is on.
175 *
176 * (email searching is a setting under search preferences).
177 *
178 * @return string
179 */
180 protected function getSortNameLabelWithEmail() {
181 return ts('Member Name or Email');
182 }
183
184 /**
185 * Get the label for the sortName field if email searching is off.
186 *
187 * (email searching is a setting under search preferences).
188 *
189 * @return string
190 */
191 protected function getSortNameLabelWithOutEmail() {
192 return ts('Member Name');
193 }
194
195 /**
196 * The post processing of the form gets done here.
197 *
198 * Key things done during post processing are
199 * - check for reset or next request. if present, skip post procesing.
200 * - now check if user requested running a saved search, if so, then
201 * the form values associated with the saved search are used for searching.
202 * - if user has done a submit with new values the regular post submissing is
203 * done.
204 * The processing consists of using a Selector / Controller framework for getting the
205 * search results.
206 */
207 public function postProcess() {
208 if ($this->_done) {
209 return;
210 }
211
212 $this->_done = TRUE;
213
214 $this->_formValues = $this->controller->exportValues($this->_name);
215
216 $this->fixFormValues();
217
218 // We don't show test records in summaries or dashboards
219 if (empty($this->_formValues['member_test']) && $this->_force) {
220 $this->_formValues["member_test"] = 0;
221 }
222
223 CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
224
225 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, $this->entityReferenceFields);
226
227 $this->set('formValues', $this->_formValues);
228 $this->set('queryParams', $this->_queryParams);
229
230 $buttonName = $this->controller->getButtonName();
231 if ($buttonName == $this->_actionButtonName) {
232 // check actionName and if next, then do not repeat a search, since we are going to the next page
233
234 // hack, make sure we reset the task values
235 $stateMachine = $this->controller->getStateMachine();
236 $formName = $stateMachine->getTaskFormName();
237 $this->controller->resetPage($formName);
238 return;
239 }
240
241 $sortID = NULL;
242 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
243 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
244 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
245 );
246 }
247
248 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, $this->entityReferenceFields);
249
250 $selector = new CRM_Member_Selector_Search($this->_queryParams,
251 $this->_action,
252 NULL,
253 $this->_single,
254 $this->_limit,
255 $this->_context
256 );
257 $selector->setKey($this->controller->_key);
258
259 $prefix = NULL;
260 if ($this->_context == 'basic') {
261 $prefix = $this->_prefix;
262 }
263
264 $controller = new CRM_Core_Selector_Controller($selector,
265 $this->get(CRM_Utils_Pager::PAGE_ID),
266 $sortID,
267 CRM_Core_Action::VIEW,
268 $this,
269 CRM_Core_Selector_Controller::SESSION,
270 $prefix
271 );
272 $controller->setEmbedded(TRUE);
273
274 $query = &$selector->getQuery();
275 $controller->run();
276 }
277
278 /**
279 * Set default values.
280 *
281 * @todo - can this function override be removed?
282 *
283 * @return array
284 */
285 public function setDefaultValues() {
286 return $this->_defaults;
287 }
288
289 /**
290 * If this search has been forced then see if there are any get values, and if so over-ride the post values.
291 *
292 * Note that this means that GET over-rides POST :) & that force with no parameters can be very destructive.
293 */
294 public function fixFormValues() {
295 if (!$this->_force) {
296 return;
297 }
298
299 $status = CRM_Utils_Request::retrieve('status', 'String',
300 CRM_Core_DAO::$_nullObject
301 );
302 if ($status) {
303 $status = explode(',', $status);
304 $this->_formValues['membership_status_id'] = $this->_defaults['membership_status_id'] = (array) $status;
305 }
306
307 $membershipType = CRM_Utils_Request::retrieve('type', 'String',
308 CRM_Core_DAO::$_nullObject
309 );
310
311 if ($membershipType) {
312 $this->_formValues['membership_type_id'] = array($membershipType);
313 $this->_defaults['membership_type_id'] = array($membershipType);
314 }
315
316 $cid = CRM_Utils_Request::retrieve('cid', 'Positive',
317 CRM_Core_DAO::$_nullObject
318 );
319
320 if ($cid) {
321 $cid = CRM_Utils_Type::escape($cid, 'Integer');
322 if ($cid > 0) {
323 $this->_formValues['contact_id'] = $cid;
324 list($display, $image) = CRM_Contact_BAO_Contact::getDisplayAndImage($cid);
325 $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid,
326 'sort_name'
327 );
328 // also assign individual mode to the template
329 $this->_single = TRUE;
330 }
331 }
332
333 $fromDate = CRM_Utils_Request::retrieve('start', 'Date',
334 CRM_Core_DAO::$_nullObject
335 );
336 if ($fromDate) {
337 list($date) = CRM_Utils_Date::setDateDefaults($fromDate);
338 $this->_formValues['member_start_date_low'] = $this->_defaults['member_start_date_low'] = $date;
339 }
340
341 $toDate = CRM_Utils_Request::retrieve('end', 'Date',
342 CRM_Core_DAO::$_nullObject
343 );
344 if ($toDate) {
345 list($date) = CRM_Utils_Date::setDateDefaults($toDate);
346 $this->_formValues['member_start_date_high'] = $this->_defaults['member_start_date_high'] = $date;
347 }
348 $joinDate = CRM_Utils_Request::retrieve('join', 'Date',
349 CRM_Core_DAO::$_nullObject
350 );
351 if ($joinDate) {
352 list($date) = CRM_Utils_Date::setDateDefaults($joinDate);
353 $this->_formValues['member_join_date_low'] = $this->_defaults['member_join_date_low'] = $date;
354 }
355
356 $joinEndDate = CRM_Utils_Request::retrieve('joinEnd', 'Date',
357 CRM_Core_DAO::$_nullObject
358 );
359 if ($joinEndDate) {
360 list($date) = CRM_Utils_Date::setDateDefaults($joinEndDate);
361 $this->_formValues['member_join_date_high'] = $this->_defaults['member_join_date_high'] = $date;
362 }
363
364 $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive',
365 $this
366 );
367
368 //LCD also allow restrictions to membership owner via GET
369 $owner = CRM_Utils_Request::retrieve('owner', 'String');
370 if ($owner) {
371 $this->_formValues['member_is_primary'] = $this->_defaults['member_is_primary'] = 2;
372 }
373 }
374
375 /**
376 * Return a descriptive name for the page, used in wizard header.
377 *
378 * @return string
379 */
380 public function getTitle() {
381 return ts('Find Memberships');
382 }
383
384 }