Completed
Push — master ( 3dd7ff...c8320e )
by Nazar
06:59
created

blocks::admin_blocks_get_prepare()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 9
c 1
b 0
f 1
nc 2
nop 3
dl 0
loc 11
ccs 0
cts 9
cp 0
crap 20
rs 9.2
1
<?php
2
/**
3
 * @package    CleverStyle Framework
4
 * @subpackage System module
5
 * @category   modules
6
 * @author     Nazar Mokrynskyi <[email protected]>
7
 * @copyright  Copyright (c) 2015-2016, Nazar Mokrynskyi
8
 * @license    MIT License, see license.txt
9
 */
10
namespace cs\modules\System\api\Controller\admin;
11
use
12
	cs\Config,
13
	cs\ExitException,
14
	cs\Permission,
15
	cs\Text;
16
17
trait blocks {
18
	/**
19
	 * Get array of blocks data or data of specific block if id specified
20
	 *
21
	 * If block id specified - extended form of data will be returned
22
	 *
23
	 * @param \cs\Request $Request
24
	 *
25
	 * @return array
26
	 *
27
	 * @throws ExitException
28
	 */
29
	public static function admin_blocks_get ($Request) {
30
		$Config = Config::instance();
31
		$Text   = Text::instance();
32
		$db_id  = $Config->module('System')->db('texts');
33
		$index  = $Request->route_ids(0);
34
		if (!$index) {
35
			/**
36
			 * @var array $blocks
37
			 */
38
			$blocks = $Config->components['blocks'];
39
			foreach ($blocks as &$block) {
40
				$block = static::admin_blocks_get_prepare($db_id, $Text, $block);
41
			}
42
			return array_values($blocks) ?: [];
43
		}
44
		$block = static::get_block_by_index($index);
45
		if (!$block) {
46
			throw new ExitException(404);
47
		}
48
		return static::admin_blocks_get_prepare($db_id, $Text, $block);
49
	}
50
	/**
51
	 * @param int   $db_id
52
	 * @param Text  $Text
53
	 * @param array $block
54
	 *
55
	 * @return array
1 ignored issue
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,array|string|integer>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
56
	 */
57
	protected static function admin_blocks_get_prepare ($db_id, $Text, $block) {
58
		$block['active']  = (int)$block['active'];
59
		$block['title']   = $Text->process($db_id, $block['title'], true);
60
		$block['content'] = $block['content'] ? $Text->process($db_id, $block['content'], true) : '';
61
		$block['start']   = date('Y-m-d\TH:i', $block['start'] ?: time());
62
		$block['expire']  = [
63
			'date'  => date('Y-m-d\TH:i', $block['expire'] ?: time()),
64
			'state' => (int)($block['expire'] != 0)
65
		];
66
		return $block;
67
	}
68
	/**
69
	 * Add new block
70
	 *
71
	 * @param \cs\Request $Request
72
	 *
73
	 * @throws ExitException
74
	 */
75
	public static function admin_blocks_post ($Request) {
76
		static::save_block_data($Request->data);
77
	}
78
	/**
79
	 * Update block's data
80
	 *
81
	 * @param \cs\Request $Request
82
	 *
83
	 * @throws ExitException
84
	 */
85
	public static function admin_blocks_put ($Request) {
86
		$index = $Request->route_ids(0);
87
		if (!$index) {
88
			throw new ExitException(400);
89
		}
90
		static::save_block_data($Request->data, $index);
91
	}
92
	/**
93
	 * Delete block
94
	 *
95
	 * @param \cs\Request $Request
96
	 *
97
	 * @throws ExitException
98
	 */
99
	public static function admin_blocks_delete ($Request) {
100
		$index = $Request->route_ids(0);
101
		if (!$index) {
102
			throw new ExitException(400);
103
		}
104
		$Config     = Config::instance();
105
		$db_id      = $Config->module('System')->db('texts');
106
		$Permission = Permission::instance();
107
		$Text       = Text::instance();
108
		$found      = false;
109
		/**
110
		 * @var array $blocks
111
		 */
112
		$blocks = $Config->components['blocks'];
113
		foreach ($blocks as $i => $block) {
114
			if ($block['index'] == $index) {
115
				unset($Config->components['blocks'][$i]);
116
				$found = $i;
117
				break;
118
			}
119
		}
120
		if ($found === false) {
121
			throw new ExitException(404);
122
		}
123
		$block_permission = $Permission->get(null, 'Block', $index);
124
		if ($block_permission) {
125
			$Permission->del($block_permission[0]['id']);
126
		}
127
		$Text->del($db_id, 'System/Config/blocks/title', $index);
128
		$Text->del($db_id, 'System/Config/blocks/content', $index);
129
		if (!$Config->save()) {
130
			throw new ExitException(500);
131
		}
132
	}
133
	/**
134
	 * Get array of available block types
135
	 */
136
	public static function admin_blocks_types () {
137
		return array_merge(['html', 'raw_html'], _mb_substr(get_files_list(BLOCKS, '/^block\..*?\.php$/i', 'f'), 6, -4));
138
	}
139
	/**
140
	 * Update blocks order
141
	 *
142
	 * @param \cs\Request $Request
143
	 *
144
	 * @throws ExitException
145
	 */
146
	public static function admin_blocks_update_order ($Request) {
147
		$order = $Request->data('order');
148
		if (!is_array($order)) {
149
			throw new ExitException(400);
150
		}
151
		$Config = Config::instance();
152
		/**
153
		 * @var array $blocks
154
		 */
155
		$blocks           = $Config->components['blocks'];
156
		$indexed_blocks   = array_combine(
157
			array_column($blocks, 'index'),
158
			$blocks
159
		);
160
		$new_blocks_order = [];
161
		$all_indexes      = [];
162
		/**
163
		 * @var array[] $order
164
		 */
165
		foreach ($order as $position => $indexes) {
166
			foreach ($indexes as $index) {
167
				$all_indexes[]      = $index;
168
				$block              = $indexed_blocks[$index];
169
				$block['position']  = $position;
170
				$new_blocks_order[] = $block;
171
			}
172
		}
173
		foreach ($blocks as $block) {
174
			if (!in_array($block['index'], $all_indexes)) {
175
				$new_blocks_order[] = $block;
176
			}
177
		}
178
		$Config->components['blocks'] = $new_blocks_order;
179
		if (!$Config->save()) {
180
			throw new ExitException(500);
181
		}
182
	}
183
	/**
184
	 * @param array     $block_new
185
	 * @param false|int $index Index of existing block, if not specified - new block being added
186
	 *
187
	 * @throws ExitException
188
	 */
189
	protected static function save_block_data ($block_new, $index = false) {
190
		$Config = Config::instance();
191
		$db_id  = $Config->module('System')->db('texts');
192
		$Text   = Text::instance();
193
		$block  = [
194
			'position' => 'floating',
195
			'type'     => xap($block_new['type']),
196
			'index'    => intval(substr(round(microtime(true) * 1000), 3), 10)
197
		];
198
		if ($index) {
199
			$block = &static::get_block_by_index($index);
200
			if (!$block) {
201
				throw new ExitException(404);
202
			}
203
		}
204
		$block['title']   = $Text->set($db_id, 'System/Config/blocks/title', $block['index'], $block_new['title']);
205
		$block['active']  = $block_new['active'];
206
		$block['type']    = $block_new['type'];
207
		$block['start']   = $block_new['start'];
208
		$block['start']   = strtotime($block_new['start']);
209
		$block['expire']  = $block_new['expire']['state'] ? strtotime($block_new['expire']['date']) : 0;
210
		$block['content'] = '';
211
		if ($block['type'] == 'html') {
212
			$block['content'] = $Text->set($db_id, 'System/Config/blocks/content', $block['index'], xap($block_new['content'], true));
213
		} elseif ($block['type'] == 'raw_html') {
214
			$block['content'] = $Text->set($db_id, 'System/Config/blocks/content', $block['index'], $block_new['content']);
215
		}
216
		if (!$index) {
217
			$Config->components['blocks'][] = $block;
218
			Permission::instance()->add('Block', $block['index']);
219
		}
220
		if (!$Config->save()) {
221
			throw new ExitException(500);
222
		}
223
	}
224
	/**
225
	 * @param int $index
226
	 *
227
	 * @return array|false
228
	 */
229
	protected static function &get_block_by_index ($index) {
230
		/**
231
		 * @var array $blocks
232
		 */
233
		$blocks = Config::instance()->components['blocks'];
234
		foreach ($blocks as &$block) {
235
			if ($block['index'] == $index) {
236
				return $block;
237
			}
238
		}
239
		$false = false;
240
		return $false;
241
	}
242
}
243