Completed
Push — develop ( 755a17...843010 )
by Daniel
07:33
created

textarea::get_detail_value()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 11
cts 11
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 3
nop 3
crap 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
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 \phpbb\template\context */
29
	protected $template_context;
30
31
	/** @var \blitze\sitemaker\services\util */
32
	protected $util;
33
34
	/** @var string */
35
	protected $phpbb_root_path;
36
37
	/** @var string */
38
	protected $php_ext;
39
40
	/**
41
	 * Constructor
42
	 *
43
	 * @param \phpbb\language\language					$language			Language object
44
	 * @param \phpbb\request\request_interface			$request			Request object
45
	 * @param \blitze\sitemaker\services\template		$ptemplate			Sitemaker template object
46
	 * @param \phpbb\auth\auth							$auth				Auth object
47
	 * @param \phpbb\config\config						$config				Config object
48
	 * @param \phpbb\pagination							$pagination			Pagination object
49
	 * @param \phpbb\template\template					$template			Template object
50
	 * @param \phpbb\template\context					$template_context	Template context object
51
	 * @param \blitze\sitemaker\services\util			$util				Sitemaker utility object
52
	 * @param string									$phpbb_root_path	Path to the phpbb includes directory.
53
	 * @param string									$php_ext			php file extension
54
	 */
55 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, \phpbb\template\context $template_context, \blitze\sitemaker\services\util $util, $phpbb_root_path, $php_ext)
56
	{
57 16
		parent::__construct($language, $request, $ptemplate);
58
59 16
		$this->auth = $auth;
60 16
		$this->config = $config;
61 16
		$this->pagination = $pagination;
62 16
		$this->template = $template;
63 16
		$this->template_context = $template_context;
64 16
		$this->util = $util;
65 16
		$this->phpbb_root_path = $phpbb_root_path;
66 16
		$this->php_ext = $php_ext;
67 16
	}
68
69
	/**
70
	 * @inheritdoc
71
	 */
72 4
	public function get_name()
73
	{
74 4
		return 'textarea';
75
	}
76
77
	/**
78
	 * @inheritdoc
79
	 */
80 3
	public function get_default_props()
81
	{
82
		return array(
83 3
			'size'		=> 'large',
84 3
			'maxlength'	=> '',
85 3
			'max_chars'	=> 200,
86 3
			'editor'	=> true,
87 3
		);
88
	}
89
90
	/**
91
	 * Display content field
92
	 *
93
	 * @param array $data
94
	 * @param string $view_mode
95
	 * @param array $topic_data
96
	 * @return mixed
97
	 */
98 11
	public function display_field(array $data = array(), $view_mode = 'summary', array $topic_data = array())
99
	{
100 11
		$toc_pattern = '(<h4>(.*?)</h4>)?';
101 11
		$pages_pattern = '<p><!-- pagebreak --></p>';
102 11
		$split_pattern = $pages_pattern . (($view_mode !== 'detail') ? $toc_pattern : '');
103
104 11
		$pages = array_filter(preg_split('#' . $split_pattern . '#s', $data['field_value']));
105
106 11
		if ($view_mode !== 'detail')
107 11
		{
108 5
			return $this->get_summary_value(trim($pages[0]), $data['field_props']['max_chars']);
109
		}
110
111
		// get page titles to generate TOC
112 6
		preg_match_all('#' . $pages_pattern . $toc_pattern . '#s', $data['field_value'], $matches);
113
114 6
		return $this->get_detail_value($pages, $matches[2], $data);
115
	}
116
117
	/**
118
	 * @inheritdoc
119
	 */
120 2
	public function show_form_field($name, array &$data, $forum_id = 0)
121
	{
122 2
		if ($data['field_props']['editor'])
123 2
		{
124 1
			$asset_path = $this->util->get_web_path();
125 1
			$this->util->add_assets(array(
126
				'js'   => array(
127 1
					$asset_path . 'assets/javascript/editor.js',
128
					'@blitze_content/assets/form/textarea.min.js'
129 1
				)
130 1
			));
131
132 1
			$data += $this->get_editor($forum_id);
133 1
		}
134
135 2
		return parent::show_form_field($name, $data, $forum_id);
0 ignored issues
show
Unused Code introduced by
The call to base::show_form_field() has too many arguments starting with $forum_id.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
136
	}
137
138
	/**
139
	 * @param int $forum_id
140
	 * @return array
141
	 */
142 1
	protected function get_editor($forum_id)
