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