Completed
Push — develop ( 06fc14...733603 )
by Daniel
27:44 queued 12:50
created

custom   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 197
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 7
Bugs 0 Features 1
Metric Value
wmc 19
c 7
b 0
f 1
lcom 1
cbo 1
dl 0
loc 197
ccs 75
cts 75
cp 1
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A get_config() 0 7 1
A __construct() 0 7 1
A display() 0 15 2
A edit() 0 18 1
A copy() 0 12 2
A get_block_data() 0 6 2
A get_content_for_display() 0 8 2
A save() 0 13 2
A show_editor() 0 8 2
A get_custom_blocks() 0 20 3
A get_default_fields() 0 10 1
1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2013 Daniel A. (blitze)
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace blitze\sitemaker\blocks;
11
12
use blitze\sitemaker\services\blocks\driver\block;
13
14
/**
15
 * Custom Block
16
 */
17
class custom extends block
18
{
19
	/** @var \phpbb\cache\driver\driver_interface */
20
	protected $cache;
21
22
	/** @var \phpbb\db\driver\driver_interface */
23
	protected $db;
24
25
	/** @var \phpbb\request\request_interface */
26
	protected $request;
27
28
	/** @var string */
29
	protected $cblocks_table;
30
31
	/**
32
	 * Constructor
33
	 *
34
	 * @param \phpbb\cache\driver\driver_interface	$cache					Cache driver interface
35
	 * @param \phpbb\db\driver\driver_interface		$db						Database object
36
	 * @param \phpbb\request\request_interface		$request				Request object
37
	 * @param string								$cblocks_table			Name of custom blocks database table
38
	 */
39 42
	public function __construct(\phpbb\cache\driver\driver_interface $cache, \phpbb\db\driver\driver_interface $db, \phpbb\request\request_interface $request, $cblocks_table)
40
	{
41 42
		$this->cache = $cache;
42 42
		$this->db = $db;
43 42
		$this->request = $request;
44 42
		$this->cblocks_table = $cblocks_table;
45 42
	}
46
47
	/**
48
	 * {@inheritdoc}
49
	 */
50 4
	public function get_config(array $settings)
51
	{
52 4
		return array(
53 4
			'legend1'		=> 'SOURCE',
54
			'source'	=> array('lang' => '', 'type' => 'textarea:20:40', 'default' => '', 'explain' => false, 'append' => 'SOURCE_EXPLAIN'),
55 4
		);
56
	}
57
58 4
	/**
59 4
	 * {@inheritdoc}
60 4
	 */
61
	public function display(array $bdata, $edit_mode = false)
62
	{
63
		$cblock = $this->get_block_data($bdata['bid']);
64
		$content = $this->get_content_for_display($cblock, $bdata['settings']['source']);
65
66
		if (!$bdata['settings']['source'])
67 3
		{
68
			$this->show_editor($cblock, $content, $edit_mode);
69 3
		}
70 3
71
		return array(
72 3
			'title'		=> 'BLOCK_TITLE',
73 3
			'content'	=> $content,
74
		);
75 3
	}
76
77 3
	/**
78
	 * @param int $block_id
79
	 * @return array
80 3
	 */
81 3
	public function edit($block_id)
82 3
	{
83 3
		$content = $this->request->variable('content', '', true);
84
		$cblocks = $this->get_custom_blocks();
85
86
		$sql_data =	$this->get_default_fields($block_id);
87
		$sql_data['block_content'] = $content;
88
89
		generate_text_for_storage($sql_data['block_content'], $sql_data['bbcode_uid'], $sql_data['bbcode_bitfield'], $sql_data['bbcode_options'], true, true, true);
90
91 1
		$this->save($sql_data, isset($cblocks[$block_id]));
92
93 1
		return array(
94
			'id'		=> $block_id,
95 1
			'content'	=> $this->get_content_for_display($sql_data),
96 1
			'callback'	=> 'previewCustomBlock',
97 1
		);
98 1
	}
99
100 1
	/**
101 1
	 * @param int $from_bid
102 1
	 * @param int $to_bid
103
	 * @see \blitze\sitemaker\services\blocks\action\copy_route::copy_custom_block
104
	 */
105
	public function copy($from_bid, $to_bid)
106
	{
107
		$cblocks = $this->get_custom_blocks();
108 4
109
		if (isset($cblocks[$from_bid]))
110 4
		{
111
			$sql_data = $cblocks[$from_bid];
112 4
			$sql_data['block_id'] = $to_bid;
113
114
			$this->save($sql_data, isset($cblocks[$to_bid]));
115
		}
116
	}
117
118
	/**
119 7
	 * @param int $bid
120
	 * @return array
121 7
	 */
122 7
	private function get_block_data($bid)
123
	{
124
		$cblock = $this->get_custom_blocks();
125
126
		return (isset($cblock[$bid])) ? $cblock[$bid] : $this->get_default_fields($bid);
127
	}
128
129 4
	/**
130
	 * @param array $data
131 4
	 * @return string
132 4
	 */
133 2
	private function get_content_for_display(array $data, $content = '')
134 2
	{
135
		if (!$content)
136
		{
137 2
			$content = generate_text_for_display($data['block_content'], $data['bbcode_uid'], $data['bbcode_bitfield'], $data['bbcode_options']);
138
		}
139 4
		return html_entity_decode($content);
140 4
	}
141 4
142
	/**
143
	 * @param array $sql_data
144
	 * @param bool $block_exists
145
	 */
146
	private function save(array $sql_data, $block_exists)
147
	{
148
		if (!$block_exists)
149 4
		{
150
			$sql = 'INSERT INTO ' . $this->cblocks_table . ' ' . $this->db->sql_build_array('INSERT', $sql_data);
151 4
		}
152 4
		else
153 2
		{
154 2
			$sql = 'UPDATE ' . $this->cblocks_table . ' SET ' . $this->db->sql_build_array('UPDATE', $sql_data) . ' WHERE block_id = ' . (int) $sql_data['block_id'];
155 2
		}
156 4
		$this->db->sql_query($sql);
157
		$this->cache->destroy('pt_cblocks');
158
	}
159
160
	/**
161 8
	 * @param array $cblock
162
	 * @param string $content
163 8
	 * @param bool $edit_mode
164 8
	 * @return string
165
	 */
166 8
	private function show_editor(array $cblock, &$content, $edit_mode)
167 8
	{
168
		if ($edit_mode !== false)
169 8
		{
170 8
			decode_message($cblock['block_content'], $cblock['bbcode_uid']);
171
			$content = '<div id="block-editor-' . $cblock['block_id'] . '" class="editable editable-block" data-service="blitze.sitemaker.block.custom" data-method="edit" data-raw="' . $cblock['block_content'] . '">' . $content . '</div>';
172 8
		}
173 8
	}
174 8
175
	/**
176 8
	 * @return array
177 8
	 */
178
	private function get_custom_blocks()
179 8
	{
180
		if (($cblocks = $this->cache->get('pt_cblocks')) === false)
181
		{
182
			$sql = 'SELECT *
183
				FROM ' . $this->cblocks_table;
184
			$result = $this->db->sql_query($sql);
185
186 5
			$cblocks = array();
187
			while ($row = $this->db->sql_fetchrow($result))
188
			{
189 5
				$cblocks[$row['block_id']] = $row;
190 5
			}
191 5
			$this->db->sql_freeresult($result);
192 5
193 5
			$this->cache->put('pt_cblocks', $cblocks);
194 5
		}
195
196
		return $cblocks;
197
	}
198
199
	/**
200
	 * @param int $bid
201
	 * @return array
202
	 */
203
	private function get_default_fields($bid)
204
	{
205
		return array(
206
			'block_id'			=> $bid,
207
			'block_content'		=> '',
208
			'bbcode_bitfield'	=> '',
209
			'bbcode_options'	=> 7,
210
			'bbcode_uid'		=> '',
211
		);
212
	}
213
}
214