Completed
Push — master ( 758d43...d0ef0e )
by Nazar
04:37
created

blocks::admin_blocks_templates()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
/**
3
 * @package    CleverStyle CMS
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\Page,
15
	cs\Permission,
16
	cs\Text;
17
18
trait blocks {
19
	/**
20
	 * Get array of blocks data or data of specific block if id specified
21
	 *
22
	 * If block id specified - extended form of data will be returned
23
	 *
24
	 * @param \cs\Request $Request
25
	 *
26
	 * @throws ExitException
27
	 */
28
	static function admin_blocks_get ($Request) {
29
		$Config = Config::instance();
30
		$Page   = Page::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
			$Page->json(array_values($blocks) ?: []);
41
			return;
42
		}
43
		$block = static::get_block_by_index($index);
44
		if (!$block) {
45
			throw new ExitException(404);
46
		}
47
		$Page->json(
48
			[
49
				'title'    => $Text->process($db_id, $block['title'], true),
50
				'type'     => $block['type'],
51
				'active'   => (int)$block['active'],
52
				'template' => $block['template'],
53
				'start'    => date('Y-m-d\TH:i', $block['start'] ?: TIME),
54
				'expire'   => [
55
					'date'  => date('Y-m-d\TH:i', $block['expire'] ?: TIME),
56
					'state' => (int)($block['expire'] != 0)
57
				],
58
				'content'  => $Text->process($db_id, $block['content'], true)
59
			]
60
		);
61
	}
62
	/**
63
	 * Add new block
64
	 *
65
	 * @param \cs\Request $Request
66
	 *
67
	 * @throws ExitException
68
	 */
69
	static function admin_blocks_post ($Request) {
70
		static::save_block_data($Request->data);
71
	}
72
	/**
73
	 * Update block's data
74
	 *
75
	 * @param \cs\Request $Request
76
	 *
77
	 * @throws ExitException
78
	 */
79
	static function admin_blocks_put ($Request) {
80
		$index = $Request->route_ids(0);
81
		if (!$index) {
82
			throw new ExitException(400);
83
		}
84
		static::save_block_data($Request->data, $index);
85
	}
86
	/**
87
	 * Delete block
88
	 *
89
	 * @param \cs\Request $Request
90
	 *
91
	 * @throws ExitException
92
	 */
93
	static function admin_blocks_delete ($Request) {
94
		$index = $Request->route_ids(0);
95
		if (!$index) {
96
			throw new ExitException(400);
97
		}
98
		$Config     = Config::instance();
99
		$db_id      = $Config->module('System')->db('texts');
100
		$Permission = Permission::instance();
101
		$Text       = Text::instance();
102
		$found      = static::get_block_by_index($index);
103
		if ($found === false) {
104
			throw new ExitException(404);
105
		}
106
		unset($Config->components['blocks'][$found]);
107
		$block_permission = $Permission->get(null, 'Block', $index);
108
		if ($block_permission) {
109
			$Permission->del($block_permission[0]['id']);
110
		}
111
		$Text->del($db_id, 'System/Config/blocks/title', $index);
112
		$Text->del($db_id, 'System/Config/blocks/content', $index);
113
		if (!$Config->save()) {
114
			throw new ExitException(500);
115
		}
116
	}
117
	/**
118
	 * Get array of available block templates
119
	 */
120
	static function admin_blocks_templates () {
121
		Page::instance()->json(
122
			_mb_substr(get_files_list(TEMPLATES.'/blocks', '/^block\..*?\.(php|html)$/i', 'f'), 6)
123
		);
124
	}
125
	/**
126
	 * Get array of available block types
127
	 */
128
	static function admin_blocks_types () {
129
		Page::instance()->json(
130
			array_merge(['html', 'raw_html'], _mb_substr(get_files_list(BLOCKS, '/^block\..*?\.php$/i', 'f'), 6, -4))
131
		);
132
	}
133
	/**
134
	 * Update blocks order
135
	 *
136
	 * @param \cs\Request $Request
137
	 *
138
	 * @throws ExitException
139
	 */
140
	static function admin_blocks_update_order ($Request) {
141
		$order = $Request->data('order');
142
		if (!is_array($order)) {
143
			throw new ExitException(400);
144
		}
145
		$Config           = Config::instance();
146
		$blocks           = $Config->components['blocks'];
147
		$indexed_blocks   = array_combine(
148
			array_column($blocks, 'index'),
149
			$blocks
150
		);
151
		$new_blocks_order = [];
152
		$all_indexes      = [];
153
		foreach ($order as $position => $indexes) {
154
			foreach ($indexes as $index) {
155
				$all_indexes[]      = $index;
156
				$block              = $indexed_blocks[$index];
157
				$block['position']  = $position;
158
				$new_blocks_order[] = $block;
159
			}
160
		}
161
		foreach ($blocks as $block) {
162
			if (!in_array($block['index'], $all_indexes)) {
163
				$new_blocks_order[] = $block;
164
			}
165
		}
166
		$Config->components['blocks'] = $new_blocks_order;
167
		if (!$Config->save()) {
168
			throw new ExitException(500);
169
		}
170
	}
171
	/**
172
	 * @param array     $block_new
173
	 * @param false|int $index Index of existing block, if not specified - new block being added
174
	 *
175
	 * @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...
176
	 *
177
	 * @throws ExitException
178
	 */
179
	protected static function save_block_data ($block_new, $index = false) {
180
		$Config = Config::instance();
181
		$db_id  = $Config->module('System')->db('texts');
182
		$Text   = Text::instance();
183
		$block  = [
184
			'position' => 'floating',
185
			'type'     => xap($block_new['type']),
186
			'index'    => substr(TIME, 3)
187
		];
188
		if ($index) {
189
			$block = &static::get_block_by_index($index);
190
			if (!$block) {
191
				throw new ExitException(404);
192
			}
193
		}
194
		$block['title']    = $Text->set($db_id, 'System/Config/blocks/title', $block['index'], $block_new['title']);
195
		$block['active']   = $block_new['active'];
196
		$block['type']     = $block_new['type'];
197
		$block['template'] = $block_new['template'];
198
		$block['start']    = $block_new['start'];
199
		$block['start']    = strtotime($block_new['start']);
200
		$block['expire']   = $block_new['expire']['state'] ? strtotime($block_new['expire']['date']) : 0;
201
		$block['content']  = '';
202
		if ($block['type'] == 'html') {
203
			$block['content'] = $Text->set($db_id, 'System/Config/blocks/content', $block['index'], xap($block_new['content'], true));
204
		} elseif ($block['type'] == 'raw_html') {
205
			$block['content'] = $Text->set($db_id, 'System/Config/blocks/content', $block['index'], $block_new['content']);
206
		}
207
		if (!$index) {
208
			$Config->components['blocks'][] = $block;
209
			Permission::instance()->add('Block', $block['index']);
210
		}
211
		if (!$Config->save()) {
212
			throw new ExitException(500);
213
		}
214
	}
215
	/**
216
	 * @param int $index
217
	 *
218
	 * @return array|false
219
	 */
220
	protected static function &get_block_by_index ($index) {
221
		foreach (Config::instance()->components['blocks'] as &$block) {
222
			if ($block['index'] == $index) {
223
				return $block;
224
			}
225
		}
226
		return false;
227
	}
228
}
229