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