Completed
Push — develop ( ff34f8...c5327c )
by Daniel
09:18
created

textarea::get_editor()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.0309

Importance

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