range   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 173
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 56
dl 0
loc 173
ccs 75
cts 75
cp 1
rs 10
c 0
b 0
f 0
wmc 16

11 Methods

Rating   Name   Duplication   Size   Complexity  
A get_name() 0 3 1
A display_field_slider() 0 18 2
A get_default_props() 0 16 1
A set_prefix() 0 3 1
A get_range() 0 3 1
A set_range() 0 13 2
A show_form_field() 0 12 1
A display_field_text() 0 10 2
A __construct() 0 5 1
A display_field() 0 9 3
A set_assets() 0 8 1
1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2013 Daniel A. (blitze)
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace blitze\content\services\form\field;
11
12
class range extends base
13
{
14
	/** @var \blitze\sitemaker\services\util */
0 ignored issues
show
Bug introduced by
The type blitze\sitemaker\services\util was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
	protected $util;
16
17
	/**
18
	 * Constructor
19
	 *
20
	 * @param \phpbb\language\language                  $language       Language object
21
	 * @param \phpbb\request\request_interface			$request		Request object
22
	 * @param \blitze\sitemaker\services\template		$ptemplate		Sitemaker template object
23
	 * @param \blitze\sitemaker\services\util			$util       	Sitemaker utility object
24
	 */
25 12
	public function __construct(\phpbb\language\language $language, \phpbb\request\request_interface $request, \blitze\sitemaker\services\template $ptemplate, \blitze\sitemaker\services\util $util)
0 ignored issues
show
Bug introduced by
The type blitze\sitemaker\services\template was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
	{
27 12
		parent::__construct($language, $request, $ptemplate);
28
29 12
		$this->util = $util;
30 12
	}
31
32
	/**
33
	 * @inheritdoc
34
	 */
35 5
	public function get_name()
36
	{
37 5
		return 'range';
38
	}
39
40
	/**
41
	 * @inheritdoc
42
	 */
43 10
	public function get_default_props()
44
	{
45
		return array(
46 10
			'display'	=> 'text',
47 10
			'type'		=> 'single',
48 10
			'skin'		=> 'flat',
49 10
			'size'		=> 100,
50 10
			'values'	=> '',
51 10
			'prefix'	=> '',
52 10
			'postfix'	=> '',
53 10
			'min'		=> '',
54 10
			'max'		=> '',
55 10
			'from'		=> '',
56 10
			'to'		=> '',
57 10
			'step'		=> 1,
58
			'grid'		=> false,
59
		);
60
	}
61
62
	/**
63 3
	 * @inheritdoc
64
	 */
65 3
	public function show_form_field(array &$data)
66
	{
67 3
		$this->util->add_assets(array(
68 3
			'js'   => array(
69 3
				100 => '@blitze_content/assets/fields/form.min.js',
70
			),
71 3
		));
72 3
73
		$this->set_assets($data['field_props']['theme']);
74 3
		$this->set_range($data);
75
76
		return parent::show_form_field($data);
77
	}
78
79
	/**
80 6
	 * @inheritdoc
81
	 */
82 6
	public function display_field(array $data, array $topic_data, $view_mode)
83 6
	{
84 1
		if (!$data['field_value'])
85
		{
86
			return '';
87 5
		}
88 5
89
		$callable = 'display_field_' . (($view_mode === 'print') ? 'text' : $data['field_props']['display']);
90
		return $this->$callable($data);
91
	}
92
93
	/**
94
	 * @param array $data
95 1
	 * @return string
96
	 */
97
	protected function display_field_slider(array $data)
98 1
	{
99 1
		// do not include assets on preview page as form already handles this
100 1
		if (!$this->request->is_set_post('cp'))
101
		{
102 1
			$this->util->add_assets(array(
103 1
				'js'   => array(
104 1
					100 => '@blitze_content/assets/fields/display.min.js',
105 1
				),
106 1
			));
107
		}
108 1
		$data['field_props']['disabled'] = true;
109 1
110 1
		$this->set_assets($data['field_props']['theme']);
111
		$this->set_range($data);
112 1
		$this->ptemplate->assign_vars($data);
113
114
		return $this->ptemplate->render_view('blitze/content', 'fields/range.html', 'range_field');
115
	}
116
117
	/**
118
	 * @param array $data
119 4
	 * @return string
120
	 */
121 4
	protected function display_field_text(array $data)
122
	{
123 4
		$range = $this->get_range($data['field_value']);
124 4
125 4
		if (sizeof($range))
126 4
		{
127
			array_walk($range, array($this, 'set_prefix'), $data['field_props']['prefix']);
128 4
		}
129
130
		return join(' - ', $range) . $data['field_props']['postfix'];
131
	}
132
133
	/**
134
	 * @param string $value
135 8
	 * @return array
136
	 */
137 8
	protected function get_range($value)
138
	{
139
		return explode(';', preg_replace('/;\s+/', ';', $value));
140
	}
141
142
	/**
143
	 * @param array $data
144 4
	 * @return void
145
	 */
146 4
	protected function set_range(array &$data)
147
	{
148 4
		list($from, $to) = $this->get_range($data['field_value']) + array('', '');
149 4
150 1
		if ($data['field_props']['values'])
151
		{
152 1
			$values = explode(',', preg_replace('/,\s+/', ',', $data['field_props']['values']));
153 1
154
			$from = array_search($from, $values);
155 1
			$to = array_search($to, $values);
156 1
157 1
			$data['field_props']['from'] = $from;
158 4
			$data['field_props']['to'] = $to;
159
		}
160
	}
161
162
	/**
163
	 * @param string $item
164
	 * @param int $key
165
	 * @param string $prefix
166 4
	 * @return void
167
	 */
168 4
	protected function set_prefix(&$item, $key, $prefix)
169 4
	{
170
		$item = $prefix . $item;
171
	}
172
173
	/**
174
	 * @param string $theme
175 4
	 * @return void
176
	 */
177 4
	protected function set_assets($theme)
178
	{
179 4
		$this->util->add_assets(array(
180 4
			'js'   => array(
181
				'@blitze_content/vendor/ion.rangeSlider/js/ion.rangeSlider.min.js',
182 4
			),
183 4
			'css'   => array(
184
				'@blitze_content/vendor/ion.rangeSlider/css/ion.rangeSlider.min.css',
185 4
			)
186 4
		));
187
	}
188
}
189