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