-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
suggestions.php
252 lines (218 loc) · 6.88 KB
/
suggestions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
use Joomla\Registry\Format\Json;
/**
* @package Joomla.Site
* @subpackage com_finder
*
* @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('_JEXEC') or die;
define('FINDER_PATH_INDEXER', JPATH_ADMINISTRATOR . '/components/com_finder/helpers/indexer');
JLoader::register('FinderIndexerHelper', FINDER_PATH_INDEXER . '/helper.php');
/**
* Suggestions model class for the Finder package.
*
* @since 2.5
*/
class FinderModelSuggestions extends JModelList
{
/**
* Context string for the model type.
*
* @var string
* @since 2.5
*/
protected $context = 'com_finder.suggestions';
/**
* Method to get an array of data items.
*
* @return array An array of data items.
*
* @since 2.5
*/
public function getItems()
{
// Get the items.
$items = parent::getItems();
// Convert them to a simple array.
foreach ($items as $k => $v)
{
$items[$k] = $v->term;
}
return $items;
}
/**
* Method to build a database query to load the list data.
*
* @return JDatabaseQuery A database query
*
* @since 2.5
*/
protected function getListQuery()
{
// Create a new query object.
$db = $this->getDbo();
if (in_array($db->name, array('mysqli', 'mysql')))
{
// Attempt to change mysql for error in large select
$db->setQuery('SET SQL_BIG_SELECTS=1');
$db->query();
}
$query = $db->getQuery(true);
// Get the static Taxonomy filter parameters if necessary
if (!is_null($this->getState('filter')))
{
// Select filter params
$query->select('ff.params')
->from ($db->quoteName('#__finder_filters') . ' AS ff')
->where('ff.filter_id = ' . $this->getState('filter'));
$db->setQuery($query);
$filterparams = json_decode($db->loadResult());
// Get a fresh query object
$query = $db->getQuery(true);
}
$groups = implode(',', $this->getState('user.groups'));
// Select required fields
$query->select('t.term')
->from($db->quoteName('#__finder_terms') . ' AS t')
->where('t.term LIKE ' . $db->quote($db->escape($this->getState('input'), true) . '%'))
->where('t.common = 0')
->where('t.language IN (' . $db->quote($db->escape($this->getState('language'), true)) . ', ' . $db->quote('*') . ')')
->order('t.links DESC')
->order('t.weight DESC');
$linkjoin = '';
// Iterate through each term mapping table and add the join.
for ($i = 0; $i < 16; $i++)
{
// We use the offset because each join needs a unique alias.
$query->join('LEFT', $db->quoteName('#__finder_links_terms' . dechex($i)) . ' AS lterms' . $i . ' ON lterms' . $i . '.term_id = t.term_id');
$linkjoin .= 'lterms' . $i . '.link_id=l.link_id';
if ($i < 15)
{
$linkjoin .= ' or ';
}
}
$query->join('INNER', $db->quoteName('#__finder_links') . ' AS l ON (' . $linkjoin . ')')
->where('l.access IN (' . $groups . ')')
->where('l.state = 1')
->where('l.published = 1');
// Get the null date and the current date, minus seconds.
$nullDate = $db->quote($db->getNullDate());
$nowDate = $db->quote(substr_replace(JFactory::getDate()->toSql(), '00', -2));
// Add the publish up and publish down filters.
$query->where('(l.publish_start_date = ' . $nullDate . ' OR l.publish_start_date <= ' . $nowDate . ')')
->where('(l.publish_end_date = ' . $nullDate . ' OR l.publish_end_date >= ' . $nowDate . ')');
// Add the static Taxonomy filter if necessary
if (!is_null($this->getState('filter')))
{
// Add the static Taxonomy filter
$query->join('INNER', $db->quoteName('#__finder_taxonomy_map') . ' AS tm ON (tm.link_id=l.link_id)')
->join('INNER', $db->quoteName('#__finder_filters') . ' AS ff ON (ff.data=tm.node_id)')
->where('ff.filter_id = ' . $this->getState('filter'));
// Add the start date filter to the query.
if ($filterparams->w1 != '')
{
// Escape the date.
$date1 = $db->quote($filterparams->d1);
// Add the appropriate WHERE condition.
if ($filterparams->w1 == -1)
{
$query->where($db->quoteName('l.start_date') . ' <= ' . $date1);
}
elseif ($filterparams->w1 == 1)
{
$query->where($db->quoteName('l.start_date') . ' >= ' . $date1);
}
else
{
$query->where($db->quoteName('l.start_date') . ' = ' . $date1);
}
}
// Add the end date filter to the query.
if ($filterparams->w2 != '')
{
// Escape the date.
$date2 = $db->quote($filterparams->d2);
// Add the appropriate WHERE condition.
if ($filterparams->w2 == -1)
{
$query->where($db->quoteName('l.start_date') . ' <= ' . $date2);
}
elseif ($filterparams->w2 == 1)
{
$query->where($db->quoteName('l.start_date') . ' >= ' . $date2);
}
else
{
$query->where($db->quoteName('l.start_date') . ' = ' . $date2);
}
}
}
return $query;
}
/**
* Method to get a store id based on model the configuration state.
*
* This is necessary because the model is used by the component and
* different modules that might need different sets of data or different
* ordering requirements.
*
* @param string $id An identifier string to generate the store id. [optional]
*
* @return string A store id.
*
* @since 2.5
*/
protected function getStoreId($id = '')
{
// Add the search query state.
$id .= ':' . $this->getState('input');
$id .= ':' . $this->getState('language');
// Add the list state.
$id .= ':' . $this->getState('list.start');
$id .= ':' . $this->getState('list.limit');
return parent::getStoreId($id);
}
/**
* Method to auto-populate the model state. Calling getState in this method will result in recursion.
*
* @param string $ordering An optional ordering field.
* @param string $direction An optional direction (asc|desc).
*
* @return void
*
* @since 2.5
*/
protected function populateState($ordering = null, $direction = null)
{
// Get the configuration options.
$app = JFactory::getApplication();
$input = $app->input;
$params = JComponentHelper::getParams('com_finder');
$user = JFactory::getUser();
// Get the query input.
$this->setState('input', $input->request->get('q', '', 'string'));
// Set the query language
if (JLanguageMultilang::isEnabled())
{
$lang = JFactory::getLanguage()->getTag();
}
else
{
$lang = FinderIndexerHelper::getDefaultLanguage();
}
$lang = FinderIndexerHelper::getPrimaryLanguage($lang);
$this->setState('language', $lang);
// Get the static taxonomy filter.
$this->setState('filter', $input->request->get('f', '', 'int'));
// Load the list state.
$this->setState('list.start', 0);
$this->setState('list.limit', 10);
// Load the parameters.
$this->setState('params', $params);
// Load the user state.
$this->setState('user.id', (int) $user->get('id'));
$this->setState('user.groups', $user->getAuthorisedViewLevels());
}
}