Completed
Push — master ( 8fec5f...ecf1c5 )
by Nazar
04:39
created

blocks::admin_blocks_templates()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
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
	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
			$blocks = $Config->components['blocks'];
36
			foreach ($blocks as &$block) {
37
				$block['title']   = $Text->process($db_id, $block['title'], true);
38
				$block['content'] = $block['content'] ? $Text->process($db_id, $block['content'], true) : '';
39
			}
40
			return array_values($blocks) ?: [];
41
		}
42
		$block = static::get_block_by_index($index);
43
		if (!$block) {
44
			throw new ExitException(404);
45
		}
46
		return [
47
			'title'    => $Text->process($db_id, $block['title'], true),
48
			'type'     => $block['type'],
49
			'active'   => (int)$block['active'],
50
			'start'    => date('Y-m-d\TH:i', $block['start'] ?: TIME),
51
			'expire'   => [
52
				'date'  => date('Y-m-d\TH:i', $block['expire'] ?: TIME),
53
				'state' => (int)($block['expire'] != 0)
54
			],
55
			'content'  => $Text->process($db_id, $block['content'], true)
56
		];
57
	}
58
	/**
59
	 * Add new block
60
	 *
61
	 * @param \cs\Request $Request
62
	 *
63
	 * @throws ExitException
64
	 */
65
	static function admin_blocks_post ($Request) {
66
		static::save_block_data($Request->data);
67
	}
68
	/**
69
	 * Update block's data
70
	 *
71
	 * @param \cs\Request $Request
72
	 *
73
	 * @throws ExitException
74
	 */
75
	static function admin_blocks_put ($Request) {
76
		$index = $Request->route_ids(0);
77
		if (!$index) {
78
			throw new ExitException(400);
79
		}
80
		static::save_block_data($Request->data, $index);
81
	}
82
	/**
83
	 * Delete block
84
	 *
85
	 * @param \cs\Request $Request
86
	 *
87
	 * @throws ExitException
88
	 */
89
	static function admin_blocks_delete ($Request) {
90
		$index = $Request->route_ids(0);
91
		if (!$index) {
92
			throw new ExitException(400);
93
		}
94
		$Config     = Config::instance();
95
		$db_id      = $Config->module('System')->db('texts');
96
		$Permission = Permission::instance();
97
		$Text       = Text::instance();
98
		$found      = false;
99
		foreach ($Config->components['blocks'] as $i => $block) {
100
			if ($block['index'] == $index) {
101
				unset($Config->components['blocks'][$i]);
102
				$found = $i;
103
				break;
104
			}
105
		}
106
		if ($found === false) {
107
			throw new ExitException(404);
108
		}
109
		$block_permission = $Permission->get(null, 'Block', $index);
110
		if ($block_permission) {
111
			$Permission->del($block_permission[0]['id']);
112
		}
113
		$Text->del($db_id, 'System/Config/blocks/title', $index);
114
		$Text->del($db_id, 'System/Config/blocks/content', $index);
115
		if (!$Config->save()) {
116
			throw new ExitException(500);
117
		}
118
	}
119
	/**
120
	 * Get array of available block types
121
	 */
122
	static function admin_blocks_types () {
123
		return array_merge(['html', 'raw_html'], _mb_substr(get_files_list(BLOCKS, '/^block\..*?\.php$/i', 'f'), 6, -4));
124
	}
125
	/**
126
	 * Update blocks order
127
	 *
128
	 * @param \cs\Request $Request
129
	 *
130
	 * @throws ExitException
131
	 */
132
	static function admin_blocks_update_order ($Request) {
133
		$order = $Request->data('order');
134
		if (!is_array($order)) {
135
			throw new ExitException(400);
136
		}
137
		$Config           = Config::instance();
138
		$blocks           = $Config->components['blocks'];
139
		$indexed_blocks   = array_combine(
140
			array_column($blocks, 'index'),
141
			$blocks
142
		);
143
		$new_blocks_order = [];
144
		$all_indexes      = [];
145
		foreach ($order as $position => $indexes) {
146
			foreach ($indexes as $index) {
147
				$all_indexes[]      = $index;
148
				$block              = $indexed_blocks[$index];
149
				$block['position']  = $position;
150
				$new_blocks_order[] = $block;
151
			}
152
		}
153
		foreach ($blocks as $block) {
154
			if (!in_array($block['index'], $all_indexes)) {
155
				$new_blocks_order[] = $block;
156
			}
157
		}
158
		$Config->components['blocks'] = $new_blocks_order;
159
		if (!$Config->save()) {
160
			throw new ExitException(500);
161
		}
162
	}
163
	/**
164
	 * @param array     $block_new
165
	 * @param false|int $index Index of existing block, if not specified - new block being added
166
	 *
167
	 * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
168
	 *
169
	 * @throws ExitException
170
	 */
171
	protected static function save_block_data ($block_new, $index = false) {
172
		$Config = Config::instance();
173
		$db_id  = $Config->module('System')->db('texts');
174
		$Text   = Text::instance();
175
		$block  = [
176
			'position' => 'floating',
177
			'type'     => xap($block_new['type']),
178
			'index'    => substr(TIME, 3)
179
		];
180
		if ($index) {
181
			$block = &static::get_block_by_index($index);
182
			if (!$block) {
183
				throw new ExitException(404);
184
			}
185
		}
186
		$block['title']    = $Text->set($db_id, 'System/Config/blocks/title', $block['index'], $block_new['title']);
187
		$block['active']   = $block_new['active'];
188
		$block['type']     = $block_new['type'];
189
		$block['start']    = $block_new['start'];
190
		$block['start']    = strtotime($block_new['start']);
191
		$block['expire']   = $block_new['expire']['state'] ? strtotime($block_new['expire']['date']) : 0;
192
		$block['content']  = '';
193
		if ($block['type'] == 'html') {
194
			$block['content'] = $Text->set($db_id, 'System/Config/blocks/content', $block['index'], xap($block_new['content'], true));
195
		} elseif ($block['type'] == 'raw_html') {
196
			$block['content'] = $Text->set($db_id, 'System/Config/blocks/content', $block['index'], $block_new['content']);
197
		}
198
		if (!$index) {
199
			$Config->components['blocks'][] = $block;
200
			Permission::instance()->add('Block', $block['index']);
201
		}
202
		if (!$Config->save()) {
203
			throw new ExitException(500);
204
		}
205
	}
206
	/**
207
	 * @param int $index
208
	 *
209
	 * @return array|false
210
	 */
211
	protected static function &get_block_by_index ($index) {
212
		foreach (Config::instance()->components['blocks'] as &$block) {
213
			if ($block['index'] == $index) {
214
				return $block;
215
			}
216
		}
217
		$false = false;
218
		return $false;
219
	}
220
}
221