• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

dragomano / Light-Portal / 19949548504

05 Dec 2025 01:26AM UTC coverage: 51.562% (+0.5%) from 51.086%
19949548504

push

github

web-flow
Merge pull request #313 from dragomano/fixes

Update to 3.0 beta 2

15 of 56 new or added lines in 15 files covered. (26.79%)

4 existing lines in 2 files now uncovered.

5660 of 10977 relevant lines covered (51.56%)

4.73 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.3
/src/Sources/LightPortal/Repositories/CommentRepository.php
1
<?php declare(strict_types=1);
2

3
/**
4
 * @package Light Portal
5
 * @link https://dragomano.ru/mods/light-portal
6
 * @author Bugo <bugo@dragomano.ru>
7
 * @copyright 2019-2025 Bugo
8
 * @license https://spdx.org/licenses/GPL-3.0-or-later.html GPL-3.0-or-later
9
 *
10
 * @version 3.0
11
 */
12

13
namespace LightPortal\Repositories;
14

15
use Bugo\Compat\Config;
16
use Bugo\Compat\ErrorHandler;
17
use Bugo\Compat\Lang;
18
use Exception;
19
use Laminas\Db\Sql\Predicate\Expression;
20
use LightPortal\Enums\NotifyType;
21
use LightPortal\Utils\Avatar;
22
use LightPortal\Utils\Setting;
23

24
if (! defined('SMF'))
1✔
25
        die('No direct access...');
×
26

