hasMany(Release::class, 'categories_id'); } /** * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function parent() { return $this->belongsTo(RootCategory::class, 'root_categories_id'); } /** * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|static[] */ public static function getRecentlyAdded() { $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_long')); $result = Cache::get(md5('RecentlyAdded')); if ($result !== null) { return $result; } $result = self::query() ->with('parent') ->where('r.adddate', '>', now()->subWeek()) ->select(['root_categories_id', DB::raw('COUNT(r.id) as count'), 'title']) ->join('releases as r', 'r.categories_id', '=', 'categories.id') ->groupBy('title') ->orderBy('count', 'desc') ->get(); Cache::put(md5('RecentlyAdded'), $result, $expiresAt); return $result; } /** * @param array $cat * * @return string */ public static function getCategorySearch(array $cat = []) { $categories = []; // If multiple categories were sent in a single array position, slice and add them if (strpos($cat[0], ',') !== false) { $tmpcats = explode(',', $cat[0]); // Reset the category to the first comma separated value in the string $cat[0] = $tmpcats[0]; // Add the remaining categories in the string to the original array foreach (\array_slice($tmpcats, 1) as $tmpcat) { $cat[] = $tmpcat; } } foreach ($cat as $category) { if (is_numeric($category) && $category !== -1 && self::isParent($category)) { foreach (RootCategory::find($category)->categories as $child) { $categories[] = $child['id']; } } elseif (is_numeric($category) && $category > 0) { $categories[] = $category; } } $catCount = \count($categories); switch ($catCount) { //No category constraint case 0: $catsrch = 'AND 1=1'; break; // One category constraint case 1: $catsrch = $categories[0] !== -1 ? ' AND r.categories_id = '.$categories[0] : ''; break; // Multiple category constraints default: $catsrch = ' AND r.categories_id IN ('.implode(', ', $categories).') '; break; } return $catsrch; } /** * Returns a concatenated list of other categories. * * @return string */ public static function getCategoryOthersGroup(): string { return implode( ',', self::OTHERS_GROUP ); } /** * @param $category * * @return mixed */ public static function getCategoryValue($category) { return \constant('self::'.$category); } /** * Check if category is parent. * * @param $cid * * @return bool */ public static function isParent($cid): bool { $ret = RootCategory::query()->where(['id' => $cid])->first(); return $ret !== null; } /** * @return \Illuminate\Database\Eloquent\Collection|static[] */ public static function getFlat() { return self::query()->get(); } /** * Get children of a parent category. * * * @param $categoryId * @return mixed */ public static function getChildren($categoryId) { $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_long')); $result = Cache::get(md5($categoryId)); if ($result !== null) { return $result; } $result = RootCategory::find($categoryId)->categories; Cache::put(md5($categoryId), $result, $expiresAt); return $result; } /** * Get names of enabled parent categories. * * @return \Illuminate\Database\Eloquent\Collection|static[] */ public static function getEnabledParentNames() { return RootCategory::query()->where('status', '=', 1)->get(['title']); } /** * Returns category ID's for site disabled categories. * * * @return \Illuminate\Database\Eloquent\Collection|static[] */ public static function getDisabledIDs() { return self::query() ->where('status', '=', 2) ->get(['id']); } /** * Get multiple categories. * * @param $ids * * @return bool|\Illuminate\Database\Eloquent\Collection|static[] */ public static function getByIds($ids) { if (\count($ids) > 0) { $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_long')); $result = Cache::get(md5(implode(',', $ids))); if ($result !== null) { return $result; } $result = self::query()->whereIn('id', $ids)->get(); Cache::put(md5(md5(implode(',', $ids))), $result, $expiresAt); return $result; } return false; } /** * Return the parent and category name from the supplied categoryID. * * * @param $categoryId * * @return string */ public static function getNameByID($categoryId): string { $cat = self::query()->where('id', $categoryId)->first(); return $cat !== null ? $cat->parent->title.' -> '.$cat->title : ''; } /** * @param $title * @param $parent * @return bool|mixed */ public static function getIdByName($title, $parent) { $cat = self::query()->where('title', $title)->with('parent.'.$parent)->first(['id']); return $cat !== null ? $cat->id : false; } /** * Update a category. * * * @param $id * @param $status * @param $desc->update * @param $disablepreview * @param $minsize * @param $maxsize * @return int */ public static function updateCategory($id, $status, $desc, $disablepreview, $minsize, $maxsize): int { return self::query()->where('id', $id)->update( [ 'disablepreview' => $disablepreview, 'status' => $status, 'minsizetoformrelease' => $minsize, 'maxsizetoformrelease' => $maxsize, 'description' => $desc, ] ); } /** * @param array $excludedCats * * @return array */ public static function getForMenu(array $excludedCats = []): array { $categoriesResult = []; $categoriesArray = RootCategory::query()->with(['categories' =>function ($query) use ($excludedCats) { if (! empty($excludedCats)) { $query->whereNotIn('id', $excludedCats); } $query->select(['id', 'title', 'root_categories_id', 'description']); }])->select(['id', 'title'])->get()->toArray(); foreach ($categoriesArray as $category) { if (! empty($category['categories'])) { $categoriesResult[] = $category; } } return $categoriesResult; } /** * @return mixed */ public static function getForApi() { $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_long')); $result = Cache::get(md5('ForApi')); if ($result !== null) { return $result; } $result = RootCategory::query()->select(['id', 'title'])->where('status', '=', self::STATUS_ACTIVE)->get(); Cache::put(md5('ForApi'), $result, $expiresAt); return $result; } /** * Return a list of categories for use in a dropdown. * * @param bool $blnIncludeNoneSelected * * @return array */ public static function getForSelect($blnIncludeNoneSelected = true): array { $categories = self::getCategories(); $temp_array = []; if ($blnIncludeNoneSelected) { $temp_array[-1] = '--Please Select--'; } foreach ($categories as $category) { $temp_array[$category->id] = $category->parent->title.' > '.$category->title; } return $temp_array; } /** * @param bool $activeOnly * @param array $excludedCats * @return array */ public static function getCategories($activeOnly = false, array $excludedCats = []) { $sql = self::query() ->with('parent') ->select(['id', 'status', 'title', 'root_categories_id']) ->orderBy('id'); if ($activeOnly) { $sql->where('status', '=', self::STATUS_ACTIVE); } if (! empty($excludedCats)) { $sql->whereNotIn('id', $excludedCats); } return $sql->get(); } }