Update copyright date for 2020
[civicrm-core.git] / CRM / PCP / Page / PCPInfo.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
f299f7db 6 | Copyright CiviCRM LLC (c) 2004-2020 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
f299f7db 31 * @copyright CiviCRM LLC (c) 2004-2020
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * PCP Info Page - Summary about the PCP
38 */
39class CRM_PCP_Page_PCPInfo extends CRM_Core_Page {
40 public $_component;
41
42 /**
43 * Run the page.
44 *
45 * This method is called after the page is created. It checks for the
46 * type of action and executes that action.
47 * Finally it calls the parent's run method.
48 *
49 * @return void
6a488035 50 */
00be9182 51 public function run() {
353ffa53
TO
52 $session = CRM_Core_Session::singleton();
53 $config = CRM_Core_Config::singleton();
6a488035 54 $permissionCheck = FALSE;
353ffa53 55 $statusMessage = '';
6a488035
TO
56 if ($config->userFramework != 'Joomla') {
57 $permissionCheck = CRM_Core_Permission::check('administer CiviCRM');
58 }
59 //get the pcp id.
60 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE);
61
62 $action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE);
63
be2fb01f 64 $prms = ['id' => $this->_id];
6a488035
TO
65
66 CRM_Core_DAO::commonRetrieve('CRM_PCP_DAO_PCP', $prms, $pcpInfo);
67 $this->_component = $pcpInfo['page_type'];
68
69 if (empty($pcpInfo)) {
70 $statusMessage = ts('The personal campaign page you requested is currently unavailable.');
71 CRM_Core_Error::statusBounce($statusMessage,
72 $config->userFrameworkBaseURL
73 );
74 }
75
76 CRM_Utils_System::setTitle($pcpInfo['title']);
77 $this->assign('pcp', $pcpInfo);
78
2158332a 79 $pcpStatus = CRM_Core_OptionGroup::values("pcp_status");
ed71bbca 80 $approvedId = CRM_Core_PseudoConstant::getKey('CRM_PCP_BAO_PCP', 'status_id', 'Approved');
6a488035
TO
81
82 // check if PCP is created by anonymous user
83 $anonymousPCP = CRM_Utils_Request::retrieve('ap', 'Boolean', $this);
84 if ($anonymousPCP) {
85 $loginURL = $config->userSystem->getLoginURL();
be2fb01f 86 $anonMessage = ts('Once you\'ve received your new account welcome email, you can <a href=%1>click here</a> to login and promote your campaign page.', [1 => $loginURL]);
6a488035
TO
87 CRM_Core_Session::setStatus($anonMessage, ts('Success'), 'success');
88 }
89 else {
90 $statusMessage = ts('The personal campaign page you requested is currently unavailable. However you can still support the campaign by making a contribution here.');
91 }
92
93 $pcpBlock = new CRM_PCP_DAO_PCPBlock();
94 $pcpBlock->entity_table = CRM_PCP_BAO_PCP::getPcpEntityTable($pcpInfo['page_type']);
95 $pcpBlock->entity_id = $pcpInfo['page_id'];
96 $pcpBlock->find(TRUE);
97
98 // Redirect back to source page in case of error.
99 if ($pcpInfo['page_type'] == 'contribute') {
100 $urlBase = 'civicrm/contribute/transact';
101 }
102 elseif ($pcpInfo['page_type'] == 'event') {
103 $urlBase = 'civicrm/event/register';
104 }
105
106 if ($pcpInfo['status_id'] != $approvedId || !$pcpInfo['is_active']) {
107 if ($pcpInfo['contact_id'] != $session->get('userID') && !$permissionCheck) {
108 CRM_Core_Error::statusBounce($statusMessage, CRM_Utils_System::url($urlBase,
353ffa53
TO
109 "reset=1&id=" . $pcpInfo['page_id'],
110 FALSE, NULL, FALSE, TRUE
111 ));
6a488035
TO
112 }
113 }
114 else {
115 $getStatus = CRM_PCP_BAO_PCP::getStatus($this->_id, $this->_component);
116 if (!$getStatus) {
117 // PCP not enabled for this contribution page. Forward everyone to source page
118 CRM_Core_Error::statusBounce($statusMessage, CRM_Utils_System::url($urlBase,
353ffa53
TO
119 "reset=1&id=" . $pcpInfo['page_id'],
120 FALSE, NULL, FALSE, TRUE
121 ));
6a488035
TO
122 }
123 }
124
be2fb01f 125 $default = [];
6a488035
TO
126
127 if ($pcpBlock->target_entity_type == 'contribute') {
128 $urlBase = 'civicrm/contribute/transact';
129 }
130 elseif ($pcpBlock->target_entity_type == 'event') {
131 $urlBase = 'civicrm/event/register';
132 }
133
134 if ($pcpBlock->entity_table == 'civicrm_event') {
135 $page_class = 'CRM_Event_DAO_Event';
136 $this->assign('pageName', CRM_Event_PseudoConstant::event($pcpInfo['page_id']));
137 CRM_Core_DAO::commonRetrieveAll($page_class, 'id',
be2fb01f 138 $pcpInfo['page_id'], $default, [
353ffa53
TO
139 'start_date',
140 'end_date',
141 'registration_start_date',
608e6658 142 'registration_end_date',
be2fb01f 143 ]
6a488035
TO
144 );
145 }
146 elseif ($pcpBlock->entity_table == 'civicrm_contribution_page') {
147 $page_class = 'CRM_Contribute_DAO_ContributionPage';
148 $this->assign('pageName', CRM_Contribute_PseudoConstant::contributionPage($pcpInfo['page_id'], TRUE));
149 CRM_Core_DAO::commonRetrieveAll($page_class, 'id',
be2fb01f 150 $pcpInfo['page_id'], $default, ['start_date', 'end_date']
6a488035
TO
151 );
152 }
153
154 $pageInfo = $default[$pcpInfo['page_id']];
155
156 if ($pcpInfo['contact_id'] == $session->get('userID')) {
157 $owner = $pageInfo;
158 $owner['status'] = CRM_Utils_Array::value($pcpInfo['status_id'], $pcpStatus);
159
160 $this->assign('owner', $owner);
161
162 $link = CRM_PCP_BAO_PCP::pcpLinks();
163
be2fb01f 164 $hints = [
6a488035
TO
165 CRM_Core_Action::UPDATE => ts('Change the content and appearance of your page'),
166 CRM_Core_Action::DETACH => ts('Send emails inviting your friends to support your campaign!'),
e049d911 167 CRM_Core_Action::VIEW => ts('Copy this link to share directly with your network!'),
6a488035
TO
168 CRM_Core_Action::BROWSE => ts('Update your personal contact information'),
169 CRM_Core_Action::DISABLE => ts('De-activate the page (you can re-activate it later)'),
170 CRM_Core_Action::ENABLE => ts('Activate the page (you can de-activate it later)'),
171 CRM_Core_Action::DELETE => ts('Remove the page (this cannot be undone!)'),
be2fb01f 172 ];
6a488035 173
be2fb01f 174 $replace = [
6a488035
TO
175 'id' => $this->_id,
176 'block' => $pcpBlock->id,
177 'pageComponent' => $this->_component,
be2fb01f 178 ];
6a488035
TO
179
180 if (!$pcpBlock->is_tellfriend_enabled || CRM_Utils_Array::value('status_id', $pcpInfo) != $approvedId) {
181 unset($link['all'][CRM_Core_Action::DETACH]);
182 }
183
184 switch ($pcpInfo['is_active']) {
185 case 1:
186 unset($link['all'][CRM_Core_Action::ENABLE]);
187 break;
188
189 case 0:
190 unset($link['all'][CRM_Core_Action::DISABLE]);
191 break;
192 }
193
194 $this->assign('links', $link['all']);
195 $this->assign('hints', $hints);
196 $this->assign('replace', $replace);
197 }
198
199 $honor = CRM_PCP_BAO_PCP::honorRoll($this->_id);
200
2c77ee60 201 $entityFile = CRM_Core_BAO_File::getEntityFile('civicrm_pcp', $this->_id);
202 if (!empty($entityFile)) {
203 $fileInfo = reset($entityFile);
b198658c 204 $fileId = $fileInfo['fileID'];
ff9aeadb 205 $fileHash = CRM_Core_BAO_File::generateFileHash($this->_id, $fileId);
6a488035 206 $image = '<img src="' . CRM_Utils_System::url('civicrm/file',
ff9aeadb 207 "reset=1&id=$fileId&eid={$this->_id}&fcs={$fileHash}"
353ffa53 208 ) . '" />';
6a488035
TO
209 $this->assign('image', $image);
210 }
211
212 $totalAmount = CRM_PCP_BAO_PCP::thermoMeter($this->_id);
213 $achieved = round($totalAmount / $pcpInfo['goal_amount'] * 100, 2);
214
6a488035
TO
215 if ($pcpBlock->is_active == 1) {
216 $linkTextUrl = CRM_Utils_System::url('civicrm/contribute/campaign',
217 "action=add&reset=1&pageId={$pcpInfo['page_id']}&component={$pcpInfo['page_type']}",
218 TRUE, NULL, TRUE,
219 TRUE
220 );
221 $this->assign('linkTextUrl', $linkTextUrl);
222 $this->assign('linkText', $pcpBlock->link_text);
223 }
224
225 $this->assign('honor', $honor);
226 $this->assign('total', $totalAmount ? $totalAmount : '0.0');
227 $this->assign('achieved', $achieved <= 100 ? $achieved : 100);
228
229 if ($achieved <= 100) {
230 $this->assign('remaining', 100 - $achieved);
231 }
232 // make sure that we are between contribution page start and end dates OR registration start date and end dates if they are set
233 if ($pcpBlock->entity_table == 'civicrm_event') {
234 $startDate = CRM_Utils_Date::unixTime(CRM_Utils_Array::value('registration_start_date', $pageInfo));
235 $endDate = CRM_Utils_Date::unixTime(CRM_Utils_Array::value('registration_end_date', $pageInfo));
236 }
237 else {
238 $startDate = CRM_Utils_Date::unixTime(CRM_Utils_Array::value('start_date', $pageInfo));
239 $endDate = CRM_Utils_Date::unixTime(CRM_Utils_Array::value('end_date', $pageInfo));
240 }
241
6a488035
TO
242 $now = time();
243 $validDate = TRUE;
244 if ($startDate && $startDate >= $now) {
245 $validDate = FALSE;
246 }
247 if ($endDate && $endDate < $now) {
248 $validDate = FALSE;
249 }
250
251 $this->assign('validDate', $validDate);
252
253 // form parent page url
254 if ($action == CRM_Core_Action::PREVIEW) {
255 $parentUrl = CRM_Utils_System::url($urlBase,
256 "id={$pcpInfo['page_id']}&reset=1&action=preview",
257 TRUE, NULL, TRUE,
258 TRUE
259 );
260 }
261 else {
262 $parentUrl = CRM_Utils_System::url($urlBase,
263 "id={$pcpInfo['page_id']}&reset=1",
264 TRUE, NULL, TRUE,
265 TRUE
266 );
267 }
268
269 $this->assign('parentURL', $parentUrl);
270
6a488035
TO
271 if ($validDate) {
272
273 $contributionText = ts('Contribute Now');
a7488080 274 if (!empty($pcpInfo['donate_link_text'])) {
6a488035
TO
275 $contributionText = $pcpInfo['donate_link_text'];
276 }
277
278 $this->assign('contributionText', $contributionText);
279
280 // we always generate urls for the front end in joomla
281 if ($action == CRM_Core_Action::PREVIEW) {
282 $url = CRM_Utils_System::url($urlBase,
283 "id=" . $pcpBlock->target_entity_id . "&pcpId={$this->_id}&reset=1&action=preview",
284 TRUE, NULL, TRUE,
285 TRUE
286 );
287 }
288 else {
289 $url = CRM_Utils_System::url($urlBase,
290 "id=" . $pcpBlock->target_entity_id . "&pcpId={$this->_id}&reset=1",
291 TRUE, NULL, TRUE,
292 TRUE
293 );
294 }
295 $this->assign('contributeURL', $url);
296 }
297
298 // we do not want to display recently viewed items, so turn off
299 $this->assign('displayRecent', FALSE);
300
301 $single = $permission = FALSE;
302 switch ($action) {
303 case CRM_Core_Action::BROWSE:
304 $subForm = 'PCPAccount';
353ffa53
TO
305 $form = "CRM_PCP_Form_$subForm";
306 $single = TRUE;
6a488035
TO
307 break;
308
309 case CRM_Core_Action::UPDATE:
310 $subForm = 'Campaign';
353ffa53
TO
311 $form = "CRM_PCP_Form_$subForm";
312 $single = TRUE;
6a488035
TO
313 break;
314 }
315
316 $userID = $session->get('userID');
317 //make sure the user has "administer CiviCRM" permission
318 //OR has created the PCP
319 if (CRM_Core_Permission::check('administer CiviCRM') ||
320 ($userID && (CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', $this->_id, 'contact_id') == $userID))
321 ) {
322 $permission = TRUE;
323 }
324 if ($single && $permission) {
325 $controller = new CRM_Core_Controller_Simple($form, $subForm, $action);
326 $controller->set('id', $this->_id);
327 $controller->set('single', TRUE);
328 $controller->process();
329 return $controller->run();
330 }
331 $session->pushUserContext(CRM_Utils_System::url(CRM_Utils_System::currentPath(), 'reset=1&id=' . $this->_id));
332 parent::run();
333 }
334
e0ef6999
EM
335 /**
336 * @return string
337 */
00be9182 338 public function getTemplateFileName() {
6a488035
TO
339 if ($this->_id) {
340 $templateFile = "CRM/PCP/Page/{$this->_id}/PCPInfo.tpl";
341 $template = &CRM_Core_Page::getTemplate();
342 if ($template->template_exists($templateFile)) {
343 return $templateFile;
344 }
345 }
346 return parent::getTemplateFileName();
347 }
96025800 348
6a488035 349}