-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.php
237 lines (201 loc) · 5.92 KB
/
helper.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
<?php
/**
* @package Joomla.Site
* @subpackage mod_jea_emphasis
* @copyright Copyright (C) 2008 - 2020 PHILIP Sylvain. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('_JEXEC') or die;
use Joomla\CMS\Factory;
use Joomla\CMS\Uri\Uri;
use Joomla\CMS\Router\Route;
use Joomla\Registry\Registry;
use Joomla\Database\DatabaseInterface;
use Joomla\CMS\Application\SiteApplication;
/**
* Helper class for mod_jea_emphasis
*
*/
class modJeaEmphasisHelper
{
/**
* Retrieve a list of properties
*
* @param Registry $params The module configuration
*
* @return null|stdClass[] Db rows of properties
*/
public static function getItems(Registry $params)
{
$app = Factory::getApplication();
assert($app instanceof SiteApplication);
$orderby = $params->get('order_by', 'created');
$selection = $params->get('selection', 'featured');
$limit = (int) $params->get('number_items', 5);
$db = Factory::getContainer()->get(DatabaseInterface::class);
$query = $db->getQuery(true);
$query->select('p.*');
$query->from('#__jea_properties AS p');
// Join properties types
$query->select('t.value AS `type`');
$query->join('LEFT', '#__jea_types AS t ON t.id = p.type_id');
// Join departments
$query->select('d.value AS department');
$query->join('LEFT', '#__jea_departments AS d ON d.id = p.department_id');
// Join towns
$query->select('town.value AS town');
$query->join('LEFT', '#__jea_towns AS town ON town.id = p.town_id');
// Join slogans
$query->select('s.value AS slogan');
$query->join('LEFT', '#__jea_slogans AS s ON s.id = p.slogan_id');
$query->where('p.published=1');
$query->where('p.language in (' . $db->quote($app->getLanguage()->getTag()) . ',' . $db->quote('*') . ')');
// Filter by access level
$user = $app->getIdentity();
$groups = implode(',', $user->getAuthorisedViewLevels());
$query->where('p.access IN (' . $groups . ')');
// Filter by start and end dates.
$nullDate = $db->Quote($db->getNullDate());
$nowDate = $db->Quote(Factory::getDate()->toSql());
$query->where('(p.publish_up = ' . $nullDate . ' OR p.publish_up <= ' . $nowDate . ')');
$query->where('(p.publish_down = ' . $nullDate . ' OR p.publish_down >= ' . $nowDate . ')');
switch ($selection)
{
case 'featured':
$query->where('p.featured=1');
break;
case 'latest':
$orderby = 'created';
break;
case 'random':
$orderby = 'random';
break;
}
switch ($orderby)
{
case 'created':
$query->order('p.created DESC');
break;
case 'ordering':
$query->order('p.ordering ASC');
break;
case 'price':
$query->order('p.price ASC');
break;
case 'hits':
$query->order('p.hits DESC');
break;
case 'random':
$query->order('RAND()');
break;
}
$db->setQuery($query, 0, $limit);
return $db->loadObjectList();
}
/**
* Get the route url for a property
*
* @param stdClass $row The property db record
*
* @return string The property route
*/
public static function getPropertyRoute($row)
{
static $menuItems;
if ($menuItems === null)
{
$menuItems = array(
'both' => 0,
'renting' => 0,
'selling' => 0
);
$app = Factory::getApplication();
assert($app instanceof SiteApplication);
$menu = $app->getMenu();
$items = $menu->getItems('component', 'com_jea');
$lang = $app->getLanguage()->getTag();
$user = $app->getIdentity();
$viewLevels = $user->getAuthorisedViewLevels();
foreach ($items as $item)
{
if (!in_array($item->access, $viewLevels))
{
continue;
}
$layout = isset($item->query['layout']) ? $item->query['layout'] : 'default';
$view = isset($item->query['view']) ? $item->query['view'] : '';
if ($view == 'properties' && ($item->language == '*' || $item->language == $lang))
{
if ($layout == 'search' || $layout == 'searchmap')
{
if (empty($menuItems['both']))
{
$menuItems['both'] = $item->id;
}
}
if ($layout == 'default')
{
$type = $item->getParams()->get('filter_transaction_type');
if ($type == 'SELLING' && empty($menuItems['selling']))
{
$menuItems['selling'] = $item->id;
}
elseif ($type == 'RENTING' && empty($menuItems['renting']))
{
$menuItems['renting'] = $item->id;
}
elseif (empty($menuItems['both']))
{
$menuItems['both'] = $item->id;
}
}
}
}
}
$slug = $row->alias ? ($row->id . ':' . $row->alias) : $row->id;
$url = 'index.php?option=com_jea&view=property&id=' . $slug;
if (!empty($menuItems['selling']) && $row->transaction_type == 'SELLING')
{
$url .= '&Itemid=' . $menuItems['selling'];
}
elseif (!empty($menuItems['renting']) && $row->transaction_type == 'RENTING')
{
$url .= '&Itemid=' . $menuItems['renting'];
}
elseif (!empty($menuItems['both']))
{
$url .= '&Itemid=' . $menuItems['both'];
}
return Route::_($url);
}
/**
* Get the main image url of a property
*
* @param stdClass $row The property db record
*
* @return string The main image url of the given property
*/
public static function getItemImg (&$row)
{
$images = json_decode($row->images);
$image = null;
if (!empty($images) && is_array($images))
{
$image = array_shift($images);
$imagePath = JPATH_ROOT . '/images/com_jea';
if (file_exists($imagePath . '/thumb-min/' . $row->id . '-' . $image->name))
{
// If the thumbnail already exists, display it directly
$baseURL = Uri::root(true);
return $baseURL . '/images/com_jea/thumb-min/' . $row->id . '-' . $image->name;
}
elseif (file_exists($imagePath . '/images/' . $row->id . '/' . $image->name))
{
// If the thumbnail doesn't exist, generate it and output it on the fly
$url = 'index.php?option=com_jea&task=thumbnail.create&size=min&id=' . $row->id . '&image=' . $image->name;
return Route::_($url);
}
}
return '';
}
}