1 <?php
2
3 4 5 6 7 8 9 10
11
12 use ICanBoogie\Exception;
13 use Icybee\Modules\Taxonomy\Terms\Term;
14
15 class taxonomy_support_WdMarkups
16 {
17 static public function popularity(array $args, Patron\Engine $patron, $template)
18 {
19 extract($args, EXTR_PREFIX_ALL, 'p');
20
21 $where = array();
22 $params = array();
23
24
25
26
27
28 if ($p_vocabulary)
29 {
30 $where[] = '(v.vocabulary = ? OR v.vocabularyslug = ?)';
31 $params[] = $p_vocabulary;
32 $params[] = $p_vocabulary;
33 }
34
35
36
37
38
39 if ($p_scope)
40 {
41 $parts = explode(',', $p_scope);
42 $parts = array_map('trim', $parts);
43
44 if (count($parts) > 1)
45 {
46 $where[] = 'vs.constructor IN (' . implode(', ', array_pad(array(), count($parts), '?')) . ')';
47 $params = array_merge($params, $parts);
48 }
49 else
50 {
51 $where[] = 'vs.constructor = ?';
52 $params[] = $p_scope;
53 }
54 }
55
56
57
58
59
60 global $core;
61
62 $entries = $core->db->query
63 (
64 'SELECT t.*,
65
66 (SELECT COUNT(nid) FROM {prefix}taxonomy_terms__nodes tn WHERE tn.vtid = t.vtid) AS `used`
67
68 FROM {prefix}taxonomy_vocabulary v
69 INNER JOIN {prefix}taxonomy_vocabulary__scopes vs USING(vid)
70 INNER JOIN {prefix}taxonomy_terms t USING(vid)
71
72 ' . ($where ? 'WHERE ' . implode(' AND ', $where) : '') . '
73
74 GROUP BY vtid ORDER BY term',
75
76 $params
77 )
78 ->fetchAll(PDO::FETCH_ASSOC);
79
80
81
82
83
84 foreach ($entries as $i => $entry)
85 {
86 if ($entry['used'])
87 {
88 continue;
89 }
90
91 unset($entries[$i]);
92 }
93
94
95
96
97
98 if ($p_scale)
99 {
100 $min = 0xFFFFFFFF;
101 $max = 0;
102
103 foreach ($entries as $entry)
104 {
105 $min = min($min, $entry['used']);
106 $max = max($max, $entry['used']);
107 }
108
109 $range = max($max - $min, 1);
110
111
112
113 foreach ($entries as &$entry)
114 {
115 $entry['popularity'] = 1 + round(($entry['used'] - $min) / $range * ($p_scale - 1));
116 }
117 }
118
119 return $patron($template, $entries);
120 }
121 }