4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.5 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2014
38 * This implements the profile page for all contacts. It uses a selector
39 * object to do the actual dispay. The fields displayd are controlled by
42 class CRM_Profile_Page_Listings
extends CRM_Core_Page
{
45 * all the fields that are listings related
53 * the custom fields for this domain
58 protected $_customFields;
61 * The input params from the request
69 * The group id that we are editing
76 * state whether to display search form or not
83 * Should we display a map
90 * Store profile ids if multiple profile ids are passed using comma separated.
91 * Currently lets implement this functionality only for dialog mode
93 protected $_profileIds = array();
96 * extracts the parameters from the request and constructs information for
97 * the selector object to do a query
103 function preProcess() {
105 $this->_search
= TRUE;
107 $search = CRM_Utils_Request
::retrieve('search', 'Boolean', $this, FALSE, 0, 'GET');
108 if (isset($search) && $search == 0) {
109 $this->_search
= FALSE;
112 $this->_gid
= $this->get('gid');
113 $this->_profileIds
= $this->get('profileIds');
115 $gids = explode(',', CRM_Utils_Request
::retrieve('gid', 'String', CRM_Core_DAO
::$_nullObject, FALSE, 0, 'GET'));
117 if ((count($gids) > 1) && !$this->_profileIds
&& empty($this->_profileIds
)) {
119 foreach ($gids as $pfId) {
120 $this->_profileIds
[] = CRM_Utils_Type
::escape($pfId, 'Positive');
124 // check if we are rendering mixed profiles
125 if (CRM_Core_BAO_UFGroup
::checkForMixProfiles($this->_profileIds
)) {
126 CRM_Core_Error
::fatal(ts('You cannot combine profiles of multiple types.'));
129 $this->_gid
= $this->_profileIds
[0];
130 $this->set('profileIds', $this->_profileIds
);
131 $this->set('gid', $this->_gid
);
135 $this->_gid
= CRM_Utils_Request
::retrieve('gid', 'Positive', $this, FALSE, 0, 'GET');
138 if (empty($this->_profileIds
)) {
142 $gids = $this->_profileIds
;
145 $this->_fields
= CRM_Core_BAO_UFGroup
::getListingFields(CRM_Core_Action
::UPDATE
,
146 CRM_Core_BAO_UFGroup
::PUBLIC_VISIBILITY | CRM_Core_BAO_UFGroup
::LISTINGS_VISIBILITY
,
147 FALSE, $gids, FALSE, 'Profile',
148 CRM_Core_Permission
::SEARCH
151 $this->_customFields
= CRM_Core_BAO_CustomField
::getFieldsForImport(NULL, FALSE, FALSE, FALSE, TRUE, TRUE);
152 $this->_params
= array();
155 'group', 'tag', 'preferred_communication_method', 'do_not_phone',
156 'do_not_email', 'do_not_mail', 'do_not_sms', 'do_not_trade', 'gender',
159 foreach ($this->_fields
as $name => $field) {
160 if ((substr($name, 0, 6) == 'custom') && !empty($field['is_search_range'])) {
161 $from = CRM_Utils_Request
::retrieve($name . '_from', 'String',
162 $this, FALSE, NULL, 'REQUEST'
164 $to = CRM_Utils_Request
::retrieve($name . '_to', 'String',
165 $this, FALSE, NULL, 'REQUEST'
169 $value['from'] = $from;
173 $value['from'] = $from;
179 elseif ((substr($name, 0, 7) == 'custom_') &&
180 (CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomField',
181 substr($name, 7), 'html_type'
184 $value = trim(CRM_Utils_Request
::retrieve($name, 'String',
185 $this, FALSE, NULL, 'REQUEST'
187 if (!empty($value) &&
188 !((substr($value, 0, 1) == '%') &&
189 (substr($value, -1, 1) == '%')
192 $value = '%' . $value . '%';
195 elseif (CRM_Utils_Array
::value('html_type', $field) == 'Multi-Select State/Province'
196 || CRM_Utils_Array
::value('html_type', $field) == 'Multi-Select Country'
198 $value = CRM_Utils_Request
::retrieve($name, 'String', $this, FALSE, NULL, 'REQUEST');
199 if (!is_array($value)) {
200 $value = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, substr($value, 1, -1));
203 elseif ($name == 'contact_sub_type') {
204 $v = CRM_Utils_Request
::retrieve($name, 'String', $this, FALSE, NULL, 'REQUEST');
205 if ($v && !is_array($v)) {
206 $v = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, trim($v, CRM_Core_DAO
::VALUE_SEPARATOR
));
209 foreach ($v as $item) {
215 $value = CRM_Utils_Request
::retrieve($name, 'String',
216 $this, FALSE, NULL, 'REQUEST'
220 if (($name == 'group' ||
$name == 'tag') && !empty($value) && !is_array($value)) {
221 $v = explode(',', $value);
223 foreach ($v as $item) {
228 $customField = CRM_Utils_Array
::value($name, $this->_customFields
);
230 if (!empty($_POST) && empty($_POST[$name])) {
232 // reset checkbox/radio because a form does not send null checkbox values
233 if (in_array($customField['html_type'],
234 array('Multi-Select', 'CheckBox', 'Multi-Select State/Province', 'Multi-Select Country', 'Radio')
236 // only reset on a POST submission if we dont see any value
238 $this->set($name, $value);
241 elseif (in_array($name, $resetArray)) {
243 $this->set($name, $value);
247 if (isset($value) && $value != NULL) {
248 if (!is_array($value)) {
249 $value = trim($value);
251 $this->_params
[$name] = $this->_fields
[$name]['value'] = $value;
255 // set the prox params
256 // need to ensure proximity searching is enabled
257 $proximityVars = array(
258 'street_address', 'city', 'postal_code', 'state_province_id',
259 'country_id', 'distance', 'distance_unit',
261 foreach ($proximityVars as $var) {
262 $value = CRM_Utils_Request
::retrieve("prox_{$var}",
264 $this, FALSE, NULL, 'REQUEST'
267 $this->_params
["prox_{$var}"] = $value;
272 // set the params in session
273 $session = CRM_Core_Session
::singleton();
274 $session->set('profileParams', $this->_params
);
278 * run this page (figure out the action needed and perform it).
285 $this->assign('recentlyViewed', FALSE);
286 $this->assign('ufGroupName', 'unknown'); // override later (if possible)
289 $ufgroupDAO = new CRM_Core_DAO_UFGroup();
290 $ufgroupDAO->id
= $this->_gid
;
291 if (!$ufgroupDAO->find(TRUE)) {
292 CRM_Core_Error
::fatal();
297 // set the title of the page
298 if ($ufgroupDAO->title
) {
299 CRM_Utils_System
::setTitle($ufgroupDAO->title
);
301 if ($ufgroupDAO->name
) {
302 $this->assign('ufGroupName', $ufgroupDAO->name
);
306 $this->assign('isReset', TRUE);
308 $formController = new CRM_Core_Controller_Simple('CRM_Profile_Form_Search',
309 ts('Search Profile'),
312 $formController->setEmbedded(TRUE);
313 $formController->set('gid', $this->_gid
);
314 $formController->process();
316 $searchError = FALSE;
317 // check if there is a POST
318 if (!empty($_POST)) {
319 if ($formController->validate() !== TRUE) {
324 // also get the search tpl name
325 $this->assign('searchTPL', $formController->getHookedTemplateFileName());
327 $this->assign('search', $this->_search
);
329 // search if search returned a form error?
330 if ((empty($_GET['reset']) ||
!empty($_GET['force'])) &&
333 $this->assign('isReset', FALSE);
335 $gidString = $this->_gid
;
336 if (empty($this->_profileIds
)) {
340 $gids = $this->_profileIds
;
341 $gidString = implode(',', $this->_profileIds
);
348 $map = $ufgroupDAO->is_map
;
349 $linkToUF = $ufgroupDAO->is_uf_link
;
350 $editLink = $ufgroupDAO->is_edit_link
;
354 $this->assign('mapURL',
355 CRM_Utils_System
::url('civicrm/profile/map',
356 "map=1&gid={$gidString}&reset=1"
360 if (!empty($this->_params
['group'])) {
361 foreach ($this->_params
['group'] as $key => $val) {
363 unset($this->_params
['group'][$key]);
368 // the selector will override this if the user does have
369 // edit permissions as determined by the mask, CRM-4341
370 // do not allow edit for anon users in joomla frontend, CRM-4668
371 $config = CRM_Core_Config
::singleton();
372 if (!CRM_Core_Permission
::check('access CiviCRM') ||
373 $config->userFrameworkFrontend
== 1
378 $selector = new CRM_Profile_Selector_Listings($this->_params
, $this->_customFields
, $gids,
379 $map, $editLink, $linkToUF
382 $controller = new CRM_Core_Selector_Controller($selector,
383 $this->get(CRM_Utils_Pager
::PAGE_ID
),
384 $this->get(CRM_Utils_Sort
::SORT_ID
),
385 CRM_Core_Action
::VIEW
,
387 CRM_Core_Selector_Controller
::TEMPLATE
389 $controller->setEmbedded(TRUE);
393 //CRM-6862 -run form cotroller after
394 //selector, since it erase $_POST
395 $formController->run();
397 return parent
::run();
401 * Function to get the list of contacts for a profile
406 * @internal param object $form
410 function getProfileContact($gid) {
411 $session = CRM_Core_Session
::singleton();
412 $params = $session->get('profileParams');
415 $ufGroupParam = array('id' => $gid);
416 CRM_Core_BAO_UFGroup
::retrieve($ufGroupParam, $details);
418 // make sure this group can be mapped
419 if (!$details['is_map']) {
420 CRM_Core_Error
::statusBounce(ts('This profile does not have the map feature turned on.'));
423 $groupId = CRM_Utils_Array
::value('limit_listings_group_id', $details);
425 // add group id to params if a uf group belong to a any group
427 if (!empty($params['group'])) {
428 $params['group'][$groupId] = 1;
431 $params['group'] = array($groupId => 1);
435 $fields = CRM_Core_BAO_UFGroup
::getListingFields(
436 CRM_Core_Action
::VIEW
,
437 CRM_Core_BAO_UFGroup
::PUBLIC_VISIBILITY | CRM_Core_BAO_UFGroup
::LISTINGS_VISIBILITY
,
442 $returnProperties = CRM_Contact_BAO_Contact
::makeHierReturnProperties($fields);
443 $returnProperties['contact_type'] = 1;
444 $returnProperties['sort_name'] = 1;
446 $queryParams = CRM_Contact_BAO_Query
::convertFormValues($params, 1);
447 $query = new CRM_Contact_BAO_Query($queryParams, $returnProperties, $fields);
449 $additionalWhereClause = 'contact_a.is_deleted = 0';
451 $ids = $query->searchQuery(0, 0, NULL,
454 $additionalWhereClause
457 $contactIds = explode(',', $ids);
463 * @param string $suffix
465 * @return null|string
467 function checkTemplateFileExists($suffix = '') {
469 $templateFile = "CRM/Profile/Page/{$this->_gid}/Listings.{$suffix}tpl";
470 $template = CRM_Core_Page
::getTemplate();
471 if ($template->template_exists($templateFile)) {
472 return $templateFile;
475 // lets see if we have customized by name
476 $ufGroupName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', $this->_gid
, 'name');
478 $templateFile = "CRM/Profile/Page/{$ufGroupName}/Listings.{$suffix}tpl";
479 if ($template->template_exists($templateFile)) {
480 return $templateFile;
488 * Use the form name to create the tpl file name
496 function getTemplateFileName() {
497 $fileName = $this->checkTemplateFileExists();
498 return $fileName ?
$fileName : parent
::getTemplateFileName();
502 * Default extra tpl file basically just replaces .tpl with .extra.tpl
503 * i.e. we dont override
511 function overrideExtraTemplateFileName() {
512 $fileName = $this->checkTemplateFileExists('extra.');
513 return $fileName ?
$fileName : parent
::overrideExtraTemplateFileName();