Passed
Push — master ( 48c78c...6f5874 )
by Darko
06:40
created

Category::getForApi()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 13
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace App\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\Relations\BelongsTo;
7
use Illuminate\Database\Eloquent\Relations\HasMany;
8
use Illuminate\Support\Facades\Cache;
9
10
/**
11
 * App\Models\Category.
12
 *
13
 * @property int $id
14
 * @property string $title
15
 * @property int|null $parentid
16
 * @property int $status
17
 * @property string|null $description
18
 * @property bool $disablepreview
19
 * @property int $minsizetoformrelease
20
 * @property int $maxsizetoformrelease
21
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Category[] $children
22
 * @property-read Category|null $parent
23
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Release[] $releases
24
 *
25
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereDescription($value)
26
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereDisablepreview($value)
27
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereId($value)
28
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereMaxsizetoformrelease($value)
29
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereMinsizetoformrelease($value)
30
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereParentid($value)
31
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereStatus($value)
32
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereTitle($value)
33
 *
34
 * @mixin \Eloquent
35
 *
36
 * @property int|null $root_categories_id
37
 *
38
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category newModelQuery()
39
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category newQuery()
40
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category query()
41
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereRootCategoriesId($value)
42
 */
43
class Category extends Model
44
{
45
    /**
46
     * Category constants.
47
     * Do NOT use the values, as they may change, always use the constant - that's what it's for.
48
     */
49
    public const OTHER_MISC = 10;
50
51
    public const OTHER_HASHED = 20;
52
53
    public const GAME_NDS = 1010;
54
55
    public const GAME_PSP = 1020;
56
57
    public const GAME_WII = 1030;
58
59
    public const GAME_XBOX = 1040;
60
61
    public const GAME_XBOX360 = 1050;
62
63
    public const GAME_WIIWARE = 1060;
64
65
    public const GAME_XBOX360DLC = 1070;
66
67
    public const GAME_PS3 = 1080;
68
69
    public const GAME_OTHER = 1999;
70
71
    public const GAME_3DS = 1110;
72
73
    public const GAME_PSVITA = 1120;
74
75
    public const GAME_WIIU = 1130;
76
77
    public const GAME_XBOXONE = 1140;
78
79
    public const GAME_PS4 = 1180;
80
81
    public const MOVIE_FOREIGN = 2010;
82
83
    public const MOVIE_OTHER = 2999;
84
85
    public const MOVIE_SD = 2030;
86
87
    public const MOVIE_HD = 2040;
88
89
    public const MOVIE_UHD = 2045;
90
91
    public const MOVIE_3D = 2050;
92
93
    public const MOVIE_BLURAY = 2060;
94
95
    public const MOVIE_DVD = 2070;
96
97
    public const MOVIE_WEBDL = 2080;
98
99
    public const MOVIE_X265 = 2090;
100
101
    public const MUSIC_MP3 = 3010;
102
103
    public const MUSIC_VIDEO = 3020;
104
105
    public const MUSIC_AUDIOBOOK = 3030;
106
107
    public const MUSIC_LOSSLESS = 3040;
108
109
    public const MUSIC_PODCAST = 3050;
110
111
    public const MUSIC_OTHER = 3999;
112
113
    public const MUSIC_FOREIGN = 3060;
114
115
    public const PC_0DAY = 4010;
116
117
    public const PC_ISO = 4020;
118
119
    public const PC_MAC = 4030;
120
121
    public const PC_PHONE_OTHER = 4040;
122
123
    public const PC_GAMES = 4050;
124
125
    public const PC_PHONE_IOS = 4060;
126
127
    public const PC_PHONE_ANDROID = 4070;
128
129
    public const TV_WEBDL = 5010;
130
131
    public const TV_FOREIGN = 5020;
132
133
    public const TV_SD = 5030;
134
135
    public const TV_HD = 5040;
136
137
    public const TV_UHD = 5045;
138
139
    public const TV_OTHER = 5999;
140
141
    public const TV_SPORT = 5060;
142
143
    public const TV_ANIME = 5070;
144
145
    public const TV_DOCU = 5080;
146
147
    public const TV_X265 = 5090;
148
149
    public const XXX_DVD = 6010;
150
151
    public const XXX_WMV = 6020;
152
153
    public const XXX_XVID = 6030;
154
155
    public const XXX_X264 = 6040;
156
157
    public const XXX_CLIPHD = 6041;
158
159
    public const XXX_CLIPSD = 6042;
160
161
    public const XXX_UHD = 6045;
162
163
    public const XXX_PACK = 6050;
164
165
    public const XXX_IMAGESET = 6060;
166
167
    public const XXX_OTHER = 6999;
168
169
    public const XXX_SD = 6080;
170
171
    public const XXX_WEBDL = 6090;
172
173
    public const BOOKS_MAGAZINES = 7010;
174
175
    public const BOOKS_EBOOK = 7020;
176
177
    public const BOOKS_COMICS = 7030;
178
179
    public const BOOKS_TECHNICAL = 7040;
180
181
    public const BOOKS_FOREIGN = 7060;
182
183
    public const BOOKS_UNKNOWN = 7999;
184
185
    public const OTHER_ROOT = 1;
186
187
    public const GAME_ROOT = 1000;
188
189
    public const MOVIE_ROOT = 2000;
190
191
    public const MUSIC_ROOT = 3000;
192
193
    public const PC_ROOT = 4000;
194
195
    public const TV_ROOT = 5000;
196
197
    public const XXX_ROOT = 6000;
198
199
    public const BOOKS_ROOT = 7000;
200
201
    public const STATUS_INACTIVE = 0;
202
203
    public const STATUS_ACTIVE = 1;
204
205
    public const STATUS_DISABLED = 2;
206
207
    public const OTHERS_GROUP =
208
        [
209
            self::BOOKS_UNKNOWN,
210
            self::GAME_OTHER,
211
            self::MOVIE_OTHER,
212
            self::MUSIC_OTHER,
213
            self::PC_PHONE_OTHER,
214
            self::TV_OTHER,
215
            self::OTHER_HASHED,
216
            self::XXX_OTHER,
217
            self::OTHER_MISC,
218
        ];
219
220
    public const MOVIES_GROUP =
221
        [
222
            self::MOVIE_FOREIGN,
223
            self::MOVIE_ROOT,
224
            self::MOVIE_OTHER,
225
            self::MOVIE_SD,
226
            self::MOVIE_HD,
227
            self::MOVIE_UHD,
228
            self::MOVIE_3D,
229
            self::MOVIE_BLURAY,
230
            self::MOVIE_DVD,
231
            self::MOVIE_WEBDL,
232
            self::MOVIE_X265,
233
        ];
234
235
    public const TV_GROUP =
236
        [
237
            self::TV_FOREIGN,
238
            self::TV_ROOT,
239
            self::TV_OTHER,
240
            self::TV_SD,
241
            self::TV_HD,
242
            self::TV_UHD,
243
            self::TV_ANIME,
244
            self::TV_DOCU,
245
            self::TV_SPORT,
246
            self::TV_WEBDL,
247
            self::TV_X265,
248
        ];
249
250
    /**
251
     * @var string
252
     */
253
254
    /**
255
     * @var bool
256
     */
257
    public $timestamps = false;
258
259
    /**
260
     * @var bool
261
     */
262
    protected $dateFormat = false;
263
264
    /**
265
     * @var array
266
     */
267
    protected $guarded = [];
268
269
    public function releases(): HasMany
270
    {
271
        return $this->hasMany(Release::class, 'categories_id');
272
    }
273
274
    public function parent(): BelongsTo
275
    {
276
        return $this->belongsTo(RootCategory::class, 'root_categories_id');
277
    }
278
279
    public static function getCategorySearch(array $cat = []): string
280
    {
281
        $categories = [];
282
        // If multiple categories were sent in a single array position, slice and add them
283
        if (strpos($cat[0], ',') !== false) {
284
            $tmpcats = explode(',', $cat[0]);
285
            // Reset the category to the first comma separated value in the string
286
            $cat[0] = $tmpcats[0];
287
            // Add the remaining categories in the string to the original array
288
            foreach (\array_slice($tmpcats, 1) as $tmpcat) {
289
                $cat[] = $tmpcat;
290
            }
291
        }
292
        foreach ($cat as $category) {
293
            if (is_numeric($category) && $category !== -1 && self::isParent($category)) {
294
                foreach (RootCategory::find($category)->categories as $child) {
0 ignored issues
show
Bug introduced by
The property categories does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
295
                    $categories[] = $child['id'];
296
                }
297
            } elseif (is_numeric($category) && $category > 0) {
298
                $categories[] = $category;
299
            }
300
        }
301
        $catCount = \count($categories);
302
        switch ($catCount) {
303
            //No category constraint
304
            case 0:
305
                $catsrch = 'AND 1=1';
306
                break;
307
                // One category constraint
308
            case 1:
309
                $catsrch = $categories[0] !== -1 ? '  AND r.categories_id = '.$categories[0] : '';
310
                break;
311
                // Multiple category constraints
312
            default:
313
314
                $catsrch = ' AND r.categories_id IN ('.implode(', ', $categories).') ';
315
                break;
316
        }
317
318
        return $catsrch;
319
    }
320
321
    /**
322
     * Returns a concatenated list of other categories.
323
     */
324
    public static function getCategoryOthersGroup(): string
325
    {
326
        return implode(
327
            ',',
328
            self::OTHERS_GROUP
329
        );
330
    }
331
332
    /**
333
     * @return mixed
334
     */
335
    public static function getCategoryValue($category)
336
    {
337
        return \constant('self::'.$category);
338
    }
339
340
    /**
341
     * Check if category is parent.
342
     */
343
    public static function isParent($cid): bool
344
    {
345
        $ret = RootCategory::query()->where(['id' => $cid])->first();
346
347
        return $ret !== null;
348
    }
349
350
    /**
351
     * @return \Illuminate\Database\Eloquent\Collection|static[]
352
     */
353
    public static function getFlat()
354
    {
355
        return self::query()->get();
356
    }
357
358
    /**
359
     * Get children of a parent category.
360
     *
361
     *
362
     * @return mixed
363
     */
364
    public static function getChildren($categoryId)
365
    {
366
        $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_long'));
367
        $result = Cache::get(md5($categoryId));
368
        if ($result !== null) {
369
            return $result;
370
        }
371
372
        $result = RootCategory::find($categoryId)->categories;
0 ignored issues
show
Bug introduced by
The property categories does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
373
        Cache::put(md5($categoryId), $result, $expiresAt);
374
375
        return $result;
376
    }
377
378
    /**
379
     * Get names of enabled parent categories.
380
     *
381
     * @return \Illuminate\Database\Eloquent\Collection|static[]
382
     */
383
    public static function getEnabledParentNames()
384
    {
385
        return RootCategory::query()->where('status', '=', 1)->get(['title']);
386
    }
387
388
    /**
389
     * Returns category ID's for site disabled categories.
390
     *
391
     *
392
     * @return \Illuminate\Database\Eloquent\Collection|static[]
393
     */
394
    public static function getDisabledIDs()
395
    {
396
        return self::query()
397
            ->where('status', '=', 2)
398
            ->get(['id']);
399
    }
400
401
    /**
402
     * Get multiple categories.
403
     *
404
     * @return bool|\Illuminate\Database\Eloquent\Collection|static[]
405
     */
406
    public static function getByIds($ids)
407
    {
408
        if (\count($ids) > 0) {
409
            $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_long'));
410
            $result = Cache::get(md5(implode(',', $ids)));
411
            if ($result !== null) {
412
                return $result;
413
            }
414
            $result = self::query()->whereIn('id', $ids)->get();
415
416
            Cache::put(md5(md5(implode(',', $ids))), $result, $expiresAt);
417
418
            return $result;
419
        }
420
421
        return false;
422
    }
423
424
    /**
425
     * Return the parent and category name from the supplied categoryID.
426
     */
427
    public static function getNameByID($categoryId): string
428
    {
429
        $cat = self::query()->where('id', $categoryId)->first();
430
431
        return $cat !== null ? $cat->parent->title.' -> '.$cat->title : '';
432
    }
433
434
    /**
435
     * @return bool|mixed
436
     */
437
    public static function getIdByName($title, $parent)
438
    {
439
        $cat = self::query()->where('title', $title)->with('parent.'.$parent)->first(['id']);
440
441
        return $cat !== null ? $cat->id : false;
442
    }
443
444
    /**
445
     * Update a category.
446
     */
447
    public static function updateCategory($id, $status, $desc, $disablepreview, $minsize, $maxsize): int
448
    {
449
        return self::query()->where('id', $id)->update(
450
            [
451
                'disablepreview' => $disablepreview,
452
                'status' => $status,
453
                'minsizetoformrelease' => $minsize,
454
                'maxsizetoformrelease' => $maxsize,
455
                'description' => $desc,
456
            ]
457
        );
458
    }
459
460
    public static function getForMenu(array $excludedCats = []): array
461
    {
462
        $categoriesResult = [];
463
        $categoriesArray = RootCategory::query()->with(['categories' => function ($query) use ($excludedCats) {
464
            if (! empty($excludedCats)) {
465
                $query->whereNotIn('id', $excludedCats);
466
            }
467
            $query->select(['id', 'title', 'root_categories_id', 'description']);
468
        }])->select(['id', 'title'])->get()->toArray();
469
470
        foreach ($categoriesArray as $category) {
471
            if (! empty($category['categories'])) {
472
                $categoriesResult[] = $category;
473
            }
474
        }
475
476
        return $categoriesResult;
477
    }
478
479
    /**
480
     * @return mixed
481
     */
482
    public static function getForApi()
483
    {
484
        $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_long'));
485
        $result = Cache::get(md5('ForApi'));
486
        if ($result !== null) {
487
            return $result;
488
        }
489
490
        $result = RootCategory::query()->select(['id', 'title'])->where('status', '=', self::STATUS_ACTIVE)->get();
491
492
        Cache::put(md5('ForApi'), $result, $expiresAt);
493
494
        return $result;
495
    }
496
497
    /**
498
     * Return a list of categories for use in a dropdown.
499
     */
500
    public static function getForSelect(bool $blnIncludeNoneSelected = true): array
501
    {
502
        $categories = self::getCategories();
503
        $temp_array = [];
504
505
        if ($blnIncludeNoneSelected) {
506
            $temp_array[-1] = '--Please Select--';
507
        }
508
        foreach ($categories as $category) {
509
            $temp_array[$category->id] = $category->parent->title.' > '.$category->title;
510
        }
511
512
        return $temp_array;
513
    }
514
515
    /**
516
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
517
     */
518
    public static function getCategories(bool $activeOnly = false, array $excludedCats = []): \Illuminate\Database\Eloquent\Collection|array
519
    {
520
        $sql = self::query()
521
            ->with('parent')
522
            ->select(['id', 'status', 'title', 'root_categories_id'])
523
            ->orderBy('id');
0 ignored issues
show
Bug introduced by
'id' of type string is incompatible with the type Closure|Illuminate\Datab...\Database\Query\Builder expected by parameter $column of Illuminate\Database\Query\Builder::orderBy(). ( Ignorable by Annotation )

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

523
            ->orderBy(/** @scrutinizer ignore-type */ 'id');
Loading history...
524
525
        if ($activeOnly) {
526
            $sql->where('status', '=', self::STATUS_ACTIVE);
527
        }
528
529
        if (! empty($excludedCats)) {
530
            $sql->whereNotIn('id', $excludedCats);
531
        }
532
533
        return $sql->get();
534
    }
535
}
536