1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Icybee\Modules\Nodes;
13
14 use ICanBoogie\ActiveRecord\RecordNotFound;
15 use ICanBoogie\Event;
16 use ICanBoogie\I18n;
17 use ICanBoogie\Operation\BeforeProcessEvent;
18
19 use Brickrouge\A;
20 use Brickrouge\Element;
21
22 class Hooks
23 {
24 25 26
27
28 static public function on_modules_activate(Event $event)
29 {
30 \Icybee\Modules\Nodes\Module::create_default_routes();
31 }
32
33 static public function on_modules_deactivate(Event $event)
34 {
35 \Icybee\Modules\Nodes\Module::create_default_routes();
36 }
37
38 39 40 41 42 43
44 static public function before_delete_user(BeforeProcessEvent $event, \Icybee\Modules\Users\DeleteOperation $operation)
45 {
46 global $core;
47
48 $uid = $operation->key;
49 $count = $core->models['nodes']->filter_by_uid($uid)->count;
50
51 if (!$count)
52 {
53 return;
54 }
55
56 $event->errors['uid'] = I18n\t('The user %name is used by :count nodes.', [ 'name' => $operation->record->name, ':count' => $count ]);
57 }
58
59 60 61 62 63 64
65 static public function on_user_collect_dependencies(\ICanBoogie\ActiveRecord\CollectDependenciesEvent $event, \Icybee\Modules\Users\User $target)
66 {
67 global $core;
68
69 $nodes = $core->models['nodes']
70 ->select('nid, constructor, title')
71 ->filter_by_uid($target->uid)
72 ->order('created_at DESC')
73 ->all(\PDO::FETCH_OBJ);
74
75
76
77 foreach ($nodes as $node)
78 {
79 $event->add($node->constructor, $node->nid, $node->title, true);
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 static public function markup_node(array $args, \Patron\Engine $patron, $template)
105 {
106 global $core;
107
108 $record = null;
109 $constructor = $args['constructor'] ?: 'nodes';
110 $select = $args['select'];
111
112 if ($select{0} == ':')
113 {
114 $select = substr($select, 1);
115 }
116
117 if (is_numeric($select))
118 {
119 $record = $core->models[$constructor][$select];
120 }
121 else
122 {
123 $record = $core->models[$constructor]->filter_by_slug($select)->ordered->own->one;
124 }
125
126 if (!$record)
127 {
128 throw new RecordNotFound('Unable to find record with the provided arguments: ' . json_encode($args), array());
129 }
130
131 return $patron($template, $record);
132 }
133
134 static public function markup_node_navigation(array $args, \Patron\Engine $patron, $template)
135 {
136 global $core;
137
138 $core->document->css->add(DIR . 'public/page.css');
139
140 $record = $patron->context['this'];
141
142 $list = null;
143 $cycle = null;
144
145 $list_url = $record->url('list');
146
147 if ($list_url)
148 {
149 $list = '<div class="list"><a href="' . \ICanBoogie\escape($list_url) . '">' . I18n\t('All records') . '</a></div>';
150 }
151
152 $next = null;
153 $previous = null;
154 $next_record = $record->next;
155 $previous_record = $record->previous;
156
157 if ($next_record)
158 {
159 $title = $next_record->title;
160
161 $next = new A
162 (
163 \ICanBoogie\escape(\ICanBoogie\shorten($title, 48, 1)), $next_record->url, [
164
165 'class' => "next",
166 'title' => I18n\t('Next: :title', [ ':title' => $title ])
167
168 ]
169 );
170 }
171
172 if ($previous_record)
173 {
174 $title = $previous_record->title;
175
176 $previous = new A
177 (
178 \ICanBoogie\escape(\ICanBoogie\shorten($title, 48, 1)), $previous_record->url, [
179
180 'class' => "previous",
181 'title' => I18n\t('Previous: :title', [ ':title' => $title ])
182
183 ]
184 );
185 }
186
187 if ($next || $previous)
188 {
189 $cycle = '<div class="cycle">' . $next . ' ' . $previous . '</div>';
190 }
191
192 if ($list || $cycle)
193 {
194 return '<div class="node-navigation">' . $list . $cycle . '</div>';
195 }
196 }
197
198 199 200
201
202 static public function dashboard_now()
203 {
204 global $core, $document;
205
206 $document->css->add(DIR . 'public/dashboard.css');
207
208 $counts = $core->models['nodes']->similar_site->count('constructor');
209
210 if (!$counts)
211 {
212 return '<p class="nothing">' . I18n\t('No record yet') . '</p>';
213 }
214
215 $categories = [
216
217 'contents' => [],
218 'resources' => [],
219 'other' => []
220
221 ];
222
223 $default_category = 'other';
224
225 foreach ($counts as $constructor => $count)
226 {
227 if (!isset($core->modules[$constructor]))
228 {
229 continue;
230 }
231
232 $descriptor = $core->modules->descriptors[$constructor];
233 $category = $descriptor[Module::T_CATEGORY];
234
235 if (!isset($categories[$category]))
236 {
237 $category = $default_category;
238 }
239
240 $title = I18n\t($descriptor[Module::T_TITLE], [], [ 'scope' => 'module_title' ]);
241 $title = I18n\t(strtr($constructor, '.', '_') . '.name.other', [], [ 'default' => $title ]);
242
243 $categories[$category][] = [ $title, $constructor, $count ];
244 }
245
246 $head = '';
247 $max_by_category = 0;
248
249 foreach ($categories as $category => $entries)
250 {
251 $max_by_category = max($max_by_category, count($entries));
252 $head .= '<th> </th><th>' . I18n\t($category, [], [ 'scope' => 'module_category' ]) . '</th>';
253 }
254
255 $body = '';
256 $path = $core->site->path;
257
258 for ($i = 0 ; $i < $max_by_category ; $i++)
259 {
260 $body .= '<tr>';
261
262 foreach ($categories as $category => $entries)
263 {
264 if (empty($entries[$i]))
265 {
266 $body .= '<td colspan="2"> </td>';
267
268 continue;
269 }
270
271 list($title, $constructor, $count) = $entries[$i];
272
273 $body .= <<<EOT
274 <td class="count">$count</td>
275 <td class="constructor"><a href="$path/admin/$constructor">$title</a></td>
276 EOT;
277 }
278
279 $body .= '</tr>';
280 }
281
282 return $rc = <<<EOT
283 <table>
284 <thead><tr>$head</tr></thead>
285 <tbody>$body</tbody>
286 </table>
287 EOT;
288 }
289
290 static public function dashboard_user_modified()
291 {
292 global $core, $document;
293
294 $document->css->add(DIR . 'public/dashboard.css');
295
296 $model = $core->models['nodes'];
297
298 $entries = $model
299 ->where('uid = ? AND (siteid = 0 OR siteid = ?)', [ $core->user_id, $core->site_id ])
300 ->order('updated_at desc')
301 ->limit(10)
302 ->all;
303
304 if (!$entries)
305 {
306 return '<p class="nothing">' . I18n\t('No record yet') . '</p>';
307 }
308
309 $last_date = null;
310 $context = $core->site->path;
311
312 $rc = '<table>';
313
314 foreach ($entries as $record)
315 {
316 $date = \ICanBoogie\I18n\date_period($record->updated_at);
317
318 if ($date === $last_date)
319 {
320 $date = '—';
321 }
322 else
323 {
324 $last_date = $date;
325 }
326
327 $title = \ICanBoogie\shorten($record->title, 48);
328 $title = \ICanBoogie\escape($title);
329
330 $rc .= <<<EOT
331 <tr>
332 <td class="date light">$date</td>
333 <td class="title"><a href="$context/admin/{$record->constructor}/{$record->nid}/edit">{$title}</a></td>
334 </tr>
335 EOT;
336 }
337
338 $rc .= '</table>';
339
340 return $rc;
341 }
342 }