Completed
Push — develop ( 733603...f85365 )
by Daniel
15:29 queued 09:35
created

custom::get_default_fields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

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