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