Completed
Push — develop-3.2 ( ec81a5...eb42f9 )
by Matt
32:34
created

bbcodes_installer   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 191
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 16
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 191
ccs 62
cts 62
cp 1
rs 10

9 Methods

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