Skip to content

Commit

Permalink
Updated extension file with proper code and formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
Raistlfiren committed May 25, 2016
1 parent 1857947 commit 6664033
Showing 1 changed file with 24 additions and 203 deletions.
227 changes: 24 additions & 203 deletions src/TaxonomyListExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
use Silex\ControllerCollection;
use Bolt\Extension\SimpleExtension;
use Silex\Application;
use Bolt\Extensions\Snippets\Location as SnippetLocation;

class TaxonomyListExtension extends SimpleExtension
{
protected function registerFrontendRoutes(ControllerCollection $collection)
{
$config = $this->getConfig();

$collection->match($config['route_path'], [$this, 'taxonomies']);
}

Expand All @@ -30,219 +29,41 @@ protected function getDefaultConfig()

protected function registerTwigFunctions()
{
return [
'taxonomylist' => 'twigTaxonomyList'
];
}

public function taxonomies(Application $app, Request $request, $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();
$app = $this->getContainer();

return new Response($body, 200, $headers);
return [
'taxonomylist' => [
[$app['taxonomylist.twig'], 'twigTaxonomyList'],

]
];
}


/**
* Return an array with items in a taxonomy
*/
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'])) {
$name = $this->config['default_taxonomy'];
} else {
$name = "tags";
}
}
// dump($this->app['paths']);

$taxonomy = $this->app['config']->get('taxonomy');

if(array_key_exists($name, $taxonomy)) {
$named = $taxonomy[$name];
if($params != false || $named['behaves_like']=='tags') {
$named = $this->getFullTaxonomy($name, $taxonomy, $params);
}
protected function registerServices(Application $app)
{
$app['taxonomylist.twig'] = $app->share(
function ($app) {
$config = $app['extensions']->get('Jadwigo/TaxonomyList')->getConfig();

if(array_key_exists('options', $named)) {
// dump($named);
foreach($named['options'] as $slug => $item) {

if(is_array($item) && $item['name']) {
$catname = $item['name'];
$itemcount = $item['count'];
} else {
$catname = $item;
$itemcount = null;
$item = array(
'name' => $catname,
'count' => null
);
}
$itemlink = $this->app['paths']['root'].$name .'/'.$slug;

$options[$slug] = array(
'slug' => $slug,
'name' => $catname,
'link' => $itemlink,
'count' => $itemcount,
);
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 new \Bolt\Extension\Jadwigo\TaxonomyList\Twig\TaxonomyListExtension($config, $app);
}
}

return null;

);
}

/**
* Get the full taxonomy data from the database, count all occurences of a certain taxonomy name
*/
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'])) {
$limit = $params['limit'];
}

if(isset($params['weighted']) && $params['weighted']==true) {
$weighted = true;
}

if(isset($params['contenttype']) && $params['contenttype']!="") {
$contenttype = $params['contenttype'];
}

$prefix = $this->app['config']->get('general/database/prefix', 'bolt_');
$tablename = $prefix . "taxonomy";

// type of sort depending on params
if($weighted) {
$sortorder = 'count DESC';
} else {
$sortorder = 'sortorder ASC';
}

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",
$tablename,
$name,
$sortorder
);
} 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",
$tablename,
$name,
$contenttype,
$contenttype_table,
$sortorder
);
}
public function taxonomies(Application $app, Request $request)
{
$config = $this->getConfig();

// append limit to query the parameter is set
if($limit) {
$query .= sprintf(' LIMIT 0, %d', $limit);
}
$taxonomy = $app['config']->get('taxonomy');
$taxonomies = array_keys($taxonomy);

// dump($query);

// fetch results from db
$rows = $this->app['db']->executeQuery( $query )->fetchAll();
// dump($rows);

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']) {
$named['maxcount']= $row['count'];
}
if(!isset($named['mincount']) || $row['count']<=$named['mincount']) {
$named['mincount']= $row['count'];
}
}

$named['deltacount'] = $named['maxcount'] - $named['mincount'] + 1;
$named['stepsize'] = $named['deltacount'] / 5;

// return only rows with results
$populatedrows = array();
foreach($rows as $row) {
$row['weightpercent'] = 1; // everything is all
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) {
$row['weightclass'] = 'xs';
} elseif($row['weight']<=40) {
$row['weightclass'] = 's';
} elseif($row['weight']<=60) {
$row['weightclass'] = 'm';
} elseif($row['weight']<=80) {
$row['weightclass'] = 'l';
} else {
$row['weightclass'] = 'xl';
}

$populatedrows[$row['slug']] = $row;
}
$named['options'] = $populatedrows;
} elseif($rows) {
// return all rows - so add the count to all existing rows
// weight is useless here
foreach($rows as $row) {
$named['options'][$row['slug']] = $row;
}
}
$template = $config['template'];

// dump($named);
return $named;
}
$body = $this->renderTemplate($template, [
'taxonomies' => $taxonomies
]);

return null;
return new Response($body);
}
}

0 comments on commit 6664033

Please sign in to comment.