Completed
Push — develop ( b18c7a...273208 )
by Daniel
11:54 queued 09:05
created

custom   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 185
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 8
Bugs 1 Features 1
Metric Value
wmc 16
c 8
b 1
f 1
lcom 1
cbo 1
dl 0
loc 185
ccs 76
cts 76
cp 1
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A display() 0 12 1
A edit() 0 18 1
A copy() 0 12 2
A get_block_data() 0 6 2
A get_content_for_display() 0 5 1
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 \phpbb\textformatter\s9e\utils */
29
	protected $text_formatter_utils;
30
31
	/** @var string */
32
	protected $cblocks_table;
33
34
	/**
35
	 * Constructor
36
	 *
37
	 * @param \phpbb\cache\driver\driver_interface	$cache					Cache driver interface
38
	 * @param \phpbb\db\driver\driver_interface		$db						Database object
39
	 * @param \phpbb\request\request_interface		$request				Request object
40
	* @param \phpbb\textformatter\s9e\utils      	$text_formatter_utils	Text manipulation utilities
41
	 * @param string								$cblocks_table			Name of custom blocks database table
42
	 */
43 42
	public function __construct(\phpbb\cache\driver\driver_interface $cache, \phpbb\db\driver\driver_interface $db, \phpbb\request\request_interface $request, \phpbb\textformatter\s9e\utils $text_formatter_utils, $cblocks_table)
44
	{
45 42
		$this->cache = $cache;
46 42
		$this->db = $db;
47 42
		$this->request = $request;
48 42
		$this->text_formatter_utils = $text_formatter_utils;
49 42
		$this->cblocks_table = $cblocks_table;
50 42
	}
51
52
	/**
53
	 * {@inheritdoc}
54
	 */
55 4
	public function display(array $bdata, $edit_mode = false)
56
	{
57 4
		$cblock = $this->get_block_data($bdata['bid']);
58 4
		$content = $this->get_content_for_display($cblock);
59
60 4
		$this->show_editor($cblock, $content, $edit_mode);
61
62
		return array(
63 4
			'title'		=> 'BLOCK_TITLE',
64 4
			'content'	=> $content,
65 4
		);
66
	}
67
68
	/**
69
	 * @param int $block_id
70
	 * @return array
71
	 */
72 3
	public function edit($block_id)
73
	{
74 3
		$content = $this->request->variable('content', '', true);
75 3
		$cblocks = $this->get_custom_blocks();
76
77 3
		$sql_data =	$this->get_default_fields($block_id);
78 3
		$sql_data['block_content'] = $content;
79
80 3
		generate_text_for_storage($sql_data['block_content'], $sql_data['bbcode_uid'], $sql_data['bbcode_bitfield'], $sql_data['bbcode_options'], true, true, true);
81
82 3
		$this->save($sql_data, isset($cblocks[$block_id]));
83
84
		return array(
85 3
			'id'		=> $block_id,
86 3
			'content'	=> $this->get_content_for_display($sql_data),
87 3
			'callback'	=> 'previewCustomBlock',
88 3
		);
89
	}
90
91
	/**
92
	 * @param int $from_bid
93
	 * @param int $to_bid
94
	 * @see \blitze\sitemaker\services\blocks\action\copy_route::copy_custom_block
95
	 */
96 1
	public function copy($from_bid, $to_bid)
97
	{
98 1
		$cblocks = $this->get_custom_blocks();
99
100 1
		if (isset($cblocks[$from_bid]))
101 1
		{
102 1
			$sql_data = $cblocks[$from_bid];
103 1
			$sql_data['block_id'] = $to_bid;
104
105 1
			$this->save($sql_data, isset($cblocks[$to_bid]));
106 1
		}
107 1
	}
108
109
	/**
110
	 * @param int $bid
111
	 * @return array
112
	 */
113 4
	private function get_block_data($bid)
114
	{
115 4
		$cblock = $this->get_custom_blocks();
116
117 4
		return (isset($cblock[$bid])) ? $cblock[$bid] : $this->get_default_fields($bid);
118
	}
119
120
	/**
121
	 * @param array $data
122
	 * @return string
123
	 */
124 7
	private function get_content_for_display(array $data)
125
	{
126 7
		$content = $this->text_formatter_utils->unparse($data['block_content']);
127 7
		return htmlspecialchars_decode($content, ENT_COMPAT);
128
	}
129
130
	/**
131
	 * @param array $sql_data
132
	 * @param bool $block_exists
133
	 */
134 5
	private function save(array $sql_data, $block_exists)
135
	{
136 4
		if (!$block_exists)
137 5
		{
138 2
			$sql = 'INSERT INTO ' . $this->cblocks_table . ' ' . $this->db->sql_build_array('INSERT', $sql_data);
139 2
		}
140
		else
141
		{
142 2
			$sql = 'UPDATE ' . $this->cblocks_table . ' SET ' . $this->db->sql_build_array('UPDATE', $sql_data) . ' WHERE block_id = ' . (int) $sql_data['block_id'];
143
		}
144 4
		$this->db->sql_query($sql);
145 4
		$this->cache->destroy('pt_cblocks');
146 4
	}
147
148
	/**
149
	 * @param array $cblock
150
	 * @param string $content
151
	 * @param bool $edit_mode
152
	 * @return string
153
	 */
154 4
	private function show_editor(array $cblock, &$content, $edit_mode)
155
	{
156 4
		if ($edit_mode !== false)
157 4
		{
158 2
			decode_message($cblock['block_content'], $cblock['bbcode_uid']);
159 2
			$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>';
160 2
		}
161 4
	}
162
163
	/**
164
	 * @return array
165
	 */
166 8
	private function get_custom_blocks()
167
	{
168 8
		if (($cblocks = $this->cache->get('pt_cblocks')) === false)
169 8
		{
170
			$sql = 'SELECT *
171 8
				FROM ' . $this->cblocks_table;
172 8
			$result = $this->db->sql_query($sql);
173
174 8
			$cblocks = array();
175 8
			while ($row = $this->db->sql_fetchrow($result))
176
			{
177 8
				$cblocks[$row['block_id']] = $row;
178 8
			}
179 8
			$this->db->sql_freeresult($result);
180
181 8
			$this->cache->put('pt_cblocks', $cblocks);
182 8
		}
183
184 8
		return $cblocks;
185
	}
186
187
	/**
188
	 * @param int $bid
189
	 * @return array
190
	 */
191 5
	private function get_default_fields($bid)
192
	{
193
		return array(
194 5
			'block_id'			=> $bid,
195 5
			'block_content'		=> '',
196 5
			'bbcode_bitfield'	=> '',
197 5
			'bbcode_options'	=> 7,
198 5
			'bbcode_uid'		=> '',
199 5
		);
200
	}
201
}
202