Completed
Push — master ( 6e2144...8f0765 )
by Nazar
04:26
created

Comments::block()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 3
b 0
f 0
nc 1
nop 2
dl 0
loc 9
rs 9.6666
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
	 * Add new comment
104
	 *
105
	 * @param int    $item   Item id
106
	 * @param string $module Module name
107
	 * @param string $text   Comment text
108
	 * @param int    $parent Parent comment id
109
	 *
110
	 * @return false|int
111
	 */
112
	function add ($item, $module, $text, $parent = 0) {
113
		$L    = Language::instance();
114
		$User = User::instance();
115
		$text = xap($text, true);
116
		if (!$text) {
117
			return false;
118
		}
119
		if ($parent) {
120
			$parent_comment = $this->read($parent);
121
			if ($parent_comment['item'] != $item || $parent_comment['module'] != $module) {
122
				return false;
123
			}
124
		}
125
		$id = $this->create($parent, $module, $item, $User->id, time(), $text, $L->clang);
126
		if ($id) {
127
			$this->cache->del("$module/$item");
128
		}
129
		return $id;
130
	}
131
	/**
132
	 * Set comment text
133
	 *
134
	 * @param int    $id
135
	 * @param string $text
136
	 *
137
	 * @return bool
138
	 */
139
	function set ($id, $text) {
140
		$text = xap($text, true);
141
		if (!$text) {
142
			return false;
143
		}
144
		$comment = $this->get($id);
145
		if (!$comment) {
146
			return false;
147
		}
148
		$comment['text'] = $text;
149
		$result          = $this->update($comment);
150
		if ($result) {
151
			$this->cache->del("$comment[module]/$comment[item]");
152
		}
153
		return $result;
154
	}
155
	/**
156
	 * Delete comment
157
	 *
158
	 * @param int $id
159
	 *
160
	 * @return bool
161
	 */
162
	function del ($id) {
163
		$comment = $this->read($id);
164
		if (
165
			!$comment ||
166
			$this->search(
167
				[
168
					'parent'      => $id,
169
					'total_count' => true
170
				]
171
			)
172
		) {
173
			return false;
174
		}
175
		$result = $this->delete($id);
176
		if ($result) {
177
			$this->cache->del("$comment[module]/$comment[item]");
178
		}
179
		return $result;
180
	}
181
	/**
182
	 * Delete all comments of specified item
183
	 *
184
	 * @param int    $item   Item id
185
	 * @param string $module Module name
186
	 *
187
	 * @return bool
188
	 */
189
	function del_all ($item, $module) {
190
		$item   = (int)$item;
191
		$result = $this->db_prime()->q(
192
			"DELETE FROM `[prefix]comments`
193
			WHERE
194
				`module`	= '%s' AND
195
				`item`		= '%d'",
196
			$module,
197
			$item
198
		);
199
		if ($result) {
200
			$this->cache->del("$module/$item");
201
		}
202
		return (bool)$result;
203
	}
204
	/**
205
	 * Count of comments for specified item
206
	 *
207
	 * @param int    $item   Item id
208
	 * @param string $module Module name
209
	 *
210
	 * @return int
211
	 */
212
	function count ($item, $module) {
213
		$item = (int)$item;
214
		$L    = Language::instance();
215
		return $this->cache->get(
216
			"$module/$item/count/$L->clang",
217
			function () use ($item, $module, $L) {
218
				return $this->search(
219
					[
220
						'module'      => $module,
221
						'item'        => $item,
222
						'lang'        => $L->clang,
223
						'total_count' => true
224
					]
225
				);
226
			}
227
		);
228
	}
229
}
230