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