path.eval.inc 4.7 KB

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