/** * @dataProvider makelinkProvider */ public function testMakelink($expected, $pageArray, $params) { $app = $this->getApp(); $_GET = $params; $app['request'] = Request::createFromGlobals(); $pager = new Pager($pageArray, $app); $this->assertEquals($expected, $pager->makelink()); }
public function getActivity($amount = 10, $minlevel = 1) { $codes = array('save content', 'login', 'logout', 'fixme', 'user'); $param = Pager::makeParameterId('activity'); /* @var $query \Symfony\Component\HttpFoundation\ParameterBag */ $query = $this->app['request']->query; $page = $query ? $query->get($param, $query->get('page', 1)) : 1; $query = sprintf("SELECT * FROM %s WHERE code IN (?) OR (level >= ?) ORDER BY date DESC", $this->tablename); $query = $this->app['db']->getDatabasePlatform()->modifyLimitQuery($query, intval($amount), intval(($page - 1) * $amount)); $params = array($codes, $minlevel); $paramTypes = array(DoctrineConn::PARAM_STR_ARRAY, \PDO::PARAM_INT); $stmt = $this->app['db']->executeQuery($query, $params, $paramTypes); $rows = $stmt->fetchAll(2); // 2 = Query::HYDRATE_COLUMN // Set up the pager $pagerQuery = sprintf("SELECT count(*) as count FROM %s WHERE code IN (?) OR (level >= ?)", $this->tablename); $params = array($codes, $minlevel); $paramTypes = array(DoctrineConn::PARAM_STR_ARRAY, \PDO::PARAM_INT); $rowcount = $this->app['db']->executeQuery($pagerQuery, $params, $paramTypes)->fetch(); $pager = array('for' => 'activity', 'count' => $rowcount['count'], 'totalpages' => ceil($rowcount['count'] / $amount), 'current' => $page, 'showing_from' => ($page - 1) * $amount + 1, 'showing_to' => ($page - 1) * $amount + count($rows)); $this->app['storage']->setPager('activity', $pager); return $rows; }
/** * Get the 'latest activity' for the dashboard. * * @param Request $request * * @return \Bolt\Response\BoltResponse */ public function latestActivity(Request $request) { // Test/get page number $param = Pager::makeParameterId('activity'); $page = $request->query ? $request->query->get($param, $request->query->get('page', 1)) : 1; $change = $this->app['logger.manager']->getActivity('change', $page, 8); $system = $this->app['logger.manager']->getActivity('system', $page, 8, ['context' => ['authentication', 'security']]); $response = $this->render('@bolt/components/panel-activity.twig', ['context' => ['change' => $change, 'system' => $system]]); $response->setPublic()->setSharedMaxAge(3600); return $response; }
/** * The search result page controller. * * @param Request $request The Symfony Request * @param array $contenttypes The content type slug(s) you want to search for * * @return BoltResponse */ public function search(Request $request, array $contenttypes = null) { $q = ''; $context = __FUNCTION__; if ($request->query->has('q')) { $q = $request->query->get('q'); } elseif ($request->query->has($context)) { $q = $request->query->get($context); } $q = Input::cleanPostedData($q, false); $param = Pager::makeParameterId($context); $page = $request->query->get($param, $request->query->get('page', 1)); $pageSize = $this->getOption('general/search_results_records') ?: ($this->getOption('general/listing_records') ?: 10); $offset = ($page - 1) * $pageSize; $limit = $pageSize; // set-up filters from URL $filters = []; foreach ($request->query->all() as $key => $value) { if (strpos($key, '_') > 0) { list($contenttypeslug, $field) = explode('_', $key, 2); if (isset($filters[$contenttypeslug])) { $filters[$contenttypeslug][$field] = $value; } else { $contenttype = $this->getContentType($contenttypeslug); if (is_array($contenttype)) { $filters[$contenttypeslug] = [$field => $value]; } } } } if (count($filters) == 0) { $filters = null; } $result = $this->app['storage']->searchContent($q, $contenttypes, $filters, $limit, $offset); $pager = ['for' => $context, 'count' => $result['no_of_results'], 'totalpages' => ceil($result['no_of_results'] / $pageSize), 'current' => $page, 'showing_from' => $offset + 1, 'showing_to' => $offset + count($result['results']), 'link' => $this->generateUrl('search', ['q' => $q]) . '&page_search=']; $this->app['storage']->setPager($context, $pager); $globals = ['records' => $result['results'], $context => $result['query']['use_q'], 'searchresult' => $result]; $template = $this->templateChooser()->search(); return $this->render($template, [], $globals); }
/** * System log overview route * * @param Request $request * * @return \Bolt\Response\BoltResponse|\Symfony\Component\HttpFoundation\RedirectResponse */ public function systemOverview(Request $request) { $action = $request->query->get('action'); if ($action == 'clear') { $this->manager()->clear('system'); $this->flashes()->success(Trans::__('The system log has been cleared.')); return $this->redirectToRoute('systemlog'); } elseif ($action == 'trim') { $this->manager()->trim('system'); $this->flashes()->success(Trans::__('The system log has been trimmed.')); return $this->redirectToRoute('systemlog'); } // Test/get page number $param = Pager::makeParameterId('activity'); $page = $request->query ? $request->query->get($param, $request->query->get('page', 1)) : 1; $options = ['level' => $request->query->get('level'), 'context' => $request->query->get('context')]; $activity = $this->manager()->getActivity('system', $page, 16, $options); return $this->render('@bolt/activity/systemlog.twig', ['entries' => $activity]); }
/** * Decodes contextual page number from current request url if found. * * @param string $context Pager id/name in url which value we find * * @return mixed Page number in context */ protected function decodePageParameter($context = '') { $param = Pager::makeParameterId($context); /* @var $query \Symfony\Component\HttpFoundation\ParameterBag */ $query = $this->app['request']->query; $page = $query ? $query->get($param, $query->get('page', 1)) : 1; return $page; }
/** * Decodes contextual page number from current request url if found. * * @param string $context Pager id/name in url which value we find * @param array $inParameters * * @return mixed Page number in context * @deprecated Not used in core anymore */ protected function decodePageParameter($context = '', $inParameters = null) { if (isset($inParameters['page']) && $inParameters['page'] !== null) { return $inParameters['page']; } else { $param = Pager::makeParameterId($context); /* @var $query \Symfony\Component\HttpFoundation\ParameterBag */ $query = $this->app['request']->query; $page = $query ? $query->get($param, $query->get('page', 1)) : 1; } return $page; }
/** * Returns an array of the parameters used in getContent for listing pages. * * @param Request $request The Symfony Request * @param string $contenttypeslug The content type slug * * @return array Parameters to use in getContent */ private function getListingParameters(Request $request, $contenttypeslug) { $contenttype = $this->getContentType(current(explode('/', $contenttypeslug))); // If the contenttype is 'viewless', don't show the listing / record page. if (isset($contenttype['viewless']) && $contenttype['viewless'] === true) { $this->abort(Response::HTTP_NOT_FOUND, "Page " . $contenttype['slug'] . " not found."); } // Build the pager $pagerid = Pager::makeParameterId($contenttype['slug']); $page = $request->query->get($pagerid, $request->query->get('page', 1)); // Theme value takes precedence over CT & default config // @see https://github.com/bolt/bolt/issues/3951 if (!($amount = $this->getOption('theme/listing_records', false))) { $amount = empty($contenttype['listing_records']) ? $this->getOption('general/listing_records') : $contenttype['listing_records']; } if (!($order = $this->getOption('theme/listing_sort', false))) { $order = empty($contenttype['sort']) ? null : $contenttype['sort']; } // If $order is not set, one of two things can happen: Either we let `getContent()` sort by itself, or we // explicitly set it to sort on the general/listing_sort setting. if ($order === null) { $taxonomies = $this->getOption('taxonomy'); $hassortorder = false; if (!empty($contenttype['taxonomy'])) { foreach ($contenttype['taxonomy'] as $contenttypetaxonomy) { if ($taxonomies[$contenttypetaxonomy]['has_sortorder']) { // We have a taxonomy with a sortorder, so we must keep $order = false, in order // to let `getContent()` handle it. We skip the fallback that's a few lines below. $hassortorder = true; } } } if (!$hassortorder) { $order = $this->getOption('general/listing_sort'); } } return ['limit' => $amount, 'order' => $order, 'page' => $page, 'paging' => true]; }
/** * The search result page controller. * * @param Request $request The Symfony Request * @param \Silex\Application $app The application/container * @param array $contenttypes The content type slug(s) you want to search for * * @return \Twig_Markup */ public function search(Request $request, Silex\Application $app, array $contenttypes = null) { $q = ''; $context = __FUNCTION__; if ($request->query->has('q')) { $q = $request->get('q'); } elseif ($request->query->has($context)) { $q = $request->get($context); } $q = Input::cleanPostedData($q, false); $param = Pager::makeParameterId($context); /* @var $query \Symfony\Component\HttpFoundation\ParameterBag */ $query = $request->query; $page = $query ? $query->get($param, $query->get('page', 1)) : 1; $config = $app['config']; $pageSize = $config->get('general/search_results_records') ?: ($config->get('general/listing_records') ?: 10); $offset = ($page - 1) * $pageSize; $limit = $pageSize; // set-up filters from URL $filters = array(); foreach ($request->query->all() as $key => $value) { if (strpos($key, '_') > 0) { list($contenttypeslug, $field) = explode('_', $key, 2); if (isset($filters[$contenttypeslug])) { $filters[$contenttypeslug][$field] = $value; } else { $contenttype = $app['storage']->getContentType($contenttypeslug); if (is_array($contenttype)) { $filters[$contenttypeslug] = array($field => $value); } } } } if (count($filters) == 0) { $filters = null; } $result = $app['storage']->searchContent($q, $contenttypes, $filters, $limit, $offset); $pager = array('for' => $context, 'count' => $result['no_of_results'], 'totalpages' => ceil($result['no_of_results'] / $pageSize), 'current' => $page, 'showing_from' => $offset + 1, 'showing_to' => $offset + count($result['results']), 'link' => $app['url_generator']->generate('search', array('q' => $q)) . '&page_search='); $app['storage']->setPager($context, $pager); $app['twig']->addGlobal('records', $result['results']); $app['twig']->addGlobal($context, $result['query']['use_q']); $app['twig']->addGlobal('searchresult', $result); $template = $app['templatechooser']->search(); return $this->render($app, $template, 'search'); }
/** * Get a specific activity log. * * @param string $log The log to query. Either 'change' or 'system' * @param integer $amount Number of results to return * @param integer $level * @param string $context * * @throws \Exception * * @return array */ public function getActivity($log, $amount = 10, $level = null, $context = null) { if ($log == 'system') { $table = $this->table_system; } elseif ($log == 'change') { $table = $this->table_change; } else { throw new \Exception("Invalid log type requested: {$log}"); } try { /** @var $reqquery \Symfony\Component\HttpFoundation\ParameterBag */ $reqquery = $this->app['request']->query; // Test/get page number $param = Pager::makeParameterId('activity'); $page = $reqquery ? $reqquery->get($param, $reqquery->get('page', 1)) : 1; // Build the base query $query = $this->app['db']->createQueryBuilder()->select('*')->from($table)->orderBy('id', 'DESC')->setMaxResults(intval($amount))->setFirstResult(intval(($page - 1) * $amount)); // Set up optional WHERE clause(s) $query = $this->setWhere($query, $level, $context); // Get the rows from the database $rows = $query->execute()->fetchAll(); // Find out how many entries we're paging form $query = $this->app['db']->createQueryBuilder()->select('COUNT(id) as count')->from($table); // Set up optional WHERE clause(s) $query = $this->setWhere($query, $level, $context); $rowcount = $query->execute()->fetch(); // Set up the pager $pager = array('for' => 'activity', 'count' => $rowcount['count'], 'totalpages' => ceil($rowcount['count'] / $amount), 'current' => $page, 'showing_from' => ($page - 1) * $amount + 1, 'showing_to' => ($page - 1) * $amount + count($rows)); $this->app['storage']->setPager('activity', $pager); } catch (DBALException $e) { // Oops. User will get a warning on the dashboard about tables that need to be repaired. $rows = array(); } if ($log == 'change') { return $this->decodeChangeLog($rows); } return $rows; }
public static function taxonomy(Silex\Application $app, $taxonomytype, $slug) { // First, get some content $context = $taxonomytype . '_' . $slug; $pagerid = Pager::makeParameterId($context); /* @var $query \Symfony\Component\HttpFoundation\ParameterBag */ $query = $app['request']->query; $page = $query->get($pagerid, $query->get('page', 1)); $amount = $app['config']->get('general/listing_records'); $order = $app['config']->get('general/listing_sort'); $content = $app['storage']->getContentByTaxonomy($taxonomytype, $slug, array('limit' => $amount, 'order' => $order, 'page' => $page)); $taxonomytype = $app['storage']->getTaxonomyType($taxonomytype); // No taxonomytype, no possible content.. if (empty($taxonomytype)) { return false; } else { $taxonomyslug = $taxonomytype['slug']; } if (!$content) { $app->abort(404, "Content for '{$taxonomyslug}/{$slug}' not found."); } $chosen = 'taxonomy'; // Set the template based on the (optional) setting in taxonomy.yml, or fall back to default listing template if ($app['config']->get('taxonomy/' . $taxonomyslug . '/listing_template')) { $template = $app['config']->get('taxonomy/' . $taxonomyslug . '/listing_template'); } else { $template = $app['config']->get('general/listing_template'); } $app['log']->setValue('templatechosen', $app['config']->get('general/theme') . "/{$template} ({$chosen})"); // Fallback: If file is not OK, show an error page $filename = $app['paths']['themepath'] . "/" . $template; if (!file_exists($filename) || !is_readable($filename)) { $error = sprintf("No template for '%s'-listing defined. Tried to use '%s/%s'.", $taxonomyslug, basename($app['config']->get('general/theme')), $template); $app['log']->setValue('templateerror', $error); $app->abort(404, $error); } $name = $slug; // Look in taxonomies in 'content', to get a display value for '$slug', perhaps. foreach ($content as $record) { $flat = \utilphp\util::array_flatten($record->taxonomy); $key = $app['paths']['root'] . $taxonomytype['slug'] . '/' . $slug; if (isset($flat[$key])) { $name = $flat[$key]; } $key = $app['paths']['root'] . $taxonomytype['singular_slug'] . '/' . $slug; if (isset($flat[$key])) { $name = $flat[$key]; } } $app['twig']->addGlobal('records', $content); $app['twig']->addGlobal('slug', $name); $app['twig']->addGlobal('taxonomy', $app['config']->get('taxonomy/' . $taxonomyslug)); $app['twig']->addGlobal('taxonomytype', $taxonomyslug); return $app['render']->render($template); }