Projet

Général

Profil

Paste
Télécharger (4,7 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / rules / modules / path.eval.inc @ 76e2e7c3

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
 * Action implementation: Path alias.
13
 */
14
function rules_action_path_alias($source, $alias, $langcode = LANGUAGE_NONE) {
15
  if (!$alias) {
16
    path_delete(array('source' => $source, 'language' => $langcode));
17
  }
18
  elseif (!$source) {
19
    path_delete(array('alias' => $alias, 'language' => $langcode));
20
  }
21
  // Only set the alias if the alias is not taken yet.
22
  elseif (!path_load(array('alias' => $alias, 'language' => $langcode))) {
23
    // Update the existing path or create a new one.
24
    if ($path = path_load(array('source' => $source, 'language' => $langcode))) {
25
      $path['alias'] = $alias;
26
    }
27
    else {
28
      $path = array('source' => $source, 'alias' => $alias, 'language' => $langcode);
29
    }
30
    path_save($path);
31
  }
32
  else {
33
    rules_log('The configured alias %alias already exists. Aborting.', array('%alias' => $alias));
34
  }
35
}
36

    
37
/**
38
 * Action Implementation: Set the URL alias for a node.
39
 */
40
function rules_action_node_path_alias($node, $alias) {
41
  $langcode = isset($node->language) ? $node->language : LANGUAGE_NONE;
42
  // Only set the alias if the alias is not taken yet.
43
  if (($path = path_load(array('alias' => $alias, 'language' => $langcode))) && (empty($node->path['pid']) || $node->path['pid'] != $path['pid'])) {
44
    rules_log('The configured alias %alias already exists. Aborting.', array('%alias' => $alias));
45
    return FALSE;
46
  }
47
  $node->path['alias'] = $alias;
48
}
49

    
50
/**
51
 * Action Implementation: Set the URL alias for a node.
52
 */
53
function rules_action_taxonomy_term_path_alias($term, $alias) {
54
  // Only set the alias if the alias is not taken yet.
55
  if (($path = path_load(array('alias' => $alias, 'language' => LANGUAGE_NONE))) && (empty($term->path['pid']) || $term->path['pid'] != $path['pid'])) {
56
    rules_log('The configured alias %alias already exists. Aborting.', array('%alias' => $alias));
57
    return FALSE;
58
  }
59
  $term->path['alias'] = $alias;
60
}
61

    
62
/**
63
 * Condition implementation: Check if the path has an alias.
64
 */
65
function rules_condition_path_has_alias($source, $langcode = LANGUAGE_NONE) {
66
  return (bool) drupal_lookup_path('alias', $source, $langcode);
67
}
68

    
69
/**
70
 * Condition implementation: Check if the URL alias exists.
71
 */
72
function rules_condition_path_alias_exists($alias, $langcode = LANGUAGE_NONE) {
73
  return (bool) drupal_lookup_path('source', $alias, $langcode);
74
}
75

    
76
/**
77
 * Cleans the given path by replacing non ASCII characters with the replacment character.
78
 *
79
 * Path cleaning may be adapted by overriding the configuration variables
80
 * @code rules_clean_path @endcode,
81
 * @code rules_path_replacement_char @endcode and
82
 * @code rules_path_transliteration @endcode
83
 * in the site's settings.php file.
84
 */
85
function rules_path_default_cleaning_method($path) {
86
  $replace = variable_get('rules_path_replacement_char', '-');
87
  if ($replace) {
88
    // If the transliteration module is enabled, transliterate the alias first.
89
    if (module_exists('transliteration') && variable_get('rules_path_transliteration', TRUE)) {
90
      $path = transliteration_get($path);
91
    }
92

    
93
    $array = variable_get('rules_clean_path', array('/[^a-zA-Z0-9\-_]+/', $replace));
94
    $array[2] = $path;
95
    // Replace it and remove trailing and leading replacement characters.
96
    $output = trim(call_user_func_array('preg_replace', $array), $replace);
97

    
98
    if (variable_get('rules_path_lower_case', TRUE)) {
99
      $output = drupal_strtolower($output);
100
    }
101
    return $output;
102
  }
103
  else {
104
    return $path;
105
  }
106
}
107

    
108
/**
109
 * Cleans the given string so it can be used as part of a URL path.
110
 */
111
function rules_clean_path($path) {
112
  $function = variable_get('rules_path_cleaning_callback', 'rules_path_default_cleaning_method');
113
  if (!function_exists($function)) {
114
    rules_log('An invalid URL path cleaning callback has been configured. Falling back to the default cleaning method.', array(), RulesLog::WARN);
115
    $function = 'rules_path_default_cleaning_method';
116
  }
117
  return $function($path);
118
}
119

    
120
/**
121
 * CTools path cleaning callback.
122
 *
123
 * @see rules_admin_settings()
124
 */
125
function rules_path_clean_ctools($path) {
126
  // Make use of the CTools cleanstring implementation.
127
  ctools_include('cleanstring');
128
  $settings = array(
129
    'separator' => variable_get('rules_path_replacement_char', '-'),
130
    'transliterate' => module_exists('transliteration') && variable_get('rules_path_transliteration', TRUE),
131
    'lower case' => variable_get('rules_path_lower_case', TRUE),
132
  );
133
  return ctools_cleanstring($path, $settings);
134
}
135

    
136
/**
137
 * Pathauto path cleaning callback.
138
 *
139
 * @see rules_admin_settings()
140
 */
141
function rules_path_clean_pathauto($path) {
142
  module_load_include('inc', 'pathauto');
143
  return pathauto_cleanstring($path);
144
}
145

    
146
/**
147
 * @}
148
 */