Completed
Push — develop-3.2 ( 3685db...4cc568 )
by Matt
32:16
created

disable_bbcodes::display_bbcodes_off()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 12
nc 2
nop 0
1
<?php
2
/**
3
 *
4
 * Advanced BBCode Box
5
 *
6
 * @copyright (c) 2018 Matt Friedman
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace vse\abbc3\migrations;
12
13
use phpbb\db\migration\container_aware_migration;
14
15
/**
16
 * Class disable_bbcodes
17
 *
18
 * This is a specialized migration. It will set original (un-altered) ABBC3 BBCodes
19
 * to be hidden from the posting pages when ABBC3 is removed. This will make it look
20
 * like the default phpBB BBCode toolbar again, but ABBC3 BBCodes are not deleted
21
 * so posts won't suddenly break. Users can manually re-enable the BBCodes they want to keep,
22
 * delete the one's they do not, etc. If ABBC3 is re-installed again, it will attempt
23
 * to re-enable them in the BBCode toolbar, restoring the ABBC3 BBCode toolbar appearance.
24
 *
25
 * @package vse\abbc3\migrations
26
 */
27
class disable_bbcodes extends container_aware_migration
28
{
29
	/** @var \phpbb\config\db_text */
30
	protected $configText;
31
32
	/**
33
	 * {@inheritDoc}
34
	 */
35
	public function effectively_installed()
36
	{
37
		return $this->getConfigText()->get('abbc3_bbcodes') === null;
38
	}
39
40
	/**
41
	 * {@inheritdoc}
42
	 */
43
	public function update_data()
44
	{
45
		return [
46
			['custom', [[$this, 'display_bbcodes_on']]],
47
			['config_text.remove', ['abbc3_bbcodes']],
48
		];
49
	}
50
51
	/**
52
	 * {@inheritdoc}
53
	 */
54
	public function revert_data()
55
	{
56
		return [
57
			['custom', [[$this, 'display_bbcodes_off']]], // use custom func because config_text.add was being called twice
58
		];
59
	}
60
61
	/**
62
	 * Get ABBC3 BBCodes that were disabled from a previous extension purge,
63
	 * and re-display them on post.
64
	 */
65
	public function display_bbcodes_on()
66
	{
67
		$bbcodes = json_decode($this->getConfigText()->get('abbc3_bbcodes'), true);
68
69
		if (is_array($bbcodes))
70
		{
71
			$bbcodes = array_map('intval', $bbcodes);
72
73
			$this->update_displayed_bbcodes($bbcodes, true);
74
		}
75
	}
76
77
	/**
78
	 * Set ABBC3 BBCodes Display on Post to 0 so they will no longer
79
	 * appear in posting buttons when extension is purged.
80
	 */
81
	public function display_bbcodes_off()
82
	{
83
		$bbcodes = [];
84
85
		$sql = 'SELECT bbcode_id 
86
			FROM ' . BBCODES_TABLE . '
87
			WHERE display_on_posting = 1
88
				AND bbcode_helpline ' . $this->db->sql_like_expression('ABBC3_' . $this->db->get_any_char());
89
		$this->db->sql_query($sql);
90
		$result = $this->db->sql_query($sql);
91
		while ($row = $this->db->sql_fetchrow($result))
92
		{
93
			$bbcodes[] = $row['bbcode_id'];
94
		}
95
		$this->db->sql_freeresult($result);
96
97
		$this->update_displayed_bbcodes($bbcodes, false);
98
99
		$this->getConfigText()->set('abbc3_bbcodes', json_encode($bbcodes));
100
	}
101
102
	/**
103
	 * Update the display on posting state for custom BBCodes
104
	 *
105
	 * @param array $bbcodes
106
	 * @param bool  $display
107
	 */
108 View Code Duplication
	protected function update_displayed_bbcodes(array $bbcodes, $display)
109
	{
110
		if (!empty($bbcodes))
111
		{
112
			$sql = 'UPDATE ' . BBCODES_TABLE . '
113
				SET display_on_posting = ' . (int) $display . '
114
				WHERE ' . $this->db->sql_in_set('bbcode_id', $bbcodes);
115
			$this->db->sql_query($sql);
116
		}
117
	}
118
119
	/**
120
	 * Get the config text object
121
	 *
122
	 * @return \phpbb\config\db_text
123
	 */
124
	protected function getConfigText()
125
	{
126
		if ($this->configText === null)
127
		{
128
			$this->configText = $this->container->get('config_text');
129
		}
130
131
		return $this->configText;
132
	}
133
}
134