Menu.php
2.71 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
<?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 RegularLabs\Library\RegEx as RL_RegEx;
use RegularLabs\Plugin\System\BetterPreview\Params;
class Menu
{
public static function setItemId(&$item)
{
$item->url = str_replace('&', '&', $item->url);
$default_menu_item = self::getDefaultMenuItem($item);
if (empty($default_menu_item))
{
return;
}
$params = Params::get();
// Return if url already contains an Itemid
if ($params->default_menu_id != -1
&& strpos($item->url, '&Itemid=') !== false
&& strpos($item->url, '&Itemid=' . $default_menu_item->id) == false
)
{
return;
}
// Link has no ItemId yet
if (strpos($item->url, '&Itemid=') === false)
{
self::addItemId($item);
return;
}
// Replace the Itemid if it is the default (home) menu id
$default_menu_url = $default_menu_item->link . '&Itemid=' . $default_menu_item->id;
// Url is the home url, so leave Itemid alone
if ($item->url == $default_menu_url)
{
return;
}
// Remove the home Itemid
if ($params->default_menu_id == -1)
{
$item->url = RL_RegEx::replace('&Itemid=' . $default_menu_item->id . '$', '', $item->url);
return;
}
// Default setting is to add the home id, so leave Itemid alone
if ( ! $params->default_menu_id)
{
return;
}
// Replace the default home Itemid with the menu id set in the Better Preview settings
$item->url = RL_RegEx::replace('&Itemid=' . $default_menu_item->id . '$', '&Itemid=' . $params->default_menu_id, $item->url);
}
private static function addItemId(&$item)
{
$params = Params::get();
// No need to add an Itemid
if ($params->default_menu_id == -1)
{
return;
}
// Add a custom Itemid
if ($params->default_menu_id)
{
$item->url .= '&Itemid=' . $params->default_menu_id;
return;
}
// Add default menu id
$default_menu_item = self::getDefaultMenuItem($item);
if (empty($default_menu_item))
{
return;
}
$item->url .= '&Itemid=' . $default_menu_item->id;
}
public static function getDefaultMenuItem(&$item)
{
$lang = isset($item->language) ? $item->language : '';
$menu = JFactory::getApplication()->getMenu('site');
$default_menu_item = ! empty($menu) ? $menu->getDefault($lang) : null;
if (empty($default_menu_item) && ! empty($menu))
{
$default_menu_item = $menu->getDefault();
}
return $default_menu_item;
}
}