Helper.php
3.54 KB
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
<?php
/**
* @package Better Preview
* @version 6.1.0
*
* @author Peter van Westen <info@regularlabs.com>
* @link http://www.regularlabs.com
* @copyright Copyright © 2018 Regular Labs All Rights Reserved
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
*/
namespace RegularLabs\Plugin\System\BetterPreview\Component;
defined('_JEXEC') or die;
use JFactory;
use JText;
class Helper
{
public static function getItemId($url)
{
$db = JFactory::getDbo();
$query = $db->getQuery(true)
->select('a.id')
->from('#__menu as a')
->where('a.link = ' . $db->quote($url))
->where('a.client_id = 0')
->where('a.published = 1');
$db->setQuery($query);
return $db->loadResult();
}
public static function getItem($id = 0, $table, $selects = [], $texts = [])
{
$db = JFactory::getDbo();
list($selects, $names) = self::getSelects($selects);
$texts = self::getTexts($texts, $names);
$query = $db->getQuery(true)
->from('#__' . $table . ' as a')
->where('a.' . $names['id'] . ' = ' . (int) $id);
foreach ($selects as $select)
{
$query->select($select);
}
$db->setQuery($query);
$item = $db->loadObject();
$itemfound = 1;
if ( ! $item)
{
$itemfound = 0;
$item = (object) [];
foreach ($selects as $k => $v)
{
$item->{$k} = '';
}
}
foreach ($texts as $k => $v)
{
$item->{$k} = JText::_($v);
}
if ($itemfound && ! $item->published)
{
$item->error = JText::_('BP_MESSAGE_ITEM_UNPUBLISHED');
}
$item->home = 0;
return $item;
}
public static function getParents(&$item, $table, $selects = [], $texts = [], $root = 0)
{
if ( ! isset($item->parent))
{
return [];
}
$db = JFactory::getDbo();
list($selects, $names) = self::getSelects($selects);
$texts = self::getTexts($texts, $names);
$id = $item->parent;
$parents = [];
while ($id > $root)
{
$query = $db->getQuery(true)
->from('#__' . $table . ' as a')
->where('a.' . $names['id'] . ' = ' . (int) $id);
foreach ($selects as $select)
{
$query->select($select);
}
$db->setQuery($query);
$parent = $db->loadObject();
if ( ! $parent)
{
break;
}
$parents[] = $parent;
$id = $parent->parent;
}
$parents = array_reverse($parents);
$unpublished = 0;
foreach ($parents as &$parent)
{
foreach ($texts as $k => $v)
{
$parent->{$k} = JText::_($v);
}
if ( ! $parent->published)
{
$unpublished = 1;
$parent->error = JText::_('BP_MESSAGE_ITEM_UNPUBLISHED');
continue;
}
if ($unpublished)
{
$parent->published = 0;
$parent->error = JText::_('BP_MESSAGE_PARENT_UNPUBLISHED');
continue;
}
}
$parents = array_reverse($parents);
if ($unpublished)
{
$item->published = 0;
$item->error = JText::_('BP_MESSAGE_PARENT_UNPUBLISHED');
}
return $parents;
}
public static function getSelects($selects)
{
$names = array_merge(
[
'id' => 'id',
'name' => 'name',
'published' => 'published',
'parent' => 'parent',
], $selects
);
$selects = [];
foreach ($names as $k => $v)
{
if ( ! $k || ! $v)
{
continue;
}
$selects[$k] = 'a.' . $v . ' as ' . $k;
}
return [$selects, $names];
}
public static function getTexts($texts, $names)
{
$text_defaults = [
'url' => '',
'type' => '',
];
foreach ($text_defaults as $k => $v)
{
if (isset($names[$k]))
{
unset($text_defaults[$k]);
}
}
return array_merge($text_defaults, $texts);
}
}