Completed
Push — develop ( 226ad1...4a255f )
by Daniel
08:09
created

image   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 1
dl 0
loc 120
ccs 49
cts 49
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A get_field_value() 0 7 2
A show_form_field() 0 21 1
A display_field() 0 9 3
A get_default_props() 0 10 1
A get_name() 0 4 1
A get_image_src() 0 4 1
A get_image_html() 0 13 3
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 image extends base
13
{
14
	/** @var \blitze\sitemaker\services\util */
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 16
	public function __construct(\phpbb\language\language $language, \phpbb\request\request_interface $request, \blitze\sitemaker\services\template $ptemplate, \blitze\sitemaker\services\util $util)
26
	{
27 16
		parent::__construct($language, $request, $ptemplate);
28
29 16
		$this->util = $util;
30 16
	}
31
32
	/**
33
	 * @inheritdoc
34
	 */
35 3
	public function get_field_value($name, $value)
36
	{
37 3
		$value = $this->request->variable($name, $value);
38 3
		$value = $this->get_image_src($value);
39
40 3
		return ($value) ? '[img]' . $value . '[/img]' : '';
41
	}
42
43
	/**
44
	 * @inheritdoc
45
	 */
46 3
	public function show_form_field($name, array &$data)
47
	{
48 3
		$bbcode_value = $this->get_field_value($name, $data['field_value']);
49
50 3
		$field = $this->get_name();
51 3
		$data['field_name'] = $name;
52 3
		$data['field_value'] = $this->get_image_src($bbcode_value);
53
54 3
		$this->util->add_assets(array(
55
			'js'   => array(
56 3
				'@blitze_content/assets/form/fields.min.js',
57 3
			),
58 3
		));
59
60 3
		$this->ptemplate->assign_vars($data);
61 3
		$field = $this->ptemplate->render_view('blitze/content', "fields/image.html", $field . '_field');
62
63 3
		$data['field_value'] = $bbcode_value;
64
65 3
		return $field;
66
	}
67
68
	/**
69
	 * @inheritdoc
70
	 */
71 10
	public function display_field(array $data, $mode = '')
72
	{
73 10
		$image = '';
74 10
		if ($data['field_value'] || $data['field_props']['default'])
75 10
		{
76 6
			$image = $this->get_image_html($data['field_value'], $mode, $data['field_props']);
77 6
		}
78 10
		return $image;
79
	}
80
81
	/**
82
	 * @inheritdoc
83
	 */
84 14
	public function get_default_props()
85
	{
86
		return array(
87 14
			'default'		=> '',
88 14
			'detail_align'	=> '',
89 14
			'detail_size'	=> '',
90 14
			'summary_align'	=> '',
91 14
			'summary_size'	=> '',
92 14
		);
93
	}
94
95
	/**
96
	 * @inheritdoc
97
	 */
98 5
	public function get_name()
99
	{
100 5
		return 'image';
101
	}
102
103
	/**
104
	 * @param string $bbcode_string
105
	 * @return string
106
	 */
107 3
	private function get_image_src($bbcode_string)
108
	{
109 3
		return str_replace(array('[img]', '[/img]'), '', $bbcode_string);
110
	}
111
112
	/**
113
	 * @param string $image
114
	 * @param string $mode
115
	 * @param array $field_props
116
	 * @return string
117
	 */
118 6
	private function get_image_html($image, $mode, array $field_props)
119
	{
120 6
		$image = $image ?: '<img src="' . $field_props['default'] . '" class="postimage" alt="Image" />';
121
122 6
		$html = '<figure class="img-ui">' . $image . '</figure>';
123 6
		if ($mode !== 'block')
124 6
		{
125 6
			$view_props = array_fill_keys(array($mode . '_size', $mode . '_align'), '');
126 6
			$image_props = array_filter(array_intersect_key($field_props, $view_props));
127 6
			$html = '<div class="' . join(' ', $image_props) . '">' . $html . '</div>';
128 6
		}
129 6
		return $html;
130
	}
131
}
132