-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenderselfidentify.php
310 lines (289 loc) · 9.82 KB
/
genderselfidentify.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
<?php
require_once 'genderselfidentify.civix.php';
/**
* Implements hook_civicrm_config
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_config
*/
function genderselfidentify_civicrm_config(&$config) {
_genderselfidentify_civix_civicrm_config($config);
}
/**
* Implements hook_civicrm_xmlMenu
*
* @param $files array(string)
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_xmlMenu
*/
function genderselfidentify_civicrm_xmlMenu(&$files) {
_genderselfidentify_civix_civicrm_xmlMenu($files);
}
/**
* Implements hook_civicrm_install
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_install
*/
function genderselfidentify_civicrm_install() {
_genderselfidentify_civix_civicrm_install();
_genderselfidentify_add_other_option();
}
/**
* Implements hook_civicrm_uninstall
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_uninstall
*/
function genderselfidentify_civicrm_uninstall() {
_genderselfidentify_civix_civicrm_uninstall();
}
/**
* Implements hook_civicrm_enable
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_enable
*/
function genderselfidentify_civicrm_enable() {
_genderselfidentify_civix_civicrm_enable();
_genderselfidentify_add_other_option();
$group = civicrm_api3('CustomGroup', 'get', array(
'name' => 'Genderselfidentify',
));
if (!empty($group['id'])) {
// CustomGroup 'create' is broken for update
civicrm_api3('CustomGroup', 'update', array(
'id' => $group['id'],
'is_reserved' => 1,
'is_active' => 1,
));
}
}
/**
* Implements hook_civicrm_disable
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_disable
*/
function genderselfidentify_civicrm_disable() {
_genderselfidentify_civix_civicrm_disable();
try {
$group = civicrm_api3('CustomGroup', 'get', array(
'name' => 'Genderselfidentify',
));
if (!empty($group['id'])) {
// CustomGroup 'create' is broken for update
civicrm_api3('CustomGroup', 'update', array(
'id' => $group['id'],
'is_reserved' => 0,
'is_active' => 0,
));
}
civicrm_api3('OptionValue', 'create', array(
'id' => CRM_Genderselfidentify_BAO_Gender::otherOption('id'),
'is_reserved' => 0,
));
}
// If custom data doesn't exist, ignore
catch (API_Exception $e) {}
}
/**
* Implements hook_civicrm_upgrade
*
* @param $op string, the type of operation being performed; 'check' or 'enqueue'
* @param $queue CRM_Queue_Queue, (for 'enqueue') the modifiable list of pending up upgrade tasks
*
* @return mixed based on op. for 'check', returns array(boolean) (TRUE if upgrades are pending)
* for 'enqueue', returns void
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_upgrade
*/
function genderselfidentify_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
return _genderselfidentify_civix_civicrm_upgrade($op, $queue);
}
/**
* Implements hook_civicrm_managed
*
* Generate a list of entities to create/deactivate/delete when this module
* is installed, disabled, uninstalled.
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_managed
*/
function genderselfidentify_civicrm_managed(&$entities) {
_genderselfidentify_civix_civicrm_managed($entities);
}
/**
* Implements hook_civicrm_alterSettingsFolders
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_alterSettingsFolders
*/
function genderselfidentify_civicrm_alterSettingsFolders(&$metaDataFolders = NULL) {
_genderselfidentify_civix_civicrm_alterSettingsFolders($metaDataFolders);
}
/**
* Implements hook_civicrm_apiWrappers
*
* @param array $wrappers
* @param array $apiRequest
*/
function genderselfidentify_civicrm_apiWrappers(&$wrappers, $apiRequest) {
if (strtolower($apiRequest['entity']) == 'contact') {
$wrappers[] = new CRM_Genderselfidentify_ContactAPIWrapper();
}
}
/**
* Implements hook_civicrm_buildForm
*
* @param string $formName
* @param CRM_Core_Form $form
*/
function genderselfidentify_civicrm_buildForm($formName, &$form) {
if (in_array($formName, array('CRM_Contact_Form_Contact', 'CRM_Contact_Form_Inline_Demographics', 'CRM_Profile_Form_Edit'))
&& $form->elementExists('gender_id')) {
$form->removeElement('gender_id');
$form->addElement('text', 'gender_id', ts('Gender'));
if (!empty($form->_contactId)) {
$form->setDefaults(array(
'gender_id' => CRM_Genderselfidentify_BAO_Gender::get($form->_contactId),
));
}
// Hide custom field from contact edit screen since it is not editable
if ($formName == 'CRM_Contact_Form_Contact') {
CRM_Core_Resources::singleton()
->addStyle('#Genderselfidentify.crm-custom-accordion {display: none;}', 99, 'html-header');
}
}
}
/**
* Implements hook_civicrm_pre
*
* @param string $op
* @param string $objectName
* @param int|null $id
* @param array $params
*/
function genderselfidentify_civicrm_pre($op, $objectName, $id, &$params) {
if ($objectName == 'Individual' && in_array($op, array('create', 'edit'))) {
// $params['version'] indicates this is an api request, which we've already handled with api_v3_GenderselfidentifyAPIWrapper
if (isset($params['gender_id']) && empty($params['version'])) {
$input = trim($params['gender_id']);
$params['gender_id'] = CRM_Genderselfidentify_BAO_Gender::match($input);
// Can't just set `$params['custom_x'] = $input` because that would be too easy
// For contact create
$params['custom_' . CRM_Genderselfidentify_BAO_Gender::getCustomFieldId() . '_-1'] = $input;
// For contact inline-edit
$params += array('custom' => array());
CRM_Core_BAO_CustomField::formatCustomField(CRM_Genderselfidentify_BAO_Gender::getCustomFieldId(), $params['custom'],
$input, 'Individual', NULL, $id, FALSE, FALSE, TRUE
);
}
}
}
/**
* Implements hook_civicrm_pageRun
*
* @param CRM_Core_Page $page
*/
function genderselfidentify_civicrm_pageRun(&$page) {
$pageClass = get_class($page);
// For contact summary view
if (in_array($pageClass, array('CRM_Contact_Page_View_Summary', 'CRM_Contact_Page_Inline_Demographics'))) {
$cid = $page->get_template_vars('id');
if ($cid) {
$page->assign('gender_display', htmlspecialchars(CRM_Genderselfidentify_BAO_Gender::get($cid)));
}
// Hide custom field from contact summary since its value is incorporated in the demographics pane
CRM_Core_Resources::singleton()
->addStyle('.customFieldGroup.Genderselfidentify {display: none;}', 99, 'html-header');
}
// For profile listings
elseif ($pageClass == 'CRM_Profile_Page_Listings') {
$genderRow = NULL;
$columnHeaders = $page->get_template_vars('columnHeaders');
if ($columnHeaders) {
foreach ($columnHeaders as $num => $col) {
if (CRM_Utils_Array::value('field_name', $col) === 'gender_id') {
$genderRow = $num;
}
}
}
if ($genderRow) {
$rows = $page->get_template_vars('rows');
if ($rows) {
$other = CRM_Genderselfidentify_BAO_Gender::otherOption('label');
foreach ($rows as &$row) {
if ($row[$genderRow] == $other) {
// Dammit, no cid in row, have to parse it from the view link in the last column
preg_match('#[&?;]id=(\d+)#', $row[count($row)-1], $matches);
if (!empty($matches[1])) {
$row[$genderRow] = htmlspecialchars(CRM_Genderselfidentify_BAO_Gender::get($matches[1]));
}
}
}
$page->assign('rows', $rows);
}
}
}
// For profile view
elseif (in_array($pageClass, array('CRM_Profile_Page_View', 'CRM_Profile_Page_Dynamic'))) {
$profileFields = $page->get_template_vars('profileFields');
$row = $page->get_template_vars('row');
foreach ($profileFields as $key => &$field) {
if ($key == 'gender_id') {
$row[$field['label']] = $field['value'] = htmlspecialchars(CRM_Genderselfidentify_BAO_Gender::get($page->get_template_vars('cid')));
$page->assign('row', $row);
$page->assign('profileFields', $profileFields);
break;
}
}
}
}
/**
* Implements hook_civicrm_searchColumns
*
* @param string $objectName
* @param array $headers
* @param array $rows
* @param $selector
*/
function genderselfidentify_civicrm_searchColumns($objectName, &$headers, &$rows, &$selector) {
if (strtolower($objectName) == 'contact') {
$other = CRM_Genderselfidentify_BAO_Gender::otherOption('label');
foreach ($rows as &$row) {
if (isset($row['gender_id']) && $row['gender_id'] == $other && !empty($row['contact_id'])) {
$row['gender_id'] = htmlspecialchars(CRM_Genderselfidentify_BAO_Gender::get($row['contact_id']));
}
}
}
}
/**
* Add "Other" gender option if it doesn't exist
* Ensure it is enabled and reserved if it already exists
*
* @throws \CiviCRM_API3_Exception
*/
function _genderselfidentify_add_other_option() {
$options = civicrm_api3('OptionValue', 'get', array('option_group_id' => 'gender'));
$maxValue = 1;
foreach ($options['values'] as $lastOption) {
if ($lastOption['name'] === 'Other') {
// Make sure it is enabled and reserved
if (empty($lastOption['is_active']) || empty($lastOption['is_reserved'])) {
civicrm_api3('OptionValue', 'create', array(
'id' => $lastOption['id'],
'is_active' => 1,
'is_reserved' => 1,
));
}
return;
}
if ($lastOption['value'] > $maxValue) {
$maxValue = $lastOption['value'];
}
}
// We're still here, so "Other" option needs to be added
civicrm_api3('OptionValue', 'create', array(
'option_group_id' => 'gender',
'name' => 'Other',
'label' => ts('Other'),
'value' => $maxValue + 1,
'weight' => $lastOption['weight'] + 1,
'is_active' => 1,
'is_reserved' => 1,
));
}