Merge pull request #12090 from eileenmcnaughton/notice
[civicrm-core.git] / CRM / PCP / BAO / PCP.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
8c9251b3 6 | Copyright CiviCRM LLC (c) 2004-2018 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
8c9251b3 31 * @copyright CiviCRM LLC (c) 2004-2018
6a488035
TO
32 */
33class CRM_PCP_BAO_PCP extends CRM_PCP_DAO_PCP {
34
35 /**
eceb18cc 36 * The action links that we need to display for the browse screen.
6a488035
TO
37 *
38 * @var array
6a488035
TO
39 */
40 static $_pcpLinks = NULL;
9d9922e7 41
e0ef6999 42 /**
ad37ac8e 43 * Class constructor.
e0ef6999 44 */
00be9182 45 public function __construct() {
6a488035
TO
46 parent::__construct();
47 }
48
49 /**
eceb18cc 50 * Add or update either a Personal Campaign Page OR a PCP Block.
6a488035 51 *
db95eff6 52 * @param array $params
f6bc51fd 53 * Values to create the pcp.
d75f2f47 54 *
6a488035
TO
55 * @return object
56 */
f6bc51fd 57 public static function create($params) {
6a488035
TO
58
59 $dao = new CRM_PCP_DAO_PCP();
60 $dao->copyValues($params);
61
62 // ensure we set status_id since it is a not null field
63 // we should change the schema and allow this to be null
64 if (!$dao->id && !isset($dao->status_id)) {
65 $dao->status_id = 0;
66 }
67
68 // set currency for CRM-1496
69 if (!isset($dao->currency)) {
f6bc51fd 70 $dao->currency = CRM_Core_Config::singleton()->defaultCurrency;
6a488035
TO
71 }
72
73 $dao->save();
74 return $dao;
75 }
76
77 /**
eceb18cc 78 * Get the Display name of a contact for a PCP.
6a488035 79 *
db95eff6
TO
80 * @param int $id
81 * Id for the PCP.
6a488035 82 *
72b3a70c
CW
83 * @return null|string
84 * Dispaly name of the contact if found
6a488035 85 */
00be9182 86 public static function displayName($id) {
6a488035
TO
87 $id = CRM_Utils_Type::escape($id, 'Integer');
88
89 $query = "
90SELECT civicrm_contact.display_name
91FROM civicrm_pcp, civicrm_contact
92WHERE civicrm_pcp.contact_id = civicrm_contact.id
93 AND civicrm_pcp.id = {$id}
94";
9d2678f4 95 return CRM_Core_DAO::singleValueQuery($query);
6a488035
TO
96 }
97
98 /**
eceb18cc 99 * Return PCP Block info for dashboard.
6a488035 100 *
100fef9d 101 * @param int $contactId
dd244018 102 *
a6c01b45
CW
103 * @return array
104 * array of Pcp if found
6a488035 105 */
00be9182 106 public static function getPcpDashboardInfo($contactId) {
6a488035
TO
107 $links = self::pcpLinks();
108
109 $query = "
743f5cb2 110SELECT pcp.*, block.is_tellfriend_enabled FROM civicrm_pcp pcp
6da0be4d 111LEFT JOIN civicrm_pcp_block block ON block.id = pcp.pcp_block_id
6a488035
TO
112WHERE pcp.is_active = 1
113 AND pcp.contact_id = %1
114ORDER BY page_type, page_id";
115
116 $params = array(1 => array($contactId, 'Integer'));
117
9d9922e7 118 $pcpInfoDao = CRM_Core_DAO::executeQuery($query, $params);
119 $pcpInfo = array();
120 $hide = $mask = array_sum(array_keys($links['all']));
6a488035
TO
121 $contactPCPPages = array();
122
9d9922e7 123 $event = CRM_Event_PseudoConstant::event(NULL, FALSE, "( is_template IS NULL OR is_template != 1 )");
6a488035 124 $contribute = CRM_Contribute_PseudoConstant::contributionPage();
9d9922e7 125 $pcpStatus = CRM_Contribute_PseudoConstant::pcpStatus();
126 $approved = CRM_Utils_Array::key('Approved', $pcpStatus);
6a488035
TO
127
128 while ($pcpInfoDao->fetch()) {
129 $mask = $hide;
130 if ($links) {
131 $replace = array(
132 'pcpId' => $pcpInfoDao->id,
133 'pcpBlock' => $pcpInfoDao->pcp_block_id,
134 'pageComponent' => $pcpInfoDao->page_type,
135 );
136 }
137
138 $pcpLink = $links['all'];
139 $class = '';
140
141 if ($pcpInfoDao->status_id != $approved || $pcpInfoDao->is_active != 1) {
142 $class = 'disabled';
6da0be4d 143 if (!$pcpInfoDao->is_tellfriend_enabled) {
6a488035
TO
144 $mask -= CRM_Core_Action::DETACH;
145 }
146 }
147
148 if ($pcpInfoDao->is_active == 1) {
149 $mask -= CRM_Core_Action::ENABLE;
150 }
151 else {
152 $mask -= CRM_Core_Action::DISABLE;
153 }
87dab4a4
AH
154 $action = CRM_Core_Action::formLink($pcpLink, $mask, $replace, ts('more'),
155 FALSE, 'pcp.dashboard.active', 'PCP', $pcpInfoDao->id);
6a488035
TO
156 $component = $pcpInfoDao->page_type;
157 $pageTitle = CRM_Utils_Array::value($pcpInfoDao->page_id, $$component);
158
159 $pcpInfo[] = array(
160 'pageTitle' => $pageTitle,
161 'pcpId' => $pcpInfoDao->id,
162 'pcpTitle' => $pcpInfoDao->title,
163 'pcpStatus' => $pcpStatus[$pcpInfoDao->status_id],
164 'action' => $action,
165 'class' => $class,
166 );
167 $contactPCPPages[$pcpInfoDao->page_type][] = $pcpInfoDao->page_id;
168 }
169
170 $excludePageClause = $clause = NULL;
171 if (!empty($contactPCPPages)) {
172 foreach ($contactPCPPages as $component => $entityIds) {
173 $excludePageClause[] = "
174( target_entity_type = '{$component}'
175AND target_entity_id NOT IN ( " . implode(',', $entityIds) . ") )";
176 }
177
178 $clause = ' AND ' . implode(' OR ', $excludePageClause);
179 }
180
181 $query = "
3636b520 182SELECT *
6a488035
TO
183FROM civicrm_pcp_block block
184LEFT JOIN civicrm_pcp pcp ON pcp.pcp_block_id = block.id
185WHERE block.is_active = 1
186{$clause}
3636b520 187GROUP BY block.id, pcp.id
6a488035
TO
188ORDER BY target_entity_type, target_entity_id
189";
190 $pcpBlockDao = CRM_Core_DAO::executeQuery($query);
9d9922e7 191 $pcpBlock = array();
192 $mask = 0;
6a488035
TO
193
194 while ($pcpBlockDao->fetch()) {
195 if ($links) {
196 $replace = array(
197 'pageId' => $pcpBlockDao->target_entity_id,
198 'pageComponent' => $pcpBlockDao->target_entity_type,
199 );
200 }
9d9922e7 201 $pcpLink = $links['add'];
87dab4a4
AH
202 $action = CRM_Core_Action::formLink($pcpLink, $mask, $replace, ts('more'),
203 FALSE, 'pcp.dashboard.other', "{$pcpBlockDao->target_entity_type}_PCP", $pcpBlockDao->target_entity_id);
9d9922e7 204 $component = $pcpBlockDao->target_entity_type;
6167c3d3
BS
205 if ($pageTitle = CRM_Utils_Array::value($pcpBlockDao->target_entity_id, $$component)) {
206 $pcpBlock[] = array(
207 'pageId' => $pcpBlockDao->target_entity_id,
208 'pageTitle' => $pageTitle,
209 'action' => $action,
210 );
211 }
6a488035
TO
212 }
213
214 return array($pcpBlock, $pcpInfo);
215 }
216
217 /**
eceb18cc 218 * Show the total amount for Personal Campaign Page on thermometer.
6a488035 219 *
db95eff6
TO
220 * @param array $pcpId
221 * Contains the pcp ID.
6a488035 222 *
ad37ac8e 223 * @return float
224 * Total amount
6a488035 225 */
00be9182 226 public static function thermoMeter($pcpId) {
6a488035
TO
227 $query = "
228SELECT SUM(cc.total_amount) as total
229FROM civicrm_pcp pcp
230LEFT JOIN civicrm_contribution_soft cs ON ( pcp.id = cs.pcp_id )
231LEFT JOIN civicrm_contribution cc ON ( cs.contribution_id = cc.id)
232WHERE pcp.id = %1 AND cc.contribution_status_id =1 AND cc.is_test = 0";
233
234 $params = array(1 => array($pcpId, 'Integer'));
235 return CRM_Core_DAO::singleValueQuery($query, $params);
236 }
237
238 /**
ad37ac8e 239 * Show the amount, nickname on honor roll.
6a488035 240 *
db95eff6
TO
241 * @param array $pcpId
242 * Contains the pcp ID.
6a488035 243 *
a6c01b45 244 * @return array
6a488035 245 */
00be9182 246 public static function honorRoll($pcpId) {
6a488035
TO
247 $query = "
248 SELECT cc.id, cs.pcp_roll_nickname, cs.pcp_personal_note,
249 cc.total_amount, cc.currency
250 FROM civicrm_contribution cc
251 LEFT JOIN civicrm_contribution_soft cs ON cc.id = cs.contribution_id
252 WHERE cs.pcp_id = {$pcpId}
253 AND cs.pcp_display_in_roll = 1
254 AND contribution_status_id = 1
255 AND is_test = 0";
9d2678f4 256 $dao = CRM_Core_DAO::executeQuery($query);
6a488035
TO
257 $honor = array();
258 while ($dao->fetch()) {
259 $honor[$dao->id]['nickname'] = ucwords($dao->pcp_roll_nickname);
260 $honor[$dao->id]['total_amount'] = CRM_Utils_Money::format($dao->total_amount, $dao->currency);
261 $honor[$dao->id]['personal_note'] = $dao->pcp_personal_note;
262 }
263 return $honor;
264 }
265
266 /**
eceb18cc 267 * Get action links.
6a488035 268 *
a6c01b45
CW
269 * @return array
270 * (reference) of action links
6a488035 271 */
00be9182 272 public static function &pcpLinks() {
6a488035
TO
273 if (!(self::$_pcpLinks)) {
274 $deleteExtra = ts('Are you sure you want to delete this Personal Campaign Page?') . '\n' . ts('This action cannot be undone.');
275
276 self::$_pcpLinks['add'] = array(
9d9922e7 277 CRM_Core_Action::ADD => array(
278 'name' => ts('Create a Personal Campaign Page'),
cbc1fad4 279 'class' => 'no-popup',
6a488035
TO
280 'url' => 'civicrm/contribute/campaign',
281 'qs' => 'action=add&reset=1&pageId=%%pageId%%&component=%%pageComponent%%',
282 'title' => ts('Configure'),
283 ),
284 );
285
286 self::$_pcpLinks['all'] = array(
9d9922e7 287 CRM_Core_Action::UPDATE => array(
288 'name' => ts('Edit Your Page'),
6a488035
TO
289 'url' => 'civicrm/pcp/info',
290 'qs' => 'action=update&reset=1&id=%%pcpId%%&component=%%pageComponent%%',
291 'title' => ts('Configure'),
292 ),
9d9922e7 293 CRM_Core_Action::DETACH => array(
294 'name' => ts('Tell Friends'),
6a488035
TO
295 'url' => 'civicrm/friend',
296 'qs' => 'eid=%%pcpId%%&blockId=%%pcpBlock%%&reset=1&pcomponent=pcp&component=%%pageComponent%%',
297 'title' => ts('Tell Friends'),
298 ),
ff06e012
DG
299 CRM_Core_Action::VIEW => array(
300 'name' => ts('URL for this Page'),
301 'url' => 'civicrm/pcp/info',
302 'qs' => 'reset=1&id=%%pcpId%%&component=%%pageComponent%%',
303 'title' => ts('URL for this Page'),
304 ),
9d9922e7 305 CRM_Core_Action::BROWSE => array(
306 'name' => ts('Update Contact Information'),
6a488035
TO
307 'url' => 'civicrm/pcp/info',
308 'qs' => 'action=browse&reset=1&id=%%pcpId%%&component=%%pageComponent%%',
309 'title' => ts('Update Contact Information'),
310 ),
9d9922e7 311 CRM_Core_Action::ENABLE => array(
312 'name' => ts('Enable'),
6a488035
TO
313 'url' => 'civicrm/pcp',
314 'qs' => 'action=enable&reset=1&id=%%pcpId%%&component=%%pageComponent%%',
315 'title' => ts('Enable'),
316 ),
9d9922e7 317 CRM_Core_Action::DISABLE => array(
318 'name' => ts('Disable'),
6a488035
TO
319 'url' => 'civicrm/pcp',
320 'qs' => 'action=disable&reset=1&id=%%pcpId%%&component=%%pageComponent%%',
321 'title' => ts('Disable'),
322 ),
9d9922e7 323 CRM_Core_Action::DELETE => array(
324 'name' => ts('Delete'),
6a488035
TO
325 'url' => 'civicrm/pcp',
326 'qs' => 'action=delete&reset=1&id=%%pcpId%%&component=%%pageComponent%%',
327 'extra' => 'onclick = "return confirm(\'' . $deleteExtra . '\');"',
328 'title' => ts('Delete'),
329 ),
330 );
331 }
332 return self::$_pcpLinks;
333 }
334
335 /**
eceb18cc 336 * Delete the campaign page.
6a488035 337 *
ad37ac8e 338 * @param int $id
db95eff6 339 * Campaign page id.
6a488035
TO
340 */
341 public static function deleteById($id) {
342 CRM_Utils_Hook::pre('delete', 'Campaign', $id, CRM_Core_DAO::$_nullArray);
343
344 $transaction = new CRM_Core_Transaction();
345
346 // delete from pcp table
347 $pcp = new CRM_PCP_DAO_PCP();
348 $pcp->id = $id;
349 $pcp->delete();
350
351 $transaction->commit();
352
353 CRM_Utils_Hook::post('delete', 'Campaign', $id, $pcp);
354 }
355
356 /**
eceb18cc 357 * Build the form object.
6a488035 358 *
db95eff6
TO
359 * @param CRM_Core_Form $form
360 * Form object.
6a488035 361 */
cad004cf 362 public static function buildPCPForm($form) {
6a488035
TO
363 $form->addElement('checkbox', 'pcp_active', ts('Enable Personal Campaign Pages?'), NULL, array('onclick' => "return showHideByValue('pcp_active',true,'pcpFields','block','radio',false);"));
364
365 $form->addElement('checkbox', 'is_approval_needed', ts('Approval required'));
366
9d9922e7 367 $profile = array();
6a488035 368 $isUserRequired = NULL;
9d9922e7 369 $config = CRM_Core_Config::singleton();
6a488035
TO
370 if ($config->userFramework != 'Standalone') {
371 $isUserRequired = 2;
372 }
9d9922e7 373 CRM_Core_DAO::commonRetrieveAll('CRM_Core_DAO_UFGroup', 'is_cms_user', $isUserRequired, $profiles, array(
374 'title',
21dfd5f5 375 'is_active',
9d9922e7 376 ));
6a488035
TO
377 if (!empty($profiles)) {
378 foreach ($profiles as $key => $value) {
379 if ($value['is_active']) {
380 $profile[$key] = $value['title'];
381 }
382 }
383 $form->assign('profile', $profile);
384 }
385
a33733e6 386 $form->add('select', 'supporter_profile_id', ts('Supporter Profile'), array('' => ts('- select -')) + $profile, TRUE);
6a488035 387
12f92dbd
N
388 //CRM-15821 - To add new option for PCP "Owner" notification
389 $ownerNotifications = CRM_Core_OptionGroup::values('pcp_owner_notify');
5798d2ec 390 $form->addRadio('owner_notify_id', ts('Owner Email Notification'), $ownerNotifications, NULL, '<br/>', TRUE);
12f92dbd 391
6a488035
TO
392 $form->addElement('checkbox', 'is_tellfriend_enabled', ts("Allow 'Tell a friend' functionality"), NULL, array('onclick' => "return showHideByValue('is_tellfriend_enabled',true,'tflimit','table-row','radio',false);"));
393
394 $form->add('text',
395 'tellfriend_limit',
396 ts("'Tell a friend' maximum recipients limit"),
397 CRM_Core_DAO::getAttribute('CRM_PCP_DAO_PCPBlock', 'tellfriend_limit')
398 );
399 $form->addRule('tellfriend_limit', ts('Please enter a valid limit.'), 'integer');
400
401 $form->add('text',
402 'link_text',
403 ts("'Create Personal Campaign Page' link text"),
404 CRM_Core_DAO::getAttribute('CRM_PCP_DAO_PCPBlock', 'link_text')
405 );
406
407 $form->add('text', 'notify_email', ts('Notify Email'), CRM_Core_DAO::getAttribute('CRM_PCP_DAO_PCPBlock', 'notify_email'));
408 }
409
410
e0ef6999 411 /**
eceb18cc 412 * Add PCP form elements to a form.
d424ffde 413 *
db95eff6 414 * @param int $pcpId
bddddc63 415 * @param CRM_Core_Form $page
e0ef6999
EM
416 * @param null $elements
417 */
5fe87df6 418 public static function buildPcp($pcpId, &$page, &$elements = NULL) {
6a488035
TO
419
420 $prms = array('id' => $pcpId);
421 CRM_Core_DAO::commonRetrieve('CRM_PCP_DAO_PCP', $prms, $pcpInfo);
6a488035
TO
422 if ($pcpSupporter = CRM_PCP_BAO_PCP::displayName($pcpId)) {
423 if ($pcpInfo['page_type'] == 'event') {
5fe87df6 424 $pcp_supporter_text = ts('This event registration is being made thanks to the efforts of <strong>%1</strong>, who supports our campaign. ', array(1 => $pcpSupporter));
eea141c0 425 $text = CRM_PCP_BAO_PCP::getPcpBlockStatus($pcpInfo['page_id'], 'event');
22e263ad 426 if (!empty($text)) {
eea141c0
DG
427 $pcp_supporter_text .= "You can support it as well - once you complete the registration, you will be able to create your own Personal Campaign Page!";
428 }
6a488035
TO
429 }
430 else {
5fe87df6 431 $pcp_supporter_text = ts('This contribution is being made thanks to the efforts of <strong>%1</strong>, who supports our campaign. ', array(1 => $pcpSupporter));
eea141c0 432 $text = CRM_PCP_BAO_PCP::getPcpBlockStatus($pcpInfo['page_id'], 'contribute');
22e263ad 433 if (!empty($text)) {
eea141c0
DG
434 $pcp_supporter_text .= "You can support it as well - once you complete the donation, you will be able to create your own Personal Campaign Page!";
435 }
6a488035 436 }
77b97be7 437
eea141c0 438 $page->assign('pcpSupporterText', $pcp_supporter_text);
6a488035
TO
439 }
440 $page->assign('pcp', TRUE);
441
442 // build honor roll fields for registration form if supporter has honor roll enabled for their PCP
443 if ($pcpInfo['is_honor_roll']) {
444 $page->assign('is_honor_roll', TRUE);
445 $page->add('checkbox', 'pcp_display_in_roll', ts('Show my support in the public honor roll'), NULL, NULL,
446 array('onclick' => "showHideByValue('pcp_display_in_roll','','nameID|nickID|personalNoteID','block','radio',false); pcpAnonymous( );")
447 );
448 $extraOption = array('onclick' => "return pcpAnonymous( );");
9d9922e7 449 $elements = array();
353ffa53
TO
450 $elements[] = &$page->createElement('radio', NULL, '', ts('Include my name and message'), 0, $extraOption);
451 $elements[] = &$page->createElement('radio', NULL, '', ts('List my support anonymously'), 1, $extraOption);
6a488035
TO
452 $page->addGroup($elements, 'pcp_is_anonymous', NULL, '&nbsp;&nbsp;&nbsp;');
453 $page->_defaults['pcp_is_anonymous'] = 0;
454
455 $page->add('text', 'pcp_roll_nickname', ts('Name'), array('maxlength' => 30));
ed71bbca 456 $page->addField('pcp_personal_note', array('entity' => 'ContributionSoft', 'context' => 'create', 'style' => 'height: 3em; width: 40em;'));
6a488035
TO
457 }
458 else {
459 $page->assign('is_honor_roll', FALSE);
460 }
461 }
462
e0ef6999 463 /**
eceb18cc 464 * Process a PCP contribution.
d424ffde 465 *
100fef9d 466 * @param int $pcpId
ad37ac8e 467 * @param string $component
468 * @param string $entity
e0ef6999
EM
469 *
470 * @return array
471 */
9d9922e7 472 public static function handlePcp($pcpId, $component, $entity) {
6a488035 473
9d9922e7 474 self::getPcpEntityTable($component);
6a488035
TO
475
476 if (!$pcpId) {
477 return FALSE;
478 }
479
2158332a 480 $pcpStatus = CRM_Core_OptionGroup::values("pcp_status");
6a488035
TO
481
482 $params = array('id' => $pcpId);
483 CRM_Core_DAO::commonRetrieve('CRM_PCP_DAO_PCP', $params, $pcpInfo);
484
485 $params = array('id' => $pcpInfo['pcp_block_id']);
486 CRM_Core_DAO::commonRetrieve('CRM_PCP_DAO_PCPBlock', $params, $pcpBlock);
487
488 $params = array('id' => $pcpInfo['page_id']);
9d9922e7 489 $now = time();
6a488035
TO
490
491 if ($component == 'event') {
492 // figure out where to redirect if an exception occurs below based on target entity
493 $urlBase = 'civicrm/event/register';
494
495 // ignore startDate for events - PCP's can be active long before event start date
496 $startDate = 0;
9d9922e7 497 $endDate = CRM_Utils_Date::unixTime(CRM_Utils_Array::value('end_date', $entity));
6a488035 498 }
6a488035
TO
499 elseif ($component == 'contribute') {
500 $urlBase = 'civicrm/contribute/transact';
501 //start and end date of the contribution page
502 $startDate = CRM_Utils_Date::unixTime(CRM_Utils_Array::value('start_date', $entity));
9d9922e7 503 $endDate = CRM_Utils_Date::unixTime(CRM_Utils_Array::value('end_date', $entity));
6a488035
TO
504 }
505
506 // define redirect url back to contrib page or event if needed
481a74f4 507 $url = CRM_Utils_System::url($urlBase, "reset=1&id={$pcpBlock['entity_id']}", FALSE, NULL, FALSE, TRUE);
ed71bbca 508 $currentPCPStatus = CRM_Core_PseudoConstant::getName('CRM_PCP_BAO_PCP', 'status_id', $pcpInfo['status_id']);
6a488035
TO
509
510 if ($pcpBlock['target_entity_id'] != $entity['id']) {
511 $statusMessage = ts('This page is not related to the Personal Campaign Page you have just visited. However you can still make a contribution here.');
512 CRM_Core_Error::statusBounce($statusMessage, $url);
513 }
ed71bbca 514 elseif ($currentPCPStatus !== 'Approved') {
6a488035
TO
515 $statusMessage = ts('The Personal Campaign Page you have just visited is currently %1. However you can still support the campaign here.', array(1 => $pcpStatus[$pcpInfo['status_id']]));
516 CRM_Core_Error::statusBounce($statusMessage, $url);
517 }
a7488080 518 elseif (empty($pcpBlock['is_active'])) {
6a488035
TO
519 $statusMessage = ts('Personal Campaign Pages are currently not enabled for this contribution page. However you can still support the campaign here.');
520 CRM_Core_Error::statusBounce($statusMessage, $url);
521 }
a7488080 522 elseif (empty($pcpInfo['is_active'])) {
6a488035
TO
523 $statusMessage = ts('The Personal Campaign Page you have just visited is currently inactive. However you can still support the campaign here.');
524 CRM_Core_Error::statusBounce($statusMessage, $url);
525 }
526 // Check if we're in range for contribution page start and end dates. for events, check if after event end date
527 elseif (($startDate && $startDate > $now) || ($endDate && $endDate < $now)) {
528 $customStartDate = CRM_Utils_Date::customFormat(CRM_Utils_Array::value('start_date', $entity));
529 $customEndDate = CRM_Utils_Date::customFormat(CRM_Utils_Array::value('end_date', $entity));
530 if ($startDate && $endDate) {
531 $statusMessage = ts('The Personal Campaign Page you have just visited is only active from %1 to %2. However you can still support the campaign here.',
532 array(1 => $customStartDate, 2 => $customEndDate)
533 );
534 CRM_Core_Error::statusBounce($statusMessage, $url);
535 }
536 elseif ($startDate) {
537 $statusMessage = ts('The Personal Campaign Page you have just visited will be active beginning on %1. However you can still support the campaign here.', array(1 => $customStartDate));
538 CRM_Core_Error::statusBounce($statusMessage, $url);
539 }
540 elseif ($endDate) {
541 if ($component == 'event') {
542 // Target_entity is an event and the event is over, redirect to event info instead of event registration page.
543 $url = CRM_Utils_System::url('civicrm/event/info',
544 "reset=1&id={$pcpBlock['entity_id']}",
545 FALSE, NULL, FALSE, TRUE
546 );
547 $statusMessage = ts('The event linked to the Personal Campaign Page you have just visited is over (as of %1).', array(1 => $customEndDate));
548 CRM_Core_Error::statusBounce($statusMessage, $url);
9d9922e7 549 }
550 else {
6a488035
TO
551 $statusMessage = ts('The Personal Campaign Page you have just visited is no longer active (as of %1). However you can still support the campaign here.', array(1 => $customEndDate));
552 CRM_Core_Error::statusBounce($statusMessage, $url);
553 }
554 }
555 }
556
557 return array(
558 'pcpId' => $pcpId,
559 'pcpBlock' => $pcpBlock,
560 'pcpInfo' => $pcpInfo,
561 );
562 }
563
564 /**
54957108 565 * Approve / Reject the campaign page.
6a488035 566 *
db95eff6
TO
567 * @param int $id
568 * Campaign page id.
6a488035 569 *
ad37ac8e 570 * @param bool $is_active
6a488035 571 */
00be9182 572 public static function setIsActive($id, $is_active) {
6a488035
TO
573 switch ($is_active) {
574 case 0:
575 $is_active = 3;
576 break;
577
578 case 1:
579 $is_active = 2;
580 break;
581 }
582
583 CRM_Core_DAO::setFieldValue('CRM_PCP_DAO_PCP', $id, 'status_id', $is_active);
584
9d9922e7 585 $pcpTitle = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', $id, 'title');
586 $pcpPageType = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', $id, 'page_type');
6a488035 587
2158332a 588 $pcpStatus = CRM_Core_OptionGroup::values("pcp_status");
6a488035
TO
589 $pcpStatus = $pcpStatus[$is_active];
590
9d9922e7 591 CRM_Core_Session::setStatus(ts("%1 status has been updated to %2.", array(
592 1 => $pcpTitle,
21dfd5f5 593 2 => $pcpStatus,
9d9922e7 594 )), 'Status Updated', 'success');
6a488035
TO
595
596 // send status change mail
597 $result = self::sendStatusUpdate($id, $is_active, FALSE, $pcpPageType);
598
599 if ($result) {
600 CRM_Core_Session::setStatus(ts("A notification email has been sent to the supporter."), ts('Email Sent'), 'success');
601 }
602 }
603
604 /**
ad37ac8e 605 * Send notification email to supporter.
606 *
6a488035
TO
607 * 1. when their PCP status is changed by site admin.
608 * 2. when supporter initially creates a Personal Campaign Page ($isInitial set to true).
609 *
db95eff6
TO
610 * @param int $pcpId
611 * Campaign page id.
612 * @param int $newStatus
613 * Pcp status id.
2a6da8d7 614 * @param bool|int $isInitial is it the first time, campaign page has been created by the user
6a488035 615 *
2a6da8d7
EM
616 * @param string $component
617 *
618 * @throws Exception
6a488035 619 * @return null
6a488035 620 */
00be9182 621 public static function sendStatusUpdate($pcpId, $newStatus, $isInitial = FALSE, $component = 'contribute') {
aef123f6
C
622 $pcpStatusName = CRM_Core_OptionGroup::values("pcp_status", FALSE, FALSE, FALSE, NULL, 'name');
623 $pcpStatus = CRM_Core_OptionGroup::values("pcp_status");
6a488035
TO
624 $config = CRM_Core_Config::singleton();
625
626 if (!isset($pcpStatus[$newStatus])) {
627 return FALSE;
628 }
629
630 require_once 'Mail/mime.php';
631
632 //set loginUrl
633 $loginURL = $config->userSystem->getLoginURL();
634
635 // used in subject templates
636 $contribPageTitle = self::getPcpPageTitle($pcpId, $component);
637
638 $tplParams = array(
639 'loginUrl' => $loginURL,
640 'contribPageTitle' => $contribPageTitle,
641 'pcpId' => $pcpId,
642 );
643
644 //get the default domain email address.
645 list($domainEmailName, $domainEmailAddress) = CRM_Core_BAO_Domain::getNameAndEmail();
646
647 if (!$domainEmailAddress || $domainEmailAddress == 'info@EXAMPLE.ORG') {
648 $fixUrl = CRM_Utils_System::url("civicrm/admin/domain", 'action=update&reset=1');
649 CRM_Core_Error::fatal(ts('The site administrator needs to enter a valid \'FROM Email Address\' in <a href="%1">Administer CiviCRM &raquo; Communications &raquo; FROM Email Addresses</a>. The email address used may need to be a valid mail account with your email service provider.', array(1 => $fixUrl)));
650 }
651
652 $receiptFrom = '"' . $domainEmailName . '" <' . $domainEmailAddress . '>';
653
654 // get recipient (supporter) name and email
655 $params = array('id' => $pcpId);
656 CRM_Core_DAO::commonRetrieve('CRM_PCP_DAO_PCP', $params, $pcpInfo);
657 list($name, $address) = CRM_Contact_BAO_Contact_Location::getEmailDetails($pcpInfo['contact_id']);
658
659 // get pcp block info
660 list($blockId, $eid) = self::getPcpBlockEntityId($pcpId, $component);
661 $params = array('id' => $blockId);
662 CRM_Core_DAO::commonRetrieve('CRM_PCP_DAO_PCPBlock', $params, $pcpBlockInfo);
663
664 // assign urls required in email template
aef123f6 665 if ($pcpStatusName[$newStatus] == 'Approved') {
6a488035
TO
666 $tplParams['isTellFriendEnabled'] = $pcpBlockInfo['is_tellfriend_enabled'];
667 if ($pcpBlockInfo['is_tellfriend_enabled']) {
668 $pcpTellFriendURL = CRM_Utils_System::url('civicrm/friend',
669 "reset=1&eid=$pcpId&blockId=$blockId&pcomponent=pcp",
670 TRUE, NULL, FALSE, TRUE
671 );
672 $tplParams['pcpTellFriendURL'] = $pcpTellFriendURL;
673 }
674 }
675 $pcpInfoURL = CRM_Utils_System::url('civicrm/pcp/info',
676 "reset=1&id=$pcpId",
677 TRUE, NULL, FALSE, TRUE
678 );
679 $tplParams['pcpInfoURL'] = $pcpInfoURL;
680 $tplParams['contribPageTitle'] = $contribPageTitle;
681 if ($emails = CRM_Utils_Array::value('notify_email', $pcpBlockInfo)) {
682 $emailArray = explode(',', $emails);
683 $tplParams['pcpNotifyEmailAddress'] = $emailArray[0];
684 }
685 // get appropriate message based on status
686 $tplParams['pcpStatus'] = $pcpStatus[$newStatus];
687
688 $tplName = $isInitial ? 'pcp_supporter_notify' : 'pcp_status_change';
689
c6327d7d 690 list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate(
6a488035
TO
691 array(
692 'groupName' => 'msg_tpl_workflow_contribution',
693 'valueName' => $tplName,
694 'contactId' => $pcpInfo['contact_id'],
695 'tplParams' => $tplParams,
696 'from' => $receiptFrom,
697 'toName' => $name,
698 'toEmail' => $address,
699 )
700 );
701 return $sent;
702 }
703
704 /**
ad37ac8e 705 * Enable / Disable the campaign page.
6a488035 706 *
db95eff6
TO
707 * @param int $id
708 * Campaign page id.
6a488035 709 *
ad37ac8e 710 * @param bool $is_active
6a488035 711 * @return null
6a488035 712 */
00be9182 713 public static function setDisable($id, $is_active) {
6a488035
TO
714 return CRM_Core_DAO::setFieldValue('CRM_PCP_DAO_PCP', $id, 'is_active', $is_active);
715 }
716
717 /**
eceb18cc 718 * Get pcp block is active.
6a488035 719 *
c490a46a 720 * @param int $pcpId
2a6da8d7 721 * @param $component
6a488035
TO
722 *
723 * @return int
6a488035 724 */
00be9182 725 public static function getStatus($pcpId, $component) {
6a488035
TO
726 $query = "
727 SELECT pb.is_active
728 FROM civicrm_pcp pcp
729 LEFT JOIN civicrm_pcp_block pb ON ( pcp.page_id = pb.entity_id )
730 WHERE pcp.id = %1
731 AND pb.entity_table = %2";
732
733 $entity_table = self::getPcpEntityTable($component);
734
735 $params = array(1 => array($pcpId, 'Integer'), 2 => array($entity_table, 'String'));
736 return CRM_Core_DAO::singleValueQuery($query, $params);
737 }
738
739 /**
eceb18cc 740 * Get pcp block is enabled for component page.
6a488035 741 *
c490a46a 742 * @param int $pageId
2a6da8d7 743 * @param $component
6a488035 744 *
c490a46a 745 * @return string
6a488035 746 */
00be9182 747 public static function getPcpBlockStatus($pageId, $component) {
6a488035
TO
748 $query = "
749 SELECT pb.link_text as linkText
eea141c0
DG
750 FROM civicrm_pcp_block pb
751 WHERE pb.is_active = 1 AND
752 pb.entity_id = %1 AND
753 pb.entity_table = %2";
6a488035
TO
754
755 $entity_table = self::getPcpEntityTable($component);
756
757 $params = array(1 => array($pageId, 'Integer'), 2 => array($entity_table, 'String'));
758 return CRM_Core_DAO::singleValueQuery($query, $params);
759 }
760
761 /**
eceb18cc 762 * Find out if the PCP block is in use by one or more PCP page.
6a488035 763 *
db95eff6
TO
764 * @param int $id
765 * Pcp block id.
6a488035 766 *
d5cc0fc2 767 * @return Bool
6a488035 768 */
00be9182 769 public static function getPcpBlockInUse($id) {
6a488035
TO
770 $query = "
771 SELECT count(*)
772 FROM civicrm_pcp pcp
773 WHERE pcp.pcp_block_id = %1";
774
775 $params = array(1 => array($id, 'Integer'));
776 $result = CRM_Core_DAO::singleValueQuery($query, $params);
777 return $result > 0;
778 }
779
780 /**
100fef9d 781 * Get email is enabled for supporter's profile
6a488035 782 *
db95eff6
TO
783 * @param int $profileId
784 * Supporter's profile id.
6a488035 785 *
d5cc0fc2 786 * @return bool
6a488035 787 */
00be9182 788 public static function checkEmailProfile($profileId) {
6a488035
TO
789 $query = "
790SELECT field_name
791FROM civicrm_uf_field
792WHERE field_name like 'email%' And is_active = 1 And uf_group_id = %1";
793
794 $params = array(1 => array($profileId, 'Integer'));
795 $dao = CRM_Core_DAO::executeQuery($query, $params);
796 if (!$dao->fetch()) {
797 return TRUE;
798 }
799 return FALSE;
800 }
801
802 /**
eceb18cc 803 * Obtain the title of page associated with a pcp.
6a488035 804 *
c490a46a 805 * @param int $pcpId
dd244018
EM
806 * @param $component
807 *
6a488035 808 * @return int
6a488035 809 */
00be9182 810 public static function getPcpPageTitle($pcpId, $component) {
6a488035
TO
811 if ($component == 'contribute') {
812 $query = "
813 SELECT cp.title
814 FROM civicrm_pcp pcp
815 LEFT JOIN civicrm_contribution_page as cp ON ( cp.id = pcp.page_id )
816 WHERE pcp.id = %1";
817 }
818 elseif ($component == 'event') {
819 $query = "
820 SELECT ce.title
821 FROM civicrm_pcp pcp
822 LEFT JOIN civicrm_event as ce ON ( ce.id = pcp.page_id )
823 WHERE pcp.id = %1";
824 }
825
826 $params = array(1 => array($pcpId, 'Integer'));
827 return CRM_Core_DAO::singleValueQuery($query, $params);
828 }
829
830 /**
100fef9d 831 * Get pcp block & entity id given pcp id
6a488035 832 *
c490a46a 833 * @param int $pcpId
dd244018
EM
834 * @param $component
835 *
d5cc0fc2 836 * @return string
6a488035 837 */
00be9182 838 public static function getPcpBlockEntityId($pcpId, $component) {
6a488035
TO
839 $entity_table = self::getPcpEntityTable($component);
840
841 $query = "
842SELECT pb.id as pcpBlockId, pb.entity_id
843FROM civicrm_pcp pcp
844LEFT JOIN civicrm_pcp_block pb ON ( pb.entity_id = pcp.page_id AND pb.entity_table = %2 )
845WHERE pcp.id = %1";
846
847 $params = array(1 => array($pcpId, 'Integer'), 2 => array($entity_table, 'String'));
848 $dao = CRM_Core_DAO::executeQuery($query, $params);
849 if ($dao->fetch()) {
850 return array($dao->pcpBlockId, $dao->entity_id);
851 }
852
853 return array();
854 }
855
856 /**
100fef9d 857 * Get pcp entity table given a component.
6a488035 858 *
2a6da8d7
EM
859 * @param $component
860 *
d5cc0fc2 861 * @return string
6a488035 862 */
00be9182 863 public static function getPcpEntityTable($component) {
6a488035
TO
864 $entity_table_map = array(
865 'event' => 'civicrm_event',
866 'civicrm_event' => 'civicrm_event',
867 'contribute' => 'civicrm_contribution_page',
868 'civicrm_contribution_page' => 'civicrm_contribution_page',
869 );
870 return isset($entity_table_map[$component]) ? $entity_table_map[$component] : FALSE;
871 }
872
873 /**
eceb18cc 874 * Get supporter profile id.
6a488035 875 *
c490a46a 876 * @param int $component_id
fd31fa4c
EM
877 * @param string $component
878 *
6a488035 879 * @return int
6a488035 880 */
c1204160 881 public static function getSupporterProfileId($component_id, $component = 'contribute') {
6a488035
TO
882 $entity_table = self::getPcpEntityTable($component);
883
884 $query = "
885SELECT pcp.supporter_profile_id
886FROM civicrm_pcp_block pcp
887INNER JOIN civicrm_uf_group ufgroup
888 ON pcp.supporter_profile_id = ufgroup.id
889 WHERE pcp.entity_id = %1
890 AND pcp.entity_table = %2
891 AND ufgroup.is_active = 1";
892
893 $params = array(1 => array($component_id, 'Integer'), 2 => array($entity_table, 'String'));
894 if (!$supporterProfileId = CRM_Core_DAO::singleValueQuery($query, $params)) {
895 CRM_Core_Error::fatal(ts('Supporter profile is not set for this Personal Campaign Page or the profile is disabled. Please contact the site administrator if you need assistance.'));
896 }
897 else {
898 return $supporterProfileId;
899 }
900 }
96025800 901
12f92dbd 902 /**
eceb18cc 903 * Get owner notification id.
12f92dbd 904 *
5fe87df6 905 * @param int $component_id
12f92dbd
N
906 * @param $component
907 *
12f92dbd
N
908 * @return int
909 */
5fe87df6
N
910 public static function getOwnerNotificationId($component_id, $component = 'contribute') {
911 $entity_table = self::getPcpEntityTable($component);
12f92dbd
N
912 $query = "
913 SELECT pb.owner_notify_id
914 FROM civicrm_pcp_block pb
5fe87df6
N
915 WHERE pb.entity_id = %1 AND pb.entity_table = %2";
916 $params = array(1 => array($component_id, 'Integer'), 2 => array($entity_table, 'String'));
917 if (!$ownerNotificationId = CRM_Core_DAO::singleValueQuery($query, $params)) {
918 CRM_Core_Error::fatal(ts('Owner Notification is not set for this Personal Campaign Page. Please contact the site administrator if you need assistance.'));
919 }
920 else {
921 return $ownerNotificationId;
922 }
12f92dbd 923 }
eceb18cc 924
6a488035 925}