Completed
Push — master ( 288f5d...10a421 )
by Aimeos
17:31
created

View::getGroups()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 17
rs 9.4285
cc 2
eloc 9
nc 1
nop 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2016
6
 * @package Slim
7
 * @subpackage Base
8
 */
9
10
namespace Aimeos\Slim\Base;
11
12
use Interop\Container\ContainerInterface;
13
use Psr\Http\Message\ServerRequestInterface;
14
use Psr\Http\Message\ResponseInterface;
15
16
17
/**
18
 * Service providing the view objects
19
 *
20
 * @package Slim
21
 * @subpackage Base
22
 */
23
class View
24
{
25
	private $container;
26
27
28
	/**
29
	 * Initializes the object
30
	 *
31
	 * @param ContainerInterface $container Dependency container
32
	 */
33
	public function __construct( ContainerInterface $container )
34
	{
35
		$this->container = $container;
36
	}
37
38
39
	/**
40
	 * Creates the view object for the HTML client.
41
	 *
42
	 * @param ServerRequestInterface $request Request object
43
	 * @param ResponseInterface $response Response object
44
	 * @param array $attr Associative list of URI parameters
45
	 * @param array $templatePaths List of base path names with relative template paths as key/value pairs
46
	 * @param string|null $locale Code of the current language or null for no translation
47
	 * @return \Aimeos\MW\View\Iface View object
48
	 */
49
	public function create( ServerRequestInterface $request, ResponseInterface $response, array $attr, array $templatePaths, $locale = null )
50
	{
51
		$params = $fixed = array();
52
		$config = $this->container->get( 'aimeos_config' );
53
54
		if( $locale !== null )
55
		{
56
			$params = $attr + (array) $request->getParsedBody() + (array) $request->getQueryParams();
57
			$fixed = $this->getFixedParams( $attr );
58
59
			$i18n = $this->container->get( 'aimeos_i18n' )->get( array( $locale ) );
60
			$translation = $i18n[$locale];
61
		}
62
		else
63
		{
64
			$translation = new \Aimeos\MW\Translation\None( 'en' );
65
		}
66
67
68
		$view = new \Aimeos\MW\View\Standard( $templatePaths );
69
70
		$helper = new \Aimeos\MW\View\Helper\Translate\Standard( $view, $translation );
71
		$view->addHelper( 'translate', $helper );
72
73
		$helper = new \Aimeos\MW\View\Helper\Url\Slim( $view, $this->container->get( 'router' ), $fixed );
74
		$view->addHelper( 'url', $helper );
75
76
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $params );
77
		$view->addHelper( 'param', $helper );
78
79
		$config = new \Aimeos\MW\Config\Decorator\Protect( clone $config, array( 'admin', 'client' ) );
80
		$helper = new \Aimeos\MW\View\Helper\Config\Standard( $view, $config );
81
		$view->addHelper( 'config', $helper );
82
83
		$sepDec = $config->get( 'client/html/common/format/seperatorDecimal', '.' );
84
		$sep1000 = $config->get( 'client/html/common/format/seperator1000', ' ' );
85
		$helper = new \Aimeos\MW\View\Helper\Number\Standard( $view, $sepDec, $sep1000 );
86
		$view->addHelper( 'number', $helper );
87
88
		$helper = new \Aimeos\MW\View\Helper\Request\Slim( $view, $request );
89
		$view->addHelper( 'request', $helper );
90
91
		$helper = new \Aimeos\MW\View\Helper\Response\Slim( $view, $response );
92
		$view->addHelper( 'response', $helper );
93
94
		$csrf = $request->getAttribute( 'csrf_name' );
95
		$helper = new \Aimeos\MW\View\Helper\Csrf\Standard( $view, $csrf, $request->getAttribute( 'csrf_value ') );
96
		$view->addHelper( 'csrf', $helper );
97
98
		$helper = new \Aimeos\MW\View\Helper\Access\Standard( $view, $this->getGroups( $context ) );
0 ignored issues
show
Bug introduced by
The variable $context does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
99
		$view->addHelper( 'access', $helper );
100
101
		return $view;
102
	}
103
104
105
	/**
106
	 * Returns the routing parameters passed in the URL
107
	 *
108
	 * @param array $attributes Associative list of route attributes
109
	 * @return array Associative list of parameters with "site", "locale" and "currency" if available
110
	 */
111
	protected function getFixedParams( array $attributes )
112
	{
113
		$fixed = array();
114
115
		if( isset( $attributes['site'] ) ) {
116
			$fixed['site'] = $attributes['site'];
117
		}
118
119
		if( isset( $attributes['locale'] ) ) {
120
			$fixed['locale'] = $attributes['locale'];
121
		}
122
123
		if( isset( $attributes['currency'] ) ) {
124
			$fixed['currency'] = $attributes['currency'];
125
		}
126
127
		return $fixed;
128
	}
129
130
131
	/**
132
	 * Returns the closure for retrieving the user groups
133
	 *
134
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
135
	 * @return \Closure Function which returns the user group codes
136
	 */
137
	protected function getGroups( \Aimeos\MShop\Context\Item\Iface $context )
138
	{
139
		return function() use ( $context )
140
		{
141
			$list = array();
142
			$manager = \Aimeos\MShop\Factory::createManager( $context, 'customer/group' );
143
144
			$search = $manager->createSearch();
145
			$search->setConditions( $search->compare( '==', 'customer.group.id', $context->getGroupIds() ) );
146
147
			foreach( $manager->searchItems( $search ) as $item ) {
148
				$list[] = $item->getCode();
149
			}
150
151
			return $list;
152
		};
153
	}
154
}
155