Merge pull request #12053 from civicrm/5.1
[civicrm-core.git] / CRM / Profile / Page / View.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
32 *
33 */
34
35 /**
36 * Main page for viewing contact.
37 *
38 */
39 class CRM_Profile_Page_View extends CRM_Core_Page {
40
41 /**
42 * The id of the contact.
43 *
44 * @var int
45 */
46 protected $_id;
47
48 /**
49 * The group id that we are editing.
50 *
51 * @var int
52 */
53 protected $_gid;
54
55 /**
56 * Heart of the viewing process. The runner gets all the meta data for
57 * the contact and calls the appropriate type of page to view.
58 *
59 */
60 public function preProcess() {
61 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive',
62 $this, FALSE
63 );
64 if (!$this->_id) {
65 $session = CRM_Core_Session::singleton();
66 $this->_id = $session->get('userID');
67 if (!$this->_id) {
68 CRM_Core_Error::fatal(ts('Could not find the required contact id parameter (id=) for viewing a contact record with a Profile.'));
69 }
70 }
71 $this->assign('cid', $this->_id);
72
73 $gids = explode(',', CRM_Utils_Request::retrieve('gid', 'String', CRM_Core_DAO::$_nullObject, FALSE, 0, 'GET'));
74
75 $profileIds = array();
76 if (count($gids) > 1) {
77 if (!empty($gids)) {
78 foreach ($gids as $pfId) {
79 $profileIds[] = CRM_Utils_Type::escape($pfId, 'Positive');
80 }
81 }
82
83 // check if we are rendering mixed profiles
84 if (CRM_Core_BAO_UFGroup::checkForMixProfiles($profileIds)) {
85 CRM_Core_Error::fatal(ts('You cannot combine profiles of multiple types.'));
86 }
87
88 $this->_gid = $profileIds[0];
89 }
90
91 if (!$this->_gid) {
92 $this->_gid = CRM_Utils_Request::retrieve('gid', 'Positive', $this, FALSE, 0, 'GET');
93 }
94
95 $anyContent = TRUE;
96 if ($this->_gid) {
97 $page = new CRM_Profile_Page_Dynamic($this->_id, $this->_gid, 'Profile', FALSE, $profileIds);
98 $profileGroup = array();
99 $profileGroup['title'] = NULL;
100 $profileGroup['content'] = $page->run();
101 if (empty($profileGroup['content'])) {
102 $anyContent = FALSE;
103 }
104 $profileGroups[] = $profileGroup;
105
106 $gidString = $this->_gid;
107 if (!empty($profileIds)) {
108 $gidString = implode(',', $profileIds);
109 }
110
111 $map = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $this->_gid, 'is_map');
112 if ($map) {
113 $this->assign('mapURL',
114 CRM_Utils_System::url("civicrm/profile/map",
115 "reset=1&pv=1&cid={$this->_id}&gid={$gidString}"
116 )
117 );
118 }
119 if (CRM_Core_Permission::ufGroupValid($this->_gid,
120 CRM_Core_Permission::SEARCH
121 )
122 ) {
123 $this->assign('listingURL',
124 CRM_Utils_System::url("civicrm/profile",
125 "force=1&gid={$gidString}"
126 )
127 );
128 }
129 }
130 else {
131 $ufGroups = CRM_Core_BAO_UFGroup::getModuleUFGroup('Profile');
132
133 $profileGroups = array();
134 foreach ($ufGroups as $groupid => $group) {
135 $page = new CRM_Profile_Page_Dynamic($this->_id, $groupid, 'Profile', FALSE, $profileIds);
136 $profileGroup = array();
137 $profileGroup['title'] = $group['title'];
138 $profileGroup['content'] = $page->run();
139 if (empty($profileGroup['content'])) {
140 $anyContent = FALSE;
141 }
142 $profileGroups[] = $profileGroup;
143 }
144 $this->assign('listingURL',
145 CRM_Utils_System::url("civicrm/profile",
146 "force=1"
147 )
148 );
149 }
150
151 $this->assign('groupID', $this->_gid);
152
153 $this->assign('profileGroups', $profileGroups);
154 $this->assign('recentlyViewed', FALSE);
155
156 // do not set title if there is no content
157 // CRM-6081
158 if (!$anyContent) {
159 CRM_Utils_System::setTitle('');
160 }
161 }
162
163 /**
164 * Build the outcome basing on the CRM_Profile_Page_Dynamic's HTML.
165 *
166 */
167 public function run() {
168 $this->preProcess();
169 return parent::run();
170 }
171
172 /**
173 * @param string $suffix
174 *
175 * @return null|string
176 */
177 public function checkTemplateFileExists($suffix = '') {
178 if ($this->_gid) {
179 $templateFile = "CRM/Profile/Page/{$this->_gid}/View.{$suffix}tpl";
180 $template = CRM_Core_Page::getTemplate();
181 if ($template->template_exists($templateFile)) {
182 return $templateFile;
183 }
184
185 // lets see if we have customized by name
186 $ufGroupName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $this->_gid, 'name');
187 if ($ufGroupName) {
188 $templateFile = "CRM/Profile/Page/{$ufGroupName}/View.{$suffix}tpl";
189 if ($template->template_exists($templateFile)) {
190 return $templateFile;
191 }
192 }
193 }
194 return NULL;
195 }
196
197 /**
198 * Use the form name to create the tpl file name.
199 *
200 * @return string
201 */
202 public function getTemplateFileName() {
203 $fileName = $this->checkTemplateFileExists();
204 return $fileName ? $fileName : parent::getTemplateFileName();
205 }
206
207 /**
208 * Default extra tpl file basically just replaces .tpl with .extra.tpl
209 * i.e. we dont override
210 *
211 * @return string
212 */
213 public function overrideExtraTemplateFileName() {
214 $fileName = $this->checkTemplateFileExists('extra.');
215 return $fileName ? $fileName : parent::overrideExtraTemplateFileName();
216 }
217
218 }