27
final class CommentRepository extends AbstractRepository implements CommentRepositoryInterface
28
{
29
        protected string $entity = 'comment';
30

31
        public function getAll(
32
                int $start,
33
                int $limit,
34
                string $sort,
35
                string $filter = '',
36
                array $whereConditions = []
37
        ): array
38
        {
39
                return $this->getByPageId();
×
40
        }
41

42
        public function getTotalCount(string $filter = '', array $whereConditions = []): int
43
        {
44
                $select = $this->sql->select()
×
45
                        ->from(['com' => 'lp_comments'])
×
46
                        ->columns(['id']);
×
47

48
                $this->addTranslationJoins($select);
×
49

50
                if ($whereConditions) {
×
51
                        $select->where($whereConditions);
×
52
                }
53

54
                $countSelect = $this->sql->select()
×
55
                        ->from(['sub' => $select])
×
56
                        ->columns(['count' => new Expression('COUNT(*)')]);
×
57

58
                $result = $this->sql->execute($countSelect)->current();
×
59

60
                return (int) $result['count'];
×
61
        }
62

63
        public function getData(int $item): array
64
        {
65
                if ($item === 0) {
2✔
66
                        return [];
×
67
                }
68

69
                $select = $this->sql->select()
2✔
70
                        ->from(['com' => 'lp_comments'])
2✔
71
                        ->join(
2✔
72
                                ['mem' => 'members'],
2✔
73
                                'com.author_id = mem.id_member',
2✔
74
                                ['author_name' => 'real_name']
2✔
75
                        )
2✔
76
                        ->where(['com.id = ?' => $item]);
2✔
77

78
                $this->addParamJoins($select, [
2✔
79
                        'primary' => 'com.id',
2✔
80
                        'entity'  => $this->entity,
2✔
81
                ]);
2✔
82

83
                $this->addTranslationJoins($select, [
2✔
84
                        'primary' => 'com.id',
2✔
85
                        'entity'  => $this->entity,
2✔
86
                        'fields'  => ['content'],
2✔
87
                ]);
2✔
88

89
                $result = $this->sql->execute($select);
2✔
90

91
                foreach ($result as $row) {
2✔
92
                        Lang::censorText($row['content']);
1✔
93

94
                        $data ??= [
1✔
95
                                'id'         => $row['id'],
1✔
96
                                'page_id'    => $row['page_id'],
1✔
97
                                'parent_id'  => $row['parent_id'],
1✔
98
                                'message'    => htmlspecialchars_decode($row['content']),
1✔
99
                                'created_at' => $row['created_at'],
1✔
100
                                'updated_at' => $row['updated_at'],
1✔
101
                                'can_edit'   => $this->isCanEdit($row['created_at']),
1✔
102
                                'poster'     => [
1✔
103
                                        'id'     => $row['author_id'],
1✔
104
                                        'name'   => $row['author_name'],
1✔
105
                                        'avatar' => Avatar::get($row['author_id']),
1✔
106
                                ],
1✔
107
                        ];
1✔
108

109
                        if (isset($row['name'])) {
1✔
110
                                $data['params'][$row['name']] = $row['value'];
×
111
                        }
112
                }
113

114
                return $data ?? [];
2✔
115
        }
116

117
        public function getByPageId(int $id = 0): array
118
        {
119
                $sorts = [
3✔
120
                        'created_at',
3✔
121
                        'created_at DESC',
3✔
122
                        'updated_at',
3✔
123
                        'updated_at DESC',
3✔
124
                ];
3✔
125

126
                $select = $this->sql->select()
3✔
127
                        ->from(['com' => 'lp_comments'])
3✔
128
                        ->join(
3✔
129
                                ['mem' => 'members'],
3✔
130
                                'com.author_id = mem.id_member',
3✔
131
                                ['author_name' => 'real_name']
3✔
132
                        )
3✔
133
                        ->order($sorts[Config::$modSettings['lp_comment_sorting'] ?? 0]);
3✔
134

135
                if ($id > 0) {
3✔
136
                        $select->where(['com.page_id = ?' => $id]);
3✔
137
                }
138

139
                $this->addParamJoins($select, [
3✔
140
                        'primary' => 'com.id',
3✔
141
                        'entity'  => $this->entity,
3✔
142
                ]);
3✔
143

144
                $this->addTranslationJoins($select, [
3✔
145
                        'primary' => 'com.id',
3✔
146
                        'entity'  => $this->entity,
3✔
147
                        'fields'  => ['content'],
3✔
148
                ]);
3✔
149

150
                $select->where($this->getTranslationFilter('com', 'id', ['content'], 'comment'));
3✔
151

152
                $result = $this->sql->execute($select);
3✔
153

154
                $comments = [];
3✔
155
                foreach ($result as $row) {
3✔
156
                        Lang::censorText($row['content']);
3✔
157

158
                        $comments[$row['id']] = [
3✔
159
                                'id'         => $row['id'],
3✔
160
                                'page_id'    => $row['page_id'],
3✔
161
                                'parent_id'  => $row['parent_id'],
3✔
162
                                'message'    => htmlspecialchars_decode($row['content']),
3✔
163
                                'created_at' => $row['created_at'],
3✔
164
                                'updated_at' => $row['updated_at'],
3✔
165
                                'can_edit'   => $this->isCanEdit($row['created_at']),
3✔
166
                                'poster'     => [
3✔
167
                                        'id'   => $row['author_id'],
3✔
168
                                        'name' => $row['author_name'],
3✔
169
                                ],
3✔
170
                        ];
3✔
171

172
                        if (isset($row['name'])) {
3✔
173
                                $comments[$row['id']]['params'][$row['name']] = $row['value'];
×
174
                        }
175
                }
176

177
                return Avatar::getWithItems($comments, 'poster');
3✔
178
        }
179

180
        public function save(array $data): int
181
        {
182
                $insert = $this->sql->insert('lp_comments', 'id')
1✔
183
                        ->values([
1✔
184
                                'parent_id'  => $data['parent_id'],
1✔
185
                                'page_id'    => $data['page_id'],
1✔
186
                                'author_id'  => $data['author_id'],
1✔
187
                                'created_at' => $data['created_at'],
1✔
188
                        ]);
1✔
189

190
                $result = $this->sql->execute($insert);
1✔
191

192
                $item = (int) $result->getGeneratedValue();
1✔
193

194
                if ($item && ! empty($data['message'])) {
1✔
195
                        $data['content'] = $data['message'];
1✔
196
                        $data['id'] = $item;
1✔
197

198
                        $this->saveTranslations($data);
1✔
199
                }
200

201
                return $item;
1✔
202
        }
203

204
        public function update(array $data): void
205
        {
206
                $update = $this->sql->update('lp_comments')
1✔
207
                        ->set(['updated_at' => time()])
1✔
208
                        ->where([
1✔
209
                                'id = ?'        => $data['id'],
1✔
210
                                'author_id = ?' => $data['user']
1✔
211
                        ]);
1✔
212

213
                $this->sql->execute($update);
1✔
214

215
                if (! empty($data['message'])) {
1✔
216
                        $data['content'] = $data['message'];
1✔
217

218
                        $this->saveTranslations($data, true);
1✔
219
                }
220
        }
221

222
        public function remove(mixed $items, bool $withResponse = false): void
223
        {
224
                $items = (array) $items;
1✔
225

226
                if ($items === [])
1✔
227
                        return;
×
228

229
                $select = $this->sql->select('lp_comments')->columns(['id', 'page_id']);
1✔
230
                $select->where->in('id', $items)->or->in('parent_id', $items);
1✔
231
                $result = $this->sql->execute($select);
1✔
232

233
                $allItems = $pageIds = [];
1✔
234
                foreach ($result as $row) {
1✔
235
                        $allItems[] = $row['id'];
1✔
236
                        $pageIds[]  = $row['page_id'];
1✔
237
                }
238

239
                if ($allItems === [])
1✔
240
                        return;
×
241

242
                $pageIds = array_unique($pageIds);
1✔
243

244
                try {
245
                        $this->transaction->begin();
1✔
246

247
                        $deleteComments = $this->sql->delete('lp_comments');
1✔
248
                        $deleteComments->where->in('id', $allItems);
1✔
249
                        $this->sql->execute($deleteComments);
1✔
250

251
                        foreach ($pageIds as $pageId) {
1✔
252
                                $update = $this->sql->update('lp_pages')
1✔
253
                                        ->set([
1✔
254
                                                'num_comments' => new Expression(
1✔
255
                                                        'CASE WHEN num_comments < ? THEN 0 ELSE num_comments - ? END',
1✔
256
                                                        [count($allItems), count($allItems)]
1✔
257
                                                )
1✔
258
                                        ])
1✔
259
                                        ->where(['page_id = ?' => $pageId]);
1✔
260
                                $this->sql->execute($update);
1✔
261

262
                                $subSelect = $this->sql->select()
1✔
263
                                        ->from(['com' => 'lp_comments'])
1✔
264
                                        ->columns([new Expression('COALESCE(MAX(com.id), 0)')])
1✔
265
                                        ->where(['com.page_id = ?' => $pageId]);
1✔
266

267
                                $updateLast = $this->sql->update('lp_pages')
1✔
268
                                        ->set(['last_comment_id' => $subSelect])
1✔
269
                                        ->where(['page_id = ?' => $pageId]);
1✔
270
                                $this->sql->execute($updateLast);
1✔
271
                        }
272

273
                        $deleteParams = $this->sql->delete('lp_params');
1✔
274
                        $deleteParams->where->in('item_id', $allItems);
1✔
275
                        $deleteParams->where->equalTo('type', $this->entity);
1✔
276
                        $this->sql->execute($deleteParams);
1✔
277

278
                        $deleteTranslations = $this->sql->delete('lp_translations');
1✔
279
                        $deleteTranslations->where->in('item_id', $allItems);
1✔
280
                        $deleteTranslations->where->equalTo('type', $this->entity);
1✔
281
                        $this->sql->execute($deleteTranslations);
1✔
282

283
                        $deleteAlerts = $this->sql->delete('user_alerts');
1✔
284
                        $deleteAlerts->where([
1✔
285
                                'content_type = ?' => NotifyType::NEW_COMMENT->name(),
1✔
286
                        ]);
1✔
287
                        $deleteAlerts->where->in('content_id', $allItems);
1✔
288
                        $this->sql->execute($deleteAlerts);
1✔
289

290
                        $this->transaction->commit();
1✔
291

292
                        $withResponse && $this->response()->exit(['success' => true, 'items' => $allItems]);
1✔
UNCOV
293
                } catch (Exception $e) {
×
294
                        $this->transaction->rollback();
×
295

296
                        ErrorHandler::fatal($e->getMessage(), false);
×
297
                }
298
        }
299

300
        public function updateLastCommentId(int $item, int $pageId): void
301
        {
302
                $update = $this->sql->update('lp_pages')
1✔
303
                        ->set([
1✔
304
                                'num_comments'    => new Expression('num_comments + 1'),
1✔
305
                                'last_comment_id' => $item,
1✔
306
                        ])
1✔
307
                        ->where(['page_id = ?' => $pageId]);
1✔
308

309
                $this->sql->execute($update);
1✔
310
        }
311

312
        private function isCanEdit(int $date): bool
313
        {
314
                $timeToChange = Setting::get('lp_time_to_change_comments', 'int', 0);
4✔
315

316
                if (empty($timeToChange))
4✔
317
                        return false;
4✔
318

319
                return time() - $date <= $timeToChange * 60;
×
320
        }
321
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc