INFRA-132 - Remove extra newlines from the bottom of docblocks
[civicrm-core.git] / CRM / Profile / Page / Dynamic.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * Create a page for displaying CiviCRM Profile Fields.
38 *
39 * Heart of this class is the run method which checks
40 * for action type and then displays the appropriate
41 * page.
42 *
43 */
44 class CRM_Profile_Page_Dynamic extends CRM_Core_Page {
45
46 /**
47 * The contact id of the person we are viewing
48 *
49 * @var int
50 */
51 protected $_id;
52
53 /**
54 * The profile group are are interested in
55 *
56 * @var int
57 */
58 protected $_gid;
59
60 /**
61 * The profile types we restrict this page to display
62 *
63 * @var string
64 */
65 protected $_restrict;
66
67 /**
68 * Should we bypass permissions
69 *
70 * @var boolean
71 */
72 protected $_skipPermission;
73
74 /**
75 * Store profile ids if multiple profile ids are passed using comma separated.
76 * Currently lets implement this functionality only for dialog mode
77 */
78 protected $_profileIds = array();
79
80 /**
81 * Contact profile having activity fields?
82 *
83 * @var string
84 */
85 protected $_isContactActivityProfile = FALSE;
86
87 /**
88 * Activity Id connected to the profile
89 *
90 * @var string
91 */
92 protected $_activityId = NULL;
93
94 protected $_multiRecord = NULL;
95
96 protected $_recordId = NULL;
97
98 /*
99 * fetch multirecord as well as non-multirecord fields
100 */
101 protected $_allFields = NULL;
102
103 /**
104 * Class constructor
105 *
106 * @param int $id
107 * The contact id.
108 * @param int $gid
109 * The group id.
110 *
111 * @param $restrict
112 * @param bool $skipPermission
113 * @param null $profileIds
114 *
115 * @return \CRM_Profile_Page_Dynamic
116 */
117 public function __construct($id, $gid, $restrict, $skipPermission = FALSE, $profileIds = NULL) {
118 parent::__construct();
119
120 $this->_id = $id;
121 $this->_gid = $gid;
122 $this->_restrict = $restrict;
123 $this->_skipPermission = $skipPermission;
124
125 if (!array_key_exists('multiRecord', $_GET)) {
126 $this->set('multiRecord', NULL);
127 }
128 if (!array_key_exists('recordId', $_GET)) {
129 $this->set('recordId', NULL);
130 }
131 if (!array_key_exists('allFields', $_GET)) {
132 $this->set('allFields', NULL);
133 }
134
135 //specifies the action being done on a multi record field
136 $multiRecordAction = CRM_Utils_Request::retrieve('multiRecord', 'String', $this);
137
138 $this->_multiRecord = (!is_numeric($multiRecordAction)) ? CRM_Core_Action::resolve($multiRecordAction) : $multiRecordAction;
139 if ($this->_multiRecord) {
140 $this->set('multiRecord', $this->_multiRecord);
141 }
142
143 if ($this->_multiRecord & CRM_Core_Action::VIEW) {
144 $this->_recordId = CRM_Utils_Request::retrieve('recordId', 'Positive', $this);
145 $this->_allFields = CRM_Utils_Request::retrieve('allFields', 'Integer', $this);
146 }
147
148 if ($profileIds) {
149 $this->_profileIds = $profileIds;
150 }
151 else {
152 $this->_profileIds = array($gid);
153 }
154
155 $this->_activityId = CRM_Utils_Request::retrieve('aid', 'Positive', $this, FALSE, 0, 'GET');
156 if (is_numeric($this->_activityId)) {
157 $latestRevisionId = CRM_Activity_BAO_Activity::getLatestActivityId($this->_activityId);
158 if ($latestRevisionId) {
159 $this->_activityId = $latestRevisionId;
160 }
161 }
162 $this->_isContactActivityProfile = CRM_Core_BAO_UFField::checkContactActivityProfileType($this->_gid);
163 }
164
165 /**
166 * Get the action links for this page.
167 *
168 * @return array
169 */
170 public function &actionLinks() {
171 return NULL;
172 }
173
174 /**
175 * Run the page.
176 *
177 * This method is called after the page is created. It checks for the
178 * type of action and executes that action.
179 *
180 * @return void
181 */
182 public function run() {
183 $template = CRM_Core_Smarty::singleton();
184 if ($this->_id && $this->_gid) {
185
186 // first check that id is part of the limit group id, CRM-4822
187 $limitListingsGroupsID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup',
188 $this->_gid,
189 'limit_listings_group_id'
190 );
191 $config = CRM_Core_Config::singleton();
192 if ($limitListingsGroupsID) {
193
194 if (!CRM_Contact_BAO_GroupContact::isContactInGroup($this->_id,
195 $limitListingsGroupsID
196 )) {
197 CRM_Utils_System::setTitle(ts('Profile View - Permission Denied'));
198 return CRM_Core_Session::setStatus(ts('You do not have permission to view this contact record. Contact the site administrator if you need assistance.'), ts('Permission Denied'), 'error');
199 }
200 }
201
202 $session = CRM_Core_Session::singleton();
203 $userID = $session->get('userID');
204
205 $this->_isPermissionedChecksum = FALSE;
206 $permissionType = CRM_Core_Permission::VIEW;
207 if ($this->_id != $userID) {
208 // do not allow edit for anon users in joomla frontend, CRM-4668, unless u have checksum CRM-5228
209 if ($config->userFrameworkFrontend) {
210 $this->_isPermissionedChecksum = CRM_Contact_BAO_Contact_Permission::validateOnlyChecksum($this->_id, $this, FALSE);
211 }
212 else {
213 $this->_isPermissionedChecksum = CRM_Contact_BAO_Contact_Permission::validateChecksumContact($this->_id, $this, FALSE);
214 }
215 }
216 // CRM-10853
217 // Users with create or edit permission should be allowed to view their own profile
218 if ($this->_id == $userID || $this->_isPermissionedChecksum) {
219 if (!CRM_Core_Permission::check('profile view')) {
220 if (CRM_Core_Permission::check('profile create') || CRM_Core_Permission::check('profile edit')) {
221 $this->_skipPermission = TRUE;
222 }
223 }
224 }
225
226 // make sure we dont expose all fields based on permission
227 $admin = FALSE;
228 if ((!$config->userFrameworkFrontend &&
229 (CRM_Core_Permission::check('administer users') ||
230 CRM_Core_Permission::check('view all contacts') ||
231 CRM_Contact_BAO_Contact_Permission::allow($this->_id)
232 )
233 ) ||
234 $this->_id == $userID ||
235 $this->_isPermissionedChecksum
236 ) {
237 $admin = TRUE;
238 }
239
240 $values = array();
241 $fields = CRM_Core_BAO_UFGroup::getFields($this->_profileIds, FALSE, CRM_Core_Action::VIEW,
242 NULL, NULL, FALSE, $this->_restrict,
243 $this->_skipPermission, NULL,
244 $permissionType
245 );
246
247 if ($this->_multiRecord & CRM_Core_Action::VIEW && $this->_recordId && !$this->_allFields) {
248 CRM_Core_BAO_UFGroup::shiftMultiRecordFields($fields, $multiRecordFields);
249 $fields = $multiRecordFields;
250 }
251 if ($this->_isContactActivityProfile && $this->_gid) {
252 $errors = CRM_Profile_Form::validateContactActivityProfile($this->_activityId, $this->_id, $this->_gid);
253 if (!empty($errors)) {
254 CRM_Core_Error::fatal(array_pop($errors));
255 }
256 }
257
258 //reformat fields array
259 foreach ($fields as $name => $field) {
260 // also eliminate all formatting fields
261 if (CRM_Utils_Array::value('field_type', $field) == 'Formatting') {
262 unset($fields[$name]);
263 }
264
265 // make sure that there is enough permission to expose this field
266 if (!$admin && $field['visibility'] == 'User and User Admin Only') {
267 unset($fields[$name]);
268 }
269 }
270
271 if ($this->_isContactActivityProfile) {
272 $contactFields = $activityFields = array();
273
274 foreach ($fields as $fieldName => $field) {
275 if (CRM_Utils_Array::value('field_type', $field) == 'Activity') {
276 $activityFields[$fieldName] = $field;
277 }
278 else {
279 $contactFields[$fieldName] = $field;
280 }
281 }
282
283 CRM_Core_BAO_UFGroup::getValues($this->_id, $contactFields, $values);
284 if ($this->_activityId) {
285 CRM_Core_BAO_UFGroup::getValues(
286 NULL,
287 $activityFields,
288 $values,
289 TRUE,
290 array(array('activity_id', '=', $this->_activityId, 0, 0))
291 );
292 }
293 }
294 else {
295 $customWhereClause = NULL;
296 if ($this->_multiRecord & CRM_Core_Action::VIEW && $this->_recordId) {
297 if ($this->_allFields) {
298 $copyFields = $fields;
299 CRM_Core_BAO_UFGroup::shiftMultiRecordFields($copyFields, $multiRecordFields);
300 $fieldKey = key($multiRecordFields);
301 }
302 else {
303 $fieldKey = key($fields);
304 }
305 if ($fieldID = CRM_Core_BAO_CustomField::getKeyID($fieldKey)) {
306 $tableColumnGroup = CRM_Core_BAO_CustomField::getTableColumnGroup($fieldID);
307 $columnName = "{$tableColumnGroup[0]}.id";
308 $customWhereClause = $columnName . ' = ' . $this->_recordId;
309 }
310 }
311 CRM_Core_BAO_UFGroup::getValues($this->_id, $fields, $values, TRUE, NULL, FALSE, $customWhereClause);
312 }
313
314 // $profileFields array can be used for customized display of field labels and values in Profile/View.tpl
315 $profileFields = array();
316 $labels = array();
317
318 foreach ($fields as $name => $field) {
319 //CRM-14338
320 // Create a unique, non-empty index for each field.
321 $index = $field['title'];
322 if ($index === '') {
323 $index = ' ';
324 }
325 while (array_key_exists($index, $labels)) {
326 $index .= ' ';
327 }
328
329 $labels[$index] = preg_replace('/\s+|\W+/', '_', $name);
330 }
331
332 foreach ($values as $title => $value) {
333 $profileFields[$labels[$title]] = array(
334 'label' => $title,
335 'value' => $value,
336 );
337 }
338
339 $template->assign_by_ref('row', $values);
340 $template->assign_by_ref('profileFields', $profileFields);
341 }
342
343 $name = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $this->_gid, 'name');
344 $this->assign('ufGroupName', $name);
345 CRM_Utils_Hook::viewProfile($name);
346
347 if (strtolower($name) == 'summary_overlay') {
348 $template->assign('overlayProfile', TRUE);
349 }
350
351 if (($this->_multiRecord & CRM_Core_Action::VIEW) && $this->_recordId && !$this->_allFields) {
352 $fieldDetail = reset($fields);
353 $fieldId = CRM_Core_BAO_CustomField::getKeyID($fieldDetail['name']);
354 $customGroupDetails = CRM_Core_BAO_CustomGroup::getGroupTitles(array($fieldId));
355 $multiRecTitle = $customGroupDetails[$fieldId]['groupTitle'];
356 }
357 else {
358 $title = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $this->_gid, 'title');
359 }
360
361 //CRM-4131.
362 $displayName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_id, 'display_name');
363 if ($displayName) {
364 $session = CRM_Core_Session::singleton();
365 $config = CRM_Core_Config::singleton();
366 if ($session->get('userID') &&
367 CRM_Core_Permission::check('access CiviCRM') &&
368 CRM_Contact_BAO_Contact_Permission::allow($session->get('userID'), CRM_Core_Permission::VIEW) &&
369 !$config->userFrameworkFrontend
370 ) {
371 $contactViewUrl = CRM_Utils_System::url('civicrm/contact/view', "action=view&reset=1&cid={$this->_id}", TRUE);
372 $this->assign('displayName', $displayName);
373 $displayName = "<a href=\"$contactViewUrl\">{$displayName}</a>";
374 }
375 $title .= ' - ' . $displayName;
376 }
377
378 $title = isset($multiRecTitle) ? ts('View %1 Record', array(1 => $multiRecTitle)) : $title;
379 CRM_Utils_System::setTitle($title);
380
381 // invoke the pagRun hook, CRM-3906
382 CRM_Utils_Hook::pageRun($this);
383
384 return trim($template->fetch($this->getHookedTemplateFileName()));
385 }
386
387 /**
388 * @param string $suffix
389 *
390 * @return null|string
391 */
392 public function checkTemplateFileExists($suffix = '') {
393 if ($this->_gid) {
394 $templateFile = "CRM/Profile/Page/{$this->_gid}/Dynamic.{$suffix}tpl";
395 $template = CRM_Core_Page::getTemplate();
396 if ($template->template_exists($templateFile)) {
397 return $templateFile;
398 }
399
400 // lets see if we have customized by name
401 $ufGroupName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $this->_gid, 'name');
402 if ($ufGroupName) {
403 $templateFile = "CRM/Profile/Page/{$ufGroupName}/Dynamic.{$suffix}tpl";
404 if ($template->template_exists($templateFile)) {
405 return $templateFile;
406 }
407 }
408 }
409 return NULL;
410 }
411
412 /**
413 * Use the form name to create the tpl file name
414 *
415 * @return string
416 */
417 /**
418 * @return string
419 */
420 public function getTemplateFileName() {
421 $fileName = $this->checkTemplateFileExists();
422 return $fileName ? $fileName : parent::getTemplateFileName();
423 }
424
425 /**
426 * Default extra tpl file basically just replaces .tpl with .extra.tpl
427 * i.e. we dont override
428 *
429 * @return string
430 */
431 /**
432 * @return string
433 */
434 public function overrideExtraTemplateFileName() {
435 $fileName = $this->checkTemplateFileExists('extra.');
436 return $fileName ? $fileName : parent::overrideExtraTemplateFileName();
437 }
438 }