Completed
Push — develop ( de7659...415144 )
by Daniel
09:44
created

custom::copy()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

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