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