-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmps.module
331 lines (289 loc) · 9.04 KB
/
mps.module
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<?php
/**
* @file
* MPS Integration module.
*/
define('MPS_DEFAULT_BASE_URL', 'http://localhost/');
define('MPS_DEFAULT_XML_API_PATH', 'request/page/xml');
define('MPS_DEFAULT_JSON_API_PATH', 'request/page/json');
define('MPS_DEFAULT_API_DESCRIPTOR', 'request/describe');
define('MPS_DEFAULT_SITE_ID', 'ivillage-web');
define('MPS_DEFAULT_EXCLUDE_PATHS', "admin\nadmin/*");
/**
* Implements hook_init().
*/
function mps_init() {
$data = array(
'path' => drupal_strtolower(drupal_get_path_alias($_GET['q'])),
);
// TODO - this needs expanding upon.
$object = menu_get_object(arg(0));
if (isset($object->nid)) {
$data['nid'] = $object->nid;
}
elseif (isset($object->tid)) {
$data['tid'] = $object->tid;
}
if ($_SERVER['QUERY_STRING']) {
$data['query'] = $_SERVER['QUERY_STRING'];
}
if (mps_in_debug_mode()) {
$data['debug'] = TRUE;
}
drupal_add_js(array('mps' => $data), 'setting');
}
/**
* Implements hook_menu().
*/
function mps_menu() {
$items['admin/config/services/mps'] = array(
'title' => 'MPS integration',
'page callback' => 'drupal_get_form',
'page arguments' => array('mps_admin_settings'),
'access arguments' => array('administer mps'),
'file' => 'mps.admin.inc',
'type' => MENU_NORMAL_ITEM,
);
$items['mps'] = array(
'page callback' => 'mps_async_request',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_block().
*/
function mps_block_info() {
$mps = mps_get_manager_class();
$blocks = array();
foreach ($mps->getAdUnitRegions() as $key => $title) {
// This example comes from node.module.
$blocks['mps_ad_' . $key] = array(
'info' => t('MPS "!title" Ad Unit', array('!title' => $title)),
'cache' => DRUPAL_CACHE_PER_PAGE,
);
}
foreach ($mps->getPageComponents() as $key => $title) {
// This example comes from node.module.
$blocks['mps_comp_' . $key] = array(
'info' => t('MPS "!title" Widget', array('!title' => $title)),
'cache' => DRUPAL_CACHE_PER_PAGE,
);
}
return $blocks;
}
/**
* Implements hook_block_view().
*
* Outputting placeholder divs for ad units, components.
*/
function mps_block_view($delta) {
$parts = preg_split('/(mps)_(ad|comp)_(.*)/', $delta, -1, PREG_SPLIT_DELIM_CAPTURE);
if ($parts[1] == 'mps') {
$classes = array('mps-placeholder');
if ($parts[2] == 'ad') {
$classes[] = 'mps-adunit';
}
elseif ($parts[2] == 'comp') {
$classes[] = 'mps-component';
}
else {
$classes[] = 'mps-' . $parts[2];
}
return !mps_path_is_excluded() ? array('content' => '<div class="' . implode(' ', $classes) . '" id="mps-' . drupal_clean_css_identifier($parts[3]) . '" ></div>') : FALSE;
}
return NULL;
}
/**
* Implements hook_perm().
*/
function mps_perm() {
return array('administer mps');
}
/**
* Callback for /mps ajax request.
*/
function mps_async_request($path = NULL, $output = TRUE) {
if ($path === NULL) {
$params = $_POST;
}
else {
$params['path'] = $path;
}
$params['path'] = ($params['path'] == variable_get('site_frontpage', 'node')) ? '' : trim($params['path'], '/');
if ($params['path']) {
menu_set_active_item(drupal_get_normal_path($params['path']));
}
else {
menu_set_active_item(drupal_get_normal_path(variable_get('site_frontpage', 'node')));
}
if (drupal_is_front_page()) {
$params['path'] = '';
}
$mps = mps_get_manager_class();
$request = $mps->buildRequest($params);
drupal_alter('mps_request', $request, $mps);
$response = $mps->executeRequest($request);
if ($response->code && $response->code < 300) {
$decoded = drupal_json_decode($response->data);
$valid_json_response = (json_last_error() == JSON_ERROR_NONE);
$response = array(
'success' => TRUE,
'data' => $valid_json_response ? $decoded : $response->data,
'request_url' => $response->request_url,
'context' => isset($_POST['lastContext']) ? $_POST['lastContext'] : '',
);
if (mps_in_debug_mode()) {
$response['debug'] = TRUE;
}
drupal_alter('mps_response', $response, $mps);
if ($output) {
drupal_json_output($response);
drupal_exit();
}
else {
return $response;
}
}
else {
watchdog('mps', 'MPS call failed (code @code): @request', array('@code' => $response->code, '@request' => $response->request));
if ($output) {
$response = array(
'success' => FALSE,
'code' => $response->code,
'request_url' => $response->request_url,
);
if (mps_in_debug_mode()) {
$response['debug'] = TRUE;
}
drupal_json_output($response);
drupal_exit();
}
}
return $response;
}
/**
* Implements hook_mps_request_alter().
*/
function mps_mps_request_alter(&$request) {
// This becomes true further down if we're looking at content.
$request['data']['is_content'] = 0;
if ($request['data']['path'] == '/' . variable_get('site_frontpage', 'node')) {
$request['data']['path'] = '/';
}
if (isset($_POST['query'])) {
$request['data']['qs'] = MpsBase64::encode($_POST['query']);
}
// If the cat mapping already has pipes in it, assume that we don't need to
// explode it from a comma-delimited list.
if (isset($request['data']['cat']) && !strpos($request['data']['cat'], '|')) {
// Categories. Taking a comma delimited token value of a hierarchical
// taxonomy term, flip it around and replace the comma delimiters with
// pipes.
$cats = array_reverse(explode(',', $request['data']['cat']));
if ($cats) {
$request['data']['cat'] = implode('|', array_map('trim', $cats));
}
}
// Next, build the Content Affinity Groups (cag).
$object = menu_get_object(arg(0));
if (isset($object->nid)) {
$request['data']['is_content'] = 1;
if ($cags = _mps_extract_cag_array($object)) {
$request['data']['cag'] = $cags;
}
}
elseif (isset($object->tid)) {
// Wat do for taxonomy terms?
}
// For non-content pages, content_id becomes a hash of the site and path.
if (!$request['data']['is_content']) {
$request['data']['content_id'] = MasterPageService::generateContentIdentifier($request['data']['site'], $request['data']['path']);
}
// Declare to MPS which adunits we are requesting.
if (!empty($_POST['lastContext']['adunits'])) {
$request['data']['adunits'] = implode('|', $_POST['lastContext']['adunits']);
}
}
/**
* Builds array of cag[] parameters.
*
* For building the cag[] array, this takes an entity bundle and for each
* term_reference field, gathers those values into a | escaped list, keyed by
* taxonomy vocabularies.
*/
function _mps_extract_cag_array($object) {
$fields = field_info_instances('node', $object->type);
$cags = array();
foreach ($fields as $field_name => $field_instance) {
$field_info = field_info_field($field_name);
if ($field_info['type'] == 'taxonomy_term_reference') {
$lang = isset($object->language) ? $object->language : 'und';
foreach ($object as $object_key => $object_key_value) {
if ($object_key == $field_name) {
foreach ($object_key_value[$lang] as $field_value) {
if (isset($field_value['tid'])) {
$tid = $field_value['tid'];
$terms = taxonomy_get_parents_all($tid);
foreach ($terms as $term) {
$vocab = $term->vocabulary_machine_name;
$cags[MasterPageService::formatCagKey($vocab)][] = $term->name;
}
}
}
}
}
}
}
foreach ($cags as $key => $contents) {
$cags[$key] = implode('|', array_reverse($contents));
}
return $cags;
}
/**
* Returns MPS management object.
*/
function mps_get_manager_class() {
$object = &drupal_static(__FUNCTION__);
if (empty($object)) {
$object = new MasterPageService();
}
return $object;
}
/**
* Fires a request to the MPS service.
*/
function mps_catch_page_request() {
mps_get_manager_class();
}
/**
* Returns a parameter mapping default.
*/
function mps_get_mapping_default($key, $default = NULL) {
$defaults = variable_get('mps_mapping_defaults', array());
return isset($defaults[$key]) ? $defaults[$key] : $default;
}
/**
* Returns TRUE if $path matches anything in the exclude list.
*/
function mps_path_is_excluded($path = NULL) {
// Convert path to lowercase. This allows comparison of the same path
// with different case. Ex: /Page, /page, /PAGE.
$pages = drupal_strtolower(variable_get('mps_exclude_paths', MPS_DEFAULT_EXCLUDE_PATHS));
$path = empty($path) ? $_GET['q'] : $path;
// Convert the Drupal path to lowercase.
$path = drupal_strtolower(drupal_get_path_alias($path));
// Compare the lowercase internal and lowercase path alias (if any).
$page_match = drupal_match_path($path, $pages);
if ($path != $_GET['q']) {
$page_match = $page_match || drupal_match_path($_GET['q'], $pages);
}
return $page_match;
}
/**
* Check if we are in debug mode.
*/
function mps_in_debug_mode() {
return (variable_get('mps_debug', 0) && (user_access('administer mps') || user_access('access devel information')));
}