Passed
Push — release-3.0.0 ( e8971e...d75c10 )
by Daniel
04:23
created

image::strip_image_bbcode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
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, $view_mode)
65
	{
66
		if ($data['field_value'])
67
		{
68
			return $this->get_image_html($data['field_value'], $view_mode, $data['field_label'], $data['field_props']);
69
		}
70
		return '';
71
	}
72
73
	/**
74
	 * @inheritdoc
75
	 */
76
	public function get_submitted_value(array $data)
77
	{
78
		$value = $this->request->variable($data['field_name'], $data['field_value']);
79
80
		// we wrap this in image bbcode so that images will still work even if this extension is uninstalled
81
		// this complicates things for us as we have to remove the bbcode when showing the form field
82
		// and match the image source (url) from the image html when displaying the field
83
		return ($value) ? '[img]' . $value . '[/img]' : '';
84
	}
85
86
	/**
87
	 * @inheritdoc
88
	 */
89
	public function show_form_field(array &$data)
90
	{
91
		$data['field_value'] = $this->strip_image_bbcode($data['field_value']);
92
93
		$this->util->add_assets(array(
94
			'js'	=> array(
95
				'@blitze_content/vendor/fancybox/dist/jquery.fancybox.min.js',
96
				100 => '@blitze_content/assets/fields/form.min.js',
97
			),
98
			'css'	=> array(
99
				'@blitze_content/vendor/fancybox/dist/jquery.fancybox.min.css',
100
			),
101
		));
102
103
		$this->set_filemanager($data);
104
		$this->ptemplate->assign_vars($data);
105
106
		return $this->ptemplate->render_view('blitze/content', "fields/image.html", $this->get_name() . '_field');
107
	}
108
109
	/**
110
	 * @inheritdoc
111
	 */
112
	public function get_default_props()
113
	{
114
		return array(
115
			'default'		=> '',
116
			'detail_align'	=> '',
117
			'detail_size'	=> '',
118
			'summary_align'	=> '',
119
			'summary_size'	=> '',
120
		);
121
	}
122
123
	/**
124
	 * @inheritdoc
125
	 */
126
	public function get_name()
127
	{
128
		return 'image';
129
	}
130
131
	/**
132
	 * @param string $bbcode_string
133
	 * @return string
134
	 */
135
	private function strip_image_bbcode($bbcode_string)
136
	{
137
		return str_replace(array('[img]', '[/img]'), '', $bbcode_string);
138
	}
139
140
	/**
141
	 * @param string $image
142
	 * @param string $mode
143
	 * @param string $title
144
	 * @param array $field_props
145
	 * @return string
146
	 */
147
	private function get_image_html($image, $mode, $title, array $field_props)
148
	{
149
		$image = '<img src="' . $image . '" class="postimage" alt="' . $title . '" />';
150
151
		$html = '<figure class="img-ui">' . $image . '</figure>';
152
		if ($mode !== 'block')
153
		{
154
			$view_props = array_fill_keys(array($mode . '_size', $mode . '_align'), '');
155
			$image_props = array_filter(array_intersect_key($field_props, $view_props));
156
			$html = '<div class="' . join(' ', $image_props) . '">' . $html . '</div>';
157
		}
158
		return $html;
159
	}
160
161
	/**
162
	 * @param array $data
163
	 * @return void
164
	 */
165
	protected function set_filemanager(array &$data)
166
	{
167
		if ($this->filemanager->is_enabled())
168
		{
169
			$this->filemanager->ensure_config_is_ready();
170
171
			$data['filemanager_path'] = append_sid("{$this->phpbb_root_path}ResponsiveFilemanager/filemanager/dialog.$this->php_ext", array(
172
				'type'			=> 1,
173
				'field_id'		=> 'smc-' . $data['field_name'],
174
				'akey'			=> $this->filemanager->get_access_key(),
175
			));
176
		}
177
	}
178
}
179