CRM-13890 : participant_status
[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 if ($pcpSupporter = CRM_PCP_BAO_PCP::displayName($pcpId)) {
422 if ($pcpInfo['page_type'] == 'event') {
423 $pcp_supporter_text = ts('This event registration is being made thanks to effort of <strong>%1</strong>, who supports our campaign. ', array(1 => $pcpSupporter));
424 $text = CRM_PCP_BAO_PCP::getPcpBlockStatus($pcpInfo['page_id'], 'event');
425 if(!empty($text)) {
426 $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!";
427 }
428 }
429 else {
430 $pcp_supporter_text = ts('This contribution is being made thanks to effort of <strong>%1</strong>, who supports our campaign. ', array(1 => $pcpSupporter));
431 $text = CRM_PCP_BAO_PCP::getPcpBlockStatus($pcpInfo['page_id'], 'contribute');
432 if(!empty($text)) {
433 $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!";
434 }
435 }
436
437 $page->assign('pcpSupporterText', $pcp_supporter_text);
438 }
439 $page->assign('pcp', TRUE);
440
441 // build honor roll fields for registration form if supporter has honor roll enabled for their PCP
442 if ($pcpInfo['is_honor_roll']) {
443 $page->assign('is_honor_roll', TRUE);
444 $page->add('checkbox', 'pcp_display_in_roll', ts('Show my support in the public honor roll'), NULL, NULL,
445 array('onclick' => "showHideByValue('pcp_display_in_roll','','nameID|nickID|personalNoteID','block','radio',false); pcpAnonymous( );")
446 );
447 $extraOption = array('onclick' => "return pcpAnonymous( );");
448 $elements = array();
449 $elements[] = & $page->createElement('radio', NULL, '', ts('Include my name and message'), 0, $extraOption);
450 $elements[] = & $page->createElement('radio', NULL, '', ts('List my support anonymously'), 1, $extraOption);
451 $page->addGroup($elements, 'pcp_is_anonymous', NULL, '&nbsp;&nbsp;&nbsp;');
452 $page->_defaults['pcp_is_anonymous'] = 0;
453
454 $page->add('text', 'pcp_roll_nickname', ts('Name'), array('maxlength' => 30));
455 $page->add('textarea', "pcp_personal_note", ts('Personal Note'), array('style' => 'height: 3em; width: 40em;'));
456 }
457 else {
458 $page->assign('is_honor_roll', FALSE);
459 }
460 }
461
462 /*
463 * Process a PCP contribution/
464 */
465 public static function handlePcp($pcpId, $component, $entity) {
466
467 self::getPcpEntityTable($component);
468
469 if (!$pcpId) {
470 return FALSE;
471 }
472
473 $approvedId = CRM_Core_OptionGroup::getValue('pcp_status', 'Approved', 'name');
474
475 $pcpStatus = CRM_Core_OptionGroup::values("pcp_status");
476
477 $params = array('id' => $pcpId);
478 CRM_Core_DAO::commonRetrieve('CRM_PCP_DAO_PCP', $params, $pcpInfo);
479
480 $params = array('id' => $pcpInfo['pcp_block_id']);
481 CRM_Core_DAO::commonRetrieve('CRM_PCP_DAO_PCPBlock', $params, $pcpBlock);
482
483 $params = array('id' => $pcpInfo['page_id']);
484 $now = time();
485
486 if ($component == 'event') {
487 // figure out where to redirect if an exception occurs below based on target entity
488 $urlBase = 'civicrm/event/register';
489
490 // ignore startDate for events - PCP's can be active long before event start date
491 $startDate = 0;
492 $endDate = CRM_Utils_Date::unixTime(CRM_Utils_Array::value('end_date', $entity));
493 }
494 elseif ($component == 'contribute') {
495 $urlBase = 'civicrm/contribute/transact';
496 //start and end date of the contribution page
497 $startDate = CRM_Utils_Date::unixTime(CRM_Utils_Array::value('start_date', $entity));
498 $endDate = CRM_Utils_Date::unixTime(CRM_Utils_Array::value('end_date', $entity));
499 }
500
501 // define redirect url back to contrib page or event if needed
502 $url = CRM_Utils_System::url($urlBase, "reset=1&id={$pcpBlock['entity_id']}", FALSE, NULL, FALSE, TRUE );
503
504 if ($pcpBlock['target_entity_id'] != $entity['id']) {
505 $statusMessage = ts('This page is not related to the Personal Campaign Page you have just visited. However you can still make a contribution here.');
506 CRM_Core_Error::statusBounce($statusMessage, $url);
507 }
508 elseif ($pcpInfo['status_id'] != $approvedId) {
509 $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']]));
510 CRM_Core_Error::statusBounce($statusMessage, $url);
511 }
512 elseif (!CRM_Utils_Array::value('is_active', $pcpBlock)) {
513 $statusMessage = ts('Personal Campaign Pages are currently not enabled for this contribution page. However you can still support the campaign here.');
514 CRM_Core_Error::statusBounce($statusMessage, $url);
515 }
516 elseif (!CRM_Utils_Array::value('is_active', $pcpInfo)) {
517 $statusMessage = ts('The Personal Campaign Page you have just visited is currently inactive. However you can still support the campaign here.');
518 CRM_Core_Error::statusBounce($statusMessage, $url);
519 }
520 // Check if we're in range for contribution page start and end dates. for events, check if after event end date
521 elseif (($startDate && $startDate > $now) || ($endDate && $endDate < $now)) {
522 $customStartDate = CRM_Utils_Date::customFormat(CRM_Utils_Array::value('start_date', $entity));
523 $customEndDate = CRM_Utils_Date::customFormat(CRM_Utils_Array::value('end_date', $entity));
524 if ($startDate && $endDate) {
525 $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.',
526 array(1 => $customStartDate, 2 => $customEndDate)
527 );
528 CRM_Core_Error::statusBounce($statusMessage, $url);
529 }
530 elseif ($startDate) {
531 $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));
532 CRM_Core_Error::statusBounce($statusMessage, $url);
533 }
534 elseif ($endDate) {
535 if ($component == 'event') {
536 // Target_entity is an event and the event is over, redirect to event info instead of event registration page.
537 $url = CRM_Utils_System::url('civicrm/event/info',
538 "reset=1&id={$pcpBlock['entity_id']}",
539 FALSE, NULL, FALSE, TRUE
540 );
541 $statusMessage = ts('The event linked to the Personal Campaign Page you have just visited is over (as of %1).', array(1 => $customEndDate));
542 CRM_Core_Error::statusBounce($statusMessage, $url);
543 }
544 else {
545 $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));
546 CRM_Core_Error::statusBounce($statusMessage, $url);
547 }
548 }
549 }
550
551 return array(
552 'pcpId' => $pcpId,
553 'pcpBlock' => $pcpBlock,
554 'pcpInfo' => $pcpInfo,
555 );
556 }
557
558 /**
559 * Function to Approve / Reject the campaign page
560 *
561 * @param int $id campaign page id
562 *
563 * @return null
564 * @access public
565 * @static
566 *
567 */
568 static function setIsActive($id, $is_active) {
569 switch ($is_active) {
570 case 0:
571 $is_active = 3;
572 break;
573
574 case 1:
575 $is_active = 2;
576 break;
577 }
578
579 CRM_Core_DAO::setFieldValue('CRM_PCP_DAO_PCP', $id, 'status_id', $is_active);
580
581 $pcpTitle = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', $id, 'title');
582 $pcpPageType = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', $id, 'page_type');
583
584 $pcpStatus = CRM_Core_OptionGroup::values("pcp_status");
585 $pcpStatus = $pcpStatus[$is_active];
586
587 CRM_Core_Session::setStatus(ts("%1 status has been updated to %2.", array(
588 1 => $pcpTitle,
589 2 => $pcpStatus
590 )), 'Status Updated', 'success');
591
592 // send status change mail
593 $result = self::sendStatusUpdate($id, $is_active, FALSE, $pcpPageType);
594
595 if ($result) {
596 CRM_Core_Session::setStatus(ts("A notification email has been sent to the supporter."), ts('Email Sent'), 'success');
597 }
598 }
599
600 /**
601 * Function to send notfication email to supporter
602 * 1. when their PCP status is changed by site admin.
603 * 2. when supporter initially creates a Personal Campaign Page ($isInitial set to true).
604 *
605 * @param int $pcpId campaign page id
606 * @param int $newStatus pcp status id
607 * @param int $isInitial is it the first time, campaign page has been created by the user
608 *
609 * @return null
610 * @access public
611 * @static
612 *
613 */
614 static function sendStatusUpdate($pcpId, $newStatus, $isInitial = FALSE, $component = 'contribute') {
615 $pcpStatus = CRM_Core_OptionGroup::values("pcp_status");
616 $config = CRM_Core_Config::singleton();
617
618 if (!isset($pcpStatus[$newStatus])) {
619 return FALSE;
620 }
621
622 require_once 'Mail/mime.php';
623
624 //set loginUrl
625 $loginURL = $config->userSystem->getLoginURL();
626
627 // used in subject templates
628 $contribPageTitle = self::getPcpPageTitle($pcpId, $component);
629
630 $tplParams = array(
631 'loginUrl' => $loginURL,
632 'contribPageTitle' => $contribPageTitle,
633 'pcpId' => $pcpId,
634 );
635
636 //get the default domain email address.
637 list($domainEmailName, $domainEmailAddress) = CRM_Core_BAO_Domain::getNameAndEmail();
638
639 if (!$domainEmailAddress || $domainEmailAddress == 'info@EXAMPLE.ORG') {
640 $fixUrl = CRM_Utils_System::url("civicrm/admin/domain", 'action=update&reset=1');
641 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)));
642 }
643
644 $receiptFrom = '"' . $domainEmailName . '" <' . $domainEmailAddress . '>';
645
646 // get recipient (supporter) name and email
647 $params = array('id' => $pcpId);
648 CRM_Core_DAO::commonRetrieve('CRM_PCP_DAO_PCP', $params, $pcpInfo);
649 list($name, $address) = CRM_Contact_BAO_Contact_Location::getEmailDetails($pcpInfo['contact_id']);
650
651 // get pcp block info
652 list($blockId, $eid) = self::getPcpBlockEntityId($pcpId, $component);
653 $params = array('id' => $blockId);
654 CRM_Core_DAO::commonRetrieve('CRM_PCP_DAO_PCPBlock', $params, $pcpBlockInfo);
655
656 // assign urls required in email template
657 if ($pcpStatus[$newStatus] == 'Approved') {
658 $tplParams['isTellFriendEnabled'] = $pcpBlockInfo['is_tellfriend_enabled'];
659 if ($pcpBlockInfo['is_tellfriend_enabled']) {
660 $pcpTellFriendURL = CRM_Utils_System::url('civicrm/friend',
661 "reset=1&eid=$pcpId&blockId=$blockId&pcomponent=pcp",
662 TRUE, NULL, FALSE, TRUE
663 );
664 $tplParams['pcpTellFriendURL'] = $pcpTellFriendURL;
665 }
666 }
667 $pcpInfoURL = CRM_Utils_System::url('civicrm/pcp/info',
668 "reset=1&id=$pcpId",
669 TRUE, NULL, FALSE, TRUE
670 );
671 $tplParams['pcpInfoURL'] = $pcpInfoURL;
672 $tplParams['contribPageTitle'] = $contribPageTitle;
673 if ($emails = CRM_Utils_Array::value('notify_email', $pcpBlockInfo)) {
674 $emailArray = explode(',', $emails);
675 $tplParams['pcpNotifyEmailAddress'] = $emailArray[0];
676 }
677 // get appropriate message based on status
678 $tplParams['pcpStatus'] = $pcpStatus[$newStatus];
679
680 $tplName = $isInitial ? 'pcp_supporter_notify' : 'pcp_status_change';
681
682 list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate(
683 array(
684 'groupName' => 'msg_tpl_workflow_contribution',
685 'valueName' => $tplName,
686 'contactId' => $pcpInfo['contact_id'],
687 'tplParams' => $tplParams,
688 'from' => $receiptFrom,
689 'toName' => $name,
690 'toEmail' => $address,
691 )
692 );
693 return $sent;
694 }
695
696 /**
697 * Function to Enable / Disable the campaign page
698 *
699 * @param int $id campaign page id
700 *
701 * @return null
702 * @access public
703 * @static
704 *
705 */
706 static function setDisable($id, $is_active) {
707 return CRM_Core_DAO::setFieldValue('CRM_PCP_DAO_PCP', $id, 'is_active', $is_active);
708 }
709
710 /**
711 * Function to get pcp block is active
712 *
713 * @param int $id campaign page id
714 *
715 * @return int
716 * @access public
717 * @static
718 *
719 */
720 static function getStatus($pcpId, $component) {
721 $query = "
722 SELECT pb.is_active
723 FROM civicrm_pcp pcp
724 LEFT JOIN civicrm_pcp_block pb ON ( pcp.page_id = pb.entity_id )
725 WHERE pcp.id = %1
726 AND pb.entity_table = %2";
727
728 $entity_table = self::getPcpEntityTable($component);
729
730 $params = array(1 => array($pcpId, 'Integer'), 2 => array($entity_table, 'String'));
731 return CRM_Core_DAO::singleValueQuery($query, $params);
732 }
733
734 /**
735 * Function to get pcp block is enabled for component page
736 *
737 * @param int $id contribution page id
738 *
739 * @return String
740 * @access public
741 * @static
742 *
743 */
744 static function getPcpBlockStatus($pageId, $component) {
745 $query = "
746 SELECT pb.link_text as linkText
747 FROM civicrm_pcp_block pb
748 WHERE pb.is_active = 1 AND
749 pb.entity_id = %1 AND
750 pb.entity_table = %2";
751
752 $entity_table = self::getPcpEntityTable($component);
753
754 $params = array(1 => array($pageId, 'Integer'), 2 => array($entity_table, 'String'));
755 return CRM_Core_DAO::singleValueQuery($query, $params);
756 }
757
758 /**
759 * Function to find out if the PCP block is in use by one or more PCP page
760 *
761 * @param int $id pcp block id
762 *
763 * @return Boolean
764 * @access public
765 * @static
766 *
767 */
768 static function getPcpBlockInUse($id) {
769 $query = "
770 SELECT count(*)
771 FROM civicrm_pcp pcp
772 WHERE pcp.pcp_block_id = %1";
773
774 $params = array(1 => array($id, 'Integer'));
775 $result = CRM_Core_DAO::singleValueQuery($query, $params);
776 return $result > 0;
777 }
778
779 /**
780 * Function to get email is enabled for supporter's profile
781 *
782 * @param int $id supporter's profile id
783 *
784 * @return boolean
785 * @access public
786 * @static
787 *
788 */
789 static function checkEmailProfile($profileId) {
790 $query = "
791 SELECT field_name
792 FROM civicrm_uf_field
793 WHERE field_name like 'email%' And is_active = 1 And uf_group_id = %1";
794
795 $params = array(1 => array($profileId, 'Integer'));
796 $dao = CRM_Core_DAO::executeQuery($query, $params);
797 if (!$dao->fetch()) {
798 return TRUE;
799 }
800 return FALSE;
801 }
802
803 /**
804 * Function to obtain the title of page associated with a pcp
805 *
806 * @param int $id campaign page id
807 *
808 * @return int
809 * @access public
810 * @static
811 *
812 */
813 static function getPcpPageTitle($pcpId, $component) {
814 if ($component == 'contribute') {
815 $query = "
816 SELECT cp.title
817 FROM civicrm_pcp pcp
818 LEFT JOIN civicrm_contribution_page as cp ON ( cp.id = pcp.page_id )
819 WHERE pcp.id = %1";
820 }
821 elseif ($component == 'event') {
822 $query = "
823 SELECT ce.title
824 FROM civicrm_pcp pcp
825 LEFT JOIN civicrm_event as ce ON ( ce.id = pcp.page_id )
826 WHERE pcp.id = %1";
827 }
828
829 $params = array(1 => array($pcpId, 'Integer'));
830 return CRM_Core_DAO::singleValueQuery($query, $params);
831 }
832
833 /**
834 * Function to get pcp block & entity id given pcp id
835 *
836 * @param int $id campaign page id
837 *
838 * @return String
839 * @access public
840 * @static
841 *
842 */
843 static function getPcpBlockEntityId($pcpId, $component) {
844 $entity_table = self::getPcpEntityTable($component);
845
846 $query = "
847 SELECT pb.id as pcpBlockId, pb.entity_id
848 FROM civicrm_pcp pcp
849 LEFT JOIN civicrm_pcp_block pb ON ( pb.entity_id = pcp.page_id AND pb.entity_table = %2 )
850 WHERE pcp.id = %1";
851
852 $params = array(1 => array($pcpId, 'Integer'), 2 => array($entity_table, 'String'));
853 $dao = CRM_Core_DAO::executeQuery($query, $params);
854 if ($dao->fetch()) {
855 return array($dao->pcpBlockId, $dao->entity_id);
856 }
857
858 return array();
859 }
860
861 /**
862 * Function to get pcp entity table given a component.
863 *
864 * @param int $id campaign page id
865 *
866 * @return String
867 * @access public
868 * @static
869 *
870 */
871 static function getPcpEntityTable($component) {
872 $entity_table_map = array(
873 'event' => 'civicrm_event',
874 'civicrm_event' => 'civicrm_event',
875 'contribute' => 'civicrm_contribution_page',
876 'civicrm_contribution_page' => 'civicrm_contribution_page',
877 );
878 return isset($entity_table_map[$component]) ? $entity_table_map[$component] : FALSE;
879 }
880
881 /**
882 * Function to get supporter profile id
883 *
884 * @param int $contributionPageId contribution page id
885 *
886 * @return int
887 * @access public
888 *
889 */
890 public function getSupporterProfileId($component_id, $component = 'contribute') {
891 $entity_table = self::getPcpEntityTable($component);
892
893 $query = "
894 SELECT pcp.supporter_profile_id
895 FROM civicrm_pcp_block pcp
896 INNER JOIN civicrm_uf_group ufgroup
897 ON pcp.supporter_profile_id = ufgroup.id
898 WHERE pcp.entity_id = %1
899 AND pcp.entity_table = %2
900 AND ufgroup.is_active = 1";
901
902 $params = array(1 => array($component_id, 'Integer'), 2 => array($entity_table, 'String'));
903 if (!$supporterProfileId = CRM_Core_DAO::singleValueQuery($query, $params)) {
904 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.'));
905 }
906 else {
907 return $supporterProfileId;
908 }
909 }
910 }
911