PageRenderTime 48ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/search/newsfeeds/newsfeeds.php

https://github.com/joebushi/joomla
PHP | 134 lines | 89 code | 17 blank | 28 comment | 7 complexity | c43e946a66f1080570bf2dfe90c1fb34 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. <?php
  2. /**
  3. * @version $Id$
  4. * @package Joomla
  5. * @copyright Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
  6. * @license GNU General Public License version 2 or later; see LICENSE.txt
  7. */
  8. // no direct access
  9. defined('_JEXEC') or die;
  10. jimport('joomla.plugin.plugin');
  11. /**
  12. * Newsfeeds Search plugin
  13. *
  14. * @package Joomla
  15. * @subpackage Search
  16. * @since 1.6
  17. */
  18. class plgSearchNewsfeeds extends JPlugin
  19. {
  20. /**
  21. * @return array An array of search areas
  22. */
  23. function onSearchAreas()
  24. {
  25. static $areas = array(
  26. 'newsfeeds' => 'Newsfeeds'
  27. );
  28. return $areas;
  29. }
  30. /**
  31. * Newsfeeds Search method
  32. *
  33. * The sql must return the following fields that are used in a common display
  34. * routine: href, title, section, created, text, browsernav
  35. * @param string Target search string
  36. * @param string mathcing option, exact|any|all
  37. * @param string ordering option, newest|oldest|popular|alpha|category
  38. * @param mixed An array if the search it to be restricted to areas, null if search all
  39. */
  40. function onSearch($text, $phrase='', $ordering='', $areas=null)
  41. {
  42. $db = &JFactory::getDbo();
  43. $user = &JFactory::getUser();
  44. $groups = implode(',', $user->authorisedLevels());
  45. if (is_array($areas)) {
  46. if (!array_intersect($areas, array_keys(plgSearchNewsfeedAreas()))) {
  47. return array();
  48. }
  49. }
  50. // load plugin params info
  51. $plugin = &JPluginHelper::getPlugin('search', 'newsfeeds');
  52. $pluginParams = new JParameter($plugin->params);
  53. $limit = $pluginParams->def('search_limit', 50);
  54. $text = trim($text);
  55. if ($text == '') {
  56. return array();
  57. }
  58. $wheres = array();
  59. switch ($phrase) {
  60. case 'exact':
  61. $text = $db->Quote('%'.$db->getEscaped($text, true).'%', false);
  62. $wheres2 = array();
  63. $wheres2[] = 'a.name LIKE '.$text;
  64. $wheres2[] = 'a.link LIKE '.$text;
  65. $where = '(' . implode(') OR (', $wheres2) . ')';
  66. break;
  67. case 'all':
  68. case 'any':
  69. default:
  70. $words = explode(' ', $text);
  71. $wheres = array();
  72. foreach ($words as $word)
  73. {
  74. $word = $db->Quote('%'.$db->getEscaped($word, true).'%', false);
  75. $wheres2 = array();
  76. $wheres2[] = 'a.name LIKE '.$word;
  77. $wheres2[] = 'a.link LIKE '.$word;
  78. $wheres[] = implode(' OR ', $wheres2);
  79. }
  80. $where = '(' . implode(($phrase == 'all' ? ') AND (' : ') OR ('), $wheres) . ')';
  81. break;
  82. }
  83. switch ($ordering) {
  84. case 'alpha':
  85. $order = 'a.name ASC';
  86. break;
  87. case 'category':
  88. $order = 'b.title ASC, a.name ASC';
  89. break;
  90. case 'oldest':
  91. case 'popular':
  92. case 'newest':
  93. default:
  94. $order = 'a.name ASC';
  95. }
  96. $searchNewsfeeds = JText::_('Newsfeeds');
  97. $query = 'SELECT a.name AS title, "" AS created, a.link AS text,'
  98. . ' CASE WHEN CHAR_LENGTH(a.alias) THEN CONCAT_WS(\':\', a.id, a.alias) ELSE a.id END as slug, '
  99. . ' CASE WHEN CHAR_LENGTH(b.alias) THEN CONCAT_WS(\':\', b.id, b.alias) ELSE b.id END as catslug, '
  100. . ' CONCAT_WS(" / ", '. $db->Quote($searchNewsfeeds) .', b.title)AS section,'
  101. . ' "1" AS browsernav'
  102. . ' FROM #__newsfeeds AS a'
  103. . ' INNER JOIN #__categories AS b ON b.id = a.catid'
  104. . ' WHERE ('. $where .')'
  105. . ' AND a.published = 1'
  106. . ' AND b.published = 1'
  107. . ' AND b.access IN ('.$groups.')'
  108. . ' ORDER BY '. $order
  109. ;
  110. $db->setQuery($query, 0, $limit);
  111. $rows = $db->loadObjectList();
  112. foreach($rows as $key => $row) {
  113. $rows[$key]->href = 'index.php?option=com_newsfeeds&view=newsfeed&catid='.$row->catslug.'&id='.$row->slug;
  114. }
  115. return $rows;
  116. }
  117. }