forked from mlutfy/pcpteams
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpcpteams.inc.php
318 lines (266 loc) · 9.09 KB
/
pcpteams.inc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<?php
define('CIVICRM_PCPTEAM_TYPE_INDIVIDUAL', 0);
define('CIVICRM_PCPTEAM_TYPE_TEAM_MEMBER', 1);
define('CIVICRM_PCPTEAM_TYPE_TEAM', 2);
/**
* Helper functions.
*/
/**
* Returns the current pcpblock "team" is_active value.
* e.g. whether the form allows PCP by teams.
*/
function pcpteams_pcpblockteam_getvalue($target_entity_type, $target_entity_id) {
$pcpblock = new CRM_PCP_DAO_PCPBlock();
$pcpblock->target_entity_type = $target_entity_type;
$pcpblock->target_entity_id = $target_entity_id;
if ($pcpblock->find(TRUE)) {
$dao = CRM_Core_DAO::executeQuery("SELECT * FROM civicrm_pcp_block_team WHERE civicrm_pcp_block_id = " . $pcpblock->id);
if ($dao->fetch()) {
return $dao->is_active;
}
}
return FALSE;
}
/**
* Sets the current pcpblock "team" is_active value.
* e.g. whether the form allows PCP by teams.
*/
function pcpteams_pcpblockteam_setvalue($target_entity_type, $target_entity_id, $pcp_team_active) {
$pcpblock = new CRM_PCP_DAO_PCPBlock();
$pcpblock->target_entity_type = $target_entity_type;
$pcpblock->target_entity_id = $target_entity_id;
$pcp_team_active = intval($pcp_team_active);
if ($pcpblock->find(TRUE)) {
$dao = CRM_Core_DAO::executeQuery("SELECT * FROM civicrm_pcp_block_team WHERE civicrm_pcp_block_id = " . $pcpblock->id);
if ($dao->fetch()) {
CRM_Core_DAO::executeQuery("UPDATE civicrm_pcp_block_team SET is_active = " . $pcp_team_active . " WHERE civicrm_pcp_block_id = " . $dao->civicrm_pcp_block_id);
}
else {
CRM_Core_DAO::executeQuery("INSERT INTO civicrm_pcp_block_team (civicrm_pcp_block_id, is_active)
VALUES ({$pcpblock->id}, $pcp_team_active)");
}
}
else {
CRM_Core_Error::fatal(ts('Could not find the PCPBlock for entity: %1 %2', array(1 => $target_entity_id, 2 => $target_entity_type)));
}
}
/**
* Sets the team for a PCP page.
* If the team is NULL, assumes it is a new team.
*
* @param Int $pcp_id ID of the PCP page in civicrm_pcp
* @param Int $pcp_team_id ID of the PCP team (NULL means the page is not part of a team).
* @param Int $pcp_type_id Type of PCP page (CIVICRM_PCPTEAM_TYPE_TEAM, CIVICRM_PCPTEAM_TYPE_TEAM_MEMBER or CIVICRM_PCPTEAM_TYPE_INDIVIDUAL).
* @param Boolean $notifications Send e-mail notifications to the pcp page owner for each contribution received.
* @returns void.
*/
function pcpteams_setteam($pcp_id, $pcp_team_id, $pcp_type_id, $notifications = 0) {
// QuickForms might put null in here, if it was not checked.
if (! $notifications) {
$notifications = 0;
}
// If it is a team page, make sure we do not allow to be part of another team
if ($pcp_type_id == CIVICRM_PCPTEAM_TYPE_TEAM || $pcp_type_id == CIVICRM_PCPTEAM_TYPE_INDIVIDUAL) {
$pcp_team_id = NULL;
}
// Strict validation of the type of PCP id, since we don't want bad data in the DB.
$valid_team_types = array(
CIVICRM_PCPTEAM_TYPE_TEAM,
CIVICRM_PCPTEAM_TYPE_TEAM_MEMBER,
CIVICRM_PCPTEAM_TYPE_INDIVIDUAL,
);
if (! in_array($pcp_type_id, $valid_team_types)) {
CRM_Core_Error::fatal('Invalid PCP type received.');
}
// Check if the PCP page already has a record associating it (or not) to a team.
$params = array(
1 => array($pcp_id, 'Positive'),
);
$dao = CRM_Core_DAO::executeQuery("SELECT * FROM civicrm_pcp_team WHERE status_id = 1 AND civicrm_pcp_id = %1", $params);
if ($dao->fetch()) {
// FIXME: Consider options for updating
CRM_Core_DAO::executeQuery("
UPDATE civicrm_pcp_team
SET status_id = 1,
civicrm_pcp_id_parent = " . ($pcp_team_id ? $pcp_team_id : 'NULL') .
", type_id = $pcp_type_id
WHERE civicrm_pcp_id = " . $pcp_id
);
// If we were a team before, then update children
if ($dao->type_id == CIVICRM_PCPTEAM_TYPE_TEAM && $pcp_type_id != CIVICRM_PCPTEAM_TYPE_TEAM) {
CRM_Core_DAO::executeQuery("
UPDATE civicrm_pcp_team
SET civicrm_pcp_id_parent = NULL
, type_id = " . CIVICRM_PCPTEAM_TYPE_INDIVIDUAL . "
WHERE civicrm_pcp_id_parent = " . $pcp_id
);
}
}
else {
if ($pcp_team_id) {
$sql = "INSERT INTO civicrm_pcp_team (civicrm_pcp_id, civicrm_pcp_id_parent, status_id, type_id, notify_on_contrib)
VALUES (%1, %2, 1, %3, %4)";
$params = array(
1 => array($pcp_id, 'Positive'),
2 => array($pcp_team_id, 'Integer'),
3 => array($pcp_type_id, 'Integer'),
4 => array($notifications, 'Integer'),
);
CRM_Core_DAO::executeQuery($sql, $params);
}
else {
$sql = "INSERT INTO civicrm_pcp_team (civicrm_pcp_id, civicrm_pcp_id_parent, status_id, type_id, notify_on_contrib)
VALUES (%1, NULL, 1, %3, %4)";
$params = array(
1 => array($pcp_id, 'Positive'),
3 => array($pcp_type_id, 'Integer'),
4 => array($notifications, 'Integer'),
);
CRM_Core_DAO::executeQuery($sql, $params);
}
}
}
/**
* Get the civicrm_pcp_team record.
*/
function pcpteams_getteaminfo($pcp_id) {
$pcp_id = intval($pcp_id);
$dao = CRM_Core_DAO::executeQuery("SELECT * FROM civicrm_pcp_team WHERE civicrm_pcp_id = " . $pcp_id);
if ($dao->fetch()) {
return $dao;
}
return NULL;
}
/**
* Returns a list of PCP-Teams.
*
* @param String $component_page_type
* Contribute/event (although right now only contribute is fully supported).
* @param Int $component_page_id
* ID of the contribution page for which we want to list teams.
* @returns Array
* Returns a sorted list of teams, keyed on the PCP ID.
*/
function pcpteams_getteamnames($component_page_type = 'contribute', $component_page_id = NULL) {
$teams = array();
// Since 4.6, editing a contribution PCP page seems to not have the $form->_component.
if (! $component_page_type) {
$component_page_type = 'contribute';
}
$sql = "
SELECT pcp.id, pcp.title
FROM civicrm_pcp_team t
LEFT JOIN civicrm_pcp pcp ON (t.civicrm_pcp_id = pcp.id)
WHERE civicrm_pcp_id_parent IS NULL
AND type_id = %1
AND pcp.is_active = 1
AND pcp.page_type = %2";
$params = array(
1 => array(CIVICRM_PCPTEAM_TYPE_TEAM, 'Positive'),
2 => array($component_page_type, 'String'),
);
if ($component_page_id) {
$sql .= ' AND pcp.page_id = %3';
$params[3] = array($component_page_id, 'Positive');
}
$dao = CRM_Core_DAO::executeQuery($sql, $params);
while ($dao->fetch()) {
$teams[$dao->id] = $dao->title;
}
natcasesort($teams);
return $teams;
}
/**
* Returns the name of a PCP team by ID
*/
function pcpteams_getteamname($pcp_id) {
$teams = array();
$pcp_id = intval($pcp_id);
$pcp_team_info = pcpteams_getteaminfo($pcp_id);
if ($pcp_team_info->civicrm_pcp_id_parent) {
$pcp = new CRM_PCP_DAO_PCP();
$pcp->id = $pcp_team_info->civicrm_pcp_id_parent;
if ($pcp->find(TRUE)) {
return $pcp->title;
}
}
return '';
}
/**
* Returns the PCP team members, if any.
* We keep the result cached because it can be called multiple times in a page.
* Ex: for the member listing, and "total amount raised".
*/
function pcpteams_getmembers($pcp_id, $show_non_approved = FALSE) {
static $members = array();
$pcp_id = intval($pcp_id);
if (isset($members[$pcp_id])) {
return $members[$pcp_id];
}
// Need to set the array for when there are no members of the team
$members[$pcp_id] = array();
// Get the status_id for 'approved'
$pcpStatus = CRM_Contribute_PseudoConstant::pcpStatus();
$approved = CRM_Utils_Array::key(ts('Approved'), $pcpStatus);
// Get the members of the team
$dao = CRM_Core_DAO::executeQuery("
SELECT team.civicrm_pcp_id as id, member.title, member.is_active
FROM civicrm_pcp_team team
INNER JOIN civicrm_pcp member ON (member.id = team.civicrm_pcp_id)
WHERE civicrm_pcp_id_parent = " . $pcp_id
. ($show_non_approved ? '' : " AND team.status_id = 1 ")
. ' AND member.status_id = ' . $approved
. ' ORDER BY member.title asc '
);
while ($dao->fetch()) {
$members[$pcp_id][$dao->id] = array(
'title' => $dao->title,
'amount' => CRM_PCP_BAO_PCP::thermoMeter($dao->id),
'is_active' => $dao->is_active,
);
}
return $members[$pcp_id];
}
/**
* Calculates the amount raised for a team.
* For individuals we can used directly CRM_PCP_BAO_PCP::thermoMeter($pcp_id)
*/
function pcpteams_getamountraised($pcp_id) {
$total = 0;
$members = pcpteams_getmembers($pcp_id);
foreach ($members as $key => $val) {
$total += $val['amount'];
}
return $total;
}
/**
* Get the contact given the userid
*/
function _pcpteams_get_contact ($userId = NULL) {
$contact_array = NULL;
if (!isset($userId)) {
return $contact_array;
}
$standard_fields = array(
'contact_id',
'contact_type',
'contact_sub_type',
'sort_name',
'display_name',
'first_name',
'last_name',
'id' ,
);
$params = array(
'version' => 3,
'id' => $userId,
'return' => $standard_fields,
'sequential' => '0'
);
$contacts = civicrm_api('Contact', 'get', $params);
if ($contacts['is_error'] == 1) {
return NULL;
}
$contact_array = $contacts['values'][$userId];
return $contact_array;
}