1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Icybee\Modules\Articles;
13
14 use ICanBoogie\Event;
15
16
17
18 class Hooks
19 {
20 static public function markup_articles(array $args, \Patron\Engine $engine, $template)
21 {
22 global $core;
23
24 extract($args, EXTR_PREFIX_ALL, 'attr');
25
26
27
28
29
30
31
32
33 $options = $args;
34
35
36
37
38
39 $where = array();
40 $params = array();
41
42 if ($attr_author)
43 {
44 $where[] = '(SELECT username FROM {prefix}user_users WHERE uid = node.uid) = ?';
45 $params[] = $attr_author;
46 }
47
48 if ($attr_date)
49 {
50 $names = array('YEAR', 'MONTH', 'DAY');
51
52 if (preg_match('#(\d{4})?-(\d{2})?#', $attr_date, $match))
53 {
54
55
56 array_shift($match);
57
58 foreach ($match as $key => $value)
59 {
60 $where[] = $names[$key] . '(`date`) = ?';
61 $params[] = $value;
62 }
63 }
64 }
65
66 $where[] = 'is_online = 1';
67
68
69
70
71
72 $arq = $core->models['articles']->where(implode(' AND ', $where), $params);
73
74 $count = $arq->count;
75
76 $options['count'] = $count;
77 $options['pages'] = $attr_limit ? ceil($count / $attr_limit) : 1;
78
79 80 81 82 83 84 85 86 87
88
89
90
91
92
93 if ($attr_order == 'random')
94 {
95 $arq->order('rand()');
96 }
97 else if ($attr_by)
98 {
99 $arq->order("$attr_by $attr_order");
100 }
101
102 if ($attr_limit)
103 {
104 $arq->limit($attr_page * $attr_limit, $attr_limit);
105 }
106
107 $entries = $arq->all;
108
109 new \BlueTihi\Context\LoadedNodesEvent($engine->context, $entries);
110
111
112
113
114
115 $engine->context['self']['range'] = array
116 (
117 'count' => $count,
118 'limit' => $attr_limit,
119 'page' => $attr_page
120 );
121
122 return $engine($template, $entries);
123 }
124
125 static public function markup_articles_read(array $args, \Patron\Engine $patron, $template)
126 {
127 global $core;
128
129 $limit = $args['limit'];
130 $constructor = 'articles';
131
132 $hits = $core->models['feedback.hits']->query
133 (
134 'SELECT hit.*, (hits / (TO_DAYS(CURRENT_DATE) - TO_DAYS(first))) AS perday
135 FROM {self} as hit
136 INNER JOIN {prefix}nodes USING(nid)
137 WHERE is_online = 1 AND constructor = ?
138 ORDER BY hits DESC LIMIT ' . $limit, array
139 (
140 $constructor
141 )
142 )
143 ->fetchAll(\PDO::FETCH_OBJ);
144
145 $nids = array();
146
147 foreach ($hits as $hit)
148 {
149 $nids[$hit->nid] = $hit;
150 }
151
152 $entries = $core->models[$constructor]->find(array_keys($nids));
153
154 foreach ($entries as $entry)
155 {
156 $nids[$entry->nid]->node = $entry;
157 }
158
159 return $patron($template, array_values($nids));
160 }
161
162
163
164 static public function markup_by_date(array $args, \Patron\Engine $patron, $template)
165 {
166 global $core;
167
168 extract($args, EXTR_PREFIX_ALL, 'p');
169
170 $query = 'node.*, article.* FROM {prefix}nodes node
171 INNER JOIN {prefix}contents article USING(nid) WHERE is_online = 1';
172 $params = array();
173
174 if ($p_group)
175 {
176 $query = 'DATE_FORMAT(`date`, ?), ' . $query;
177 $params[] = $p_group;
178 }
179
180 $query .= ' ORDER BY `date` ' . $p_order;
181
182 if ($p_limit)
183 {
184 $query .= " LIMIT $p_start, $p_limit";
185 }
186 else if ($p_start)
187 {
188 $query .= " LIMIT $p_start";
189 }
190
191 $model = $core->models['articles'];
192
193 $entries = $model->query('SELECT ' . $query, $params)
194 ->fetchAll($p_group ? \PDO::FETCH_GROUP | \PDO::FETCH_CLASS : \PDO::FETCH_CLASS, __NAMESPACE__ . '\Article', array($model));
195
196 return $patron($template, $entries);
197 }
198
199
200
201
202 static public function markup_by_author(array $args, \Patron\Engine $patron, $template)
203 {
204 global $core;
205
206 $model = $core->models['articles'];
207
208 $entries = $model->query
209 (
210 'SELECT username, node.*, article.*
211 FROM {prefix}nodes node
212 INNER JOIN {self} article USING(nid)
213 INNER JOIN {prefix}users USING(uid)
214 WHERE is_online = 1 ORDER BY `username`, `date` DESC'
215 )
216 ->fetchAll(\PDO::FETCH_GROUP | \PDO::FETCH_CLASS, __NAMESPACE__ . '\Article', array($model));
217
218 return $patron($template, $entries);
219 }
220 }