addPolicy(new ContextRequiredPolicy($request)); return parent::authorize($request, $args, $roleAssignments); } /** * View the content of a category. * * @param array $args [ * * @option string Category path * @option int Page number if available * ] * * @param PKPRequest $request */ public function category($args, $request) { $page = isset($args[1]) ? (int) $args[1] : 1; $templateMgr = TemplateManager::getManager($request); $context = $request->getContext(); // Get the category $category = Repo::category()->getCollector() ->filterByPaths([$args[0]]) ->filterByContextIds([$context->getId()]) ->getMany() ->first(); if (!$category) { $this->getDispatcher()->handle404(); } $this->setupTemplate($request); $orderOption = $category->getSortOption() ? $category->getSortOption() : Collector::ORDERBY_DATE_PUBLISHED . '-' . DAO::SORT_DIRECTION_DESC; [$orderBy, $orderDir] = explode('-', $orderOption); $count = $context->getData('itemsPerPage') ? $context->getData('itemsPerPage') : Config::getVar('interface', 'items_per_page'); $offset = $page > 1 ? ($page - 1) * $count : 0; $collector = Repo::submission() ->getCollector() ->filterByContextIds([$context->getId()]) ->filterByCategoryIds([$category->getId()]) ->filterByStatus([Submission::STATUS_PUBLISHED]) ->orderBy($orderBy, $orderDir === DAO::SORT_DIRECTION_ASC ? Collector::ORDER_DIR_ASC : Collector::ORDER_DIR_DESC); // Featured items are only in OMP at this time if (method_exists($collector, 'orderByFeatured')) { $collector->orderByFeatured(true); } $total = $collector->getCount(); $submissions = $collector->limit($count)->offset($offset)->getMany(); // Provide the parent category and a list of subcategories $parentCategory = $category->getParentId() ? Repo::category()->get($category->getParentId()) : null; $subcategories = Repo::category()->getCollector() ->filterByParentIds([$category->getId()]) ->getMany(); $this->_setupPaginationTemplate($request, count($submissions), $page, $count, $offset, $total); $templateMgr->assign([ 'category' => $category, 'parentCategory' => $parentCategory, 'subcategories' => iterator_to_array($subcategories), 'publishedSubmissions' => $submissions->toArray(), 'authorUserGroups' => Repo::userGroup()->getCollector() ->filterByRoleIds([Role::ROLE_ID_AUTHOR]) ->filterByContextIds([$context->getId()])->getMany()->remember(), ]); return $templateMgr->display('frontend/pages/catalogCategory.tpl'); } /** * Serve the full sized image for a category. * * @param array $args * @param PKPRequest $request */ public function fullSize($args, $request) { switch ($request->getUserVar('type')) { case 'category': $context = $request->getContext(); $category = Repo::category()->get((int) $request->getUserVar('id')); if (!$category || $category->getContextId() != $context->getId()) { $this->getDispatcher()->handle404(); } $imageInfo = $category->getImage(); $contextFileManager = new ContextFileManager($context->getId()); $contextFileManager->downloadByPath($contextFileManager->getBasePath() . '/categories/' . $imageInfo['name'], null, true); break; default: fatalError('invalid type specified'); } } /** * Serve the thumbnail for a category. * * @param array $args * @param PKPRequest $request */ public function thumbnail($args, $request) { switch ($request->getUserVar('type')) { case 'category': $context = $request->getContext(); $category = Repo::category()->get((int) $request->getUserVar('id')); if (!$category || $category->getContextId() != $context->getId()) { $this->getDispatcher()->handle404(); } $imageInfo = $category->getImage(); $contextFileManager = new ContextFileManager($context->getId()); $contextFileManager->downloadByPath($contextFileManager->getBasePath() . '/categories/' . $imageInfo['thumbnailName'], null, true); break; default: fatalError('invalid type specified'); } } /** * Assign the pagination template variables * * @param PKPRequest $request * @param int $submissionsCount Number of monographs being shown * @param int $page Page number being shown * @param int $count Max number of monographs being shown * @param int $offset Starting position of monographs * @param int $total Total number of monographs available */ protected function _setupPaginationTemplate($request, $submissionsCount, $page, $count, $offset, $total) { $showingStart = $offset + 1; $showingEnd = min($offset + $count, $offset + $submissionsCount); $nextPage = $total > $showingEnd ? $page + 1 : null; $prevPage = $showingStart > 1 ? $page - 1 : null; $templateMgr = TemplateManager::getManager($request); $templateMgr->assign([ 'showingStart' => $showingStart, 'showingEnd' => $showingEnd, 'total' => $total, 'nextPage' => $nextPage, 'prevPage' => $prevPage, ]); } }