Completed
Push — develop ( 4d26b2...764757 )
by Daniel
07:05
created

textarea::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 10
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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 base
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 */
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)
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'	=> '',
80 3
			'max_chars'	=> 200,
81 3
			'editor'	=> true,
82 3
		);
83
	}
84
85
	/**
86
	 * Display content field
87
	 *
88
	 * @param array $data
89
	 * @param string $view_mode
90
	 * @param array $topic_data
91
	 * @return mixed
92
	 */
93 11
	public function display_field(array $data = array(), $view_mode = 'summary', array $topic_data = array())
94
	{
95 11
		$toc_pattern = '(<h4>(.*?)</h4>)?';
96 11
		$pages_pattern = '<p><!-- pagebreak --></p>';
97 11
		$split_pattern = $pages_pattern . (($view_mode !== 'detail') ? $toc_pattern : '');
98
99 11
		$pages = array_filter(preg_split('#' . $split_pattern . '#s', $data['field_value']));
100
101 11
		if ($view_mode !== 'detail')
102 11
		{
103 5
			return $this->get_summary_value(trim($pages[0]), $data['field_props']['max_chars']);
104
		}
105
106
		// get page titles to generate TOC
107 6
		preg_match_all('#' . $pages_pattern . $toc_pattern . '#s', $data['field_value'], $matches);
108
109 6
		return $this->get_detail_value($pages, $matches[2], $data);
110
	}
111
112
	/**
113
	 * @inheritdoc
114
	 */
115 2
	public function show_form_field($name, array &$data, $forum_id = 0)
116
	{
117 2
		if ($data['field_props']['editor'])
118 2
		{
119 1
			$asset_path = $this->util->get_web_path();
120 1
			$this->util->add_assets(array(
121
				'js'   => array(
122 1
					$asset_path . 'assets/javascript/editor.js',
123
					'@blitze_content/assets/fields/textarea.min.js'
124 1
				)
125 1
			));
126
127 1
			$data += $this->get_editor($forum_id);
128 1
		}
129
130 2
		return parent::show_form_field($name, $data);
131
	}
132
133
	/**
134
	 * @param int $forum_id
135
	 * @return array
136
	 */
137 1
	protected function get_editor($forum_id)
138
	{
139 1
		if (!$this->allow_bbcode($forum_id))
140 1
		{
141 1
			return array();
142
		}
143
144
		$this->set_custom_bbcodes();
145
146
		// HTML, BBCode, Smilies, Images and Flash status
147
		return array(
148
			'S_BBCODE_IMG'			=> $this->auth->acl_get('f_img', $forum_id),
149
			'S_LINKS_ALLOWED'		=> (bool) $this->config['allow_post_links'],
150
			'S_BBCODE_FLASH'		=> $this->allow_flash_bbcode($forum_id),
151
			'S_BBCODE_QUOTE'		=> false,
152
			'S_SMILIES_ALLOWED'		=> false,
153
		);
154
	}
155
156
	/**
157
	 * @param string $html
158
	 * @param int $max_chars
159
	 * @return string
160
	 */
161 5
	protected function get_summary_value($html, $max_chars)
162
	{
163
		if ($max_chars)
164 5
		{
165 1
			$truncateService = new TruncateService();
166 1
			$html = $truncateService->truncate($html, $max_chars);
167 1
		}
168 5
		return $html;
169
	}
170
171
	/**
172
	 * @param array $pages
173
	 * @param array $titles
174
	 * @param array $data
175
	 * @return mixed
176
	 */
177 6
	protected function get_detail_value(array $pages, array $titles, array $data)
178
	{
179 6
		if ($this->request->is_set('preview') || $this->request->variable('view', '') === 'print')
180 6
		{
181 1
			return join('<p><hr class="dashed"></p>', $pages);
182
		}
183
184 5
		$page = $this->request->variable('page', 0);
185
186 5
		$start = isset($pages[$page]) ? $page : 0;
187 5
		$total_pages = sizeof($pages);
188 5
		$topic_url = build_url(array('page'));
189
190 5
		$this->generate_page_nav($topic_url, $total_pages, $start);
191 5
		$this->generate_toc($start, $topic_url, array_slice($titles, 0, $total_pages - 1));
192
193 5
		return $this->get_page_content($start, $data['field_name'], $pages);
194
	}
195
196
	/**
197
	 * Generate pagination for topic subpages
198
	 *
199
	 * @param string $topic_url
200
	 * @param int $total_pages
201
	 * @param int $start
202
	 * @return void
203
	 */
204 5
	protected function generate_page_nav($topic_url, $total_pages, &$start)
205
	{
206 5
		$start = $this->pagination->validate_start($start, 1, $total_pages);
207 5
		$this->pagination->generate_template_pagination($topic_url, 'page', 'page', $total_pages, 1, $start);
208 5
		$this->template->assign_var('S_NOT_LAST_PAGE', !($start === ($total_pages - 1)));
209 5
	}
210
211
	/**
212
	 * Generate Table of contents
213
	 *
214
	 * @param int $start
215
	 * @param string $topic_url
216
	 * @param array $page_titles
217
	 * @return void
218
	 */
219 5
	protected function generate_toc($start, $topic_url, array $page_titles)
220
	{
221 5
		if (sizeof(array_filter($page_titles)))
222 5
		{
223 2
			$page_titles = array_merge(array($this->language->lang('CONTENT_TOC_OVERVIEW')), $page_titles);
224
225 2
			foreach ($page_titles as $page => $title)
226
			{
227 2
				$this->template->assign_block_vars('toc', array(
228 2
					'TITLE'		=> ($title) ? $title : $this->language->lang('CONTENT_TOC_UNTITLED'),
229 2
					'S_PAGE'	=> ($page === $start),
230 2
					'U_VIEW'	=> append_sid($topic_url, ($page) ? 'page=' . $page : false),
231 2
				));
232 2
			}
233 2
		}
234 5
	}
235
236
	/**
237
	 * When Previewing topic, we show all pages
238
	 *
239
	 * @param int $start
240
	 * @param string $field_name
241
	 * @param array $pages
242
	 * @return mixed
243
	 */
244 5
	protected function get_page_content($start, $field_name, array $pages)
245
	{
246 5
		$value = trim($pages[$start]);
247
248
		// Hide all other fields if we're looking at page 2+
249
		if ($start)
250 5
		{
251
			$value = array(
252 2
				$field_name => $value,
253 2
			);
254 2
		}
255
256 5
		return $value;
257
	}
258
259
	/**
260
	 * @param int $forum_id
261
	 * @return bool
262
	 */
263 1
	protected function allow_bbcode($forum_id)
264
	{
265 1
		return ($this->config['allow_bbcode'] && $this->auth->acl_get('f_bbcode', $forum_id)) ? true : false;
266
	}
267
268
	/**
269
	 * @param int $forum_id
270
	 * @return bool
271
	 */
272
	protected function allow_flash_bbcode($forum_id)
273
	{
274
		return ($this->auth->acl_get('f_flash', $forum_id) && $this->config['allow_post_flash']) ? true : false;
275
	}
276
277
	protected function set_custom_bbcodes()
278
	{
279
		// Assigning custom bbcodes
280
		if (!function_exists('display_custom_bbcodes'))
281
		{
282
			include($this->phpbb_root_path . 'includes/functions_display.' . $this->php_ext);
283
		}
284
285
		display_custom_bbcodes();
286
287
		$this->ptemplate->assign_block_vars_array('custom_tags', $this->template->retrieve_block_vars('custom_tags', array()));
288
	}
289
}
290