Completed
Pull Request — master (#28)
by Matt
15:58
created

bbcodes_installer::build_bbcode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1.0013

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
ccs 8
cts 9
cp 0.8889
rs 9.4285
cc 1
eloc 9
nc 1
nop 1
crap 1.0013
1
<?php
2
/**
3
 *
4
 * Advanced BBCode Box
5
 *
6
 * @copyright (c) 2016 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\request\request;
15
use phpbb\user;
16
17
/**
18
 * Class bbcodes_installer
19
 *
20
 * @package vse\abbc3\core
21
 */
22
class bbcodes_installer extends acp_manager
23
{
24
	/** @var \acp_bbcodes */
25
	protected $acp_bbcodes;
26
27
	/** @var string */
28
	protected $phpbb_root_path;
29
30
	/** @var string */
31
	protected $php_ext;
32
33
	/**
34
	 * Constructor
35
	 *
36
	 * @param driver_interface $db
37
	 * @param request          $request
38
	 * @param user             $user
39
	 * @param string           $phpbb_root_path
40
	 * @param string           $php_ext
41
	 * @access public
42
	 */
43 2
	public function __construct(driver_interface $db, request $request, user $user, $phpbb_root_path, $php_ext)
44
	{
45 2
		parent::__construct($db, $request, $user);
46
47 2
		$this->phpbb_root_path = $phpbb_root_path;
48 2
		$this->php_ext         = $php_ext;
49
50 2
		$this->acp_bbcodes     = $this->get_acp_bbcodes();
51 2
	}
52
53
	/**
54
	 * Installs bbcodes, used by migrations to perform add/updates
55
	 *
56
	 * @param array $bbcodes Array of bbcodes to install
57
	 * @return null
58
	 * @access public
59
	 */
60 2
	public function install_bbcodes(array $bbcodes)
61
	{
62 2
		foreach ($bbcodes as $bbcode_name => $bbcode_data)
63
		{
64 2
			$bbcode_data = $this->build_bbcode($bbcode_data);
65
66 2
			if ($bbcode = $this->bbcode_exists($bbcode_name, $bbcode_data['bbcode_tag']))
67 2
			{
68 1
				$this->update_bbcode($bbcode, $bbcode_data);
69 1
			}
70
			else
71
			{
72 2
				$this->add_bbcode($bbcode_data);
73
			}
74 2
		}
75
76 2
		$this->resynchronize_bbcode_order();
77 2
	}
78
79
	/**
80
	 * Get the acp_bbcodes class
81
	 *
82
	 * @return \acp_bbcodes
83
	 * @access protected
84
	 */
85 2
	protected function get_acp_bbcodes()
86
	{
87 2
		if (!class_exists('acp_bbcodes'))
88 2
		{
89 1
			include($this->phpbb_root_path . 'includes/acp/acp_bbcodes.' . $this->php_ext);
90 1
		}
91
92 2
		return new \acp_bbcodes();
93
	}
94
95
	/**
96
	 * Build the bbcode
97
	 *
98
	 * @param array $bbcode_data Initial bbcode data
99
	 * @return array Complete bbcode data array
100
	 * @access protected
101
	 */
102 2
	protected function build_bbcode(array $bbcode_data)
103
	{
104 2
		$data = $this->acp_bbcodes->build_regexp($bbcode_data['bbcode_match'], $bbcode_data['bbcode_tpl']);
105
106
		$bbcode_data += array(
107 2
			'bbcode_tag'          => $data['bbcode_tag'],
108 2
			'first_pass_match'    => $data['first_pass_match'],
109 2
			'first_pass_replace'  => $data['first_pass_replace'],
110 2
			'second_pass_match'   => $data['second_pass_match'],
111 2
			'second_pass_replace' => $data['second_pass_replace'],
112
		);
113
114 2
		return $bbcode_data;
115
	}
116
117
	/**
118
	 * Get the max bbcode id value
119
	 *
120
	 * @return int bbcode identifier
121
	 * @access protected
122
	 */
123 2
	protected function get_max_bbcode_id()
124
	{
125 2
		return $this->get_max_column_value('bbcode_id');
126
	}
127
128
	/**
129
	 * Check if bbcode exists
130
	 *
131
	 * @param string $bbcode_name Name of bbcode
132
	 * @param string $bbcode_tag  Tag name of bbcode
133
	 * @return mixed Existing bbcode data array or false if not found
134
	 * @access protected
135
	 */
136 2
	protected function bbcode_exists($bbcode_name, $bbcode_tag)
137
	{
138
		$sql = 'SELECT bbcode_id
139 2
			FROM ' . BBCODES_TABLE . "
140 2
			WHERE LOWER(bbcode_tag) = '" . strtolower($bbcode_name) . "'
141 2
			OR LOWER(bbcode_tag) = '" . strtolower($bbcode_tag) . "'";
142 2
		$result = $this->db->sql_query($sql);
143 2
		$row = $this->db->sql_fetchrow($result);
144 2
		$this->db->sql_freeresult($result);
145
146 2
		return $row;
147
	}
148
149
	/**
150
	 * Update existing bbcode
151
	 *
152
	 * @param array $old_bbcode Existing bbcode data
153
	 * @param array $new_bbcode New bbcode data
154
	 * @return null
155
	 * @access protected
156
	 */
157 1
	protected function update_bbcode(array $old_bbcode, array $new_bbcode)
158
	{
159 1
		$sql = 'UPDATE ' . BBCODES_TABLE . '
160 1
			SET ' . $this->db->sql_build_array('UPDATE', $new_bbcode) . '
161 1
			WHERE bbcode_id = ' . (int) $old_bbcode['bbcode_id'];
162 1
		$this->db->sql_query($sql);
163 1
	}
164
165
	/**
166
	 * Add new bbcode
167
	 *
168
	 * @param array $bbcode_data New bbcode data
169
	 * @return null
170
	 * @access protected
171
	 */
172 2
	protected function add_bbcode(array $bbcode_data)
173
	{
174 2
		$bbcode_id = $this->get_max_bbcode_id() + 1;
175
176 2
		if ($bbcode_id <= NUM_CORE_BBCODES)
177 2
		{
178 1
			$bbcode_id = NUM_CORE_BBCODES + 1;
179 1
		}
180
181 2
		if ($bbcode_id <= BBCODE_LIMIT)
182 2
		{
183 2
			$bbcode_data['bbcode_id'] = (int) $bbcode_id;
184
			// set display_on_posting to 1 by default, so if 0 is desired, set it in our data array
185 2
			$bbcode_data['display_on_posting'] = (int) !isset($bbcode_data['display_on_posting']);
186
187 2
			$this->db->sql_query('INSERT INTO ' . BBCODES_TABLE . ' ' . $this->db->sql_build_array('INSERT', $bbcode_data));
188 2
		}
189 2
	}
190
}
191