-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathusername.php
77 lines (67 loc) · 2.65 KB
/
username.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
<?php
require_once 'username.civix.php';
use CRM_Username_ExtensionUtil as E;
/**
* Implements hook_civicrm_config().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_config
*/
function username_civicrm_config(&$config) {
_username_civix_civicrm_config($config);
}
/**
* Implements hook_civicrm_install().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_install
*/
function username_civicrm_install() {
_username_civix_civicrm_install();
}
/**
* Implements hook_civicrm_enable().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_enable
*/
function username_civicrm_enable() {
_username_civix_civicrm_enable();
}
/**
* Implements hook_civicrm_validateForm().
* Checks if the osm username is valid.
* The field label *must* be: OSM username
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_validateForm/
*/
function username_civicrm_validateForm($formName, &$fields, &$files, &$form, &$errors) {
if ( $formName == 'CRM_Contact_Form_Contact' or
$formName == 'CRM_Contact_Form_Inline_CustomData' or
$formName == 'CRM_Contribute_Form_Contribution_Main'
) {
$osmfield = civicrm_api3('CustomField', 'getsingle', array('label' => 'OSM username'));
if(! $osmfield){
throw new InvalidArgumentException(sprintf("Could not find custom field with 'OSM username' as its label"));
}
if ( $formName == 'CRM_Contribute_Form_Contribution_Main') {
# Sometimes, field id is like custom_1
$osmfieldid = 'custom_'.$osmfield['id'];
} else{
$customRecId = $osm = CRM_Utils_Array::value( "customRecId", $fields, FALSE );
# And the rest of the time, field id is like custom_1_329. Go figure!
$osmfieldid = 'custom_'.$osmfield['id'].'_'.$customRecId;
}
$osm = CRM_Utils_Array::value( $osmfieldid, $fields, FALSE );
if (strlen((string)$osm) > 0) {
$url = 'https://api.openstreetmap.org/api/0.6/changesets?time=9999-01-01&display_name='.rawurlencode($osm);
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
/* Get the HTML or whatever is linked in $url. */
$response = curl_exec($handle);
/* Check for 404 (file not found). */
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
if($httpCode == 404) {
$errors[$osmfieldid] = ts( 'OSM username does not exist. Remember that usernames are case sensitive.' );
}
curl_close($handle);
}
}
return;
}