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