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