Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
0f03f337 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
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 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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
0f03f337 | 31 | * @copyright CiviCRM LLC (c) 2004-2017 |
6a488035 TO |
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 | |
abdd5e3e | 38 | * the admin. |
6a488035 TO |
39 | */ |
40 | class CRM_Profile_Page_Listings extends CRM_Core_Page { | |
41 | ||
42 | /** | |
abdd5e3e | 43 | * All the fields that are listings related. |
6a488035 TO |
44 | * |
45 | * @var array | |
6a488035 TO |
46 | */ |
47 | protected $_fields; | |
48 | ||
49 | /** | |
abdd5e3e | 50 | * The custom fields for this domain. |
6a488035 TO |
51 | * |
52 | * @var array | |
6a488035 TO |
53 | */ |
54 | protected $_customFields; | |
55 | ||
56 | /** | |
abdd5e3e | 57 | * The input params from the request. |
6a488035 TO |
58 | * |
59 | * @var array | |
6a488035 TO |
60 | */ |
61 | protected $_params; | |
62 | ||
63 | /** | |
abdd5e3e | 64 | * The group id that we are editing. |
6a488035 TO |
65 | * |
66 | * @var int | |
67 | */ | |
68 | protected $_gid; | |
69 | ||
70 | /** | |
abdd5e3e | 71 | * State whether to display search form or not. |
6a488035 TO |
72 | * |
73 | * @var int | |
74 | */ | |
75 | protected $_search; | |
76 | ||
77 | /** | |
abdd5e3e | 78 | * Should we display a map. |
6a488035 TO |
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 | /** | |
100fef9d | 91 | * Extracts the parameters from the request and constructs information for |
6a488035 TO |
92 | * the selector object to do a query |
93 | * | |
6a488035 | 94 | */ |
00be9182 | 95 | public function preProcess() { |
6a488035 TO |
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( | |
353ffa53 TO |
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', | |
6a488035 TO |
156 | ); |
157 | ||
158 | foreach ($this->_fields as $name => $field) { | |
8cc574cf | 159 | if ((substr($name, 0, 6) == 'custom') && !empty($field['is_search_range'])) { |
3130209f CW |
160 | $from = CRM_Utils_Request::retrieve($name . '_from', 'String', $this); |
161 | $to = CRM_Utils_Request::retrieve($name . '_to', 'String', $this); | |
6a488035 TO |
162 | $value = array(); |
163 | if ($from && $to) { | |
2c30b3f4 | 164 | $value[$name . '_from'] = $from; |
165 | $value[$name . '_to'] = $to; | |
6a488035 TO |
166 | } |
167 | elseif ($from) { | |
2c30b3f4 | 168 | $value[$name . '_from'] = $from; |
6a488035 TO |
169 | } |
170 | elseif ($to) { | |
2c30b3f4 | 171 | $value[$name . '_to'] = $to; |
6a488035 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', | |
353ffa53 TO |
180 | $this, FALSE, NULL, 'REQUEST' |
181 | )); | |
6a488035 TO |
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 | ||
8cc574cf | 225 | if (!empty($_POST) && empty($_POST[$name])) { |
6a488035 TO |
226 | if ($customField) { |
227 | // reset checkbox/radio because a form does not send null checkbox values | |
228 | if (in_array($customField['html_type'], | |
e3467d2e | 229 | array('Multi-Select', 'CheckBox', 'Multi-Select State/Province', 'Multi-Select Country', 'Radio', 'Select') |
353ffa53 | 230 | )) { |
abdd5e3e | 231 | // only reset on a POST submission if we don't see any value |
6a488035 TO |
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 | } | |
3130209f CW |
246 | $operator = CRM_Utils_Request::retrieve($name . '_operator', 'String', $this); |
247 | if ($operator) { | |
248 | $this->_params[$name . '_operator'] = $operator; | |
249 | } | |
2c30b3f4 | 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 | } | |
6a488035 TO |
256 | } |
257 | } | |
258 | ||
259 | // set the prox params | |
260 | // need to ensure proximity searching is enabled | |
261 | $proximityVars = array( | |
353ffa53 TO |
262 | 'street_address', |
263 | 'city', | |
264 | 'postal_code', | |
265 | 'state_province_id', | |
266 | 'country_id', | |
267 | 'distance', | |
268 | 'distance_unit', | |
6a488035 TO |
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 | ||
6a488035 TO |
280 | // set the params in session |
281 | $session = CRM_Core_Session::singleton(); | |
282 | $session->set('profileParams', $this->_params); | |
283 | } | |
284 | ||
285 | /** | |
100fef9d | 286 | * Run this page (figure out the action needed and perform it). |
6a488035 | 287 | * |
6a488035 | 288 | */ |
00be9182 | 289 | public function run() { |
6a488035 TO |
290 | $this->preProcess(); |
291 | ||
292 | $this->assign('recentlyViewed', FALSE); | |
c5c263ca AH |
293 | // override later (if possible): |
294 | $this->assign('ufGroupName', 'unknown'); | |
6a488035 TO |
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 | } | |
46312a4d TO |
309 | if ($ufgroupDAO->name) { |
310 | $this->assign('ufGroupName', $ufgroupDAO->name); | |
311 | } | |
6a488035 TO |
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 | |
8aac22c8 | 333 | $this->assign('searchTPL', $formController->getHookedTemplateFileName()); |
6a488035 TO |
334 | |
335 | $this->assign('search', $this->_search); | |
336 | ||
337 | // search if search returned a form error? | |
8cc574cf | 338 | if ((empty($_GET['reset']) || !empty($_GET['force'])) && |
6a488035 TO |
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 | ||
353ffa53 | 352 | $map = 0; |
6a488035 TO |
353 | $linkToUF = 0; |
354 | $editLink = FALSE; | |
355 | if ($this->_gid) { | |
353ffa53 | 356 | $map = $ufgroupDAO->is_map; |
6a488035 TO |
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 | } | |
a7488080 | 368 | if (!empty($this->_params['group'])) { |
6a488035 TO |
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 | ||
abdd5e3e | 401 | //CRM-6862 -run form controller after |
6a488035 TO |
402 | //selector, since it erase $_POST |
403 | $formController->run(); | |
404 | ||
405 | return parent::run(); | |
406 | } | |
407 | ||
408 | /** | |
fe482240 | 409 | * Get the list of contacts for a profile. |
6a488035 | 410 | * |
100fef9d | 411 | * @param int $gid |
77b97be7 EM |
412 | * |
413 | * @return array | |
6a488035 | 414 | */ |
c3c3e099 | 415 | public static function getProfileContact($gid) { |
6a488035 TO |
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) { | |
a7488080 | 432 | if (!empty($params['group'])) { |
6a488035 TO |
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 | ||
ffd93213 EM |
467 | /** |
468 | * @param string $suffix | |
469 | * | |
470 | * @return null|string | |
471 | */ | |
00be9182 | 472 | public function checkTemplateFileExists($suffix = '') { |
6a488035 TO |
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 | ||
ffd93213 | 492 | /** |
fe482240 | 493 | * Use the form name to create the tpl file name. |
ffd93213 EM |
494 | * |
495 | * @return string | |
ffd93213 | 496 | */ |
00be9182 | 497 | public function getTemplateFileName() { |
6a488035 TO |
498 | $fileName = $this->checkTemplateFileExists(); |
499 | return $fileName ? $fileName : parent::getTemplateFileName(); | |
500 | } | |
501 | ||
ffd93213 EM |
502 | /** |
503 | * Default extra tpl file basically just replaces .tpl with .extra.tpl | |
504 | * i.e. we dont override | |
505 | * | |
506 | * @return string | |
ffd93213 | 507 | */ |
00be9182 | 508 | public function overrideExtraTemplateFileName() { |
6a488035 TO |
509 | $fileName = $this->checkTemplateFileExists('extra.'); |
510 | return $fileName ? $fileName : parent::overrideExtraTemplateFileName(); | |
511 | } | |
96025800 | 512 | |
6a488035 | 513 | } |