Passed
Push — master ( 04c44e...059b28 )
by Darko
10:01 queued 04:00
created

Category::getIdByName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
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
        return Cache::flexible(md5($categoryId), [config('nntmux.cache_expiry_medium'), config('nntmux.cache_expiry_long')], function () use ($categoryId) {
367
            return 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...
368
        });
369
    }
370
371
    /**
372
     * Get names of enabled parent categories.
373
     *
374
     * @return \Illuminate\Database\Eloquent\Collection|static[]
375
     */
376
    public static function getEnabledParentNames()
377
    {
378
        return RootCategory::query()->where('status', '=', 1)->get(['title']);
379
    }
380
381
    /**
382
     * Returns category ID's for site disabled categories.
383
     *
384
     *
385
     * @return \Illuminate\Database\Eloquent\Collection|static[]
386
     */
387
    public static function getDisabledIDs()
388
    {
389
        return self::query()
390
            ->where('status', '=', 2)
391
            ->get(['id']);
392
    }
393
394
    /**
395
     * Get multiple categories.
396
     *
397
     * @return bool|\Illuminate\Database\Eloquent\Collection|static[]
398
     */
399
    public static function getByIds($ids)
400
    {
401
        if (\count($ids) > 0) {
402
            return Cache::flexible(md5(md5(implode(',', $ids))), [config('nntmux.cache_expiry_medium'), config('nntmux.cache_expiry_long')], function () use ($ids) {
403
                return self::query()->whereIn('id', $ids)->get();
404
            });
405
        }
406
407
        return false;
408
    }
409
410
    /**
411
     * Return the parent and category name from the supplied categoryID.
412
     */
413
    public static function getNameByID($categoryId): string
414
    {
415
        $cat = self::query()->where('id', $categoryId)->first();
416
417
        return $cat !== null ? $cat->parent->title.' -> '.$cat->title : '';
418
    }
419
420
    /**
421
     * @return bool|mixed
422
     */
423
    public static function getIdByName($title, $parent)
424
    {
425
        $cat = self::query()->where('title', $title)->with('parent.'.$parent)->first(['id']);
426
427
        return $cat !== null ? $cat->id : false;
428
    }
429
430
    /**
431
     * Update a category.
432
     */
433
    public static function updateCategory($id, $status, $desc, $disablepreview, $minsize, $maxsize): int
434
    {
435
        return self::query()->where('id', $id)->update(
436
            [
437
                'disablepreview' => $disablepreview,
438
                'status' => $status,
439
                'minsizetoformrelease' => $minsize,
440
                'maxsizetoformrelease' => $maxsize,
441
                'description' => $desc,
442
            ]
443
        );
444
    }
445
446
    public static function getForMenu(array $excludedCats = []): array
447
    {
448
        $categoriesResult = [];
449
        $categoriesArray = RootCategory::query()->with(['categories' => function ($query) use ($excludedCats) {
450
            if (! empty($excludedCats)) {
451
                $query->whereNotIn('id', $excludedCats);
452
            }
453
            $query->select(['id', 'title', 'root_categories_id', 'description']);
454
        }])->select(['id', 'title'])->get()->toArray();
455
456
        foreach ($categoriesArray as $category) {
457
            if (! empty($category['categories'])) {
458
                $categoriesResult[] = $category;
459
            }
460
        }
461
462
        return $categoriesResult;
463
    }
464
465
    /**
466
     * Get categories for the API.
467
     */
468
    public static function getForApi()
469
    {
470
        return Cache::flexible('ForApi', [config('nntmux.cache_expiry_medium'), config('nntmux.cache_expiry_long')], function () {
471
            return RootCategory::query()->select(['id', 'title'])->where('status', '=', self::STATUS_ACTIVE)->get();
472
        });
473
    }
474
475
    /**
476
     * Return a list of categories for use in a dropdown.
477
     */
478
    public static function getForSelect(bool $blnIncludeNoneSelected = true): array
479
    {
480
        $categories = self::getCategories();
481
        $temp_array = [];
482
483
        if ($blnIncludeNoneSelected) {
484
            $temp_array[-1] = '--Please Select--';
485
        }
486
        foreach ($categories as $category) {
487
            $temp_array[$category->id] = $category->parent->title.' > '.$category->title;
488
        }
489
490
        return $temp_array;
491
    }
492
493
    /**
494
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
495
     */
496
    public static function getCategories(bool $activeOnly = false, array $excludedCats = []): \Illuminate\Database\Eloquent\Collection|array
497
    {
498
        $sql = self::query()
499
            ->with('parent')
500
            ->select(['id', 'status', 'title', 'root_categories_id'])
501
            ->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

501
            ->orderBy(/** @scrutinizer ignore-type */ 'id');
Loading history...
502
503
        if ($activeOnly) {
504
            $sql->where('status', '=', self::STATUS_ACTIVE);
505
        }
506
507
        if (! empty($excludedCats)) {
508
            $sql->whereNotIn('id', $excludedCats);
509
        }
510
511
        return $sql->get();
512
    }
513
}
514