null, ]; $options += $defaults; $this->pdo = ($options['Settings'] instanceof Settings ? $options['Settings'] : new Settings()); } /** * Parse category search constraints * * @param array $cat * * @return string $catsrch */ public function getCategorySearch($cat = array()) { $catsrch = ' ('; foreach ($cat as $category) { $chlist = '-99'; if ($category != -1 && $this->isParent($category)) { $children = $this->getChildren($category); foreach ($children as $child) { $chlist .= ', ' . $child['id']; } } if ($chlist != '-99') { $catsrch .= ' r.categoryid IN (' . $chlist . ') OR '; } else { $catsrch .= sprintf(' r.categoryid = %d OR ', $category); } $catsrch .= '1=2 )'; } return $catsrch; } /** * Determine if a category is a parent. */ public function isParent($cid) { $db = new newznab\db\Settings(); $ret = $db->queryOneRow(sprintf("select count(*) as count from category where id = %d and parentid is null", $cid), true); if ($ret['count']) return true; else return false; } /** * Get a list of all child categories for a parent. */ public function getChildren($cid) { $db = new newznab\db\Settings(); return $db->query(sprintf("select c.* from category c where parentid = %d", $cid), true); } /** * Get a list of categories and their parents. */ public function getFlat($activeonly = false) { $db = new newznab\db\Settings(); $act = ""; if ($activeonly) $act = sprintf(" where c.status = %d ", Category::STATUS_ACTIVE); return $db->query("select c.*, (SELECT title FROM category WHERE id=c.parentid) AS parentName from category c " . $act . " ORDER BY c.id"); } /** * Get names of enabled parent categories. * * @return array */ public function getEnabledParentNames() { $db = new newznab\db\Settings(); return $db->query("SELECT title FROM category WHERE parentid IS NULL AND status = 1"); } /** * Returns category id's for site disabled categories. * * @return array */ public function getDisabledIDs() { $db = new newznab\db\Settings(); return $db->query("SELECT id FROM category WHERE status = 2 OR parentid IN (SELECT id FROM category WHERE status = 2 AND parentid IS NULL)"); } /** * Get a category row by its id. */ public function getById($id) { $db = new newznab\db\Settings(); return $db->queryOneRow(sprintf("SELECT c.disablepreview, c.id, c.description, c.minsizetoformrelease, c.maxsizetoformrelease, CONCAT(COALESCE(cp.title,'') , CASE WHEN cp.title IS NULL THEN '' ELSE ' > ' END , c.title) as title, c.status, c.parentid from category c left outer join category cp on cp.id = c.parentid where c.id = %d", $id)); } public function getSizeRangeById($id) { $db = new newznab\db\Settings(); $res = $db->queryOneRow(sprintf("SELECT c.minsizetoformrelease, c.maxsizetoformrelease, cp.minsizetoformrelease as p_minsizetoformrelease, cp.maxsizetoformrelease as p_maxsizetoformrelease" . " from category c left outer join category cp on cp.id = c.parentid where c.id = %d", $id ) ); if (!$res) return null; $min = intval($res['minsizetoformrelease']); $max = intval($res['maxsizetoformrelease']); if ($min == 0 && $max == 0) { # Size restriction disabled; now check parent $min = intval($res['p_minsizetoformrelease']); $max = intval($res['p_maxsizetoformrelease']); if ($min == 0 && $max == 0) { # no size restriction return null; } else if ($max > 0) { $min = 0; $max = intval($res['p_maxsizetoformrelease']); } else { $min = intval($res['p_minsizetoformrelease']); $max = PHP_INT_MAX; } } else if ($max > 0) { $min = 0; $max = intval($res['maxsizetoformrelease']); } else { $min = intval($res['minsizetoformrelease']); $max = PHP_INT_MAX; } # If code reaches here, then content is enabled return array('min' => $min, 'max' => $max); } /* * Return min/max size range (in array(min, max)) otherwise, none is returned * if no size restrictions are set */ /** * Get a list of categories by an array of IDs. */ public function getByIds($ids) { $db = new newznab\db\Settings(); return $db->query(sprintf("SELECT concat(cp.title, ' > ',c.title) as title from category c inner join category cp on cp.id = c.parentid where c.id in (%s)", implode(',', $ids))); } public function getNameByID($ID) { $db = new newznab\db\Settings(); $parent = $db->queryOneRow(sprintf("SELECT title FROM category WHERE id = %d", substr($ID, 0, 1) . "000")); $cat = $db->queryOneRow(sprintf("SELECT title FROM category WHERE id = %d", $ID)); return $parent["title"] . " " . $cat["title"]; } /** * Update a category. */ public function update($id, $status, $desc, $disablepreview, $minsize, $maxsize) { $db = new newznab\db\Settings(); return $db->queryExec(sprintf("update category set disablepreview = %d, status = %d, minsizetoformrelease = %d, maxsizetoformrelease = %d, description = %s where id = %d", $disablepreview, $status, $minsize, $maxsize, $db->escapeString($desc), $id)); } /** * Get the categories in a format for use by the headermenu.tpl. */ public function getForMenu($excludedcats = array()) { $db = new newznab\db\Settings(); $ret = array(); $exccatlist = ""; if (count($excludedcats) > 0) $exccatlist = " and id not in (" . implode(",", $excludedcats) . ")"; $arr = $db->query(sprintf("select * from category where status = %d %s", Category::STATUS_ACTIVE, $exccatlist), true); foreach ($arr as $a) if ($a["parentid"] == "") $ret[] = $a; foreach ($ret as $key => $parent) { $subcatlist = array(); $subcatnames = array(); 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. */ public function getForSelect($blnIncludeNoneSelected = true) { $categories = $this->get(); $temp_array = array(); if ($blnIncludeNoneSelected) { $temp_array[-1] = "--Please Select--"; } foreach ($categories as $category) $temp_array[$category["id"]] = $category["title"]; return $temp_array; } /** * Get a list of categories. */ public function get($activeonly = false, $excludedcats = array()) { $db = new newznab\db\Settings(); $exccatlist = ""; if (count($excludedcats) > 0) $exccatlist = " and c.id not in (" . implode(",", $excludedcats) . ")"; $act = ""; if ($activeonly) $act = sprintf(" where c.status = %d ", Category::STATUS_ACTIVE); if ($exccatlist != "") $act .= $exccatlist; return $db->query("select c.id, concat(cp.title, ' > ',c.title) as title, cp.id as parentid, c.status from category c inner join category cp on cp.id = c.parentid " . $act . " ORDER BY c.id", true); } }