Completed
Push — master ( 41eb78...9a8234 )
by Nazar
04:02
created

blocks::get_block_by_index()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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