Guest User

Untitled

a guest
Feb 17th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. DELIMITER ;;
  2.  
  3. DROP FUNCTION IF EXISTS `slugify`;;
  4. CREATE FUNCTION `slugify`(dirty_string varchar(255)) RETURNS varchar(255) CHARSET utf8
  5. DETERMINISTIC
  6. BEGIN
  7. DECLARE x, y , z , k INT;
  8. DECLARE temp_string, new_string, accents, noAccents VARCHAR(255);
  9. DECLARE is_allowed BOOL;
  10. DECLARE c, check_char VARCHAR(1);
  11.  
  12. -- IF NULL DO NOT PROCEED
  13. If dirty_string IS NULL Then
  14. return dirty_string;
  15. End If;
  16.  
  17. set temp_string = LOWER(dirty_string);
  18.  
  19. -- REPLACE ACCENTS
  20. -- WITH CAPS
  21. -- set accents = 'ŠšŽžÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÑÒÓÔÕÖØÙÚÛÜÝŸÞàáâãäåæçèéêëìíîïñòóôõöøùúûüýÿþƒ';
  22. -- set noAccents = 'SsZzAAAAAAACEEEEIIIINOOOOOOUUUUYYBaaaaaaaceeeeiiiinoooooouuuuyybf';
  23. -- ONLY SMALL CAPS
  24. set accents = 'ýỳỷỹỵáàảãạâấầẩẫậăắằẳẵặéèẻẽẹêếềểễệúùủũụưứừửữựíìỉĩịóòỏõọơớờởỡợôốồổỗộđ·/_,:;';
  25. set noAccents = 'yyyyyaaaaaaaaaaaaaaaaaeeeeeeeeeeeuuuuuuuuuuuiiiiioooooooooooooooood------';
  26. set k = CHAR_LENGTH(accents);
  27.  
  28. while k > 0 do
  29. set temp_string = REPLACE(temp_string, SUBSTRING(accents, k, 1), SUBSTRING(noAccents, k, 1));
  30. set k = k - 1;
  31. end while;
  32.  
  33. -- CONVERT & TO EMPTY SPACE
  34. Set temp_string = REPLACE(temp_string, '&', '');
  35.  
  36. -- REPLACE ALL UNWANTED CHARS
  37. Select temp_string REGEXP('[^a-z0-9\-]+') into x;
  38. If x = 1 then
  39. set z = 1;
  40. set k = CHAR_LENGTH(temp_string);
  41. While z <= k Do
  42. Set c = SUBSTRING(temp_string, z, 1);
  43. Set is_allowed = FALSE;
  44. If !((ascii(c) = 45) or (ascii(c) >= 48 and ascii(c) <= 57) or (ascii(c) >= 97 and ascii(c) <= 122)) Then
  45. Set temp_string = REPLACE(temp_string, c, '-');
  46. End If;
  47. set z = z + 1;
  48. End While;
  49. End If;
  50.  
  51. Select temp_string REGEXP("^-|-$|'") into x;
  52. If x = 1 Then
  53. Set temp_string = Replace(temp_string, "'", '');
  54. Set z = CHAR_LENGTH(temp_string);
  55. Set y = CHAR_LENGTH(temp_string);
  56. Dash_check: While z > 1 Do
  57. If STRCMP(SUBSTRING(temp_string, -1, 1), '-') = 0 Then
  58. Set temp_string = SUBSTRING(temp_string,1, y-1);
  59. Set y = y - 1;
  60. Else
  61. Leave Dash_check;
  62. End If;
  63. Set z = z - 1;
  64. End While;
  65. End If;
  66.  
  67. Repeat
  68. Select temp_string REGEXP("--") into x;
  69. If x = 1 Then
  70. Set temp_string = REPLACE(temp_string, "--", "-");
  71. End If;
  72. Until x <> 1 End Repeat;
  73.  
  74. If LOCATE('-', temp_string) = 1 Then
  75. Set temp_string = SUBSTRING(temp_string, 2);
  76. End If;
  77.  
  78. Return temp_string;
  79. END;;
  80.  
  81. DELIMITER ;
  82.  
  83. SELECT DISTINCT
  84. nd.news_id AS id,
  85. unix_timestamp(date_added) AS createdAt,
  86. unix_timestamp(date_modified) AS updatedAt,
  87. name AS title,
  88. slugify(name) AS slug,
  89. image AS thumbUrl,
  90. short_description AS description,
  91. description AS content,
  92. viewed AS viewCount,
  93. status as isActive,
  94. news_category_id as category
  95. FROM oc_news n
  96. LEFT JOIN oc_news_description nd ON (n.news_id = nd.news_id)
  97. LEFT JOIN oc_news_to_category n2c ON (n.news_id = n2c.news_id)
Add Comment
Please, Sign In to add comment