Passed
Push — release-3.0.0 ( d75c10...523432 )
by Daniel
05:03
created

image::get_image_html()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 4
dl 0
loc 12
rs 10
c 0
b 0
f 0
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\filemanager\setup */
0 ignored issues
show
Bug introduced by
The type blitze\sitemaker\services\filemanager\setup 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 $filemanager;
16
17
	/** @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...
18
	protected $util;
19
20
	/** @var string */
21
	protected $phpbb_root_path;
22
23
	/** @var string */
24
	protected $php_ext;
25
26
	/**
27
	 * Constructor
28
	 *
29
	 * @param \phpbb\language\language                  	$language       	Language object
30
	 * @param \phpbb\request\request_interface				$request			Request object
31
	 * @param \blitze\sitemaker\services\template			$ptemplate			Sitemaker template object
32
	 * @param \blitze\sitemaker\services\filemanager\setup	$filemanager		Filemanager object
33
	 * @param \blitze\sitemaker\services\util				$util       		Sitemaker utility object
34
	 * @param string										$phpbb_root_path	phpBB root path
35
	 * @param string										$php_ext			php file extension
36
	 */
37
	public function __construct(\phpbb\language\language $language, \phpbb\request\request_interface $request, \blitze\sitemaker\services\template $ptemplate, \blitze\sitemaker\services\filemanager\setup $filemanager, \blitze\sitemaker\services\util $util, $phpbb_root_path, $php_ext)
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...
38
	{
39
		parent::__construct($language, $request, $ptemplate);
40
41
		$this->filemanager = $filemanager;
42
		$this->util = $util;
43
		$this->phpbb_root_path = $phpbb_root_path;
44
		$this->php_ext = $php_ext;
45
	}
46
47
	/**
48
	 * @inheritdoc
49
	 */
50
	public function get_field_value(array $data)
51
	{
52
		if ($data['field_value'])
53
		{
54
			preg_match('/src="(.*?)"/i', $data['field_value'], $match);
55
			return (!empty($match[1])) ? $match[1] : '';
56
		}
57
58
		return $data['field_props']['default'];
59
	}
60
61
	/**
62
	 * @inheritdoc
63
	 */
64
	public function display_field(array $data, array $topic_data, $display_mode, $view_mode)
65
	{
66
		if ($data['field_value'])
67
		{
68
			$image = '<img src="' . $data['field_value'] . '" class="postimage" alt="' . $data['field_label'] . '" />';
69
			$html = '<figure class="img-ui">' . $image . '</figure>';
70
71
			if ($display_mode !== 'block')
72
			{
73
				$view_props = array_fill_keys(array($view_mode . '_size', $view_mode . '_align'), '');
74
				$image_props = array_filter(array_intersect_key($data['field_props'], $view_props));
75
				$html = '<div class="' . join(' ', $image_props) . '">' . $html . '</div>';
76
			}
77
			return $html;
78
		}
79
		return '';
80
	}
81
82
	/**
83
	 * @inheritdoc
84
	 */
85
	public function get_submitted_value(array $data)
86
	{
87
		$value = $this->request->variable($data['field_name'], $data['field_value']);
88
89
		// we wrap this in image bbcode so that images will still work even if this extension is uninstalled
90
		// this complicates things for us as we have to remove the bbcode when showing the form field
91
		// and match the image source (url) from the image html when displaying the field
92
		return ($value) ? '[img]' . $value . '[/img]' : '';
93
	}
94
95
	/**
96
	 * @inheritdoc
97
	 */
98
	public function show_form_field(array &$data)
99
	{
100
		$data['field_value'] = $this->strip_image_bbcode($data['field_value']);
101
102
		$this->util->add_assets(array(
103
			'js'	=> array(
104
				'@blitze_content/vendor/fancybox/dist/jquery.fancybox.min.js',
105
				100 => '@blitze_content/assets/fields/form.min.js',
106
			),
107
			'css'	=> array(
108
				'@blitze_content/vendor/fancybox/dist/jquery.fancybox.min.css',
109
			),
110
		));
111
112
		$this->set_filemanager($data);
113
		$this->ptemplate->assign_vars($data);
114
115
		return $this->ptemplate->render_view('blitze/content', "fields/image.html", $this->get_name() . '_field');
116
	}
117
118
	/**
119
	 * @inheritdoc
120
	 */
121
	public function get_default_props()
122
	{
123
		return array(
124
			'default'		=> '',
125
			'detail_align'	=> '',
126
			'detail_size'	=> '',
127
			'summary_align'	=> '',
128
			'summary_size'	=> '',
129
		);
130
	}
131
132
	/**
133
	 * @inheritdoc
134
	 */
135
	public function get_name()
136
	{
137
		return 'image';
138
	}
139
140
	/**
141
	 * @param string $bbcode_string
142
	 * @return string
143
	 */
144
	private function strip_image_bbcode($bbcode_string)
145
	{
146
		return str_replace(array('[img]', '[/img]'), '', $bbcode_string);
147
	}
148
149
	/**
150
	 * @param array $data
151
	 * @return void
152
	 */
153
	protected function set_filemanager(array &$data)
154
	{
155
		if ($this->filemanager->is_enabled())
156
		{
157
			$this->filemanager->ensure_config_is_ready();
158
159
			$data['filemanager_path'] = append_sid("{$this->phpbb_root_path}ResponsiveFilemanager/filemanager/dialog.$this->php_ext", array(
160
				'type'			=> 1,
161
				'field_id'		=> 'smc-' . $data['field_name'],
162
				'akey'			=> $this->filemanager->get_access_key(),
163
			));
164
		}
165
	}
166
}
167