textarea   A
last analyzed

Complexity

Total Complexity 33

Size/Duplication

Total Lines 269
Duplicated Lines 0 %

Test Coverage

Coverage 84.47%

Importance

Changes 0
Metric Value
eloc 81
dl 0
loc 269
ccs 87
cts 103
cp 0.8447
rs 9.76
c 0
b 0
f 0
wmc 33

14 Methods

Rating   Name   Duplication   Size   Complexity  
A allow_bbcode() 0 3 3
A allow_flash_bbcode() 0 3 3
A get_name() 0 3 1
A __construct() 0 11 1
A get_detail_value() 0 17 4
A display_field() 0 17 4
A get_default_props() 0 7 1
A get_page_content() 0 13 2
A get_editor() 0 16 2
A generate_page_nav() 0 5 1
A generate_toc() 0 12 5
A set_custom_bbcodes() 0 11 2
A show_form_field() 0 16 2
A get_summary_value() 0 8 2
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
use Urodoz\Truncate\TruncateService;
13
14
class textarea extends text
15
{
16
	/** @var \phpbb\auth\auth */
17
	protected $auth;
18
19
	/** @var \phpbb\config\config */
20
	protected $config;
21
22
	/** @var \phpbb\pagination */
23
	protected $pagination;
24
25
	/** @var \phpbb\template\template */
26
	protected $template;
27
28
	/** @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...
29
	protected $util;
30
31
	/** @var string */
32
	protected $phpbb_root_path;
33
34
	/** @var string */
35
	protected $php_ext;
36
37
	/**
38
	 * Constructor
39
	 *
40
	 * @param \phpbb\language\language					$language			Language object
41
	 * @param \phpbb\request\request_interface			$request			Request object
42
	 * @param \blitze\sitemaker\services\template		$ptemplate			Sitemaker template object
43
	 * @param \phpbb\auth\auth							$auth				Auth object
44
	 * @param \phpbb\config\config						$config				Config object
45
	 * @param \phpbb\pagination							$pagination			Pagination object
46
	 * @param \phpbb\template\template					$template			Template object
47
	 * @param \blitze\sitemaker\services\util			$util				Sitemaker utility object
48
	 * @param string									$phpbb_root_path	Path to the phpbb includes directory.
49
	 * @param string									$php_ext			php file extension
50
	 */
51 16
	public function __construct(\phpbb\language\language $language, \phpbb\request\request_interface $request, \blitze\sitemaker\services\template $ptemplate, \phpbb\auth\auth $auth, \phpbb\config\config $config, \phpbb\pagination $pagination, \phpbb\template\template $template, \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...
52
	{
53 16
		parent::__construct($language, $request, $ptemplate);
54
55 16
		$this->auth = $auth;
56 16
		$this->config = $config;
57 16
		$this->pagination = $pagination;
58 16
		$this->template = $template;
59 16
		$this->util = $util;
60 16
		$this->phpbb_root_path = $phpbb_root_path;
61 16
		$this->php_ext = $php_ext;
62 16
	}
63
64
	/**
65
	 * @inheritdoc
66
	 */
67 4
	public function get_name()
68
	{
69 4
		return 'textarea';
70
	}
71
72
	/**
73
	 * @inheritdoc
74
	 */
75 3
	public function get_default_props()
76
	{
77
		return array(
78 3
			'size'		=> 'large',
79 3
			'maxlength'	=> '',			// input characters
80 3
			'max_chars'	=> 200,			// display characters
81 3
			'editor'	=> true,
82 3
		);
83
	}
84
85
	/**
86
	 * @inheritdoc
87
	 */
88 11
	public function display_field(array $data, array $topic_data, $view_mode)
89
	{
90 11
		$toc_pattern = '(<h4>(.*?)</h4>)?';
91 11
		$pages_pattern = '<p><!-- pagebreak --></p>';
92 11
		$split_pattern = $pages_pattern . (($view_mode === 'summary') ? $toc_pattern : '');
93
94 11
		$pages = array_filter((array) preg_split('#' . $split_pattern . '#s', $data['field_value']));
95
96 11
		if ($view_mode === 'summary' || $view_mode === 'block')
97 11
		{
98 5
			return $this->get_summary_value(trim($pages[0]), $data['field_props']['max_chars']);
99
		}
100
101
		// get page titles to generate TOC
102 6
		preg_match_all('#' . $pages_pattern . $toc_pattern . '#s', $data['field_value'], $matches);
103
104 6
		return $this->get_detail_value($pages, $matches[2], $data, $view_mode);
105
	}
106
107
	/**
108
	 * @inheritdoc
109
	 */
110 2
	public function show_form_field(array &$data, $forum_id = 0)
111
	{
112 2
		if ($data['field_props']['editor'])
113 2
		{
114 1
			$asset_path = $this->util->get_web_path();
115 1
			$this->util->add_assets(array(
116
				'js'   => array(
117 1
					$asset_path . 'assets/javascript/editor.js',
118
					'@blitze_content/assets/fields/textarea.min.js'
119 1
				)
120 1
			));
121
122 1
			$data += $this->get_editor($forum_id);
123 1
		}
124
125 2
		return parent::show_form_field($data);
126
	}
127
128
	/**
129
	 * @param int $forum_id
130
	 * @return array
131
	 */
132 1
	protected function get_editor($forum_id)
133
	{
134 1
		if (!$this->allow_bbcode($forum_id))
135 1
		{
136 1
			return array();
137
		}
138
139
		$this->set_custom_bbcodes();
140
141
		// HTML, BBCode, Smilies, Images and Flash status
142
		return array(
143
			'S_BBCODE_IMG'			=> $this->auth->acl_get('f_img', $forum_id),
144
			'S_LINKS_ALLOWED'		=> (bool) $this->config['allow_post_links'],
145
			'S_BBCODE_FLASH'		=> $this->allow_flash_bbcode($forum_id),
146
			'S_BBCODE_QUOTE'		=> false,
147
			'S_SMILIES_ALLOWED'		=> false,
148
		);
149
	}
150
151
	/**
152
	 * @param string $html
153
	 * @param int $max_chars
154
	 * @return string
155
	 */
156 5
	protected function get_summary_value($html, $max_chars)
157
	{
158
		if ($max_chars)
159 5
		{
160 1
			$truncateService = new TruncateService();
161 1
			$html = $truncateService->truncate($html, $max_chars);
162 1
		}
163 5
		return $html;
164
	}
165
166
	/**
167
	 * @param array $pages
168
	 * @param array $titles
169
	 * @param array $data
170
	 * @return mixed
171
	 */
172 6
	protected function get_detail_value(array $pages, array $titles, array $data, $view_mode)
173
	{
174 6
		if ($view_mode === 'preview' || $view_mode === 'print')
175 6
		{
176 1
			return join('<p><hr class="dashed"></p>', $pages);
177
		}
178
179 5
		$page = $this->request->variable('page', 0);
180
181 5
		$start = isset($pages[$page]) ? $page : 0;
182 5
		$total_pages = sizeof($pages);
183 5
		$topic_url = build_url(array('page'));
184
185 5
		$this->generate_page_nav($topic_url, $total_pages, $start);
186 5
		$this->generate_toc($start, $topic_url, array_slice($titles, 0, $total_pages - 1));
187
188 5
		return $this->get_page_content($start, $data['field_name'], $pages);
189
	}
190
191
	/**
192
	 * Generate pagination for topic subpages
193
	 *
194
	 * @param string $topic_url
195
	 * @param int $total_pages
196
	 * @param int $start
197
	 * @return void
198
	 */
199 5
	protected function generate_page_nav($topic_url, $total_pages, &$start)
200
	{
201 5
		$start = $this->pagination->validate_start($start, 1, $total_pages);
202 5
		$this->pagination->generate_template_pagination($topic_url, 'page', 'page', $total_pages, 1, $start);
203 5
		$this->template->assign_var('S_NOT_LAST_PAGE', !($start === ($total_pages - 1)));
204 5
	}
205
206
	/**
207
	 * Generate Table of contents
208
	 *
209
	 * @param int $start
210
	 * @param string $topic_url
211
	 * @param array $page_titles
212
	 * @return void
213
	 */
214 5
	protected function generate_toc($start, $topic_url, array $page_titles)
215
	{
216 5
		if (sizeof(array_filter($page_titles)))
217 5
		{
218 2
			$page_titles = array_merge(array($this->language->lang('CONTENT_TOC_OVERVIEW')), $page_titles);
219
220 2
			foreach ($page_titles as $page => $title)
221
			{
222 2
				$this->template->assign_block_vars('toc', array(
223 2
					'TITLE'		=> ($title) ? $title : $this->language->lang('CONTENT_TOC_UNTITLED'),
224 2
					'S_PAGE'	=> ($page === $start),
225 2
					'U_VIEW'	=> append_sid($topic_url, ($page) ? 'page=' . $page : false),
226 2
				));
227 2
			}
228 2
		}
229 5
	}
230
231
	/**
232
	 * When Previewing topic, we show all pages
233
	 *
234
	 * @param int $start
235
	 * @param string $field_name
236
	 * @param array $pages
237
	 * @return mixed
238
	 */
239 5
	protected function get_page_content($start, $field_name, array $pages)
240
	{
241 5
		$value = trim($pages[$start]);
242
243
		// Hide all other fields if we're looking at page 2+
244
		if ($start)
245 5
		{
246
			$value = array(
247 2
				$field_name => $value,
248 2
			);
249 2
		}
250
251 5
		return $value;
252
	}
253
254
	/**
255
	 * @param int $forum_id
256
	 * @return bool
257
	 */
258 1
	protected function allow_bbcode($forum_id)
259
	{
260 1
		return ($this->config['allow_bbcode'] && $this->auth->acl_get('f_bbcode', $forum_id)) ? true : false;
261
	}
262
263
	/**
264
	 * @param int $forum_id
265
	 * @return bool
266
	 */
267
	protected function allow_flash_bbcode($forum_id)
268
	{
269
		return ($this->auth->acl_get('f_flash', $forum_id) && $this->config['allow_post_flash']) ? true : false;
270
	}
271
272
	protected function set_custom_bbcodes()
273
	{
274
		// Assigning custom bbcodes
275
		if (!function_exists('display_custom_bbcodes'))
276
		{
277
			include($this->phpbb_root_path . 'includes/functions_display.' . $this->php_ext);
278
		}
279
280
		display_custom_bbcodes();
281
282
		$this->ptemplate->assign_block_vars_array('custom_tags', $this->template->retrieve_block_vars('custom_tags', array()));
283
	}
284
}
285