Completed
Push — master ( e1d9a4...42f285 )
by Matt
34:19 queued 03:12
created

bbcodes_help::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 5
1
<?php
2
/**
3
 *
4
 * Advanced BBCode Box
5
 *
6
 * @copyright (c) 2017 Matt Friedman
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace vse\abbc3\core;
12
13
use phpbb\db\driver\driver_interface;
14
use phpbb\language\language;
15
use phpbb\template\template;
16
use phpbb\user;
17
18
/**
19
 * ABBC3 core BBCodes parser class
20
 */
21
class bbcodes_help
22
{
23
	/** @var driver_interface */
24
	protected $db;
25
26
	/** @var language */
27
	protected $language;
28
29
	/** @var template */
30
	protected $template;
31
32
	/** @var user */
33
	protected $user;
34
	/**
35
	 * @var \vse\abbc3\core\bbcodes_display
36
	 */
37
	private $bbcodes_display;
38
39
	/**
40
	 * Constructor
41
	 *
42
	 * @param \vse\abbc3\core\bbcodes_display   $bbcodes_display
43
	 * @param \phpbb\db\driver\driver_interface $db
44
	 * @param language                          $language
45
	 * @param \phpbb\template\template          $template
46
	 * @param \phpbb\user                       $user
47
	 */
48
	public function __construct(bbcodes_display $bbcodes_display, driver_interface $db, language $language, template $template, user $user)
49
	{
50
		$this->bbcodes_display = $bbcodes_display;
51
		$this->db = $db;
52
		$this->language = $language;
53
		$this->template = $template;
54
		$this->user = $user;
55
	}
56
57
	/**
58
	 * Generate BBCode help FAQ for ABBC3's custom BBCodes
59
	 */
60
	public function faq()
61
	{
62
		// Set the block template data
63
		$this->template->assign_block_vars('faq_block', [
64
			'BLOCK_TITLE'	=> $this->language->lang('ABBC3_FAQ_TITLE'),
65
			'SWITCH_COLUMN'	=> false,
66
		]);
67
68
		$example_text = $this->language->lang('ABBC3_FAQ_SAMPLE_TEXT');
69
		$abbc3_faq_data = array_intersect_key([
70
			'ABBC3_FONT_HELPLINE'		=> "[font=Comic Sans MS]{$example_text}[/font]",
71
			'ABBC3_HIGHLIGHT_HELPLINE'	=> "[highlight=yellow]{$example_text}[/highlight]",
72
			'ABBC3_ALIGN_HELPLINE'		=> "[align=center]{$example_text}[/align]",
73
			'ABBC3_FLOAT_HELPLINE'		=> "[float=right]{$example_text}[/float]",
74
			'ABBC3_STRIKE_HELPLINE'		=> "[s]{$example_text}[/s]",
75
			'ABBC3_SUB_HELPLINE'		=> "[sub]{$example_text}[/sub] {$example_text}",
76
			'ABBC3_SUP_HELPLINE'		=> "[sup]{$example_text}[/sup] {$example_text}",
77
			'ABBC3_GLOW_HELPLINE'		=> "[glow=red]{$example_text}[/glow]",
78
			'ABBC3_SHADOW_HELPLINE'		=> "[shadow=blue]{$example_text}[/shadow]",
79
			'ABBC3_DROPSHADOW_HELPLINE'	=> "[dropshadow=blue]{$example_text}[/dropshadow]",
80
			'ABBC3_BLUR_HELPLINE'		=> "[blur=blue]{$example_text}[/blur]",
81
			'ABBC3_FADE_HELPLINE'		=> "[fade]{$example_text}[/fade]",
82
			'ABBC3_PREFORMAT_HELPLINE'	=> "[pre]{$example_text}\n\t{$example_text}[/pre]",
83
			'ABBC3_DIR_HELPLINE'		=> "[dir=rtl]{$example_text}[/dir]",
84
			'ABBC3_MARQUEE_HELPLINE'	=> "[marq=left]{$example_text}[/marq]",
85
			'ABBC3_SPOILER_HELPLINE'	=> "[spoil]{$example_text}[/spoil]",
86
			'ABBC3_HIDDEN_HELPLINE'		=> "[hidden]{$example_text}[/hidden]",
87
			'ABBC3_MOD_HELPLINE'		=> "[mod=\"{$this->language->lang('USERNAME')}\"]{$example_text}[/mod]",
88
			'ABBC3_OFFTOPIC_HELPLINE'	=> "[offtopic]{$example_text}[/offtopic]",
89
			'ABBC3_NFO_HELPLINE'		=> '[nfo]༼ つ ◕_◕ ༽つ    ʕ•ᴥ•ʔ   ¯\_(ツ)_/¯[/nfo]',
90
			'ABBC3_BBVIDEO_HELPLINE'	=> '[bbvideo]http://www.youtube.com/watch?v=sP4NMoJcFd4[/bbvideo]',
91
		], $this->allowed_bbcodes());
92
93
		// Process faq data for display as parsed and un-parsed bbcodes
94
		foreach ($abbc3_faq_data as $key => $question)
95
		{
96
			$uid = $bitfield = $flags = '';
97
			generate_text_for_storage($question, $uid, $bitfield, $flags, true);
98
			$example = generate_text_for_edit($question, $uid, $flags);
99
			$result = generate_text_for_display($question, $uid, $bitfield, $flags);
100
			$title = explode(':', $this->language->lang($key), 2);
101
102
			$this->template->assign_block_vars('faq_block.faq_row', [
103
				'FAQ_QUESTION'	=> $title[0],
104
				'FAQ_ANSWER'	=> $this->language->lang('ABBC3_FAQ_ANSWER', $title[1], $example['text'], $result),
105
			]);
106
		}
107
	}
108
109
	/**
110
	 * Get custom BBCodes the current user is allowed to use
111
	 *
112
	 * @return array
113
	 */
114
	protected function allowed_bbcodes()
115
	{
116
		$allowed = [];
117
		$sql = 'SELECT bbcode_helpline, bbcode_group
118
			FROM ' . BBCODES_TABLE . '
119
			WHERE bbcode_helpline ' . $this->db->sql_like_expression('ABBC3_' . $this->db->get_any_char());
120
		$result = $this->db->sql_query($sql);
121
		while ($row = $this->db->sql_fetchrow($result))
122
		{
123
			if ($this->bbcodes_display->user_in_bbcode_group($row['bbcode_group']))
124
			{
125
				$allowed[$row['bbcode_helpline']] = true;
126
			}
127
		}
128
		$this->db->sql_freeresult($result);
129
130
		return $allowed;
131
	}
132
}
133