-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpostcodeanywhere.inc
More file actions
166 lines (151 loc) · 5.53 KB
/
postcodeanywhere.inc
File metadata and controls
166 lines (151 loc) · 5.53 KB
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
<?php
/**
* @file
* Include file to handle the PCA callbacks.
*/
/**
* AJAX callback to lists address records for the given postcode.
*/
function postcodeanywhere_autocomplete($string = "") {
// Make a string lowercase and remove all spaces.
$string = str_replace(' ', '', strtolower($string));
try {
$data = [];
foreach (postcodeanywhere_find($string, TRUE) as $row) {
$id = !empty($row['Id']) ? current($row['Id']) : 0;
$place = !empty($row['Place']) ? current($row['Place']) : '';
$street = !empty($row['StreetAddress']) ? current($row['StreetAddress']) : '';
$data[$id] = $place . (!empty($street) ? ", " . $street : '');
}
if (count($data) > 2) {
$data['number-results'] = t('%result found, keep typing to refine',
array(
'%result' => format_plural(count($data), t('1 result'), '@count ' . t('results.'))
)
);
}
$data['no-match'] = t("I can't see my address");
}
catch (Exception $e) {
// Check logs for further details.
$data = array('no-match' => t("I can't see the address."));
}
drupal_json_output($data);
}
/**
* Form element validate handler for PCA autocomplete element.
*/
function postcodeanywhere_autocomplete_validate($element, &$form_state) {
$values = $form_state['values'];
// @todo? strlen($string) <= 3
// form_error($form['node'], t('Sorry, the post code is too short.'));
}
/**
* AJAX callback to lists address records matching the specified search term.
*/
function postcodeanywhere_find($postcode = POSTCODEANYWHERE_PC_TEST, $ret_array = FALSE) {
require_once drupal_get_path('module', 'postcodeanywhere') . "/classes/PostcodeAnywhere_Interactive_Find.class.inc";
$key = variable_get('postcodeanywhere_licence', POSTCODEANYWHERE_DEMO_KEY);
$cid = str_replace(' ', '', "pca_pc_" . $postcode);
if (!($data = _pca_get_cache($cid))) {
try {
$pca = new PostcodeAnywhere_Interactive_Find($key, $postcode, "");
$pca->MakeRequest();
if ($data = $pca->HasData()) {
_pca_set_cache($cid, $data);
}
}
catch (Exception $e) {
watchdog_exception('postcodeanywhere', $e);
}
}
return $ret_array ? (array) $data : drupal_json_output($data ?: array());
}
/**
* AJAX callback to lists address records for the given postcode.
*/
function postcodeanywhere_findbypostcode($postcode = POSTCODEANYWHERE_PC_TEST, $ret_array = FALSE) {
require_once drupal_get_path('module', 'postcodeanywhere') . "/classes/PostcodeAnywhere_Interactive_FindByPostcode.class.inc";
$key = variable_get('postcodeanywhere_licence', POSTCODEANYWHERE_DEMO_KEY);
$cid = str_replace(' ', '', "pca_pc_" . $postcode);
if (!($data = _pca_get_cache($cid))) {
try {
$pca = new PostcodeAnywhere_Interactive_FindByPostcode($key, $postcode, "");
$pca->MakeRequest();
if ($data = $pca->HasData()) {
_pca_set_cache($cid, $data);
}
}
catch (Exception $e) {
watchdog_exception('postcodeanywhere', $e);
}
}
return $ret_array ? (array) $data : drupal_json_output($data ?: array());
}
/**
* AJAX callback to lists address records for the given postcode.
*/
function postcodeanywhere_findbypartialpostcode($postcode = "WR2", $ret_array = FALSE) {
require_once drupal_get_path('module', 'postcodeanywhere') . "/classes/PostcodeAnywhere_Interactive_FindByPartialPostcode.class.inc";
$key = variable_get('postcodeanywhere_licence', POSTCODEANYWHERE_DEMO_KEY);
$cid = str_replace(' ', '', "pca_pc_" . $postcode);
if (!($data = _pca_get_cache($cid))) {
try {
$pca = new PostcodeAnywhere_Interactive_FindByPartialPostcode($key, $postcode, "");
$pca->MakeRequest();
if ($data = $pca->HasData()) {
_pca_set_cache($cid, $data);
}
}
catch (Exception $e) {
watchdog_exception('postcodeanywhere', $e);
}
}
return $ret_array ? $data : drupal_json_output($data ?: array());
}
/**
* AJAX callback to returns the full address details based on the Id.
*/
function postcodeanywhere_retrievebyid($id) {
require_once drupal_get_path('module', 'postcodeanywhere') . "/classes/PostcodeAnywhere_Interactive_RetrieveById.class.inc";
$key = variable_get('postcodeanywhere_licence', POSTCODEANYWHERE_DEMO_KEY);
$cid = str_replace(' ', '', "pca_id_" . $id);
if (!($data = _pca_get_cache($cid))) {
try {
$pca = new PostcodeAnywhere_Interactive_RetrieveById($key, $id, "English", "");
$pca->MakeRequest();
if ($data = $pca->HasData()) {
_pca_set_cache($cid, $data);
}
}
catch (Exception $e) {
watchdog_exception('postcodeanywhere', $e);
}
}
drupal_json_output($data ?: array());
}
/**
* Helper function to get results from cache.
*/
function _pca_get_cache($cid) {
$format = variable_get('postcodeanywhere_format', POSTCODEANYWHERE_FORMAT);
if ($cache = cache_get($cid, POSTCODEANYWHERE_CACHE)) {
// Serialization of SimpleXMLElement is not alloweḑ, so doing some workaround.
$data = $format == 'xmla.ws' || $format == 'xmle.ws' ? json_decode($cache->data, TRUE) : $cache->data;
}
if (!empty($data)) {
drupal_add_http_header('X-Cached-By', 'Drupal PCA');
return $data;
}
else {
return FALSE;
}
}
/**
* Helper function to cache the results.
*/
function _pca_set_cache($cid, $data) {
$format = variable_get('postcodeanywhere_format', POSTCODEANYWHERE_FORMAT);
// Serialization of SimpleXMLElement is not alloweḑ, so doing some workaround.
return cache_set($cid, $format == 'xmla.ws' || $format == 'xmle.ws' ? json_encode($data) : $data, POSTCODEANYWHERE_CACHE);
}