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