Merge pull request #21566 from eileenmcnaughton/render
[civicrm-core.git] / CRM / Profile / Page / View.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
19 * Main page for viewing contact.
20 *
21 */
22class CRM_Profile_Page_View extends CRM_Core_Page {
23
24 /**
fe482240 25 * The id of the contact.
6a488035
TO
26 *
27 * @var int
28 */
29 protected $_id;
30
31 /**
fe482240 32 * The group id that we are editing.
6a488035
TO
33 *
34 * @var int
35 */
36 protected $_gid;
37
b7edabe8 38 /**
39 * Should the primary email be converted into a link, if emailabe.
40 *
41 * @var bool
42 */
43 protected $isShowEmailTaskLink = FALSE;
44
6a488035
TO
45 /**
46 * Heart of the viewing process. The runner gets all the meta data for
47 * the contact and calls the appropriate type of page to view.
48 *
f9d8a5d1 49 */
00be9182 50 public function preProcess() {
6a488035
TO
51 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive',
52 $this, FALSE
53 );
b7edabe8 54 $this->isShowEmailTaskLink = CRM_Utils_Request::retrieve('is_show_email_task', 'Positive', $this);
6a488035
TO
55 if (!$this->_id) {
56 $session = CRM_Core_Session::singleton();
57 $this->_id = $session->get('userID');
58 if (!$this->_id) {
79e11805 59 CRM_Core_Error::statusBounce(ts('Could not find the required contact id parameter (id=) for viewing a contact record with a Profile.'));
6a488035
TO
60 }
61 }
62 $this->assign('cid', $this->_id);
63
64 $gids = explode(',', CRM_Utils_Request::retrieve('gid', 'String', CRM_Core_DAO::$_nullObject, FALSE, 0, 'GET'));
65
be2fb01f 66 $profileIds = [];
6a488035
TO
67 if (count($gids) > 1) {
68 if (!empty($gids)) {
69 foreach ($gids as $pfId) {
70 $profileIds[] = CRM_Utils_Type::escape($pfId, 'Positive');
71 }
72 }
73
74 // check if we are rendering mixed profiles
75 if (CRM_Core_BAO_UFGroup::checkForMixProfiles($profileIds)) {
79e11805 76 CRM_Core_Error::statusBounce(ts('You cannot combine profiles of multiple types.'));
6a488035
TO
77 }
78
79 $this->_gid = $profileIds[0];
80 }
81
82 if (!$this->_gid) {
83 $this->_gid = CRM_Utils_Request::retrieve('gid', 'Positive', $this, FALSE, 0, 'GET');
84 }
85
86 $anyContent = TRUE;
87 if ($this->_gid) {
b7edabe8 88 $page = new CRM_Profile_Page_Dynamic($this->_id, $this->_gid, 'Profile', FALSE, $profileIds, $this->isShowEmailTaskLink);
be2fb01f 89 $profileGroup = [];
6a488035
TO
90 $profileGroup['title'] = NULL;
91 $profileGroup['content'] = $page->run();
92 if (empty($profileGroup['content'])) {
93 $anyContent = FALSE;
94 }
95 $profileGroups[] = $profileGroup;
96
97 $gidString = $this->_gid;
98 if (!empty($profileIds)) {
99 $gidString = implode(',', $profileIds);
100 }
101
102 $map = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $this->_gid, 'is_map');
103 if ($map) {
104 $this->assign('mapURL',
105 CRM_Utils_System::url("civicrm/profile/map",
106 "reset=1&pv=1&cid={$this->_id}&gid={$gidString}"
107 )
108 );
109 }
110 if (CRM_Core_Permission::ufGroupValid($this->_gid,
353ffa53
TO
111 CRM_Core_Permission::SEARCH
112 )
113 ) {
6a488035
TO
114 $this->assign('listingURL',
115 CRM_Utils_System::url("civicrm/profile",
116 "force=1&gid={$gidString}"
117 )
118 );
119 }
120 }
121 else {
122 $ufGroups = CRM_Core_BAO_UFGroup::getModuleUFGroup('Profile');
123
be2fb01f 124 $profileGroups = [];
6a488035
TO
125 foreach ($ufGroups as $groupid => $group) {
126 $page = new CRM_Profile_Page_Dynamic($this->_id, $groupid, 'Profile', FALSE, $profileIds);
be2fb01f 127 $profileGroup = [];
6a488035
TO
128 $profileGroup['title'] = $group['title'];
129 $profileGroup['content'] = $page->run();
130 if (empty($profileGroup['content'])) {
131 $anyContent = FALSE;
132 }
133 $profileGroups[] = $profileGroup;
134 }
135 $this->assign('listingURL',
136 CRM_Utils_System::url("civicrm/profile",
137 "force=1"
138 )
139 );
140 }
141
142 $this->assign('groupID', $this->_gid);
143
144 $this->assign('profileGroups', $profileGroups);
145 $this->assign('recentlyViewed', FALSE);
146
147 // do not set title if there is no content
148 // CRM-6081
149 if (!$anyContent) {
150 CRM_Utils_System::setTitle('');
151 }
152 }
153
154 /**
abdd5e3e 155 * Build the outcome basing on the CRM_Profile_Page_Dynamic's HTML.
6a488035 156 *
6a488035 157 */
00be9182 158 public function run() {
6a488035
TO
159 $this->preProcess();
160 return parent::run();
161 }
162
ffd93213
EM
163 /**
164 * @param string $suffix
165 *
166 * @return null|string
167 */
00be9182 168 public function checkTemplateFileExists($suffix = '') {
6a488035
TO
169 if ($this->_gid) {
170 $templateFile = "CRM/Profile/Page/{$this->_gid}/View.{$suffix}tpl";
171 $template = CRM_Core_Page::getTemplate();
172 if ($template->template_exists($templateFile)) {
173 return $templateFile;
174 }
175
176 // lets see if we have customized by name
177 $ufGroupName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $this->_gid, 'name');
178 if ($ufGroupName) {
179 $templateFile = "CRM/Profile/Page/{$ufGroupName}/View.{$suffix}tpl";
180 if ($template->template_exists($templateFile)) {
181 return $templateFile;
182 }
183 }
184 }
185 return NULL;
186 }
187
ffd93213 188 /**
fe482240 189 * Use the form name to create the tpl file name.
ffd93213
EM
190 *
191 * @return string
ffd93213 192 */
00be9182 193 public function getTemplateFileName() {
6a488035
TO
194 $fileName = $this->checkTemplateFileExists();
195 return $fileName ? $fileName : parent::getTemplateFileName();
196 }
197
ffd93213
EM
198 /**
199 * Default extra tpl file basically just replaces .tpl with .extra.tpl
200 * i.e. we dont override
201 *
202 * @return string
ffd93213 203 */
00be9182 204 public function overrideExtraTemplateFileName() {
6a488035
TO
205 $fileName = $this->checkTemplateFileExists('extra.');
206 return $fileName ? $fileName : parent::overrideExtraTemplateFileName();
207 }
96025800 208
6a488035 209}