path.eval.inc 4.7 KB

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