Completed
Push — master ( 825b72...88de5a )
by Nazar
05:05
created

Categories::get_all()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 21
nc 2
nop 0
dl 0
loc 31
rs 8.5806
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\Cache\Prefix,
13
	cs\Config,
14
	cs\Language,
15
	cs\Singleton;
16
17
/**
18
 * @method static $this instance($check = false)
19
 */
20
class Categories {
21
	use
22
		CRUD_helpers,
23
		Singleton;
24
	protected $data_model          = [
25
		'id'     => 'int',
26
		'title'  => 'ml:text',
27
		'path'   => 'ml:text',
28
		'parent' => 'int'
29
	];
30
	protected $table               = '[prefix]static_pages_categories';
31
	protected $data_model_ml_group = 'Static_pages/categories';
32
	/**
33
	 * @var Prefix
34
	 */
35
	protected $cache;
36
37
	protected function construct () {
38
		$this->cache = new Prefix('Static_pages');
39
	}
40
	/**
41
	 * Returns database index
42
	 *
43
	 * @return int
44
	 */
45
	protected function cdb () {
46
		return Config::instance()->module('Static_pages')->db('pages');
47
	}
48
	/**
49
	 * Get data of specified category
50
	 *
51
	 * @param int|int[] $id
52
	 *
53
	 * @return array|array[]|false
54
	 */
55
	public function get ($id) {
56
		if (is_array($id)) {
57
			return array_map([$this, 'get'], $id);
58
		}
59
		$L  = Language::instance();
60
		$id = (int)$id;
61
		return $this->cache->get(
62
			"categories/$id/$L->clang",
63
			function () use ($id) {
64
				$data = $this->read($id);
65
				if ($data) {
66
					$data['pages']      = Pages::instance()->get_for_category_count($id);
67
					$data['full_title'] = [$data['title']];
68
					$data['full_path']  = [$data['path']];
69
					$parent             = $data['parent'];
70
					while ($parent > 0) {
71
						$category             = $this->get($parent);
72
						$data['full_title'][] = $category['title'];
73
						$data['full_path'][]  = $category['path'];
74
						$parent               = $category['parent'];
75
					}
76
					$data['full_title'] = implode(' :: ', array_reverse($data['full_title']));
77
					$data['full_path']  = implode('/', array_reverse($data['full_path']));
78
				}
79
				return $data;
80
			}
81
		);
82
	}
83
	/**
84
	 * Get data all categories
85
	 *
86
	 * @return array[]
87
	 */
88
	public function get_all () {
89
		$L = Language::instance();
90
		return $this->cache->get(
91
			"categories/all/$L->clang",
92
			function () use ($L) {
93
				$result = $this->get(
94
					$this->search([], 1, PHP_INT_MAX, 'id', true) ?: []
95
				);
96
				if ($result) {
97
					usort(
98
						$result,
99
						function ($a, $b) {
100
							return strcmp($a['full_title'], $b['full_title']);
101
						}
102
					);
103
					array_unshift(
104
						$result,
105
						[
106
							'id'         => 0,
107
							'title'      => $L->static_pages_root_category,
108
							'full_title' => $L->static_pages_root_category,
109
							'path'       => '',
110
							'full_path'  => '',
111
							'pages'      => Pages::instance()->get_for_category_count(0)
112
						]
113
					);
114
				}
115
				return $result;
116
			}
117
		) ?: [];
118
	}
119
	/**
120
	 * Add new category
121
	 *
122
	 * @param int    $parent
123
	 * @param string $title
124
	 * @param string $path
125
	 *
126
	 * @return false|int Id of created category on success of `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...
127
	 */
128
	public function add ($parent, $title, $path) {
129
		$id = $this->create($title, path($path ?: $title), $parent);
130
		if ($id) {
131
			unset($this->cache->structure, $this->cache->categories);
132
		}
133
		return $id;
134
	}
135
	/**
136
	 * Set data of specified category
137
	 *
138
	 * @param int    $id
139
	 * @param int    $parent
140
	 * @param string $title
141
	 * @param string $path
142
	 *
143
	 * @return bool
144
	 */
145
	public function set ($id, $parent, $title, $path) {
146
		$result = $this->update($id, $title, path($path ?: $title), $parent);
147
		if ($result) {
148
			unset($this->cache->structure, $this->cache->categories);
149
		}
150
		return $result;
151
	}
152
	/**
153
	 * Delete specified category
154
	 *
155
	 * @param int|int[] $id
156
	 *
157
	 * @return bool
158
	 */
159
	public function del ($id) {
160
		$result = $this->delete($id);
161
		if ($result) {
162
			$this->cache->del('/');
163
		}
164
		return $result;
165
	}
166
}
167