143
	{
144
		// Assigning custom bbcodes
145 1
		if (!function_exists('display_custom_bbcodes'))
146 1
		{
147 1
			include($this->phpbb_root_path . 'includes/functions_display.' . $this->php_ext);
148 1
		}
149
150 1
		display_custom_bbcodes();
151
152 1
		$bbcode_status	= ($this->config['allow_bbcode'] && $this->auth->acl_get('f_bbcode', $forum_id)) ? true : false;
153
154 1
		$dataref = $this->template_context->get_data_ref();
155 1
		$this->ptemplate->assign_block_vars_array('custom_tags', (isset($dataref['custom_tags'])) ? $dataref['custom_tags'] : array());
156
157
		// HTML, BBCode, Smilies, Images and Flash statusf
158
		return array(
159 1
			'S_BBCODE_IMG'			=> ($bbcode_status && $this->auth->acl_get('f_img', $forum_id)) ? true : false,
160 1
			'S_LINKS_ALLOWED'		=> ($this->config['allow_post_links']) ? true : false,
161 1
			'S_BBCODE_FLASH'		=> ($bbcode_status && $this->auth->acl_get('f_flash', $forum_id) && $this->config['allow_post_flash']) ? true : false,
162 1
			'S_BBCODE_QUOTE'		=> false,
163 1
			'S_SMILIES_ALLOWED'		=> false,
164 1
		);
165
	}
166
167
	/**
168
	 * @param string $html
169
	 * @param int $max_chars
170
	 * @return string
171
	 */
172 5
	 protected function get_summary_value($html, $max_chars)
173
	 {
174
	 	if ($max_chars)
175 5
		{
176 1
			$truncateService = new TruncateService();
177 1
			$html = $truncateService->truncate($html, $max_chars);
178 1
		}
179 5
		return $html;
180
	 }
181
182
	/**
183
	 * @param array $pages
184
	 * @param array $titles
185
	 * @param array $data
186
	 * @return mixed
187
	 */
188 6
	protected function get_detail_value(array $pages, array $titles, array $data)
189
	{
190 6
		if ($this->request->is_set('preview'))
191 6
		{
192 1
			return join('<p><hr class="dashed"></p>', $pages);
193
		}
194
195 5
		$page = $this->request->variable('page', 0);
196
197 5
		$start = isset($pages[$page]) ? $page : 0;
198 5
		$total_pages = sizeof($pages);
199 5
		$topic_url = build_url(array('page'));
200
201 5
		$this->generate_page_nav($topic_url, $total_pages, $start);
202 5
		$this->generate_toc($start, $topic_url, array_slice($titles, 0, $total_pages - 1));
203
204 5
		return $this->get_page_content($start, $data['field_name'], $pages);
205
	}
206
207
	/**
208
	 * Generate pagination for topic subpages
209
	 *
210
	 * @param string $topic_url
211
	 * @param int $total_pages
212
	 * @param int $start
213
	 * @return void
214
	 */
215 5
	protected function generate_page_nav($topic_url, $total_pages, &$start)
216
	{
217 5
		$start = $this->pagination->validate_start($start, 1, $total_pages);
218 5
		$this->pagination->generate_template_pagination($topic_url, 'page', 'page', $total_pages, 1, $start);
219 5
		$this->template->assign_var('S_NOT_LAST_PAGE', !($start === ($total_pages - 1)));
220 5
	}
221
222
	/**
223
	 * Generate Table of contents
224
	 *
225
	 * @param int $start
226
	 * @param string $topic_url
227
	 * @param array $page_titles
228
	 * @return void
229
	 */
230 5
	protected function generate_toc($start, $topic_url, array $page_titles)
231
	{
232 5
		if (sizeof(array_filter($page_titles)))
233 5
		{
234 2
			$page_titles = array_merge(array($this->language->lang('CONTENT_TOC_OVERVIEW')), $page_titles);
235
236 2
			foreach ($page_titles as $page => $title)
237
			{
238 2
				$this->template->assign_block_vars('toc', array(
239 2
					'TITLE'		=> ($title) ? $title : $this->language->lang('CONTENT_TOC_UNTITLED'),
240 2
					'S_PAGE'	=> ($page === $start),
241 2
					'U_VIEW'	=> append_sid($topic_url, ($page) ? 'page=' . $page : false),
242 2
				));
243 2
			}
244 2
		}
245 5
	}
246
247
	/**
248
	 * When Previewing topic, we show all pages
249
	 *
250
	 * @param array $pages
251
	 * @return mixed
252
	 */
253 5
	protected function get_page_content($start, $field_name, array $pages)
254
	{
255 5
		$value = trim($pages[$start]);
256
257
		// Hide all other fields if we're looking at page 2+
258
		if ($start)
259 5
		{
260
			$value = array(
261 2
				$field_name => $value,
262 2
			);
263 2
		}
264
265 5
		return $value;
266
	}
267
}
268