-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpostcodeanywhere.module
More file actions
245 lines (220 loc) · 9.25 KB
/
postcodeanywhere.module
File metadata and controls
245 lines (220 loc) · 9.25 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
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
<?php
/**
* @file
* PCA Predict module file.
*/
define('POSTCODEANYWHERE_DEMO_KEY', 'RA24-BZ17-JR72-WN38');
// Define test postode; WR5 3DA is a test code (no credits are needed).
define('POSTCODEANYWHERE_PC_TEST', 'WR5 3DA');
define('POSTCODEANYWHERE_URL', 'http://services.postcodeanywhere.co.uk/');
// Define PCA format. Supported formats: xmla.ws or json.ws (needs some work).
define('POSTCODEANYWHERE_FORMAT', 'xmla.ws');
define('POSTCODEANYWHERE_CACHE', 'cache_postcodeanywhere');
/**
* Implements hook_menu().
*/
function postcodeanywhere_menu() {
$items = array();
$items['admin/config/content/postcodeanywhere'] = array(
'title' => 'Postcode Anywhere',
'description' => 'Manage PCA settings.',
'page callback' => 'drupal_get_form',
'page arguments' => array('postcodeanywhere_settings_form'),
'access arguments' => array('administer postcodeanywhere'),
'file' => 'postcodeanywhere.admin.inc',
);
// AJAX callbacks.
$items['pca/autocomplete'] = array(
'title' => 'Autocomplete callback for PCA Predict.',
'description' => 'Returns lists address records for the given partial postcode.',
'page callback' => 'postcodeanywhere_autocomplete',
'page arguments' => array(2),
'access arguments' => array('access content'),
'file' => 'postcodeanywhere.inc',
'type' => MENU_CALLBACK,
);
$items['pca/findbypostcode/%'] = array(
'title' => 'PCA Predict FindByPostcode callback',
'description' => 'Lists address records for the given postcode.',
'page callback' => 'postcodeanywhere_findbypostcode',
'page arguments' => array(2),
'access arguments' => array('access content'),
'file' => 'postcodeanywhere.inc',
'type' => MENU_CALLBACK,
);
$items['pca/findbypartialpostcode/%'] = array(
'title' => 'PCA Predict FindByPartialPostcode callback',
'description' => 'Lists address records for the given partial postcode.',
'page callback' => 'postcodeanywhere_findbypartialpostcode',
'page arguments' => array(2),
'access arguments' => array('access content'),
'file' => 'postcodeanywhere.inc',
'type' => MENU_CALLBACK,
);
$items['pca/retrievebyid'] = array(
'title' => 'PCA Predict RetrieveById callback',
'description' => 'Returns the full address details based on the Id.',
'page callback' => 'postcodeanywhere_retrievebyid',
'page arguments' => array(2),
'access arguments' => array('access content'),
'file' => 'postcodeanywhere.inc',
'type' => MENU_CALLBACK,
);
$items['admin/help/pca_predict'] = array(
'title' => 'PCA Predict (Postcode Anywhere) help',
'page callback' => 'postcodeanywhere_index_page',
'access arguments' => array('view advanced help index'),
);
return $items;
}
/**
* Implements hook_permission().
*/
function postcodeanywhere_permission() {
return array(
'administer postcodeanywhere' => array(
'title' => t('Administer postcode anywhere settings'),
'description' => t('Perform administration tasks for my module.'),
),
'use PHP for postcodeanywhere visibility' => array(
'title' => t('Administer postcode anywhere settings'),
'description' => t('Use PHP for postcodeanywhere visibility settings.'),
),
);
}
/**
* Implements hook_form_alter().
*/
function postcodeanywhere_form_alter(&$form, &$form_state, $form_id) {
// Check whether path matches one defined in postcodeanywhere settings.
$postcodeanywhere_rules = variable_get('postcodeanywhere_page_visibility_rules', '');
$postcodeanywhere_mode = variable_get('postcodeanywhere_page_visibility_mode', '');
if ($postcodeanywhere_rules) {
if ($postcodeanywhere_mode < 2) {
$path = drupal_get_path_alias($_GET['q']);
// Compare with the internal and path alias (if any).
$page_match = drupal_match_path($path, $postcodeanywhere_rules);
if ($path != $_GET['q']) {
$page_match = $page_match || drupal_match_path($_GET['q'], $postcodeanywhere_rules);
}
// When $postcodeanywhere_mode has a value of 0, the block is displayed on
// all pages except those listed in $block->pages. When set to 1, it
// is displayed only on those pages listed in $postcodeanywhere_rules.
$page_match = !($postcodeanywhere_mode xor $page_match);
}
else {
$page_match = php_eval($postcodeanywhere_rules);
}
}
else {
$page_match = TRUE;
}
if ($page_match) {
// Include postcodeanywhere JS file after form is built.
$form['#after_build'][] = 'postcodeanywhere_form_after_build';
// Ensures an include file is loaded whenever the form is processed.
form_load_include($form_state, 'inc', 'postcodeanywhere');
}
}
/**
* Implements #after_build function.
*
* Helper function to include postcodeanywhere js files after form is built.
*/
function postcodeanywhere_form_after_build($form_element, &$form_state) {
postcodeanywhere_form_add_js();
return $form_element;
}
/**
* Helper function to include postcodeanywhere js files.
*/
function postcodeanywhere_form_add_js($extra_settings = NULL) {
static $pca_loaded = FALSE;
// Don't allow to load the JS file twice.
if ($pca_loaded) {
return;
}
$pca_loaded = TRUE;
// Add postcodeanywhere settings to Drupal's global storage.
$settings = array(
'postcodeanywhere' => array(
'account_code' => variable_get('postcodeanywhere_account_code', ''),
'licence' => variable_get('postcodeanywhere_licence', POSTCODEANYWHERE_DEMO_KEY),
'id_wrapper' => variable_get('postcodeanywhere_id_wrapper', ''),
'id_postcode' => variable_get('postcodeanywhere_id_postcode', ''),
'id_postcode_manual' => variable_get('postcodeanywhere_id_manual-postcode', ''),
'id_lookup_button' => '#postcodeanywhere-lookup-button',
'id_company' => variable_get('postcodeanywhere_id_company', ''),
'id_company_wrapper' => variable_get('postcodeanywhere_id_company_wrapper', ''),
'id_address_wrapper' => variable_get('postcodeanywhere_id_address_wrapper', ".id_address_wrapper"),
'id_line1' => variable_get('postcodeanywhere_id_line1', ''),
'id_line1_wrapper' => variable_get('postcodeanywhere_id_line1_wrapper', ''),
'id_line2' => variable_get('postcodeanywhere_id_line2', ''),
'id_line2_wrapper' => variable_get('postcodeanywhere_id_line2_wrapper', ''),
'id_line3' => variable_get('postcodeanywhere_id_line3', ''),
'id_line3_wrapper' => variable_get('postcodeanywhere_id_line3_wrapper', ''),
'id_town' => variable_get('postcodeanywhere_id_town', ''),
'id_town_wrapper' => variable_get('postcodeanywhere_id_town_wrapper', ''),
'id_county' => variable_get('postcodeanywhere_id_county', ''),
'id_county_wrapper' => variable_get('postcodeanywhere_id_county_wrapper', ''),
'id_country' => variable_get('postcodeanywhere_id_country', ''),
'id_country_uk_value' => variable_get('postcodeanywhere_id_country_uk_value', 'GB'),
'submit_label_value' => variable_get('postcodeanywhere_submit_label_value', 'Find Address'),
'showAlert' => variable_get('postcodeanywhere_showalert', 1),
'url' => variable_get('postcodeanywhere_url', POSTCODEANYWHERE_URL),
'addressfield' => variable_get('postcodeanywhere_addressfield', FALSE) && module_exists('addressfield'),
),
);
// Add extra settings to JS setting value.
if ($extra_settings && is_array($extra_settings)) {
foreach ($extra_settings as $key => $value) {
$settings['postcodeanywhere'][$key] .= ',' . $value;
// Set as already loaded,
// so don't try to load it again (e.g. hook_form_alter).
$pca_loaded = TRUE;
}
}
// Append settings into JS file.
drupal_add_js($settings, 'setting');
drupal_add_js(drupal_get_path('module', 'postcodeanywhere') . '/postcodeanywhere.js', 'file');
}
/**
* Helper function to parse URL callback to PCA.
*/
function postcodeanywhere_parse_url($url, $format = 'json') {
$arr = parse_url($url);
if ($api_url = parse_url(variable_get('postcodeanywhere_url', POSTCODEANYWHERE_URL))) {
$apath = explode('/', $arr['path']);
$wsdl = end($apath);
$format = variable_get('postcodeanywhere_format', POSTCODEANYWHERE_FORMAT);
if ($wsdl != $format) {
// Change format if necessary.
$arr['path'] = str_replace($wsdl, $format, $arr['path']);
}
if (variable_get('postcodeanywhere_example_test', FALSE)) {
$api_url['scheme'] = parse_url(url(current_path(), array('absolute' => TRUE)), PHP_URL_SCHEME);
$api_url['host'] = parse_url(url(current_path(), array('absolute' => TRUE)), PHP_URL_HOST);
$api_url['port'] = parse_url(url(current_path(), array('absolute' => TRUE)), PHP_URL_PORT);
}
// TODO: Consider appending: &CallbackFunction=TestCallBack.
return sprintf("%s://%s%s%s?%s", $api_url['scheme'], $api_url['host'], !empty($api_url['port']) ? ":{$api_url['port']}" : "", $arr['path'], $arr['query']);
}
else {
// Return previous if we can't parse it.
return $url;
}
}
/**
* Helper function to parse data from PCA.
*/
function postcodeanywhere_make_request() {
$format = variable_get('postcodeanywhere_format', POSTCODEANYWHERE_FORMAT);
}
/**
* Topic index callback.
*/
function postcodeanywhere_index_page() {
$output = '';
$output .= ' ' . t('Click the help icon to view help about the PCA Predict (formely Postcode Anywhere).');
return $output;
}