diff --git a/composer.json b/composer.json index 601f695..85a8ae3 100644 --- a/composer.json +++ b/composer.json @@ -12,8 +12,10 @@ "tagclouds" ], "require": { - "bolt/bolt": ">=2.0.0,<3.0.0" + "bolt/bolt": "^3.0" }, + "minimum-stability": "dev", + "prefer-stable": true, "authors": [ { "name": "Lodewijk Evers", @@ -22,16 +24,11 @@ } ], "autoload": { - "files": [ - "init.php" - ], "psr-4" : { - "Bolt\\Extension\\Jadwigo\\TaxonomyList\\" : "" + "Bolt\\Extension\\Jadwigo\\TaxonomyList\\" : "src" } }, "extra": { - "branch-alias": { - "dev-master": "2.0.*" - } + "bolt-class": "Bolt\\Extension\\Jadwigo\\TaxonomyList\\TaxonomyListExtension" } } diff --git a/config.yml.dist b/config/config.yml.dist similarity index 67% rename from config.yml.dist rename to config/config.yml.dist index cc3a577..8baee76 100644 --- a/config.yml.dist +++ b/config/config.yml.dist @@ -1,6 +1,6 @@ # taxonomylist settings -template: assets/taxonomylist.twig +template: taxonomylist.twig default_taxonomy: tags route_path: /taxonomies diff --git a/init.php b/init.php deleted file mode 100644 index e1a40a0..0000000 --- a/init.php +++ /dev/null @@ -1,5 +0,0 @@ -register(new Extension($app)); diff --git a/src/TaxonomyListExtension.php b/src/TaxonomyListExtension.php new file mode 100644 index 0000000..734c537 --- /dev/null +++ b/src/TaxonomyListExtension.php @@ -0,0 +1,86 @@ +getConfig(); + + $collection->match($config['route_path'], [$this, 'taxonomies']); + } + + /** + * @return array + */ + protected function getDefaultConfig() + { + return [ + 'template' => 'taxonomylist.twig', + 'default_taxonomy' => 'tags', + 'route_path' => '/taxonomies' + ]; + } + + /** + * @return array + */ + protected function registerTwigFunctions() + { + $app = $this->getContainer(); + + return [ + 'taxonomylist' => [ + [$app['taxonomylist.twig'], 'twigTaxonomyList'], + + ] + ]; + } + + /** + * @param Application $app + */ + protected function registerServices(Application $app) + { + $app['taxonomylist.twig'] = $app->share( + function ($app) { + $config = $app['extensions']->get('Jadwigo/TaxonomyList')->getConfig(); + + return new \Bolt\Extension\Jadwigo\TaxonomyList\Twig\TaxonomyListExtension($config, $app); + } + ); + } + + /** + * @param Application $app + * @param Request $request + * @return Response + */ + public function taxonomies(Application $app, Request $request) + { + $config = $this->getConfig(); + + $taxonomy = $app['config']->get('taxonomy'); + $taxonomies = array_keys($taxonomy); + + $template = $config['template']; + + $body = $this->renderTemplate($template, [ + 'taxonomies' => $taxonomies + ]); + + return new Response($body); + } +} diff --git a/Extension.php b/src/Twig/TaxonomyListExtension.php similarity index 54% rename from Extension.php rename to src/Twig/TaxonomyListExtension.php index 4a68d83..65985a9 100644 --- a/Extension.php +++ b/src/Twig/TaxonomyListExtension.php @@ -1,72 +1,33 @@ app['config']->getWhichEnd() == 'frontend') { - // Add Twig functions - if (empty($this->config['default_taxonomy'])) { - $this->config['default_taxonomy'] = 'tags'; - } - - if (empty($this->config['route_path'])) { - $this->config['route_path'] = '/taxonomies'; - } - - // Set up the routes for the sitemap.. - $this->app->match($this->config['route_path'], array($this, 'taxonomies')); - - $this->addTwigFunction('taxonomylist', 'twigTaxonomyList'); - } + $this->config = $config; + $this->app = $app; } - - public function taxonomies($xml = false) - { - $taxonomy = $this->app['config']->get('taxonomy'); - $taxonomies = array_keys($taxonomy); - - $template = $this->config['template']; - - $this->app['twig.loader.filesystem']->addPath(__DIR__); - - $body = $this->app['render']->render($template, array( - 'taxonomies' => $taxonomies - )); - - $headers = array(); - - return new Response($body, 200, $headers); - - } - - /** * Return an array with items in a taxonomy + * + * @param bool $name + * @param bool $params + * @return null */ - function twigTaxonomyList($name = false, $params = false) { - + public function twigTaxonomyList($name = false, $params = false) + { // if $name isn't set, use the one from the config.yml. Unless that's empty too, then use "tags". if (empty($name)) { if (!empty($this->config['default_taxonomy'])) { @@ -75,21 +36,18 @@ function twigTaxonomyList($name = false, $params = false) { $name = "tags"; } } - // dump($this->app['paths']); $taxonomy = $this->app['config']->get('taxonomy'); - if(array_key_exists($name, $taxonomy)) { + if (array_key_exists($name, $taxonomy)) { $named = $taxonomy[$name]; - if($params != false || $named['behaves_like']=='tags') { + if ($params != false || $named['behaves_like']=='tags') { $named = $this->getFullTaxonomy($name, $taxonomy, $params); } - if(array_key_exists('options', $named)) { - // dump($named); - foreach($named['options'] as $slug => $item) { - - if(is_array($item) && $item['name']) { + if (array_key_exists('options', $named)) { + foreach ($named['options'] as $slug => $item) { + if (is_array($item) && $item['name']) { $catname = $item['name']; $itemcount = $item['count']; } else { @@ -108,40 +66,43 @@ function twigTaxonomyList($name = false, $params = false) { 'link' => $itemlink, 'count' => $itemcount, ); - if(array_key_exists('weight', $item) && $item['weight']>=0) { + if (array_key_exists('weight', $item) && $item['weight']>=0) { $options[$slug]['weight'] = $item['weight']; $options[$slug]['weightclass'] = $item['weightclass']; } } - // dump($named); - // dump($options); return $options; } } return null; - } /** * Get the full taxonomy data from the database, count all occurences of a certain taxonomy name + * + * @param null $name + * @param null $taxonomy + * @param null $params + * @return null + * @throws \Doctrine\DBAL\DBALException */ - function getFullTaxonomy($name = null, $taxonomy = null, $params = null) { - - if(array_key_exists($name, $taxonomy)) { + protected function getFullTaxonomy($name = null, $taxonomy = null, $params = null) + { + if (array_key_exists($name, $taxonomy)) { $named = $taxonomy[$name]; // default params $limit = $weighted = $contenttype = false; - if(isset($params['limit']) && is_numeric($params['limit'])) { + if (isset($params['limit']) && is_numeric($params['limit'])) { $limit = $params['limit']; } - if(isset($params['weighted']) && $params['weighted']==true) { + if (isset($params['weighted']) && $params['weighted']==true) { $weighted = true; } - if(isset($params['contenttype']) && $params['contenttype']!="") { + if (isset($params['contenttype']) && $params['contenttype']!="") { $contenttype = $params['contenttype']; } @@ -149,36 +110,36 @@ function getFullTaxonomy($name = null, $taxonomy = null, $params = null) { $tablename = $prefix . "taxonomy"; // type of sort depending on params - if($weighted) { + if ($weighted) { $sortorder = 'count DESC'; } else { $sortorder = 'sortorder ASC'; } - if(!$contenttype) { + if (!$contenttype) { // the normal query $query = sprintf( "SELECT COUNT(name) as count, slug, name - FROM %s - WHERE taxonomytype IN ('%s') - GROUP BY name, slug, sortorder - ORDER BY %s", + FROM %s + WHERE taxonomytype IN ('%s') + GROUP BY name, slug, sortorder + ORDER BY %s", $tablename, $name, $sortorder ); - } elseif($contenttype!=false) { + } elseif ($contenttype!=false) { // TODO: get the contenttype table from the contenttype slug instead of guessing $contenttype_table = $prefix . $contenttype; // the normal query with only published items $query = sprintf( "SELECT COUNT(name) as count, slug, name - FROM %s - WHERE taxonomytype = '%s' - AND contenttype = '%s' - AND content_id IN (SELECT id FROM %s WHERE status = 'published' AND id = content_id) - GROUP BY name, slug, sortorder - ORDER BY %s", + FROM %s + WHERE taxonomytype = '%s' + AND contenttype = '%s' + AND content_id IN (SELECT id FROM %s WHERE status = 'published' AND id = content_id) + GROUP BY name, slug, sortorder + ORDER BY %s", $tablename, $name, $contenttype, @@ -188,25 +149,22 @@ function getFullTaxonomy($name = null, $taxonomy = null, $params = null) { } // append limit to query the parameter is set - if($limit) { + if ($limit) { $query .= sprintf(' LIMIT 0, %d', $limit); } - // dump($query); - // fetch results from db - $rows = $this->app['db']->executeQuery( $query )->fetchAll(); - // dump($rows); + $rows = $this->app['db']->executeQuery($query)->fetchAll(); - if($rows && ($weighted || $limit)) { + if ($rows && ($weighted || $limit)) { // find the max / min for the results $named['maxcount'] = 0; $named['number_of_tags'] = count($named['options']); - foreach($rows as $row) { - if($row['count']>=$named['maxcount']) { + foreach ($rows as $row) { + if ($row['count']>=$named['maxcount']) { $named['maxcount']= $row['count']; } - if(!isset($named['mincount']) || $row['count']<=$named['mincount']) { + if (!isset($named['mincount']) || $row['count']<=$named['mincount']) { $named['mincount']= $row['count']; } } @@ -216,20 +174,20 @@ function getFullTaxonomy($name = null, $taxonomy = null, $params = null) { // return only rows with results $populatedrows = array(); - foreach($rows as $row) { + foreach ($rows as $row) { $row['weightpercent'] = 1; // everything is all - if($named['maxcount'] != $named['mincount']) { // prevent divide by zero + if ($named['maxcount'] != $named['mincount']) { // prevent divide by zero $row['weightpercent'] = ($row['count'] - $named['mincount']) / ($named['maxcount'] - $named['mincount']); } $row['weight'] = round($row['weightpercent'] * 100); - if($row['weight']<=20) { + if ($row['weight']<=20) { $row['weightclass'] = 'xs'; - } elseif($row['weight']<=40) { + } elseif ($row['weight']<=40) { $row['weightclass'] = 's'; - } elseif($row['weight']<=60) { + } elseif ($row['weight']<=60) { $row['weightclass'] = 'm'; - } elseif($row['weight']<=80) { + } elseif ($row['weight']<=80) { $row['weightclass'] = 'l'; } else { $row['weightclass'] = 'xl'; @@ -238,18 +196,17 @@ function getFullTaxonomy($name = null, $taxonomy = null, $params = null) { $populatedrows[$row['slug']] = $row; } $named['options'] = $populatedrows; - } elseif($rows) { + } elseif ($rows) { // return all rows - so add the count to all existing rows // weight is useless here - foreach($rows as $row) { + foreach ($rows as $row) { $named['options'][$row['slug']] = $row; } } - // dump($named); return $named; } return null; } -} +} \ No newline at end of file diff --git a/assets/taxonomylist.twig b/templates/taxonomylist.twig similarity index 100% rename from assets/taxonomylist.twig rename to templates/taxonomylist.twig