1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Icybee\Modules\Taxonomy\Terms;
13
14 use ICanBoogie\ActiveRecord;
15 use ICanBoogie\Event;
16 use ICanBoogie\Exception;
17
18 class Hooks
19 {
20 static public function on_nodes_delete(Event $event)
21 {
22 global $core;
23
24 $core->models['taxonomy.terms/nodes']->filter_by_nid($event->rc)->delete();
25 }
26
27 static public function markup_terms(array $args, \Patron\Engine $patron, $template)
28 {
29 global $core;
30
31 if (isset($args['scope']))
32 {
33 throw new Exception('The "scope" parameter is deprecated, use "construtor" instead.');
34
35 $args['constructor'] = $args['scope'];
36 }
37
38 $conditions = array();
39 $conditions_args = array();
40
41 $inner = ' INNER JOIN {prefix}taxonomy_terms term USING(vid)';
42
43 $constructor = $args['constructor'];
44
45 if ($constructor)
46 {
47 $inner .= ' INNER JOIN {prefix}taxonomy_vocabulary__scopes USING(vid)';
48
49 $conditions[] = 'constructor = ?';
50 $conditions_args[] = $constructor;
51 }
52
53 $vocabulary = $args['vocabulary'];
54
55 if ($vocabulary)
56 {
57 if (is_numeric($vocabulary))
58 {
59 $conditions[] = 'vid = ?';
60 $conditions_args[] = $vocabulary;
61 }
62 else
63 {
64 $conditions[] = '(vocabulary = ? OR vocabularyslug = ?)';
65 $conditions_args[] = $vocabulary;
66 $conditions_args[] = $vocabulary;
67 }
68 }
69
70 $conditions[] = '(SELECT GROUP_CONCAT(nid) FROM {prefix}taxonomy_terms__nodes tnode
71 INNER JOIN {prefix}nodes node USING(nid)
72 WHERE is_online = 1 AND tnode.vtid = term.vtid) IS NOT NULL';
73
74
75 $where = $conditions ? ' WHERE ' . implode(' AND ', $conditions) : null;
76
77 $model = $core->models['taxonomy.terms'];
78
79 $entries = $model->query
80 (
81 'SELECT voc.*, term.*,
82
83 (SELECT GROUP_CONCAT(nid) FROM {prefix}taxonomy_terms__nodes tnode
84 INNER JOIN {prefix}nodes node USING(nid)
85 WHERE is_online = 1 AND tnode.vtid = term.vtid
86 ORDER BY tnode.weight) AS nodes_ids
87
88 FROM {prefix}taxonomy_vocabulary voc' . $inner . $where . ' ORDER BY term.weight, term',
89
90 $conditions_args
91 )
92 ->fetchAll(\PDO::FETCH_CLASS, 'Icybee\Modules\Taxonomy\Terms\Term', array($model));
93
94 if ($constructor)
95 {
96 foreach ($entries as $entry)
97 {
98 $entry->nodes_constructor = $constructor;
99 }
100 }
101
102 return $patron($template, $entries);
103 }
104
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
124 static public function markup_nodes(array $args, \Patron\Engine $patron, $template)
125 {
126 global $core;
127
128 $term = $patron->context['this'];
129 $order = $args['order'];
130
131 if ($term instanceof \Icybee\Modules\Taxonomy\Terms\Term)
132 {
133 $constructor = $term->nodes_constructor;
134 $order = $args['order'] ? strtr($args['order'], ':', ' ') : 'FIELD (nid, ' . $term->nodes_ids . ')';
135
136 $entries = $core->models[$constructor]->where('is_online = 1 AND nid IN(' . $term->nodes_ids . ')')->order($order)->all;
137
138 $taxonomy_property = $term->vocabularyslug;
139 $taxonomy_property_slug = $taxonomy_property . 'slug';
140
141 foreach ($entries as $entry)
142 {
143 $entry->$taxonomy_property = $term;
144 $entry->$taxonomy_property_slug = $term->termslug;
145 }
146 }
147 else
148 {
149 $term = $args['term'];
150 $vocabulary = $args['vocabulary'];
151 $constructor = $args['constructor'];
152
153 $vocabulary = $core->models['taxonomy.vocabulary']
154 ->joins('INNER JOIN {self}__scopes USING(vid)')
155 ->joins('INNER JOIN {prefix}taxonomy_terms USING(vid)')
156 ->where('vocabularyslug = ? AND constructor = ? AND termslug = ?', $vocabulary, $constructor, $term)
157 ->one;
158
159 $patron->context['self']['vocabulary'] = $vocabulary;
160
161 $ids = $core->db->query
162 (
163 'SELECT nid FROM {prefix}taxonomy_vocabulary voc
164 INNER JOIN {prefix}taxonomy_vocabulary__scopes scopes USING(vid)
165 INNER JOIN {prefix}taxonomy_terms term USING(vid)
166 INNER JOIN {prefix}taxonomy_terms__nodes tnode USING(vtid)
167 WHERE constructor = ? AND term.termslug = ?', array
168 (
169 $constructor, $term
170 )
171 )
172 ->fetchAll(\PDO::FETCH_COLUMN);
173
174 if (!$ids)
175 {
176 return;
177 }
178
179 $limit = $args['limit'];
180 $offset = (isset($args['page']) ? $args['page'] : 0) * $limit;
181
182 $arr = $core->models[$constructor]
183 ->where(array('is_online' => true, 'nid' => $ids))
184 ->order($order);
185
186 $count = $arr->count;
187 $entries = $arr->limit($offset, $limit)->all;
188
189 $patron->context['self']['range'] = array
190 (
191 'count' => $count,
192 'limit' => $limit,
193 'page' => isset($args['page']) ? $args['page'] : 0
194 );
195 }
196
197 return $patron($template, $entries);
198 }
199 }