/modules/mod_tags_similar/helper.php

https://github.com/andreatarr/joomla-cms · PHP · 155 lines · 104 code · 25 blank · 26 comment · 25 complexity · 00d91d20c40ddb3d5519eca13dba7fa1 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Site
  4. * @subpackage mod_tags_popular
  5. *
  6. * @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. defined('_JEXEC') or die;
  10. use Joomla\Registry\Registry;
  11. /**
  12. * Helper for mod_tags_popular
  13. *
  14. * @package Joomla.Site
  15. * @subpackage mod_tags_popular
  16. * @since 3.1
  17. */
  18. abstract class ModTagssimilarHelper
  19. {
  20. /**
  21. * Get a list of tags
  22. *
  23. * @param Registry &$params Module parameters
  24. *
  25. * @return mixed Results array / null
  26. */
  27. public static function getList(&$params)
  28. {
  29. $app = JFactory::getApplication();
  30. $option = $app->input->get('option');
  31. $view = $app->input->get('view');
  32. // For now assume com_tags and com_users do not have tags.
  33. // This module does not apply to list views in general at this point.
  34. if ($option == 'com_tags' || $view == 'category' || $option == 'com_users')
  35. {
  36. return;
  37. }
  38. $db = JFactory::getDbo();
  39. $user = JFactory::getUser();
  40. $groups = implode(',', $user->getAuthorisedViewLevels());
  41. $matchtype = $params->get('matchtype', 'all');
  42. $maximum = $params->get('maximum', 5);
  43. $ordering = $params->get('ordering', 'count');
  44. $tagsHelper = new JHelperTags;
  45. $prefix = $option . '.' . $view;
  46. $id = $app->input->getInt('id');
  47. $now = JFactory::getDate()->toSql();
  48. $nullDate = $db->getNullDate();
  49. $tagsToMatch = $tagsHelper->getTagIds($id, $prefix);
  50. if (!$tagsToMatch || is_null($tagsToMatch))
  51. {
  52. return;
  53. }
  54. $tagCount = substr_count($tagsToMatch, ',') + 1;
  55. $query = $db->getQuery(true)
  56. ->select(
  57. array(
  58. $db->quoteName('m.core_content_id'),
  59. $db->quoteName('m.content_item_id'),
  60. $db->quoteName('m.type_alias'),
  61. 'COUNT( ' . $db->quoteName('tag_id') . ') AS ' . $db->quoteName('count'),
  62. $db->quoteName('ct.router'),
  63. $db->quoteName('cc.core_title'),
  64. $db->quoteName('cc.core_alias'),
  65. $db->quoteName('cc.core_catid'),
  66. $db->quoteName('cc.core_language'),
  67. $db->quoteName('cc.core_params')
  68. )
  69. );
  70. $query->from($db->quoteName('#__contentitem_tag_map', 'm'));
  71. $query->join('INNER', $db->quoteName('#__tags', 't') . ' ON m.tag_id = t.id')
  72. ->join('INNER', $db->quoteName('#__ucm_content', 'cc') . ' ON m.core_content_id = cc.core_content_id')
  73. ->join('INNER', $db->quoteName('#__content_types', 'ct') . ' ON m.type_alias = ct.type_alias');
  74. $query->where($db->quoteName('m.tag_id') . ' IN (' . $tagsToMatch . ')');
  75. $query->where('t.access IN (' . $groups . ')');
  76. $query->where('(cc.core_access IN (' . $groups . ') OR cc.core_access = 0)');
  77. // Don't show current item
  78. $query->where('(' . $db->quoteName('m.content_item_id') . ' <> ' . $id
  79. . ' OR ' . $db->quoteName('m.type_alias') . ' <> ' . $db->quote($prefix) . ')');
  80. // Only return published tags
  81. $query->where($db->quoteName('cc.core_state') . ' = 1 ')
  82. ->where('(' . $db->quoteName('cc.core_publish_up') . '=' . $db->quote($nullDate) . ' OR '
  83. . $db->quoteName('cc.core_publish_up') . '<=' . $db->quote($now) . ')')
  84. ->where('(' . $db->quoteName('cc.core_publish_down') . '=' . $db->quote($nullDate) . ' OR '
  85. . $db->quoteName('cc.core_publish_down') . '>=' . $db->quote($now) . ')');
  86. // Optionally filter on language
  87. $language = JComponentHelper::getParams('com_tags')->get('tag_list_language_filter', 'all');
  88. if ($language != 'all')
  89. {
  90. if ($language == 'current_language')
  91. {
  92. $language = JHelperContent::getCurrentLanguage();
  93. }
  94. $query->where($db->quoteName('cc.core_language') . ' IN (' . $db->quote($language) . ', ' . $db->quote('*') . ')');
  95. }
  96. $query->group(
  97. $db->quoteName(
  98. array('m.core_content_id', 'm.content_item_id', 'm.type_alias', 'ct.router', 'cc.core_title',
  99. 'cc.core_alias', 'cc.core_catid', 'cc.core_language', 'cc.core_params')
  100. )
  101. );
  102. if ($matchtype == 'all' && $tagCount > 0)
  103. {
  104. $query->having('COUNT( ' . $db->quoteName('tag_id') . ') = ' . $tagCount);
  105. }
  106. elseif ($matchtype == 'half' && $tagCount > 0)
  107. {
  108. $tagCountHalf = ceil($tagCount / 2);
  109. $query->having('COUNT( ' . $db->quoteName('tag_id') . ') >= ' . $tagCountHalf);
  110. }
  111. if ($ordering == 'count' || $ordering == 'countrandom')
  112. {
  113. $query->order($db->quoteName('count') . ' DESC');
  114. }
  115. if ($ordering == 'random' || $ordering == 'countrandom')
  116. {
  117. $query->order('RAND()');
  118. }
  119. $db->setQuery($query, 0, $maximum);
  120. $results = $db->loadObjectList();
  121. foreach ($results as $result)
  122. {
  123. $explodedAlias = explode('.', $result->type_alias);
  124. $result->link = 'index.php?option=' . $explodedAlias[0] . '&view=' . $explodedAlias[1]
  125. . '&id=' . $result->content_item_id . '-' . $result->core_alias;
  126. $result->core_params = new Registry($result->core_params);
  127. }
  128. return $results;
  129. }
  130. }