1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Contains rules integration for the path module needed during evaluation.
|
6
|
*
|
7
|
* @addtogroup rules
|
8
|
*
|
9
|
* @{
|
10
|
*/
|
11
|
|
12
|
/**
|
13
|
* Action implementation: Path alias.
|
14
|
*/
|
15
|
function rules_action_path_alias($source, $alias, $langcode = LANGUAGE_NONE) {
|
16
|
if (!$alias) {
|
17
|
path_delete(array('source' => $source, 'language' => $langcode));
|
18
|
}
|
19
|
elseif (!$source) {
|
20
|
path_delete(array('alias' => $alias, 'language' => $langcode));
|
21
|
}
|
22
|
// Only set the alias if the alias is not taken yet.
|
23
|
elseif (!path_load(array('alias' => $alias, 'language' => $langcode))) {
|
24
|
// Update the existing path or create a new one.
|
25
|
if ($path = path_load(array('source' => $source, 'language' => $langcode))) {
|
26
|
$path['alias'] = $alias;
|
27
|
}
|
28
|
else {
|
29
|
$path = array('source' => $source, 'alias' => $alias, 'language' => $langcode);
|
30
|
}
|
31
|
path_save($path);
|
32
|
}
|
33
|
else {
|
34
|
rules_log('The configured alias %alias already exists. Aborting.', array('%alias' => $alias));
|
35
|
}
|
36
|
}
|
37
|
|
38
|
/**
|
39
|
* Action Implementation: Set the URL alias for a node.
|
40
|
*/
|
41
|
function rules_action_node_path_alias($node, $alias) {
|
42
|
$langcode = isset($node->language) ? $node->language : LANGUAGE_NONE;
|
43
|
// Only set the alias if the alias is not taken yet.
|
44
|
if (($path = path_load(array('alias' => $alias, 'language' => $langcode))) && (empty($node->path['pid']) || $node->path['pid'] != $path['pid'])) {
|
45
|
rules_log('The configured alias %alias already exists. Aborting.', array('%alias' => $alias));
|
46
|
return FALSE;
|
47
|
}
|
48
|
$node->path['alias'] = $alias;
|
49
|
}
|
50
|
|
51
|
/**
|
52
|
* Action Implementation: Set the URL alias for a node.
|
53
|
*/
|
54
|
function rules_action_taxonomy_term_path_alias($term, $alias) {
|
55
|
// Only set the alias if the alias is not taken yet.
|
56
|
if (($path = path_load(array('alias' => $alias, 'language' => LANGUAGE_NONE))) && (empty($term->path['pid']) || $term->path['pid'] != $path['pid'])) {
|
57
|
rules_log('The configured alias %alias already exists. Aborting.', array('%alias' => $alias));
|
58
|
return FALSE;
|
59
|
}
|
60
|
$term->path['alias'] = $alias;
|
61
|
}
|
62
|
|
63
|
/**
|
64
|
* Condition implementation: Check if the path has an alias.
|
65
|
*/
|
66
|
function rules_condition_path_has_alias($source, $langcode = LANGUAGE_NONE) {
|
67
|
return (bool) drupal_lookup_path('alias', $source, $langcode);
|
68
|
}
|
69
|
|
70
|
/**
|
71
|
* Condition implementation: Check if the URL alias exists.
|
72
|
*/
|
73
|
function rules_condition_path_alias_exists($alias, $langcode = LANGUAGE_NONE) {
|
74
|
return (bool) drupal_lookup_path('source', $alias, $langcode);
|
75
|
}
|
76
|
|
77
|
/**
|
78
|
* Cleans the given path.
|
79
|
*
|
80
|
* A path is cleaned by replacing non ASCII characters in the path with the
|
81
|
* replacement character.
|
82
|
*
|
83
|
* Path cleaning may be customized by overriding the configuration variables:
|
84
|
* @code rules_clean_path @endcode,
|
85
|
* @code rules_path_replacement_char @endcode and
|
86
|
* @code rules_path_transliteration @endcode
|
87
|
* in the site's settings.php file.
|
88
|
*/
|
89
|
function rules_path_default_cleaning_method($path) {
|
90
|
$replace = variable_get('rules_path_replacement_char', '-');
|
91
|
if ($replace) {
|
92
|
// If the transliteration module is enabled, transliterate the alias first.
|
93
|
if (module_exists('transliteration') && variable_get('rules_path_transliteration', TRUE)) {
|
94
|
$path = transliteration_get($path);
|
95
|
}
|
96
|
|
97
|
$array = variable_get('rules_clean_path', array('/[^a-zA-Z0-9\-_]+/', $replace));
|
98
|
$array[2] = $path;
|
99
|
// Replace it and remove trailing and leading replacement characters.
|
100
|
$output = trim(call_user_func_array('preg_replace', $array), $replace);
|
101
|
|
102
|
if (variable_get('rules_path_lower_case', TRUE)) {
|
103
|
$output = drupal_strtolower($output);
|
104
|
}
|
105
|
return $output;
|
106
|
}
|
107
|
else {
|
108
|
return $path;
|
109
|
}
|
110
|
}
|
111
|
|
112
|
/**
|
113
|
* Cleans the given string so it can be used as part of a URL path.
|
114
|
*/
|
115
|
function rules_clean_path($path) {
|
116
|
$function = variable_get('rules_path_cleaning_callback', 'rules_path_default_cleaning_method');
|
117
|
if (!function_exists($function)) {
|
118
|
rules_log('An invalid URL path cleaning callback has been configured. Falling back to the default cleaning method.', array(), RulesLog::WARN);
|
119
|
$function = 'rules_path_default_cleaning_method';
|
120
|
}
|
121
|
return $function($path);
|
122
|
}
|
123
|
|
124
|
/**
|
125
|
* CTools path cleaning callback.
|
126
|
*
|
127
|
* @see rules_admin_settings()
|
128
|
*/
|
129
|
function rules_path_clean_ctools($path) {
|
130
|
// Make use of the CTools cleanstring implementation.
|
131
|
ctools_include('cleanstring');
|
132
|
$settings = array(
|
133
|
'separator' => variable_get('rules_path_replacement_char', '-'),
|
134
|
'transliterate' => module_exists('transliteration') && variable_get('rules_path_transliteration', TRUE),
|
135
|
'lower case' => variable_get('rules_path_lower_case', TRUE),
|
136
|
);
|
137
|
return ctools_cleanstring($path, $settings);
|
138
|
}
|
139
|
|
140
|
/**
|
141
|
* Pathauto path cleaning callback.
|
142
|
*
|
143
|
* @see rules_admin_settings()
|
144
|
*/
|
145
|
function rules_path_clean_pathauto($path) {
|
146
|
module_load_include('inc', 'pathauto');
|
147
|
return pathauto_cleanstring($path);
|
148
|
}
|
149
|
|
150
|
/**
|
151
|
* @}
|
152
|
*/
|