Completed
Push — master ( a78503...736f57 )
by Aimeos
18:25
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 MIT, http://opensource.org/licenses/MIT
5
 * @copyright Aimeos (aimeos.org), 2015-2016
6
 * @package laravel
7
 * @subpackage Base
8
 */
9
10
namespace Aimeos\Shop\Base;
11
12
13
use Illuminate\Support\Facades\Input;
14
use Illuminate\Support\Facades\Route;
15
use Illuminate\Support\Facades\Request;
16
use Illuminate\Support\Facades\Response;
17
18
19
/**
20
 * Service providing the view objects
21
 *
22
 * @package laravel
23
 * @subpackage Base
24
 */
25
class View
26
{
27
	/**
28
	 * Creates the view object for the HTML client.
29
	 *
30
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
31
	 * @param array $templatePaths List of base path names with relative template paths as key/value pairs
32
	 * @param string|null $locale Code of the current language or null for no translation
33
	 * @return \Aimeos\MW\View\Iface View object
34
	 */
35
	public function create( \Aimeos\MShop\Context\Item\Iface $context, array $templatePaths, $locale = null )
36
	{
37
		$params = $fixed = array();
38
39
		if( $locale !== null )
40
		{
41
			$params = Route::current()->parameters() + Input::all();
42
			$fixed = $this->getFixedParams();
43
44
			$i18n = app('\Aimeos\Shop\Base\I18n')->get( array( $locale ) );
45
			$translation = $i18n[$locale];
46
		}
47
		else
48
		{
49
			$translation = new \Aimeos\MW\Translation\None( 'en' );
50
		}
51
52
53
		$view = new \Aimeos\MW\View\Standard( $templatePaths );
54
55
		$helper = new \Aimeos\MW\View\Helper\Translate\Standard( $view, $translation );
56
		$view->addHelper( 'translate', $helper );
57
58
		$helper = new \Aimeos\MW\View\Helper\Url\Laravel5( $view, app('url'), $fixed );
59
		$view->addHelper( 'url', $helper );
60
61
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $params );
62
		$view->addHelper( 'param', $helper );
63
64
		$config = new \Aimeos\MW\Config\Decorator\Protect( clone $context->getConfig(), array( 'admin', 'client' ) );
65
		$helper = new \Aimeos\MW\View\Helper\Config\Standard( $view, $config );
66
		$view->addHelper( 'config', $helper );
67
68
		$sepDec = $config->get( 'client/html/common/format/seperatorDecimal', '.' );
69
		$sep1000 = $config->get( 'client/html/common/format/seperator1000', ' ' );
70
		$helper = new \Aimeos\MW\View\Helper\Number\Standard( $view, $sepDec, $sep1000 );
71
		$view->addHelper( 'number', $helper );
72
73
		$helper = new \Aimeos\MW\View\Helper\Request\Laravel5( $view, Request::instance() );
0 ignored issues
show
Bug introduced by
The method instance() does not exist on Illuminate\Support\Facades\Request. Did you maybe mean clearResolvedInstance()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
74
		$view->addHelper( 'request', $helper );
75
76
		$helper = new \Aimeos\MW\View\Helper\Response\Laravel5( $view );
77
		$view->addHelper( 'response', $helper );
78
79
		$helper = new \Aimeos\MW\View\Helper\Csrf\Standard( $view, '_token', csrf_token() );
80
		$view->addHelper( 'csrf', $helper );
81
82
		$helper = new \Aimeos\MW\View\Helper\Access\Standard( $view, $this->getGroups( $context ) );
83
		$view->addHelper( 'access', $helper );
84
85
		return $view;
86
	}
87
88
89
	/**
90
	 * Returns the closure for retrieving the user groups
91
	 *
92
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
93
	 * @return \Closure Function which returns the user group codes
94
	 */
95
	protected function getGroups( \Aimeos\MShop\Context\Item\Iface $context )
96
	{
97
		return function() use ( $context )
98
		{
99
			$list = array();
100
			$manager = \Aimeos\MShop\Factory::createManager( $context, 'customer/group' );
101
102
			$search = $manager->createSearch();
103
			$search->setConditions( $search->compare( '==', 'customer.group.id', $context->getGroupIds() ) );
104
105
			foreach( $manager->searchItems( $search ) as $item ) {
106
				$list[] = $item->getCode();
107
			}
108
109
			return $list;
110
		};
111
	}
112
113
114
	/**
115
	 * Returns the routing parameters passed in the URL
116
	 *
117
	 * @return array Associative list of parameters with "site", "locale" and "currency" if available
118
	 */
119
	protected function getFixedParams()
120
	{
121
		$fixed = array();
122
123
		if( ( $value = Route::input( 'site' ) ) !== null ) {
124
			$fixed['site'] = $value;
125
		}
126
127
		if( ( $value = Route::input( 'locale' ) ) !== null ) {
128
			$fixed['locale'] = $value;
129
		}
130
131
		if( ( $value = Route::input( 'currency' ) ) !== null ) {
132
			$fixed['currency'] = $value;
133
		}
134
135
		return $fixed;
136
	}
137
}