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