Completed
Push — master ( 276fbe...eb73da )
by Nazar
05:10
created

Comments::get_for_module_item_count()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 8
rs 9.4285
1
<?php
2
/**
3
 * @package   Comments
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\Comments;
10
use
11
	h,
12
	cs\Cache,
13
	cs\Config,
14
	cs\Language,
15
	cs\Request,
16
	cs\User,
17
	cs\CRUD_helpers,
18
	cs\Singleton;
19
20
/**
21
 * @method static $this instance($check = false)
22
 */
23
class Comments {
24
	use
25
		CRUD_helpers,
26
		Singleton;
27
28
	/**
29
	 * @var Cache\Prefix
30
	 */
31
	protected $cache;
32
33
	protected $data_model = [
34
		'id'     => 'int:1',
35
		'parent' => 'int:0',
36
		'module' => 'text',
37
		'item'   => 'int:1',
38
		'user'   => 'int:1',
39
		'date'   => 'int:1',
40
		'text'   => 'html',
41
		'lang'   => 'text'
42
	];
43
44
	protected $table = '[prefix]comments';
45
46
	protected function construct () {
47
		$this->cache = Cache::prefix('Comments');
48
	}
49
	/**
50
	 * Returns database index
51
	 *
52
	 * @return int
53
	 */
54
	protected function cdb () {
55
		return Config::instance()->module('Comments')->db('comments');
56
	}
57
	/**
58
	 * Get comment data
59
	 *
60
	 * @param int|int[] $id
61
	 *
62
	 * @return array|false
63
	 */
64
	function get ($id) {
65
		return $this->read($id);
66
	}
67
	/**
68
	 * Get comment data with user details and formatted date
69
	 *
70
	 * @param int|int[] $id
71
	 *
72
	 * @return array|false
73
	 */
74
	function get_extended ($id) {
75
		if (is_array($id)) {
76
			foreach ($id as &$i) {
77
				$i = $this->get_extended($i);
78
			}
79
			return $id;
80
		}
81
		$comment                   = $this->get($id);
82
		$profile                   = User::instance()->get(['username', 'avatar'], $comment['user']);
83
		$comment['username']       = $profile['username'];
84
		$comment['avatar']         = $profile['avatar'];
85
		$comment['date_formatted'] = date(Language::instance()->_datetime, $comment['date']);
86
		$comment['time_formatted'] = date(Language::instance()->_datetime, $comment['date']);
87
		return $comment;
88
	}
89
	/**
90
	 * @param string $module
91
	 * @param int    $item
92
	 *
93
	 * @return int[]
94
	 */
95
	function get_for_module_item ($module, $item) {
96
		$search_parameters = [
97
			'module' => $module,
98
			'item'   => $item
99
		];
100
		return $this->search($search_parameters, 1, PHP_INT_MAX, 'id', true) ?: [];
101
	}
102
	/**
103
	 * @param string $module
104
	 * @param int    $item
105
	 *
106
	 * @return int
1 ignored issue
show
Documentation introduced by
Should the return type not be false|integer|array|double|string? Also, consider making the array more specific, something like array<String>, or String[].

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.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
107
	 */
108
	function get_for_module_item_count ($module, $item) {
109
		$search_parameters = [
110
			'module'      => $module,
111
			'item'        => $item,
112
			'total_count' => true
113
		];
114
		return $this->search($search_parameters);
115
	}
116
	/**
117
	 * Add new comment
118
	 *
119
	 * @param int    $item   Item id
120
	 * @param string $module Module name
121
	 * @param string $text   Comment text
122
	 * @param int    $parent Parent comment id
123
	 *
124
	 * @return false|int
125
	 */
126
	function add ($item, $module, $text, $parent = 0) {
127
		$L    = Language::instance();
128
		$User = User::instance();
129
		$text = xap($text, true);
130
		if (!$text) {
131
			return false;
132
		}
133
		if ($parent) {
134
			$parent_comment = $this->read($parent);
135
			if ($parent_comment['item'] != $item || $parent_comment['module'] != $module) {
136
				return false;
137
			}
138
		}
139
		$id = $this->create($parent, $module, $item, $User->id, time(), $text, $L->clang);
140
		if ($id) {
141
			$this->cache->del("$module/$item");
142
		}
143
		return $id;
144
	}
145
	/**
146
	 * Set comment text
147
	 *
148
	 * @param int    $id
149
	 * @param string $text
150
	 *
151
	 * @return bool
152
	 */
153
	function set ($id, $text) {
154
		$text = xap($text, true);
155
		if (!$text) {
156
			return false;
157
		}
158
		$comment = $this->get($id);
159
		if (!$comment) {
160
			return false;
161
		}
162
		$comment['text'] = $text;
163
		$result          = $this->update($comment);
164
		if ($result) {
165
			$this->cache->del("$comment[module]/$comment[item]");
166
		}
167
		return $result;
168
	}
169
	/**
170
	 * Delete comment
171
	 *
172
	 * @param int $id
173
	 *
174
	 * @return bool
175
	 */
176
	function del ($id) {
177
		$comment = $this->read($id);
178
		if (
179
			!$comment ||
180
			$this->search(
181
				[
182
					'parent'      => $id,
183
					'total_count' => true
184
				]
185
			)
186
		) {
187
			return false;
188
		}
189
		$result = $this->delete($id);
190
		if ($result) {
191
			$this->cache->del("$comment[module]/$comment[item]");
192
		}
193
		return $result;
194
	}
195
	/**
196
	 * Delete all comments of specified item
197
	 *
198
	 * @param int    $item   Item id
199
	 * @param string $module Module name
200
	 *
201
	 * @return bool
202
	 */
203
	function del_all ($item, $module) {
204
		$item   = (int)$item;
205
		$result = $this->db_prime()->q(
206
			"DELETE FROM `[prefix]comments`
207
			WHERE
208
				`module`	= '%s' AND
209
				`item`		= '%d'",
210
			$module,
211
			$item
212
		);
213
		if ($result) {
214
			$this->cache->del("$module/$item");
215
		}
216
		return (bool)$result;
217
	}
218
}
219