Merge pull request #21845 from colemanw/relationship_cache_case_id
[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 */
17
18 /**
19 * PCP Info Page - Summary about the PCP
20 */
21 class 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
32 */
33 public function run() {
34 $session = CRM_Core_Session::singleton();
35 $config = CRM_Core_Config::singleton();
36 $permissionCheck = FALSE;
37 $statusMessage = '';
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
46 $prms = ['id' => $this->_id];
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);
60 $this->assign('currency', $pcpInfo['currency']);
61
62 $pcpStatus = CRM_Core_OptionGroup::values("pcp_status");
63 $approvedId = CRM_Core_PseudoConstant::getKey('CRM_PCP_BAO_PCP', 'status_id', 'Approved');
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();
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]);
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,
92 "reset=1&id=" . $pcpInfo['page_id'],
93 FALSE, NULL, FALSE, TRUE
94 ));
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,
102 "reset=1&id=" . $pcpInfo['page_id'],
103 FALSE, NULL, FALSE, TRUE
104 ));
105 }
106 }
107
108 $default = [];
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',
121 $pcpInfo['page_id'], $default, [
122 'start_date',
123 'end_date',
124 'registration_start_date',
125 'registration_end_date',
126 ]
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',
133 $pcpInfo['page_id'], $default, ['start_date', 'end_date']
134 );
135 }
136
137 $pageInfo = $default[$pcpInfo['page_id']];
138
139 if ($pcpInfo['contact_id'] == $session->get('userID')) {
140 $owner = $pageInfo;
141 $owner['status'] = $pcpStatus[$pcpInfo['status_id']] ?? NULL;
142
143 $this->assign('owner', $owner);
144
145 $link = CRM_PCP_BAO_PCP::pcpLinks($pcpInfo['id']);
146
147 $hints = [
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!'),
150 CRM_Core_Action::VIEW => ts('Copy this link to share directly with your network!'),
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!)'),
155 ];
156
157 $replace = [
158 'id' => $this->_id,
159 'block' => $pcpBlock->id,
160 'pageComponent' => $this->_component,
161 ];
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
184 $entityFile = CRM_Core_BAO_File::getEntityFile('civicrm_pcp', $this->_id);
185 if (!empty($entityFile)) {
186 $fileInfo = reset($entityFile);
187 $fileId = $fileInfo['fileID'];
188 $altText = htmlspecialchars($fileInfo['description'] ?? '');
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 ) . '" alt="' . $altText . '"/>';
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 }