/components/com_finder/models/suggestions.php

https://bitbucket.org/eternaware/joomus · PHP · 132 lines · 48 code · 16 blank · 68 comment · 0 complexity · 3c765dfd289d6ba11726778edb246619 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Site
  4. * @subpackage com_finder
  5. *
  6. * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('_JEXEC') or die;
  10. /**
  11. * Suggestions model class for the Finder package.
  12. *
  13. * @package Joomla.Site
  14. * @subpackage com_finder
  15. * @since 2.5
  16. */
  17. class FinderModelSuggestions extends JModelList
  18. {
  19. /**
  20. * Context string for the model type.
  21. *
  22. * @var string
  23. * @since 2.5
  24. */
  25. protected $context = 'com_finder.suggestions';
  26. /**
  27. * Method to get an array of data items.
  28. *
  29. * @return array An array of data items.
  30. *
  31. * @since 2.5
  32. */
  33. public function getItems()
  34. {
  35. // Get the items.
  36. $items = parent::getItems();
  37. // Convert them to a simple array.
  38. foreach ($items as $k => $v)
  39. {
  40. $items[$k] = $v->term;
  41. }
  42. return $items;
  43. }
  44. /**
  45. * Method to build a database query to load the list data.
  46. *
  47. * @return JDatabaseQuery A database query
  48. *
  49. * @since 2.5
  50. */
  51. protected function getListQuery()
  52. {
  53. // Create a new query object.
  54. $db = $this->getDbo();
  55. $query = $db->getQuery(true);
  56. // Select required fields
  57. $query->select('t.term');
  58. $query->from($db->quoteName('#__finder_terms') . ' AS t');
  59. $query->where('t.term LIKE ' . $db->quote($db->escape($this->getState('input'), true) . '%'));
  60. $query->where('t.common = 0');
  61. $query->order('t.links DESC');
  62. $query->order('t.weight DESC');
  63. return $query;
  64. }
  65. /**
  66. * Method to get a store id based on model the configuration state.
  67. *
  68. * This is necessary because the model is used by the component and
  69. * different modules that might need different sets of data or different
  70. * ordering requirements.
  71. *
  72. * @param string $id An identifier string to generate the store id. [optional]
  73. *
  74. * @return string A store id.
  75. *
  76. * @since 2.5
  77. */
  78. protected function getStoreId($id = '')
  79. {
  80. // Add the search query state.
  81. $id .= ':' . $this->getState('input');
  82. $id .= ':' . $this->getState('language');
  83. // Add the list state.
  84. $id .= ':' . $this->getState('list.start');
  85. $id .= ':' . $this->getState('list.limit');
  86. return parent::getStoreId($id);
  87. }
  88. /**
  89. * Method to auto-populate the model state. Calling getState in this method will result in recursion.
  90. *
  91. * @param string $ordering An optional ordering field.
  92. * @param string $direction An optional direction (asc|desc).
  93. *
  94. * @return void
  95. *
  96. * @since 2.5
  97. */
  98. protected function populateState($ordering = null, $direction = null)
  99. {
  100. // Get the configuration options.
  101. $app = JFactory::getApplication();
  102. $input = $app->input;
  103. $params = JComponentHelper::getParams('com_finder');
  104. $user = JFactory::getUser();
  105. // Get the query input.
  106. $this->setState('input', $input->request->get('q', '', 'string'));
  107. $this->setState('language', $input->request->get('l', '', 'string'));
  108. // Load the list state.
  109. $this->setState('list.start', 0);
  110. $this->setState('list.limit', 10);
  111. // Load the parameters.
  112. $this->setState('params', $params);
  113. // Load the user state.
  114. $this->setState('user.id', (int) $user->get('id'));
  115. }
  116. }