Completed
Push — develop-3.2.x ( 762012...85e047 )
by Matt
06:54
created

bbcodes_installer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
cc 1
eloc 5
nc 1
nop 6
crap 1
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\request\request;
16
use phpbb\user;
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 request          $request
40
	 * @param user             $user
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, request $request, user $user, $phpbb_root_path, $php_ext)
46
	{
47 2
		parent::__construct($db, $group_helper, $request, $user);
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
	 * @return null
60
	 * @access public
61
	 */
62 2
	public function install_bbcodes(array $bbcodes)
63
	{
64 2
		foreach ($bbcodes as $bbcode_name => $bbcode_data)
65
		{
66 2
			$bbcode_data = $this->build_bbcode($bbcode_data);
67
68 2
			if ($bbcode = $this->bbcode_exists($bbcode_name, $bbcode_data['bbcode_tag']))
69 2
			{
70 1
				$this->update_bbcode($bbcode, $bbcode_data);
71 1
			}
72
			else
73
			{
74 2
				$this->add_bbcode($bbcode_data);
75
			}
76 2
		}
77
78 2
		$this->resynchronize_bbcode_order();
79 2
	}
80
81
	/**
82
	 * Get the acp_bbcodes class
83
	 *
84
	 * @return \acp_bbcodes
85
	 * @access protected
86
	 */
87 2
	protected function get_acp_bbcodes()
88
	{
89 2
		if (!class_exists('acp_bbcodes'))
90 2
		{
91 1
			include($this->phpbb_root_path . 'includes/acp/acp_bbcodes.' . $this->php_ext);
92 1
		}
93
94 2
		return new \acp_bbcodes();
95
	}
96
97
	/**
98
	 * Build the bbcode
99
	 *
100
	 * @param array $bbcode_data Initial bbcode data
101
	 * @return array Complete bbcode data array
102
	 * @access protected
103
	 */
104 2
	protected function build_bbcode(array $bbcode_data)
105
	{
106 2
		$data = $this->acp_bbcodes->build_regexp($bbcode_data['bbcode_match'], $bbcode_data['bbcode_tpl']);
107
108
		$bbcode_data += array(
109 2
			'bbcode_tag'          => $data['bbcode_tag'],
110 2
			'first_pass_match'    => $data['first_pass_match'],
111 2
			'first_pass_replace'  => $data['first_pass_replace'],
112 2
			'second_pass_match'   => $data['second_pass_match'],
113 2
			'second_pass_replace' => $data['second_pass_replace'],
114
		);
115
116 2
		return $bbcode_data;
117
	}
118
119
	/**
120
	 * Get the max bbcode id value
121
	 *
122
	 * @return int bbcode identifier
123
	 * @access protected
124
	 */
125 2
	protected function get_max_bbcode_id()
126
	{
127 2
		return $this->get_max_column_value('bbcode_id');
128
	}
129
130
	/**
131
	 * Check if bbcode exists
132
	 *
133
	 * @param string $bbcode_name Name of bbcode
134
	 * @param string $bbcode_tag  Tag name of bbcode
135
	 * @return mixed Existing bbcode data array or false if not found
136
	 * @access protected
137
	 */
138 2
	protected function bbcode_exists($bbcode_name, $bbcode_tag)
139
	{
140
		$sql = 'SELECT bbcode_id
141 2
			FROM ' . BBCODES_TABLE . "
142 2
			WHERE LOWER(bbcode_tag) = '" . strtolower($bbcode_name) . "'
143 2
			OR LOWER(bbcode_tag) = '" . strtolower($bbcode_tag) . "'";
144 2
		$result = $this->db->sql_query($sql);
145 2
		$row = $this->db->sql_fetchrow($result);
146 2
		$this->db->sql_freeresult($result);
147
148 2
		return $row;
149
	}
150
151
	/**
152
	 * Update existing bbcode
153
	 *
154
	 * @param array $old_bbcode Existing bbcode data
155
	 * @param array $new_bbcode New bbcode data
156
	 * @return null
157
	 * @access protected
158
	 */
159 1
	protected function update_bbcode(array $old_bbcode, array $new_bbcode)
160
	{
161 1
		$sql = 'UPDATE ' . BBCODES_TABLE . '
162 1
			SET ' . $this->db->sql_build_array('UPDATE', $new_bbcode) . '
163 1
			WHERE bbcode_id = ' . (int) $old_bbcode['bbcode_id'];
164 1
		$this->db->sql_query($sql);
165 1
	}
166
167
	/**
168
	 * Add new bbcode
169
	 *
170
	 * @param array $bbcode_data New bbcode data
171
	 * @return null
172
	 * @access protected
173
	 */
174 2
	protected function add_bbcode(array $bbcode_data)
175
	{
176 2
		$bbcode_id = $this->get_max_bbcode_id() + 1;
177
178 2
		if ($bbcode_id <= NUM_CORE_BBCODES)
179 2
		{
180 1
			$bbcode_id = NUM_CORE_BBCODES + 1;
181 1
		}
182
183 2
		if ($bbcode_id <= BBCODE_LIMIT)
184 2
		{
185 2
			$bbcode_data['bbcode_id'] = (int) $bbcode_id;
186
			// set display_on_posting to 1 by default, so if 0 is desired, set it in our data array
187 2
			$bbcode_data['display_on_posting'] = (int) !isset($bbcode_data['display_on_posting']);
188
189 2
			$this->db->sql_query('INSERT INTO ' . BBCODES_TABLE . ' ' . $this->db->sql_build_array('INSERT', $bbcode_data));
190 2
		}
191 2
	}
192
}
193