Skip to content

Commit

Permalink
add support for the published items only
Browse files Browse the repository at this point in the history
  • Loading branch information
jadwigo committed Nov 16, 2015
1 parent f2fc6c2 commit 3349a89
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 13 deletions.
58 changes: 45 additions & 13 deletions Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function twigTaxonomyList($name = false, $params = false) {
$name = "tags";
}
}
// \Dumper::dump($this->app['paths']);
// dump($this->app['paths']);

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

Expand All @@ -86,7 +86,7 @@ function twigTaxonomyList($name = false, $params = false) {
}

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

if(is_array($item) && $item['name']) {
Expand All @@ -113,8 +113,8 @@ function twigTaxonomyList($name = false, $params = false) {
$options[$slug]['weightclass'] = $item['weightclass'];
}
}
// \Dumper::dump($named);
// \Dumper::dump($options);
// dump($named);
// dump($options);
return $options;
}
}
Expand All @@ -132,14 +132,19 @@ function getFullTaxonomy($name = null, $taxonomy = null, $params = null) {
$named = $taxonomy[$name];

// default params
$limit = $weighted = false;
$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";

Expand All @@ -150,21 +155,48 @@ function getFullTaxonomy($name = null, $taxonomy = null, $params = null) {
$sortorder = 'sortorder ASC';
}

// 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
);
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
);
}

// append limit to query the parameter is set
if($limit) {
$query .= sprintf(' LIMIT 0, %d', $limit);
}

// 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
Expand Down Expand Up @@ -214,7 +246,7 @@ function getFullTaxonomy($name = null, $taxonomy = null, $params = null) {
}
}

// \Dumper::dump($named);
// dump($named);
return $named;
}

Expand Down
21 changes: 21 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ This extension adds a twig tag for Taxonomy Listings.

You can use this extension to create category listings and tagclouds.

Simple lookup
-------------

The quick lookup only reads the taxonomy config and does not count items in the database. Usage with quick lookup:

```twig
Expand All @@ -20,6 +23,9 @@ The quick lookup only reads the taxonomy config and does not count items in the
</ul>
```

Tags lookup
-----------

If your taxonomy behaves like a `tags` category, there will automatically be a full lookup - because the individual tags are not visible in the config file.

The full lookup counts all items in the database for each category and returns this in ``{{ item.count }}``. Usage with full lookup:
Expand All @@ -38,6 +44,9 @@ The full lookup counts all items in the database for each category and returns t
</ul>
```

Tags lookup
-----------

A tagcloud, usage with limit and weight in a full lookup (it does not make sense to do a quick lookup with weighted tags, or limits):

```twig
Expand All @@ -56,3 +65,15 @@ Weighted will return the tags with the most matches first, unweighted will retur

The weighted query also returns ``{{ item.weight }}`` and ``{{ item.weightclass }}``. The ``{{ item.weight }}`` is a percentage.
The ``{{ item.weightclass }}`` is one of xl, x, m, s, xs.


Only published items
--------------------

If you only want to count the published items you will need to know the content type as well as the taxonomy.

```twig
{% set list = taxonomylist('categories', { 'limit': 10, 'weighted': true, 'contenttype': 'pages' } ) %}
```

_This might be a problem if you share taxonomies with more contentypes._

0 comments on commit 3349a89

Please sign in to comment.