/** * The taxonomy listing page controller. * * @param Request $request The Symfony Request * @param string $taxonomytype The taxonomy type slug * @param string $slug The taxonomy slug * * @return BoltResponse|false */ public function taxonomy(Request $request, $taxonomytype, $slug) { $taxonomy = $this->storage()->getTaxonomyType($taxonomytype); // No taxonomytype, no possible content. if (empty($taxonomy)) { return false; } else { $taxonomyslug = $taxonomy['slug']; } // First, get some content $context = $taxonomy['singular_slug'] . '_' . $slug; $page = $this->app['pager']->getCurrentPage($context); // Theme value takes precedence over default config @see https://github.com/bolt/bolt/issues/3951 $amount = $this->getOption('theme/listing_records', false) ?: $this->getOption('general/listing_records'); // Handle case where listing records has been override for specific taxonomy if (array_key_exists('listing_records', $taxonomy) && is_int($taxonomy['listing_records'])) { $amount = $taxonomy['listing_records']; } $order = $this->getOption('theme/listing_sort', false) ?: $this->getOption('general/listing_sort'); $content = $this->storage()->getContentByTaxonomy($taxonomytype, $slug, ['limit' => $amount, 'order' => $order, 'page' => $page]); if (!$this->isTaxonomyValid($content, $slug, $taxonomy)) { $this->abort(Response::HTTP_NOT_FOUND, "No slug '{$slug}' in taxonomy '{$taxonomyslug}'"); return; } $template = $this->templateChooser()->taxonomy($taxonomyslug); $name = $slug; // Look in taxonomies in 'content', to get a display value for '$slug', perhaps. foreach ($content as $record) { $flat = util::array_flatten($record->taxonomy); $key = $this->app['resources']->getUrl('root') . $taxonomy['slug'] . '/' . $slug; if (isset($flat[$key])) { $name = $flat[$key]; } $key = $this->app['resources']->getUrl('root') . $taxonomy['singular_slug'] . '/' . $slug; if (isset($flat[$key])) { $name = $flat[$key]; } } $globals = ['records' => $content, 'slug' => $name, 'taxonomy' => $this->getOption('taxonomy/' . $taxonomyslug), 'taxonomytype' => $taxonomyslug]; return $this->render($template, [], $globals); }
/** * The taxonomy listing page controller. * * @param Request $request The Symfony Request * @param string $taxonomytype The taxonomy type slug * @param string $slug The taxonomy slug * * @return BoltResponse|false */ public function taxonomy($request, $taxonomytype, $slug) { $taxonomy = $this->app['storage']->getTaxonomyType($taxonomytype); // No taxonomytype, no possible content. if (empty($taxonomy)) { return false; } else { $taxonomyslug = $taxonomy['slug']; } // First, get some content $context = $taxonomy['singular_slug'] . '_' . $slug; $pagerid = Pager::makeParameterId($context); /* @var $query \Symfony\Component\HttpFoundation\ParameterBag */ $query = $request->query; $page = $query->get($pagerid, $query->get('page', 1)); $amount = $this->getOption('general/listing_records'); $order = $this->getOption('general/listing_sort'); $content = $this->app['storage']->getContentByTaxonomy($taxonomytype, $slug, ['limit' => $amount, 'order' => $order, 'page' => $page]); // See https://github.com/bolt/bolt/pull/2310 if ($taxonomy['behaves_like'] === 'tags' && !$content || in_array($taxonomy['behaves_like'], ['categories', 'grouping']) && !in_array($slug, isset($taxonomy['options']) ? array_keys($taxonomy['options']) : [])) { $this->abort(Response::HTTP_NOT_FOUND, "No slug '{$slug}' in taxonomy '{$taxonomyslug}'"); return null; } $template = $this->templateChooser()->taxonomy($taxonomyslug); $name = $slug; // Look in taxonomies in 'content', to get a display value for '$slug', perhaps. foreach ($content as $record) { $flat = util::array_flatten($record->taxonomy); $key = $this->app['resources']->getUrl('root') . $taxonomy['slug'] . '/' . $slug; if (isset($flat[$key])) { $name = $flat[$key]; } $key = $this->app['resources']->getUrl('root') . $taxonomy['singular_slug'] . '/' . $slug; if (isset($flat[$key])) { $name = $flat[$key]; } } $globals = ['records' => $content, 'slug' => $name, 'taxonomy' => $this->getOption('taxonomy/' . $taxonomyslug), 'taxonomytype' => $taxonomyslug]; return $this->render($template, [], $globals); }
/** * The taxonomy listing page controller. * * @param Request $request The Symfony Request * @param string $taxonomytype The taxonomy type slug * @param string $slug The taxonomy slug * * @return BoltResponse|false */ public function taxonomy(Request $request, $taxonomytype, $slug) { $taxonomy = $this->storage()->getTaxonomyType($taxonomytype); // No taxonomytype, no possible content. if (empty($taxonomy)) { return false; } else { $taxonomyslug = $taxonomy['slug']; } // First, get some content $context = $taxonomy['singular_slug'] . '_' . $slug; $pagerid = Pager::makeParameterId($context); /* @var $query \Symfony\Component\HttpFoundation\ParameterBag */ $query = $request->query; $page = $query->get($pagerid, $query->get('page', 1)); // Theme value takes precedence over default config @see https://github.com/bolt/bolt/issues/3951 $amount = $this->getOption('theme/listing_records', false) ?: $this->getOption('general/listing_records'); $order = $this->getOption('theme/listing_sort', false) ?: $this->getOption('general/listing_sort'); $content = $this->storage()->getContentByTaxonomy($taxonomytype, $slug, ['limit' => $amount, 'order' => $order, 'page' => $page]); if (!$this->isTaxonomyValid($content, $slug, $taxonomy)) { $this->abort(Response::HTTP_NOT_FOUND, "No slug '{$slug}' in taxonomy '{$taxonomyslug}'"); return; } $template = $this->templateChooser()->taxonomy($taxonomyslug); $name = $slug; // Look in taxonomies in 'content', to get a display value for '$slug', perhaps. foreach ($content as $record) { $flat = util::array_flatten($record->taxonomy); $key = $this->app['resources']->getUrl('root') . $taxonomy['slug'] . '/' . $slug; if (isset($flat[$key])) { $name = $flat[$key]; } $key = $this->app['resources']->getUrl('root') . $taxonomy['singular_slug'] . '/' . $slug; if (isset($flat[$key])) { $name = $flat[$key]; } } $globals = ['records' => $content, 'slug' => $name, 'taxonomy' => $this->getOption('taxonomy/' . $taxonomyslug), 'taxonomytype' => $taxonomyslug]; return $this->render($template, [], $globals); }
/** * The taxonomy listing page controller. * * @param \Silex\Application $app The application/container * @param string $taxonomytype The taxonomy type slug * @param string $slug The taxonomy slug * * @return \Twig_Markup */ public function taxonomy(Silex\Application $app, $taxonomytype, $slug) { $taxonomy = $app['storage']->getTaxonomyType($taxonomytype); // No taxonomytype, no possible content. if (empty($taxonomy)) { return false; } else { $taxonomyslug = $taxonomy['slug']; } // First, get some content $context = $taxonomy['singular_slug'] . '_' . $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)); // Handle case where listing records has been override for specific taxonomy if (array_key_exists('listing_records', $taxonomy) && is_int($taxonomy['listing_records'])) { $amount = $taxonomy['listing_records']; } // See https://github.com/bolt/bolt/pull/2310 if ($taxonomy['behaves_like'] === 'tags' && !$content || in_array($taxonomy['behaves_like'], array('categories', 'grouping')) && !in_array($slug, isset($taxonomy['options']) ? array_keys($taxonomy['options']) : array())) { return $app->abort(Response::HTTP_NOT_FOUND, "No slug '{$slug}' in taxonomy '{$taxonomyslug}'"); } $template = $app['templatechooser']->taxonomy($taxonomyslug); $name = $slug; // Look in taxonomies in 'content', to get a display value for '$slug', perhaps. foreach ($content as $record) { $flat = util::array_flatten($record->taxonomy); $key = $app['paths']['root'] . $taxonomy['slug'] . '/' . $slug; if (isset($flat[$key])) { $name = $flat[$key]; } $key = $app['paths']['root'] . $taxonomy['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 $this->render($app, $template, $taxonomyslug); }
public static function taxonomy(Silex\Application $app, $taxonomytype, $slug) { // First, get some content $page = $app['request']->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); }
public function test_array_flatten() { $input = array('a', 'b', 'c', 'd', array('first' => 'e', 'f', 'second' => 'g', array('h', 'third' => 'i', array(array(array(array('j', 'k', 'l'))))))); $expectNoKeys = range('a', 'l'); $expectWithKeys = array('a', 'b', 'c', 'd', 'first' => 'e', 'f', 'second' => 'g', 'h', 'third' => 'i', 'j', 'k', 'l'); $this->assertEquals($expectWithKeys, util::array_flatten($input)); $this->assertEquals($expectNoKeys, util::array_flatten($input, false)); $this->assertEquals($expectWithKeys, util::array_flatten($input, true)); }