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