null, ]; $options += $defaults; $this->pdo = ($options['Settings'] instanceof DB ? $options['Settings'] : new DB()); } /** * Parse category search constraints. * * @param array $cat * * @return string $catsrch */ public function getCategorySearch(array $cat = []): string { $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 ($category !== -1 && $this->isParent($category)) { foreach ($this->getChildren($category) as $child) { $categories[] = $child['id']; } } elseif ($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::BOOKS_UNKNOWN, self::GAME_OTHER, self::MOVIE_OTHER, self::MUSIC_OTHER, self::PC_PHONE_OTHER, self::TV_OTHER, self::OTHER_HASHED, self::XXX_OTHER, self::OTHER_MISC, ] ); } /** * @param $category * * @return mixed */ public static function getCategoryValue($category) { return constant('self::'.$category); } /** * Check if category is parent. * * @param $cid * * @return bool */ public function isParent($cid): bool { $ret = CategoryModel::query()->where(['id' => $cid, 'parentid' => null])->first(); return $ret !== null; } /** * @return \Illuminate\Database\Eloquent\Collection|static[] */ public function getFlat() { return CategoryModel::query()->get(); } /** * Get children of a parent category. * * * @param $cid * @return mixed */ public function getChildren($cid) { return CategoryModel::find($cid)->children; } /** * Get names of enabled parent categories. * * @return \Illuminate\Database\Eloquent\Collection|static[] */ public function getEnabledParentNames() { return CategoryModel::query()->where(['parentid' => null, 'status' => 1])->get(['title']); } /** * Returns category ID's for site disabled categories. * * * @return \Illuminate\Database\Eloquent\Collection|static[] */ public function getDisabledIDs() { return CategoryModel::query()->where('status', '=', 2)->orWhere(['status' => 2, 'parentid' => null])->get(['id']); } /** * Get a category row by its id. * * * @param $id * @return \Illuminate\Database\Eloquent\Model|null|static */ public function getById($id) { return CategoryModel::query()->where('id', $id)->first(); } /** * Get multiple categories. * * @param array $ids * * @return array|bool */ public function getByIds($ids) { if (count($ids) > 0) { return CategoryModel::query()->whereIn('id', $ids)->get(); } return false; } /** * Return the parent and category name from the supplied categoryID. * * * @param $ID * @return string */ public function getNameByID($ID): string { $cat = CategoryModel::query()->where('id', $ID)->first(); return $cat !== null ? $cat->parent->title.' -> '.$cat->title : ''; } /** * Update a category. * * * @param $id * @param $status * @param $desc->update * @param $disablepreview * @param $minsize * @param $maxsize * @return int */ public function update($id, $status, $desc, $disablepreview, $minsize, $maxsize): int { return CategoryModel::query()->where('id', $id)->update( [ 'disablepreview' => $disablepreview, 'status' => $status, 'minsizetoformrelease' => $minsize, 'maxsizetoformrelease' => $maxsize, 'description' => $desc, ] ); } /** * @param array $excludedcats * * @param array $roleexcludedcats * * @return array */ public function getForMenu(array $excludedcats = [], array $roleexcludedcats = []): array { $ret = []; $exccatlist = ''; if (count($excludedcats) > 0 && count($roleexcludedcats) == 0) { $exccatlist = ' AND id NOT IN ('.implode(',', $excludedcats).')'; } elseif (count($excludedcats) > 0 && count($roleexcludedcats) > 0) { $exccatlist = ' AND id NOT IN ('.implode(',', $excludedcats).','.implode(',', $roleexcludedcats).')'; } elseif (count($excludedcats) === 0 && count($roleexcludedcats) > 0) { $exccatlist = ' AND id NOT IN ('.implode(',', $roleexcludedcats).')'; } $arr = $this->pdo->query( sprintf('SELECT * FROM categories WHERE status = %d %s', self::STATUS_ACTIVE, $exccatlist), true, NN_CACHE_EXPIRY_LONG ); foreach ($arr as $key => $val) { if ($val['id'] === '0') { $item = $arr[$key]; unset($arr[$key]); $arr[] = $item; break; } } foreach ($arr as $a) { if (empty($a['parentid'])) { $ret[] = $a; } } foreach ($ret as $key => $parent) { $subcatlist = []; $subcatnames = []; foreach ($arr as $a) { if ($a['parentid'] === $parent['id']) { $subcatlist[] = $a; $subcatnames[] = $a['title']; } } if (count($subcatlist) > 0) { array_multisort($subcatnames, SORT_ASC, $subcatlist); $ret[$key]['subcatlist'] = $subcatlist; } else { unset($ret[$key]); } } return $ret; } /** * Return a list of categories for use in a dropdown. * * @param bool $blnIncludeNoneSelected * * @return array */ public function getForSelect($blnIncludeNoneSelected = true): array { $categories = $this->getCategories(); $temp_array = []; if ($blnIncludeNoneSelected) { $temp_array[-1] = '--Please Select--'; } foreach ($categories as $category) { $temp_array[$category['id']] = $category['title']; } return $temp_array; } /** * @param bool $activeonly * @param array $excludedcats * @return string|static */ public function getCategories($activeonly = false, array $excludedcats = []) { return $this->pdo->query( "SELECT c.id, CONCAT(cp.title, ' > ',c.title) AS title, cp.id AS parentid, c.status FROM categories c INNER JOIN categories cp ON cp.id = c.parentid ". ( $activeonly ? sprintf( ' WHERE c.status = %d %s ', self::STATUS_ACTIVE, (count($excludedcats) > 0 ? ' AND c.id NOT IN ('.implode(',', $excludedcats).')' : '') ) : '' ). ' ORDER BY c.id' ); } }