1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Plugin to provide access control/visibility based on path.
|
6
|
*/
|
7
|
|
8
|
$plugin = array(
|
9
|
'title' => t('String: URL path'),
|
10
|
'description' => t('Control access by the current path.'),
|
11
|
'callback' => 'ctools_path_visibility_ctools_access_check',
|
12
|
'settings form' => 'ctools_path_visibility_ctools_access_settings',
|
13
|
'summary' => 'ctools_path_visibility_ctools_access_summary',
|
14
|
'required context' => new ctools_context_optional(t('Path'), 'string'),
|
15
|
'default' => array('visibility_setting' => 1, 'paths' => ''),
|
16
|
);
|
17
|
|
18
|
/**
|
19
|
* Settings form.
|
20
|
*/
|
21
|
function ctools_path_visibility_ctools_access_settings($form, &$form_state, $conf) {
|
22
|
$form['settings']['note'] = array(
|
23
|
'#value' => '<div class="description">' . t('Note: if no context is chosen, the current page path will be used.') . '</div>',
|
24
|
);
|
25
|
|
26
|
$form['settings']['visibility_setting'] = array(
|
27
|
'#type' => 'radios',
|
28
|
'#options' => array(
|
29
|
1 => t('Allow access on the following pages'),
|
30
|
0 => t('Allow access on all pages except the following pages'),
|
31
|
),
|
32
|
'#default_value' => $conf['visibility_setting'],
|
33
|
);
|
34
|
|
35
|
$form['settings']['paths'] = array(
|
36
|
'#type' => 'textarea',
|
37
|
'#title' => t('Paths'),
|
38
|
'#default_value' => $conf['paths'],
|
39
|
'#description' => t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array('%blog' => 'blog', '%blog-wildcard' => 'blog/*', '%front' => '<front>')),
|
40
|
);
|
41
|
return $form;
|
42
|
}
|
43
|
|
44
|
/**
|
45
|
* Check for access.
|
46
|
*/
|
47
|
function ctools_path_visibility_ctools_access_check($conf, $context) {
|
48
|
if (isset($context->data)) {
|
49
|
$base_path = $context->data;
|
50
|
}
|
51
|
else {
|
52
|
$base_path = $_GET['q'];
|
53
|
}
|
54
|
|
55
|
$path = drupal_get_path_alias($base_path);
|
56
|
$page_match = drupal_match_path($path, $conf['paths']);
|
57
|
|
58
|
// If there's a path alias, we may still be at the un-aliased path
|
59
|
// so check that as well.
|
60
|
if (!isset($context->data) && $path != $base_path) {
|
61
|
$page_match = $page_match || drupal_match_path($base_path, $conf['paths']);
|
62
|
}
|
63
|
|
64
|
// When $conf['visibility_setting'] has a value of 0, the block is displayed
|
65
|
// on all pages except those listed in $block->pages. When set to 1, it
|
66
|
// is displayed only on those pages listed in $block->pages.
|
67
|
$page_match = !($conf['visibility_setting'] xor $page_match);
|
68
|
|
69
|
return $page_match;
|
70
|
}
|
71
|
|
72
|
/**
|
73
|
* Provide a summary description.
|
74
|
*/
|
75
|
function ctools_path_visibility_ctools_access_summary($conf, $context) {
|
76
|
$paths = array();
|
77
|
foreach (explode("\n", $conf['paths']) as $path) {
|
78
|
$paths[] = check_plain($path);
|
79
|
}
|
80
|
|
81
|
$identifier = $context->type == 'any' ? t('Current path') : $context->identifier;
|
82
|
if ($conf['visibility_setting']) {
|
83
|
return format_plural(count($paths), '@identifier is "@paths"', '@identifier type is one of "@paths"', array('@paths' => implode(', ', $paths), '@identifier' => $identifier));
|
84
|
}
|
85
|
else {
|
86
|
return format_plural(count($paths), '@identifier is not "@paths"', '@identifier type is not one of "@paths"', array('@paths' => implode(', ', $paths), '@identifier' => $identifier));
|
87
|
}
|
88
|
}
|