Issues (1358)

modules/Blogs/Sections.php (12 issues)

1
<?php
2
/**
3
 * @package  Blogs
4
 * @category modules
5
 * @author   Nazar Mokrynskyi <[email protected]>
6
 * @license  0BSD
7
 */
8
namespace cs\modules\Blogs;
9
use
10
	cs\Cache,
11
	cs\Config,
12
	cs\Language,
13
	cs\CRUD_helpers,
14
	cs\Singleton;
15
16
/**
17
 * @method static $this instance($check = false)
18
 */
19
class Sections {
20
	use
21
		CRUD_helpers,
22
		Singleton;
23
24
	protected $data_model          = [
25
		'id'     => 'int:0',
26
		'parent' => 'int:0',
27
		'title'  => 'ml:text',
28
		'path'   => 'ml:text'
29
	];
30
	protected $table               = '[prefix]blogs_sections';
31
	protected $data_model_ml_group = 'Blogs/sections';
32
	/**
33
	 * @var Cache\Prefix
34
	 */
35
	protected $cache;
36
37
	protected function construct () {
38
		$this->cache = Cache::prefix('Blogs');
39
	}
40
	/**
41
	 * Returns database index
42
	 *
43
	 * @return int
44
	 */
45
	protected function cdb () {
46
		return Config::instance()->module('Blogs')->db('posts');
47
	}
48
	/**
49
	 * Get data of specified section
50
	 *
51
	 * @param int|int[] $id
52
	 *
53
	 * @return 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
			"sections/$id/$L->clang",
63
			function () use ($id) {
64
				$data = $this->read($id);
65
				if ($data) {
66
					$data['posts']      = Posts::instance()->get_for_section_count($id);
67
					$data['full_title'] = [$data['title']];
68
					$data['full_path']  = [$data['path']];
69
					$parent             = $data['parent'];
70
					while ($parent > 0) {
71
						$section              = $this->get($parent);
72
						$data['full_title'][] = $section['title'];
73
						$data['full_path'][]  = $section['path'];
74
						$parent               = $section['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
	 * @return array[]
85
	 */
86
	public function get_all () {
87
		$L = Language::instance();
88
		return $this->cache->get(
89
			"sections/all/$L->clang",
90
			function () {
91
				$result = $this->get(
92
					$this->search([], 1, PHP_INT_MAX, 'id', true) ?: []
93
				);
94
				usort(
95
					$result,
0 ignored issues
show
It seems like $result can also be of type false; however, parameter $array of usort() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

95
					/** @scrutinizer ignore-type */ $result,
Loading history...
96
					function ($a, $b) {
97
						return strcmp($a['full_title'], $b['full_title']);
98
					}
99
				);
100
				return $result;
101
			}
102
		) ?: [];
103
	}
104
	/**
105
	 * Get sections ids for each section in full path
106
	 *
107
	 * @param string|string[] $path
108
	 *
109
	 * @return false|int[]
110
	 */
111
	public function get_by_path ($path) {
112
		$full_path = implode('/', (array)$path);
113
		$sections  = $this->get_all();
114
		$found     = false;
115
		foreach ($sections as $section) {
116
			if ($section['full_path'] == $full_path) {
117
				$found = true;
118
			}
119
		}
120
		if (!$found) {
121
			return false;
122
		}
123
		/** @noinspection PhpUndefinedVariableInspection */
124
		$ids = [$section['id']];
125
		while ($section['parent']) {
126
			$section = $this->get($section['parent']);
127
			$ids[]   = $section['id'];
128
		}
129
		return array_reverse($ids);
130
	}
131
	/**
132
	 * Add new section
133
	 *
134
	 * @param int    $parent
135
	 * @param string $title
136
	 * @param string $path
137
	 *
138
	 * @return false|int Id of created section on success of <b>false</> on failure
139
	 */
140
	public function add ($parent, $title, $path) {
141
		$id = $this->create($parent, $title, path($path ?: $title));
0 ignored issues
show
$parent of type integer is incompatible with the type array expected by parameter $arguments of cs\modules\Blogs\Sections::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

141
		$id = $this->create(/** @scrutinizer ignore-type */ $parent, $title, path($path ?: $title));
Loading history...
$title of type string is incompatible with the type array expected by parameter $arguments of cs\modules\Blogs\Sections::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

141
		$id = $this->create($parent, /** @scrutinizer ignore-type */ $title, path($path ?: $title));
Loading history...
142
		if ($id) {
143
			$this->db_prime()->q(
144
				"UPDATE `[prefix]blogs_posts_sections`
145
				SET `section` = $id
146
				WHERE `section` = '%d'",
147
				$parent
0 ignored issues
show
$parent of type integer is incompatible with the type array expected by parameter $parameters of cs\DB\_Abstract::q(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

147
				/** @scrutinizer ignore-type */ $parent
Loading history...
148
			);
149
			unset(
150
				$this->cache->posts,
0 ignored issues
show
Bug Best Practice introduced by
The property posts does not exist on cs\Cache\Prefix. Since you implemented __get, consider adding a @property annotation.
Loading history...
151
				$this->cache->sections
0 ignored issues
show
Bug Best Practice introduced by
The property sections does not exist on cs\Cache\Prefix. Since you implemented __get, consider adding a @property annotation.
Loading history...
152
			);
153
		}
154
		return $id;
155
	}
156
	/**
157
	 * Set data of specified section
158
	 *
159
	 * @param int    $id
160
	 * @param int    $parent
161
	 * @param string $title
162
	 * @param string $path
163
	 *
164
	 * @return bool
165
	 */
166
	public function set ($id, $parent, $title, $path) {
167
		$result = $this->update($id, $parent, $title, path($path ?: $title));
0 ignored issues
show
$id of type integer is incompatible with the type array expected by parameter $arguments of cs\modules\Blogs\Sections::update(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

167
		$result = $this->update(/** @scrutinizer ignore-type */ $id, $parent, $title, path($path ?: $title));
Loading history...
$title of type string is incompatible with the type array expected by parameter $arguments of cs\modules\Blogs\Sections::update(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

167
		$result = $this->update($id, $parent, /** @scrutinizer ignore-type */ $title, path($path ?: $title));
Loading history...
168
		if ($result) {
169
			unset($this->cache->sections);
0 ignored issues
show
Bug Best Practice introduced by
The property sections does not exist on cs\Cache\Prefix. Since you implemented __get, consider adding a @property annotation.
Loading history...
170
		}
171
		return $result;
172
	}
173
	/**
174
	 * Delete specified section
175
	 *
176
	 * @param int $id
177
	 *
178
	 * @return bool
179
	 */
180
	public function del ($id) {
181
		$id      = (int)$id;
182
		$section = $this->read($id);
183
		if (!$section || !$this->delete($id)) {
184
			return false;
185
		}
186
		$new_posts_section = $this->db_prime()->qfs(
187
			"SELECT `id`
0 ignored issues
show
'SELECT `id` FROM `'....ent` = '%s' LIMIT 1' of type string is incompatible with the type string[] expected by parameter $query of cs\DB\_Abstract::qfs(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

187
			/** @scrutinizer ignore-type */ "SELECT `id`
Loading history...
188
			FROM `$this->table`
189
			WHERE `parent` = '%s'
190
			LIMIT 1",
191
			$section['parent']
192
		) ?: $section['parent'];
193
		$update            = $this->db_prime()->q(
194
			[
195
				"UPDATE `[prefix]blogs_sections`
196
				SET `parent` = '%2\$d'
197
				WHERE `parent` = '%1\$d'",
198
				"UPDATE IGNORE `[prefix]blogs_posts_sections`
199
				SET `section` = '%3\$d'
200
				WHERE `section` = '%1\$d'"
201
			],
202
			$id,
0 ignored issues
show
$id of type integer is incompatible with the type array expected by parameter $parameters of cs\DB\_Abstract::q(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

202
			/** @scrutinizer ignore-type */ $id,
Loading history...
203
			$section['parent'],
204
			$new_posts_section
0 ignored issues
show
It seems like $new_posts_section can also be of type integer and string; however, parameter $parameters of cs\DB\_Abstract::q() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

204
			/** @scrutinizer ignore-type */ $new_posts_section
Loading history...
205
		);
206
		if ($update) {
207
			$this->cache->del('/');
208
		}
209
		return $update;
210
	}
211
}
212