forked from jadwigo/TaxonomyList
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Extension.php
216 lines (176 loc) · 7.12 KB
/
Extension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
// Taxonomy listing Extension for Bolt, by Lodewijk Evers
namespace Bolt\Extension\Jadwigo\TaxonomyList;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Bolt\Extensions\Snippets\Location as SnippetLocation;
class Extension extends \Bolt\BaseExtension
{
const NAME = 'TaxonomyList';
/**
* Provide default Extension Name
*/
public function getName()
{
return Extension::NAME;
}
/**
* Initialize TaxonomyList. Called during bootstrap phase.
*/
public function initialize()
{
if ($this->app['config']->getWhichEnd() == 'frontend') {
// Add Twig functions
if (empty($this->config['default_taxonomy'])) {
$this->config['default_taxonomy'] = 'tags';
}
// Set up the routes for the sitemap..
$this->app->match("/taxonomies", array($this, 'taxonomies'));
$this->addTwigFunction('taxonomylist', 'twigTaxonomyList');
}
}
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
*/
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";
}
}
// \Dumper::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);
}
if(array_key_exists('options', $named)) {
// \Dumper::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'];
}
}
// \Dumper::dump($named);
// \Dumper::dump($options);
return $options;
}
}
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 = false;
if(isset($params['limit']) && is_numeric($params['limit'])) {
$limit = $params['limit'];
}
if(isset($params['weighted']) && $params['weighted']==true) {
$weighted = true;
}
$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';
}
// the normal query
$query = sprintf(
"SELECT COUNT(name) as count, slug, name FROM %s WHERE taxonomytype IN ('%s') GROUP BY name ORDER BY %s",
$tablename,
$name,
$sortorder
);
// append limit to query the parameter is set
if($limit) {
$query .= sprintf(' LIMIT 0, %d', $limit);
}
// fetch results from db
$rows = $this->app['db']->executeQuery( $query )->fetchAll();
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'] = ($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;
}
}
// \Dumper::dump($named);
return $named;
}
return null;
}
}