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