Passed
Push — master ( 5be6aa...551c4d )
by Aimeos
08:05
created

Standard::can()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2022
6
 * @package MW
7
 * @subpackage View
8
 */
9
10
11
namespace Aimeos\Base\View\Helper\Site;
12
13
14
/**
15
 * View helper class for easy access to site information
16
 *
17
 * @package MW
18
 * @subpackage View
19
 */
20
class Standard extends \Aimeos\Base\View\Helper\Base implements Iface
21
{
22
	private $siteItem;
23
24
25
	/**
26
	 * Initializes the view helper
27
	 *
28
	 * @param \Aimeos\Base\View\Iface $view View object
29
	 */
30
	public function __construct( \Aimeos\Base\View\Iface $view )
31
	{
32
		parent::__construct( $view );
33
		$this->siteItem = $view->pageSiteItem;
34
	}
35
36
37
	/**
38
	 * Returns the site view helper
39
	 *
40
	 * @return \Aimeos\Base\View\Helper\Site\Iface Site view helper
41
	 */
42
	public function transform() : \Aimeos\Base\View\Helper\Site\Iface
43
	{
44
		return $this;
45
	}
46
47
48
	/**
49
	 * Checks if the item can be deleted or modified
50
	 *
51
	 * @param string $siteid ID of a site item
52
	 * @return bool TRUE if the item can be deleted/modified
53
	 */
54
	public function can( string $siteid ) : ?bool
55
	{
56
		if( $this->view()->access( ['super'] ) ) {
57
			return true;
58
		}
59
60
		$current = $this->siteItem->getSiteId();
61
62
		if( !strncmp( $current, $siteid, strlen( $current ) ) ) {
63
			return true;
64
		}
65
66
		return false;
67
	}
68
69
70
	/**
71
	 * Returns the site label of the current site
72
	 *
73
	 * @return string|null Label of the site item or null if not available
74
	 */
75
	public function label() : ?string
76
	{
77
		return $this->siteItem->getLabel();
78
	}
79
80
81
	/**
82
	 * Returns the label of the matching site
83
	 *
84
	 * @param string|null $siteid ID of a site item
85
	 * @return string|null Label of the site item or null if not found
86
	 */
87
	public function match( string $siteid = null ) : ?string
88
	{
89
		if( $this->siteItem->getSiteId() == $siteid ) {
90
			return $this->siteItem->getLabel();
91
		}
92
93
		return null;
94
	}
95
96
97
	/**
98
	 * Returns "readonly" if the item is inherited from another site
99
	 *
100
	 * @param string|null $siteid ID of a site item
101
	 * @return string|null "readonly" if item is from a parent site, null if not
102
	 */
103
	public function readonly( string $siteid = null ) : ?string
104
	{
105
		if( !$siteid && $this->view()->access( ['super'] ) ) {
106
			return null;
107
		}
108
109
		if( $this->siteItem->getSiteId() != $siteid ) {
110
			return 'readonly';
111
		}
112
113
		return null;
114
	}
115
116
117
	/**
118
	 * Returns the site ID of the current site
119
	 *
120
	 * @return string|null Site ID or null if not available
121
	 */
122
	public function siteid() : ?string
123
	{
124
		return $this->siteItem->getSiteId();
125
	}
126
}
127