Merge pull request #9979 from mattwire/disable_payment_processor2
[civicrm-core.git] / CRM / Profile / Page / Listings.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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-2017
32 *
33 */
34
35 /**
36 * This implements the profile page for all contacts. It uses a selector
37 * object to do the actual dispay. The fields displayd are controlled by
38 * the admin.
39 */
40 class CRM_Profile_Page_Listings extends CRM_Core_Page {
41
42 /**
43 * All the fields that are listings related.
44 *
45 * @var array
46 */
47 protected $_fields;
48
49 /**
50 * The custom fields for this domain.
51 *
52 * @var array
53 */
54 protected $_customFields;
55
56 /**
57 * The input params from the request.
58 *
59 * @var array
60 */
61 protected $_params;
62
63 /**
64 * The group id that we are editing.
65 *
66 * @var int
67 */
68 protected $_gid;
69
70 /**
71 * State whether to display search form or not.
72 *
73 * @var int
74 */
75 protected $_search;
76
77 /**
78 * Should we display a map.
79 *
80 * @var int
81 */
82 protected $_map;
83
84 /**
85 * Store profile ids if multiple profile ids are passed using comma separated.
86 * Currently lets implement this functionality only for dialog mode
87 */
88 protected $_profileIds = array();
89
90 /**
91 * Extracts the parameters from the request and constructs information for
92 * the selector object to do a query
93 *
94 */
95 public function preProcess() {
96
97 $this->_search = TRUE;
98
99 $search = CRM_Utils_Request::retrieve('search', 'Boolean', $this, FALSE, 0, 'GET');
100 if (isset($search) && $search == 0) {
101 $this->_search = FALSE;
102 }
103
104 $this->_gid = $this->get('gid');
105 $this->_profileIds = $this->get('profileIds');
106
107 $gids = explode(',', CRM_Utils_Request::retrieve('gid', 'String', CRM_Core_DAO::$_nullObject, FALSE, 0, 'GET'));
108
109 if ((count($gids) > 1) && !$this->_profileIds && empty($this->_profileIds)) {
110 if (!empty($gids)) {
111 foreach ($gids as $pfId) {
112 $this->_profileIds[] = CRM_Utils_Type::escape($pfId, 'Positive');
113 }
114 }
115
116 // check if we are rendering mixed profiles
117 if (CRM_Core_BAO_UFGroup::checkForMixProfiles($this->_profileIds)) {
118 CRM_Core_Error::fatal(ts('You cannot combine profiles of multiple types.'));
119 }
120
121 $this->_gid = $this->_profileIds[0];
122 $this->set('profileIds', $this->_profileIds);
123 $this->set('gid', $this->_gid);
124 }
125
126 if (!$this->_gid) {
127 $this->_gid = CRM_Utils_Request::retrieve('gid', 'Positive', $this, FALSE, 0, 'GET');
128 }
129
130 if (empty($this->_profileIds)) {
131 $gids = $this->_gid;
132 }
133 else {
134 $gids = $this->_profileIds;
135 }
136
137 $this->_fields = CRM_Core_BAO_UFGroup::getListingFields(CRM_Core_Action::UPDATE,
138 CRM_Core_BAO_UFGroup::PUBLIC_VISIBILITY | CRM_Core_BAO_UFGroup::LISTINGS_VISIBILITY,
139 FALSE, $gids, FALSE, 'Profile',
140 CRM_Core_Permission::SEARCH
141 );
142
143 $this->_customFields = CRM_Core_BAO_CustomField::getFieldsForImport(NULL, FALSE, FALSE, FALSE, TRUE, TRUE);
144 $this->_params = array();
145
146 $resetArray = array(
147 'group',
148 'tag',
149 'preferred_communication_method',
150 'do_not_phone',
151 'do_not_email',
152 'do_not_mail',
153 'do_not_sms',
154 'do_not_trade',
155 'gender',
156 );
157
158 foreach ($this->_fields as $name => $field) {
159 if ((substr($name, 0, 6) == 'custom') && !empty($field['is_search_range'])) {
160 $from = CRM_Utils_Request::retrieve($name . '_from', 'String', $this);
161 $to = CRM_Utils_Request::retrieve($name . '_to', 'String', $this);
162 $value = array();
163 if ($from && $to) {
164 $value[$name . '_from'] = $from;
165 $value[$name . '_to'] = $to;
166 }
167 elseif ($from) {
168 $value[$name . '_from'] = $from;
169 }
170 elseif ($to) {
171 $value[$name . '_to'] = $to;
172 }
173 }
174 elseif ((substr($name, 0, 7) == 'custom_') &&
175 (CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField',
176 substr($name, 7), 'html_type'
177 ) == 'TextArea')
178 ) {
179 $value = trim(CRM_Utils_Request::retrieve($name, 'String',
180 $this, FALSE, NULL, 'REQUEST'
181 ));
182 if (!empty($value) &&
183 !((substr($value, 0, 1) == '%') &&
184 (substr($value, -1, 1) == '%')
185 )
186 ) {
187 $value = '%' . $value . '%';
188 }
189 }
190 elseif (CRM_Utils_Array::value('html_type', $field) == 'Multi-Select State/Province'
191 || CRM_Utils_Array::value('html_type', $field) == 'Multi-Select Country'
192 ) {
193 $value = CRM_Utils_Request::retrieve($name, 'String', $this, FALSE, NULL, 'REQUEST');
194 if (!is_array($value)) {
195 $value = explode(CRM_Core_DAO::VALUE_SEPARATOR, substr($value, 1, -1));
196 }
197 }
198 elseif ($name == 'contact_sub_type') {
199 $v = CRM_Utils_Request::retrieve($name, 'String', $this, FALSE, NULL, 'REQUEST');
200 if ($v && !is_array($v)) {
201 $v = explode(CRM_Core_DAO::VALUE_SEPARATOR, trim($v, CRM_Core_DAO::VALUE_SEPARATOR));
202 }
203 if (!empty($v)) {
204 foreach ($v as $item) {
205 $value[$item] = 1;
206 }
207 }
208 }
209 else {
210 $value = CRM_Utils_Request::retrieve($name, 'String',
211 $this, FALSE, NULL, 'REQUEST'
212 );
213 }
214
215 if (($name == 'group' || $name == 'tag') && !empty($value) && !is_array($value)) {
216 $v = explode(',', $value);
217 $value = array();
218 foreach ($v as $item) {
219 $value[$item] = 1;
220 }
221 }
222
223 $customField = CRM_Utils_Array::value($name, $this->_customFields);
224
225 if (!empty($_POST) && empty($_POST[$name])) {
226 if ($customField) {
227 // reset checkbox/radio because a form does not send null checkbox values
228 if (in_array($customField['html_type'],
229 array('Multi-Select', 'CheckBox', 'Multi-Select State/Province', 'Multi-Select Country', 'Radio', 'Select')
230 )) {
231 // only reset on a POST submission if we don't see any value
232 $value = NULL;
233 $this->set($name, $value);
234 }
235 }
236 elseif (in_array($name, $resetArray)) {
237 $value = NULL;
238 $this->set($name, $value);
239 }
240 }
241
242 if (isset($value) && $value != NULL) {
243 if (!is_array($value)) {
244 $value = trim($value);
245 }
246 $operator = CRM_Utils_Request::retrieve($name . '_operator', 'String', $this);
247 if ($operator) {
248 $this->_params[$name . '_operator'] = $operator;
249 }
250 if ((substr($name, 0, 6) == 'custom') && !empty($field['is_search_range'])) {
251 $this->_params += $value;
252 }
253 else {
254 $this->_params[$name] = $this->_fields[$name]['value'] = $value;
255 }
256 }
257 }
258
259 // set the prox params
260 // need to ensure proximity searching is enabled
261 $proximityVars = array(
262 'street_address',
263 'city',
264 'postal_code',
265 'state_province_id',
266 'country_id',
267 'distance',
268 'distance_unit',
269 );
270 foreach ($proximityVars as $var) {
271 $value = CRM_Utils_Request::retrieve("prox_{$var}",
272 'String',
273 $this, FALSE, NULL, 'REQUEST'
274 );
275 if ($value) {
276 $this->_params["prox_{$var}"] = $value;
277 }
278 }
279
280 // set the params in session
281 $session = CRM_Core_Session::singleton();
282 $session->set('profileParams', $this->_params);
283 }
284
285 /**
286 * Run this page (figure out the action needed and perform it).
287 *
288 */
289 public function run() {
290 $this->preProcess();
291
292 $this->assign('recentlyViewed', FALSE);
293 // override later (if possible):
294 $this->assign('ufGroupName', 'unknown');
295
296 if ($this->_gid) {
297 $ufgroupDAO = new CRM_Core_DAO_UFGroup();
298 $ufgroupDAO->id = $this->_gid;
299 if (!$ufgroupDAO->find(TRUE)) {
300 CRM_Core_Error::fatal();
301 }
302 }
303
304 if ($this->_gid) {
305 // set the title of the page
306 if ($ufgroupDAO->title) {
307 CRM_Utils_System::setTitle($ufgroupDAO->title);
308 }
309 if ($ufgroupDAO->name) {
310 $this->assign('ufGroupName', $ufgroupDAO->name);
311 }
312 }
313
314 $this->assign('isReset', TRUE);
315
316 $formController = new CRM_Core_Controller_Simple('CRM_Profile_Form_Search',
317 ts('Search Profile'),
318 CRM_Core_Action::ADD
319 );
320 $formController->setEmbedded(TRUE);
321 $formController->set('gid', $this->_gid);
322 $formController->process();
323
324 $searchError = FALSE;
325 // check if there is a POST
326 if (!empty($_POST)) {
327 if ($formController->validate() !== TRUE) {
328 $searchError = TRUE;
329 }
330 }
331
332 // also get the search tpl name
333 $this->assign('searchTPL', $formController->getHookedTemplateFileName());
334
335 $this->assign('search', $this->_search);
336
337 // search if search returned a form error?
338 if ((empty($_GET['reset']) || !empty($_GET['force'])) &&
339 !$searchError
340 ) {
341 $this->assign('isReset', FALSE);
342
343 $gidString = $this->_gid;
344 if (empty($this->_profileIds)) {
345 $gids = $this->_gid;
346 }
347 else {
348 $gids = $this->_profileIds;
349 $gidString = implode(',', $this->_profileIds);
350 }
351
352 $map = 0;
353 $linkToUF = 0;
354 $editLink = FALSE;
355 if ($this->_gid) {
356 $map = $ufgroupDAO->is_map;
357 $linkToUF = $ufgroupDAO->is_uf_link;
358 $editLink = $ufgroupDAO->is_edit_link;
359 }
360
361 if ($map) {
362 $this->assign('mapURL',
363 CRM_Utils_System::url('civicrm/profile/map',
364 "map=1&gid={$gidString}&reset=1"
365 )
366 );
367 }
368 if (!empty($this->_params['group'])) {
369 foreach ($this->_params['group'] as $key => $val) {
370 if (!$val) {
371 unset($this->_params['group'][$key]);
372 }
373 }
374 }
375
376 // the selector will override this if the user does have
377 // edit permissions as determined by the mask, CRM-4341
378 // do not allow edit for anon users in joomla frontend, CRM-4668
379 $config = CRM_Core_Config::singleton();
380 if (!CRM_Core_Permission::check('access CiviCRM') ||
381 $config->userFrameworkFrontend == 1
382 ) {
383 $editLink = FALSE;
384 }
385
386 $selector = new CRM_Profile_Selector_Listings($this->_params, $this->_customFields, $gids,
387 $map, $editLink, $linkToUF
388 );
389
390 $controller = new CRM_Core_Selector_Controller($selector,
391 $this->get(CRM_Utils_Pager::PAGE_ID),
392 $this->get(CRM_Utils_Sort::SORT_ID),
393 CRM_Core_Action::VIEW,
394 $this,
395 CRM_Core_Selector_Controller::TEMPLATE
396 );
397 $controller->setEmbedded(TRUE);
398 $controller->run();
399 }
400
401 //CRM-6862 -run form controller after
402 //selector, since it erase $_POST
403 $formController->run();
404
405 return parent::run();
406 }
407
408 /**
409 * Get the list of contacts for a profile.
410 *
411 * @param int $gid
412 *
413 * @return array
414 */
415 public static function getProfileContact($gid) {
416 $session = CRM_Core_Session::singleton();
417 $params = $session->get('profileParams');
418
419 $details = array();
420 $ufGroupParam = array('id' => $gid);
421 CRM_Core_BAO_UFGroup::retrieve($ufGroupParam, $details);
422
423 // make sure this group can be mapped
424 if (!$details['is_map']) {
425 CRM_Core_Error::statusBounce(ts('This profile does not have the map feature turned on.'));
426 }
427
428 $groupId = CRM_Utils_Array::value('limit_listings_group_id', $details);
429
430 // add group id to params if a uf group belong to a any group
431 if ($groupId) {
432 if (!empty($params['group'])) {
433 $params['group'][$groupId] = 1;
434 }
435 else {
436 $params['group'] = array($groupId => 1);
437 }
438 }
439
440 $fields = CRM_Core_BAO_UFGroup::getListingFields(
441 CRM_Core_Action::VIEW,
442 CRM_Core_BAO_UFGroup::PUBLIC_VISIBILITY | CRM_Core_BAO_UFGroup::LISTINGS_VISIBILITY,
443 FALSE,
444 $gid
445 );
446
447 $returnProperties = CRM_Contact_BAO_Contact::makeHierReturnProperties($fields);
448 $returnProperties['contact_type'] = 1;
449 $returnProperties['sort_name'] = 1;
450
451 $queryParams = CRM_Contact_BAO_Query::convertFormValues($params, 1);
452 $query = new CRM_Contact_BAO_Query($queryParams, $returnProperties, $fields);
453
454 $additionalWhereClause = 'contact_a.is_deleted = 0';
455
456 $ids = $query->searchQuery(0, 0, NULL,
457 FALSE, FALSE, FALSE,
458 TRUE, FALSE,
459 $additionalWhereClause
460 );
461
462 $contactIds = explode(',', $ids);
463
464 return $contactIds;
465 }
466
467 /**
468 * @param string $suffix
469 *
470 * @return null|string
471 */
472 public function checkTemplateFileExists($suffix = '') {
473 if ($this->_gid) {
474 $templateFile = "CRM/Profile/Page/{$this->_gid}/Listings.{$suffix}tpl";
475 $template = CRM_Core_Page::getTemplate();
476 if ($template->template_exists($templateFile)) {
477 return $templateFile;
478 }
479
480 // lets see if we have customized by name
481 $ufGroupName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $this->_gid, 'name');
482 if ($ufGroupName) {
483 $templateFile = "CRM/Profile/Page/{$ufGroupName}/Listings.{$suffix}tpl";
484 if ($template->template_exists($templateFile)) {
485 return $templateFile;
486 }
487 }
488 }
489 return NULL;
490 }
491
492 /**
493 * Use the form name to create the tpl file name.
494 *
495 * @return string
496 */
497 public function getTemplateFileName() {
498 $fileName = $this->checkTemplateFileExists();
499 return $fileName ? $fileName : parent::getTemplateFileName();
500 }
501
502 /**
503 * Default extra tpl file basically just replaces .tpl with .extra.tpl
504 * i.e. we dont override
505 *
506 * @return string
507 */
508 public function overrideExtraTemplateFileName() {
509 $fileName = $this->checkTemplateFileExists('extra.');
510 return $fileName ? $fileName : parent::overrideExtraTemplateFileName();
511 }
512
513 }