Completed
Push — dev ( 27415e...169927 )
by Darko
09:04
created

Category::getCategories()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 16
ccs 0
cts 0
cp 0
rs 9.9666
c 0
b 0
f 0
cc 3
nc 4
nop 2
crap 12
1
<?php
2
3
namespace App\Models;
4
5
use Watson\Rememberable\Rememberable;
6
use Illuminate\Database\Eloquent\Model;
7
8
/**
9
 * App\Models\Category.
10
 *
11
 * @property int $id
12
 * @property string $title
13
 * @property int|null $parentid
14
 * @property int $status
15
 * @property string|null $description
16
 * @property bool $disablepreview
17
 * @property int $minsizetoformrelease
18
 * @property int $maxsizetoformrelease
19
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Category[] $children
20
 * @property-read \App\Models\Category|null $parent
21
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Release[] $releases
22
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereDescription($value)
23
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereDisablepreview($value)
24
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereId($value)
25
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereMaxsizetoformrelease($value)
26
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereMinsizetoformrelease($value)
27
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereParentid($value)
28
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereStatus($value)
29
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereTitle($value)
30
 * @mixin \Eloquent
31
 */
32
class Category extends Model
33
{
34
    use Rememberable;
0 ignored issues
show
introduced by
The trait Watson\Rememberable\Rememberable requires some properties which are not provided by App\Models\Category: $rememberFor, $rememberCacheTag, $rememberCachePrefix, $rememberCacheDriver
Loading history...
35
36
    /**
37
     * Category constants.
38
     * Do NOT use the values, as they may change, always use the constant - that's what it's for.
39
     */
40
    public const OTHER_MISC = 10;
41
    public const OTHER_HASHED = 20;
42
    public const GAME_NDS = 1010;
43
    public const GAME_PSP = 1020;
44
    public const GAME_WII = 1030;
45
    public const GAME_XBOX = 1040;
46
    public const GAME_XBOX360 = 1050;
47
    public const GAME_WIIWARE = 1060;
48
    public const GAME_XBOX360DLC = 1070;
49
    public const GAME_PS3 = 1080;
50
    public const GAME_OTHER = 1999;
51
    public const GAME_3DS = 1110;
52
    public const GAME_PSVITA = 1120;
53
    public const GAME_WIIU = 1130;
54
    public const GAME_XBOXONE = 1140;
55
    public const GAME_PS4 = 1180;
56
    public const MOVIE_FOREIGN = 2010;
57
    public const MOVIE_OTHER = 2999;
58
    public const MOVIE_SD = 2030;
59
    public const MOVIE_HD = 2040;
60
    public const MOVIE_UHD = 2045;
61
    public const MOVIE_3D = 2050;
62
    public const MOVIE_BLURAY = 2060;
63
    public const MOVIE_DVD = 2070;
64
    public const MOVIE_WEBDL = 2080;
65
    public const MOVIE_X265 = 2090;
66
    public const MUSIC_MP3 = 3010;
67
    public const MUSIC_VIDEO = 3020;
68
    public const MUSIC_AUDIOBOOK = 3030;
69
    public const MUSIC_LOSSLESS = 3040;
70
    public const MUSIC_OTHER = 3999;
71
    public const MUSIC_FOREIGN = 3060;
72
    public const PC_0DAY = 4010;
73
    public const PC_ISO = 4020;
74
    public const PC_MAC = 4030;
75
    public const PC_PHONE_OTHER = 4040;
76
    public const PC_GAMES = 4050;
77
    public const PC_PHONE_IOS = 4060;
78
    public const PC_PHONE_ANDROID = 4070;
79
    public const TV_WEBDL = 5010;
80
    public const TV_FOREIGN = 5020;
81
    public const TV_SD = 5030;
82
    public const TV_HD = 5040;
83
    public const TV_UHD = 5045;
84
    public const TV_OTHER = 5999;
85
    public const TV_SPORT = 5060;
86
    public const TV_ANIME = 5070;
87
    public const TV_DOCU = 5080;
88
    public const TV_X265 = 5090;
89
    public const XXX_DVD = 6010;
90
    public const XXX_WMV = 6020;
91
    public const XXX_XVID = 6030;
92
    public const XXX_X264 = 6040;
93
    public const XXX_CLIPHD = 6041;
94
    public const XXX_CLIPSD = 6042;
95
    public const XXX_UHD = 6045;
96
    public const XXX_PACK = 6050;
97
    public const XXX_IMAGESET = 6060;
98
    public const XXX_OTHER = 6999;
99
    public const XXX_SD = 6080;
100
    public const XXX_WEBDL = 6090;
101
    public const BOOKS_MAGAZINES = 7010;
102
    public const BOOKS_EBOOK = 7020;
103
    public const BOOKS_COMICS = 7030;
104
    public const BOOKS_TECHNICAL = 7040;
105
    public const BOOKS_FOREIGN = 7060;
106
    public const BOOKS_UNKNOWN = 7999;
107
    public const OTHER_ROOT = 1;
108
    public const GAME_ROOT = 1000;
109
    public const MOVIE_ROOT = 2000;
110
    public const MUSIC_ROOT = 3000;
111
    public const PC_ROOT = 4000;
112
    public const TV_ROOT = 5000;
113
    public const XXX_ROOT = 6000;
114
    public const BOOKS_ROOT = 7000;
115
    public const STATUS_INACTIVE = 0;
116
    public const STATUS_ACTIVE = 1;
117
    public const STATUS_DISABLED = 2;
118
119
    public const OTHERS_GROUP =
120
        [
121
            self::BOOKS_UNKNOWN,
122
            self::GAME_OTHER,
123
            self::MOVIE_OTHER,
124
            self::MUSIC_OTHER,
125
            self::PC_PHONE_OTHER,
126
            self::TV_OTHER,
127
            self::OTHER_HASHED,
128
            self::XXX_OTHER,
129
            self::OTHER_MISC,
130
        ];
131
132
    public const MOVIES_GROUP =
133
        [
134
            self::MOVIE_FOREIGN,
135
            self::MOVIE_ROOT,
136
            self::MOVIE_OTHER,
137
            self::MOVIE_SD,
138
            self::MOVIE_HD,
139
            self::MOVIE_UHD,
140
            self::MOVIE_3D,
141
            self::MOVIE_BLURAY,
142
            self::MOVIE_DVD,
143
            self::MOVIE_WEBDL,
144
            self::MOVIE_X265,
145
        ];
146
147
    public const TV_GROUP =
148
        [
149
            self::TV_FOREIGN,
150
            self::TV_ROOT,
151
            self::TV_OTHER,
152
            self::TV_SD,
153
            self::TV_HD,
154
            self::TV_UHD,
155
            self::TV_ANIME,
156
            self::TV_DOCU,
157
            self::TV_SPORT,
158
            self::TV_WEBDL,
159
            self::TV_X265,
160
        ];
161
162
    /**
163
     * Tag constants.
164
     * Do NOT use the values, as they may change, always use the constant - that's what it's for.
165
     */
166
    public const TAG_OTHER_MISC = 'Other_Misc';
167
    public const TAG_OTHER_HASHED = 'Other_Hashed';
168
    public const TAG_GAME_NDS = 'Game_NDS';
169
    public const TAG_GAME_PSP = 'Game_PSP';
170
    public const TAG_GAME_WII = 'Game_Wii';
171
    public const TAG_GAME_XBOX = 'Game_Xbox';
172
    public const TAG_GAME_XBOX360 = 'Game_Xbox360';
173
    public const TAG_GAME_WIIWARE = 'Game_WiiWare';
174
    public const TAG_GAME_XBOX360DLC = 'Game_Xbox360_DLC';
175
    public const TAG_GAME_PS3 = 'Game_PS3';
176
    public const TAG_GAME_OTHER = 'Game_Other';
177
    public const TAG_GAME_3DS = 'Game_3DS';
178
    public const TAG_GAME_PSVITA = 'Game_PS_Vita';
179
    public const TAG_GAME_WIIU = 'Game_WiiU';
180
    public const TAG_GAME_XBOXONE = 'Game_Xbox_One';
181
    public const TAG_GAME_PS4 = 'Game_PS4';
182
    public const TAG_MOVIE_FOREIGN = 'Movie_Foreign';
183
    public const TAG_MOVIE_OTHER = 'Movie_Other';
184
    public const TAG_MOVIE_SD = 'Movie_SD';
185
    public const TAG_MOVIE_HD = 'Movie_HD';
186
    public const TAG_MOVIE_UHD = 'Movie_UHD';
187
    public const TAG_MOVIE_3D = 'Movie_3D';
188
    public const TAG_MOVIE_BLURAY = 'Movie_BluRay';
189
    public const TAG_MOVIE_DVD = 'Movie_DVD';
190
    public const TAG_MOVIE_WEBDL = 'Movie_Web_DL';
191
    public const TAG_MOVIE_X265 = 'Movie_x265';
192
    public const TAG_MUSIC_MP3 = 'Music_MP3';
193
    public const TAG_MUSIC_VIDEO = 'Music_Video';
194
    public const TAG_MUSIC_AUDIOBOOK = 'Music_Audio_Book';
195
    public const TAG_MUSIC_LOSSLESS = 'Music_LossLess';
196
    public const TAG_MUSIC_OTHER = 'Music_Other';
197
    public const TAG_MUSIC_FOREIGN = 'Music_Foreign';
198
    public const TAG_PC_0DAY = 'Pc_0Day';
199
    public const TAG_PC_ISO = 'Pc_Iso';
200
    public const TAG_PC_MAC = 'Pc_Mac';
201
    public const TAG_PC_PHONE_OTHER = 'Pc_Phone_Other';
202
    public const TAG_PC_GAMES = 'Pc_Games';
203
    public const TAG_PC_PHONE_IOS = 'Pc_Phone_Ios';
204
    public const TAG_PC_PHONE_ANDROID = 'Pc_Phone_Android';
205
    public const TAG_TV_WEBDL = 'Tv_Web_DL';
206
    public const TAG_TV_FOREIGN = 'Tv_Foreign';
207
    public const TAG_TV_SD = 'Tv_SD';
208
    public const TAG_TV_HD = 'Tv_HD';
209
    public const TAG_TV_UHD = 'Tv_UHD';
210
    public const TAG_TV_OTHER = 'Tv_Other';
211
    public const TAG_TV_SPORT = 'Tv_Sport';
212
    public const TAG_TV_ANIME = 'Tv_Anime';
213
    public const TAG_TV_DOCU = 'Tv_Documentary';
214
    public const TAG_TV_X265 = 'Tv_x265';
215
    public const TAG_XXX_DVD = 'Xxx_DVD';
216
    public const TAG_XXX_WMV = 'Xxx_Wmv';
217
    public const TAG_XXX_XVID = 'Xxx_XvID';
218
    public const TAG_XXX_X264 = 'Xxx_X264';
219
    public const TAG_XXX_CLIPHD = 'Xxx_Clip_HD';
220
    public const TAG_XXX_CLIPSD = 'Xxx_Clip_SD';
221
    public const TAG_XXX_UHD = 'Xxx_UHD';
222
    public const TAG_XXX_PACK = 'Xxx_Pack';
223
    public const TAG_XXX_IMAGESET = 'Xxx_ImageSet';
224
    public const TAG_XXX_OTHER = 'Xxx_Other';
225
    public const TAG_XXX_SD = 'Xxx_SD';
226
    public const TAG_XXX_WEBDL = 'Xxx_Web_DL';
227
    public const TAG_BOOKS_MAGAZINES = 'Magazines';
228
    public const TAG_BOOKS_EBOOK = 'Ebook';
229
    public const TAG_BOOKS_COMICS = 'Comics';
230
    public const TAG_BOOKS_TECHNICAL = 'Technical';
231
    public const TAG_BOOKS_FOREIGN = 'Books_Foreign';
232
    public const TAG_BOOKS_UNKNOWN = 'Books_Unknown';
233
234
    /**
235
     * @var string
236
     */
237
    protected $table = 'categories';
238
239
    /**
240
     * @var bool
241
     */
242
    public $timestamps = false;
243
244
    /**
245
     * @var bool
246
     */
247
    protected $dateFormat = false;
248
249
    /**
250
     * @var array
251
     */
252
    protected $guarded = [];
253
254
    /**
255
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
256
     */
257
    public function releases()
258
    {
259
        return $this->hasMany(Release::class, 'categories_id');
260
    }
261
262
    /**
263
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
264
     */
265
    public function parent()
266
    {
267
        return $this->belongsTo(RootCategory::class, 'root_categories_id');
268
    }
269
270
    /**
271
     * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|static[]
272
     */
273
    public static function getRecentlyAdded()
274
    {
275
        return self::query()
276
            ->with('parent')
277
            ->remember(config('nntmux.cache_expiry_long'))
278
            ->where('r.adddate', '>', now()->subWeek())
279
            ->selectRaw('CONCAT(cp.title, " > ", categories.title) as title')
280
            ->selectRaw('COUNT(r.id) as count')
281
            ->join('releases as r', 'r.categories_id', '=', 'categories.id')
282
            ->groupBy('title')
283
            ->orderBy('count', 'desc')
284
            ->get();
285
    }
286
287
    /**
288
     * @param array $cat
289
     *
290
     * @return string
291
     */
292
    public static function getCategorySearch(array $cat = [])
293
    {
294
        $categories = [];
295
        // If multiple categories were sent in a single array position, slice and add them
296
        if (strpos($cat[0], ',') !== false) {
297
            $tmpcats = explode(',', $cat[0]);
298
            // Reset the category to the first comma separated value in the string
299
            $cat[0] = $tmpcats[0];
300
            // Add the remaining categories in the string to the original array
301
            foreach (\array_slice($tmpcats, 1) as $tmpcat) {
302
                $cat[] = $tmpcat;
303
            }
304
        }
305
        foreach ($cat as $category) {
306
            if ($category !== -1 && self::isParent($category)) {
307
                foreach (RootCategory::find($category)->categories as $child) {
308
                    $categories[] = $child['id'];
309
                }
310
            } elseif ($category > 0) {
311
                $categories[] = $category;
312
            }
313
        }
314
        $catCount = \count($categories);
315
        switch ($catCount) {
316
            //No category constraint
317
            case 0:
318
                    $catsrch = 'AND 1=1';
319
                break;
320
            // One category constraint
321
            case 1:
322
                    $catsrch = $categories[0] !== -1 ? '  AND r.categories_id = '.$categories[0] : '';
323
                break;
324
            // Multiple category constraints
325
            default:
0 ignored issues
show
Coding Style introduced by
The default body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a default statement must start on the line immediately following the statement.

switch ($expr) {
    default:
        doSomething(); //right
        break;
}


switch ($expr) {
    default:

        doSomething(); //wrong
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
326
327
                    $catsrch = ' AND r.categories_id IN ('.implode(', ', $categories).') ';
328
                break;
329
        }
330
331
        return $catsrch;
332
    }
333
334
    /**
335
     * Returns a concatenated list of other categories.
336
     *
337
     * @return string
338
     */
339
    public static function getCategoryOthersGroup(): string
340
    {
341
        return implode(
342
            ',',
343
            self::OTHERS_GROUP
344
        );
345
    }
346
347
    /**
348
     * @param $category
349
     *
350
     * @return mixed
351
     */
352
    public static function getCategoryValue($category)
353
    {
354
        return \constant('self::'.$category);
355
    }
356
357
    /**
358
     * Check if category is parent.
359
     *
360
     * @param $cid
361
     *
362
     * @return bool
363
     */
364
    public static function isParent($cid): bool
365
    {
366
        $ret = RootCategory::query()->where(['id' => $cid])->first();
367
368
        return $ret !== null;
369
    }
370
371
    /**
372
     * @return \Illuminate\Database\Eloquent\Collection|static[]
373
     */
374
    public static function getFlat()
375
    {
376
        return self::query()->get();
377
    }
378
379
    /**
380
     * Get children of a parent category.
381
     *
382
     *
383
     * @param $categoryId
384
     * @return mixed
385
     */
386
    public static function getChildren($categoryId)
387
    {
388
        return RootCategory::remember(config('nntmux.cache_expiry_long'))->find($categoryId)->categories;
389
    }
390
391
    /**
392
     * Get names of enabled parent categories.
393
     *
394
     * @return \Illuminate\Database\Eloquent\Collection|static[]
395
     */
396
    public static function getEnabledParentNames()
397
    {
398
        return RootCategory::query()->where('status', '=', 1)->get(['title']);
399
    }
400
401
    /**
402
     * Returns category ID's for site disabled categories.
403
     *
404
     *
405
     * @return \Illuminate\Database\Eloquent\Collection|static[]
406
     */
407
    public static function getDisabledIDs()
408
    {
409
        return self::query()
410
            ->where('status', '=', 2)
411
            ->get(['id']);
412
    }
413
414
    /**
415
     * Get multiple categories.
416
     *
417
     * @param $ids
418
     *
419
     * @return bool|\Illuminate\Database\Eloquent\Collection|static[]
420
     */
421
    public static function getByIds($ids)
422
    {
423
        if (\count($ids) > 0) {
424
            return self::query()->remember(config('nntmux.cache_expiry_long'))->whereIn('id', $ids)->get();
425
        }
426
427
        return false;
428
    }
429
430
    /**
431
     * Return the parent and category name from the supplied categoryID.
432
     *
433
     *
434
     * @param $categoryId
435
     *
436
     * @return string
437
     */
438
    public static function getNameByID($categoryId): string
439
    {
440
        $cat = self::query()->where('id', $categoryId)->first();
441
442
        return $cat !== null ? $cat->parent->title.' -> '.$cat->title : '';
443
    }
444
445
    /**
446
     * @param $title
447
     * @param $parent
448
     * @return bool|mixed
449
     */
450
    public static function getIdByName($title, $parent)
451
    {
452
        $cat = self::query()->where('title', $title)->with('parent.'.$parent)->first(['id']);
453
454
        return $cat !== null ? $cat->id : false;
455
    }
456
457
    /**
458
     * Update a category.
459
     *
460
     *
461
     * @param $id
462
     * @param $status
463
     * @param $desc->update
464
     * @param $disablepreview
465
     * @param $minsize
466
     * @param $maxsize
467
     * @return int
468
     */
469
    public static function updateCategory($id, $status, $desc, $disablepreview, $minsize, $maxsize): int
470
    {
471
        return self::query()->where('id', $id)->update(
472
            [
473
                'disablepreview' => $disablepreview,
474
                'status' => $status,
475
                'minsizetoformrelease' => $minsize,
476
                'maxsizetoformrelease' => $maxsize,
477
                'description' => $desc,
478
            ]
479
        );
480
    }
481
482
    /**
483
     * @param array $excludedCats
484
     *
485
     * @return array
486
     */
487
    public static function getForMenu(array $excludedCats = []): array
488
    {
489
        $categoriesResult = [];
490
        $categoriesArray = RootCategory::query()->with(['categories' =>function ($query) use ($excludedCats) {
491
            if (! empty($excludedCats)) {
492
                $query->whereNotIn('id', $excludedCats);
493
            }
494
            $query->select(['id', 'title', 'root_categories_id', 'description']);
495
        }])->select(['id', 'title'])->get()->toArray();
496
497
        foreach ($categoriesArray as $category) {
498
            if (! empty($category['categories'])) {
499
                $categoriesResult[] = $category;
500
            }
501
        }
502
503
        return $categoriesResult;
504
    }
505
506
    /**
507
     * @return mixed
508
     */
509
    public static function getForApi()
510
    {
511
        return RootCategory::query()->remember(config('nntmux.cache_expiry_long'))->select(['id', 'title'])->where('status', '=', self::STATUS_ACTIVE)->get();
512
    }
513
514
    /**
515
     * Return a list of categories for use in a dropdown.
516
     *
517
     * @param bool $blnIncludeNoneSelected
518
     *
519
     * @return array
520
     */
521
    public static function getForSelect($blnIncludeNoneSelected = true): array
522
    {
523
        $categories = self::getCategories();
524
        $temp_array = [];
525
526
        if ($blnIncludeNoneSelected) {
527
            $temp_array[-1] = '--Please Select--';
528
        }
529
        foreach ($categories as $category) {
530
            $temp_array[$category->id] = $category->parent->title.' > '.$category->title;
531
        }
532
533
        return $temp_array;
534
    }
535
536
    /**
537
     * @param bool $activeOnly
538
     * @param array $excludedCats
539
     * @return array
540
     */
541
    public static function getCategories($activeOnly = false, array $excludedCats = [])
542
    {
543
        $sql = self::query()
544
            ->with('parent')
545
            ->select(['id', 'status', 'title', 'root_categories_id'])
546
            ->orderBy('id');
547
548
        if ($activeOnly) {
549
            $sql->where('status', '=', self::STATUS_ACTIVE);
550
        }
551
552
        if (\count($excludedCats) > 0) {
553
            $sql->whereNotIn('id', $excludedCats);
554
        }
555
556
        return $sql->get();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $sql->get() also could return the type Illuminate\Database\Eloquent\Collection which is incompatible with the documented return type array.
Loading history...
557
    }
558
}
559