-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcivithermometer.php
235 lines (215 loc) · 7.59 KB
/
civithermometer.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
<?php
require_once 'civithermometer.civix.php';
use CRM_Civithermometer_ExtensionUtil as E;
/**
* Implements hook_civicrm_config().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_config/
*/
function civithermometer_civicrm_config(&$config) {
_civithermometer_civix_civicrm_config($config);
}
/**
* Implements hook_civicrm_install().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_install
*/
function civithermometer_civicrm_install() {
_civithermometer_civix_civicrm_install();
}
/**
* Implements hook_civicrm_enable().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_enable
*/
function civithermometer_civicrm_enable() {
_civithermometer_civix_civicrm_enable();
}
// --- Functions below this ship commented out. Uncomment as required. ---
/**
* Implements hook_civicrm_preProcess().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_preProcess
*
// */
/**
* Implements hook_civicrm_navigationMenu().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_navigationMenu
*/
function civithermometer_civicrm_navigationMenu(&$menu) {
_civithermometer_civix_insert_navigation_menu($menu, 'Administer/CiviContribute', [
'label' => E::ts('CiviThermometer Settings'),
'name' => 'civithermometer_settings',
'url' => 'civicrm/admin/thermometer',
'permission' => 'administer CiviCRM',
'operator' => 'OR',
'separator' => 0,
]);
_civithermometer_civix_navigationMenu($menu);
}
function civithermometer_civicrm_tabset($tabsetName, &$tabs, $context) {
// check if the tab set is Contribution page manage
if ($tabsetName == 'civicrm/admin/contribute') {
if (!empty($context['contribution_page_id'])) {
$contribID = $context['contribution_page_id'];
$tab['thermometer'] = [
'title' => ts('Thermometer'),
'link' => NULL,
'valid' => 1,
'active' => 1,
'current' => false,
];
// Insert this tab into position 2
$tabs = array_merge(
array_slice($tabs, 0, 2),
$tab,
array_slice($tabs, 2)
);
}
if (!empty($context['urlString']) && !empty($context['urlParams'])) {
$tabs[CRM_Core_Action::VIEW] = [
'title' => ts('Thermometer'),
'name' => ts('Thermometer'),
'url' => $context['urlString'] . 'thermometer',
'qs' => $context['urlParams'],
'uniqueName' => 'thermometer',
];
}
}
}
function civithermometer_civicrm_entityTypes(&$entityTypes) {
$entityTypes['ContributionPage']['fields_callback'][] = function($class, &$fields) {
$fields['thermometer_is_enabled'] = [
'name' => 'thermometer_is_enabled',
'title' => E::ts('Add thermometer to the page'),
'type' => CRM_Utils_Type::T_BOOLEAN,
'entity' => 'ContributionPage',
'add' => '5.0',
'bao' => 'CRM_Contribute_BAO_ContributionPage',
'localizable' => 0,
'html' => [
'type' => 'CheckBox',
],
];
$fields['thermometer_is_double'] = [
'name' => 'thermometer_is_double',
'title' => E::ts('Is this a double your donation thermometer? (optional)'),
'type' => CRM_Utils_Type::T_BOOLEAN,
'entity' => 'ContributionPage',
'add' => '5.0',
'bao' => 'CRM_Contribute_BAO_ContributionPage',
'localizable' => 0,
'html' => [
'type' => 'CheckBox',
],
];
$fields['thermometer_stretch_goal'] = [
'name' => 'thermometer_stretch_goal',
'title' => E::ts('Stretch goal if goal amount is reached? (optional)'),
'type' => CRM_Utils_Type::T_MONEY,
'entity' => 'ContributionPage',
'add' => '5.0',
'bao' => 'CRM_Contribute_BAO_ContributionPage',
'localizable' => 0,
'html' => [
'type' => 'Text',
],
];
$fields['thermometer_offset_amount'] = [
'name' => 'thermometer_offset_amount',
'title' => E::ts('Adjust existing contribution total? (optional; use negative numbers to subtract)'),
'type' => CRM_Utils_Type::T_MONEY,
'entity' => 'ContributionPage',
'add' => '5.0',
'bao' => 'CRM_Contribute_BAO_ContributionPage',
'localizable' => 0,
'html' => [
'type' => 'Text',
],
];
$fields['thermometer_offset_donors'] = [
'name' => 'thermometer_offset_donors',
'title' => E::ts('Adjust existing number of contributors? (optional; use negative numbers to subtract)'),
'type' => CRM_Utils_Type::T_INT,
'entity' => 'ContributionPage',
'add' => '5.0',
'bao' => 'CRM_Contribute_BAO_ContributionPage',
'localizable' => 0,
'html' => [
'type' => 'Text',
],
];
};
}
function civithermometer_civicrm_buildForm($formName, &$form) {
// Only focus on Contribution Pages
if ($formName == 'CRM_Contribute_Form_Contribution_Main') {
$formId = $form->_id;
$contribPage = \Civi\Api4\ContributionPage::get()
->setSelect([
'thermometer_is_enabled',
'thermometer_is_double',
'thermometer_stretch_goal',
'goal_amount',
'thermometer_offset_amount',
'thermometer_offset_donors',
])
->addWhere('id', '=', $formId)
->setCheckPermissions(FALSE)
->execute();
// Only continue if the thermometer_is_enabled variable is set to 1
if ($contribPage->first()['thermometer_is_enabled'] == 1 && $contribPage->first()['goal_amount'] != NULL) {
$contributions = \Civi\Api4\Contribution::get()
->addWhere('is_test', '=', 0)
->addWhere('contribution_status_id', '=', 1)
->addWhere('contribution_page_id', '=', $formId)
->setCheckPermissions(FALSE)
->execute();
// Prepare variables to pass through to Javascript
$amountGoal = $contribPage->first()['goal_amount'];
$amountStretch = $contribPage->first()['thermometer_stretch_goal'];
$isDouble = $contribPage->first()['thermometer_is_double'];
$numberDonors = $contributions->count();
$amountRaised = 0;
$offsetRaised = $contribPage->first()['thermometer_offset_amount'];
$offsetDonors = $contribPage->first()['thermometer_offset_donors'];
if ($numberDonors > 0) {
$amounts = array_column((array) $contributions, 'total_amount');
$amountRaised = array_reduce($amounts, function ($a, $b) {
return ($a += $b);
});
}
// Apply offsets if defined
$amountRaised += $offsetRaised;
$numberDonors += $offsetDonors;
// Get thermometer HTML and CSS
$thermo_settings = \Civi\Api4\Setting::get()
->setSelect([
'civithermometer_css',
'civithermometer_html',
])
->setCheckPermissions(FALSE)
->execute();
$css = $thermo_settings[0]['value'];
$html = $thermo_settings[1]['value'];
// Add thermo data to the page so our JS can access it
CRM_Core_Resources::singleton()->addVars('civithermo', [
'numberDonors' => $numberDonors,
'amountGoal' => $amountGoal,
'amountStretch' => $amountStretch,
'amountRaised' => $amountRaised,
'currency' => $form->_values['currency'],
'isDouble' => $isDouble,
]);
$intro_text = $form->_values['intro_text'];
if (empty($form->_pcpInfo['id']) && !empty($intro_text)) {
$intro_text = html_entity_decode($intro_text . $html);
$form->assign('intro_text', $intro_text);
}
CRM_Core_Resources::singleton()->addStyle($css);
CRM_Core_Resources::singleton()->addScriptFile('civithermometer', 'js/civithermo.js', ['weight' => 0]);
CRM_Core_Resources::singleton()->addScript('civithermo_render();', ['weight' => 10]);
}
}
}