Completed
Push — master ( b07195...7e4a95 )
by Nazar
05:05 queued 21s
created

Pages::del()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package   Static Pages
4
 * @category  modules
5
 * @author    Nazar Mokrynskyi <[email protected]>
6
 * @copyright Copyright (c) 2011-2016, Nazar Mokrynskyi
7
 * @license   MIT License, see license.txt
8
 */
9
namespace cs\modules\Static_pages;
10
use
11
	cs\CRUD_helpers,
12
	cs\Singleton,
13
	cs\Cache\Prefix,
14
	cs\Config,
15
	cs\Language;
16
17
/**
18
 * @method static $this instance($check = false)
19
 */
20
class Pages {
21
	use
22
		CRUD_helpers,
23
		Singleton;
24
	protected $data_model                  = [
25
		'id'        => 'int',
26
		'category'  => 'int',
27
		'title'     => 'ml:text',
28
		'path'      => 'ml:text',
29
		'content'   => 'ml:',
30
		'interface' => 'int:0..1'
31
	];
32
	protected $table                       = '[prefix]static_pages';
33
	protected $data_model_ml_group         = 'Static_pages/pages';
34
	protected $data_model_files_tag_prefix = 'Static_pages/pages';
35
	/**
36
	 * @var Prefix
37
	 */
38
	protected $cache;
39
40
	protected function construct () {
41
		$this->cache = new Prefix('Static_pages');
42
	}
43
	/**
44
	 * Returns database index
45
	 *
46
	 * @return int
47
	 */
48
	protected function cdb () {
49
		return Config::instance()->module('Static_pages')->db('pages');
50
	}
51
	/**
52
	 * Get data of specified page
53
	 *
54
	 * @param int|int[] $id
55
	 *
56
	 * @return array|array|false
57
	 */
58
	public function get ($id) {
59
		if (is_array($id)) {
60
			return array_map([$this, 'get'], $id);
61
		}
62
		$L  = Language::instance();
63
		$id = (int)$id;
64
		return $this->cache->get(
65
			"pages/$id/$L->clang",
66
			function () use ($id) {
67
				$data = $this->read($id);
68
				if ($data) {
69
					if ($data['category']) {
70
						$category          = Categories::instance()->get($data['category']);
71
						$data['full_path'] = "$category[full_path]/$data[path]";
72
					} else {
73
						$data['full_path'] = $data['path'];
74
					}
75
				}
76
				return $data;
77
			}
78
		);
79
	}
80
	/**
81
	 * Get pages for category
82
	 *
83
	 * @param int $category
84
	 *
85
	 * @return int[]
86
	 */
87
	public function get_for_category ($category) {
88
		$search_parameters = [
89
			'category' => $category
90
		];
91
		return $this->search($search_parameters) ?: [];
92
	}
93
	/**
94
	 * Get number of pages for category
95
	 *
96
	 * @param int $category
97
	 *
98
	 * @return int
99
	 */
100
	public function get_for_category_count ($category) {
101
		$search_parameters = [
102
			'category'    => $category,
103
			'total_count' => true
104
		];
105
		return $this->search($search_parameters);
106
	}
107
	/**
108
	 * Add new page
109
	 *
110
	 * @param int    $category
111
	 * @param string $title
112
	 * @param string $path
113
	 * @param string $content
114
	 * @param int    $interface
115
	 *
116
	 * @return false|int Id of created page on success of <b>false</> on failure
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|string|false?

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...
117
	 */
118
	public function add ($category, $title, $path, $content, $interface) {
119
		$id = $this->create($category, $title, path($path ?: $title), $content, $interface);
120
		if ($id) {
121
			unset($this->cache->{'/'});
122
		}
123
		return $id;
124
	}
125
	/**
126
	 * Set data of specified page
127
	 *
128
	 * @param int    $id
129
	 * @param int    $category
130
	 * @param string $title
131
	 * @param string $path
132
	 * @param string $content
133
	 * @param int    $interface
134
	 *
135
	 * @return bool
136
	 */
137
	public function set ($id, $category, $title, $path, $content, $interface) {
138
		$result = $this->update($id, $category, $title, path($path ?: $title), $content, $interface);
139
		if ($result) {
140
			$Cache = $this->cache;
141
			unset(
142
				$Cache->structure,
143
				$Cache->{"pages/$id"}
144
			);
145
		}
146
		return $result;
147
	}
148
	/**
149
	 * Delete specified page
150
	 *
151
	 * @param int $id
152
	 *
153
	 * @return bool
154
	 */
155
	public function del ($id) {
156
		$result = $this->delete($id);
157
		if ($result) {
158
			$Cache = $this->cache;
159
			unset(
160
				$Cache->structure,
161
				$Cache->{"pages/$id"}
162
			);
163
		}
164
		return $result;
165
	}
166
	/**
167
	 * Get array of pages structure
168
	 *
169
	 * @return array|false
170
	 */
171
	public function get_structure () {
172
		$L = Language::instance();
173
		return $this->cache->get(
174
			"structure/$L->clang",
175
			function () {
176
				return $this->get_structure_internal();
177
			}
178
		);
179
	}
180
	private function get_structure_internal ($parent = 0) {
181
		$Categories = Categories::instance();
182
		$structure  = ['id' => $parent];
183
		if ($parent != 0) {
184
			$structure = array_merge(
185
				$structure,
186
				$Categories->get($parent)
187
			);
188
		}
189
		$pages              = $this->db()->qfas(
190
			"SELECT `id`
191
			FROM `$this->table`
192
			WHERE `category` = '%s'",
193
			$parent
194
		) ?: [];
195
		$structure['pages'] = [];
196
		foreach ($pages as $id) {
197
			$structure['pages'][$this->get($id)['path']] = $id;
198
		}
199
		unset($pages);
200
		$categories              = $this->db()->qfa(
201
			"SELECT
202
				`id`,
203
				`path`
204
			FROM `{$this->table}_categories`
205
			WHERE `parent` = '%s'",
206
			$parent
207
		) ?: [];
208
		$structure['categories'] = [];
209
		foreach ($categories as $category) {
210
			$structure['categories'][$category['path']] = $this->get_structure_internal($category['id']);
211
		}
212
		return $structure;
213
	}
214
}
215