1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Icybee\Modules\Users;
13
14 use ICanBoogie\ActiveRecord\Query;
15 use ICanBoogie\I18n;
16
17 class ManageBlock extends \Icybee\ManageBlock
18 {
19 static protected function add_assets(\Brickrouge\Document $document)
20 {
21 parent::add_assets($document);
22
23 $document->css->add(DIR . 'public/admin.css');
24 $document->js->add('manage.js');
25 }
26
27 public function __construct($module, array $attributes=[])
28 {
29 parent::__construct($module, $attributes + [
30
31 self::T_COLUMNS_ORDER => array(User::USERNAME, User::IS_ACTIVATED, User::EMAIL, 'roles', User::CREATED_AT, User::LOGGED_AT),
32 self::T_ORDER_BY => array('created_at', 'desc')
33
34 ]);
35 }
36
37 38 39 40 41 42 43 44 45 46
47 protected function get_available_columns()
48 {
49 return array_merge(parent::get_available_columns(), [
50
51 User::USERNAME => __CLASS__ . '\UsernameColumn',
52 User::EMAIL => __CLASS__ . '\EmailColumn',
53 'roles' => __CLASS__ . '\RolesColumn',
54 User::CREATED_AT => 'Icybee\ManageBlock\DateTimeColumn',
55 User::LOGGED_AT => __CLASS__ . '\LoggedAtColumn',
56 User::IS_ACTIVATED => __CLASS__ . '\IsActivatedColumn'
57
58 ]);
59 }
60
61 62 63
64 protected function alter_query(Query $query, array $filters)
65 {
66 return parent::alter_query($query, $filters)
67 ->filter_by_constructor($this->module->id);
68 }
69
70 71 72 73 74 75
76 protected function get_available_jobs()
77 {
78 return array_merge(parent::get_available_jobs(), [
79
80 Module::OPERATION_ACTIVATE => I18n\t('activate.operation.title'),
81 Module::OPERATION_DEACTIVATE => I18n\t('deactivate.operation.title')
82
83 ]);
84 }
85 }
86
87 namespace Icybee\Modules\Users\ManageBlock;
88
89 use ICanBoogie\ActiveRecord\Model;
90 use ICanBoogie\ActiveRecord\Query;
91 use ICanBoogie\ActiveRecord\RecordNotFound;
92 use ICanBoogie\I18n;
93
94 use Brickrouge\Element;
95
96 use Icybee\ManageBlock\BooleanColumn;
97 use Icybee\ManageBlock\Column;
98 use Icybee\ManageBlock\DateTimeColumn;
99 use Icybee\ManageBlock\EditDecorator;
100 use Icybee\ManageBlock\FilterDecorator;
101
102 103 104
105 class UsernameColumn extends Column
106 {
107 public function render_cell($record)
108 {
109 $label = $record->username;
110 $name = $record->name;
111
112 if ($label != $name)
113 {
114 $label .= ' <small>(' . $name . ')</small>';
115 }
116
117 return new Element('a', [
118
119 Element::INNER_HTML => $label,
120
121 'class' => 'edit',
122 'href' => \ICanBoogie\Routing\contextualize("/admin/{$record->constructor}/{$record->uid}/edit"),
123 'title' => I18n\t('manage.edit')
124
125 ]);
126 }
127 }
128
129 130 131
132 class EmailColumn extends Column
133 {
134 public function render_cell($record)
135 {
136 $email = $record->email;
137
138 return '<a href="mailto:' . $email . '" title="' . $this->manager->t('Send an E-mail') . '">' . $email . '</a>';
139 }
140 }
141
142 143 144
145 class RolesColumn extends Column
146 {
147 public function render_cell($record)
148 {
149 if ($record->uid == 1)
150 {
151 return '<em>Admin</em>';
152 }
153 else if ($record->roles)
154 {
155 $label = '';
156
157 foreach ($record->roles as $role)
158 {
159 if ($role->rid == 2)
160 {
161 continue;
162 }
163
164 $label .= ', ' . $role->name;
165 }
166
167 $label = substr($label, 2);
168 }
169
170 return $label;
171 }
172 }
173
174 175 176
177 class LoggedAtColumn extends DateTimeColumn
178 {
179 public function render_cell($record)
180 {
181 $logged_at = $record->logged_at;
182
183 if ($logged_at->is_empty)
184 {
185 return '<em class="small">' . $this->manager->t("Never connected") . '</em>';
186 }
187
188 return parent::render_cell($record);
189 }
190 }
191
192 193 194
195 class IsActivatedColumn extends BooleanColumn
196 {
197 public function __construct(\Icybee\ManageBlock $manager, $id, array $options=[])
198 {
199 parent::__construct($manager, $id, $options + [
200
201 'title' => null,
202 'class' => 'cell-fitted',
203 'filters' => [
204
205 'options' => [
206
207 '=1' => 'Activated',
208 '=0' => 'Deactivated'
209
210 ]
211
212 ]
213
214 ]);
215 }
216
217 public function render_cell($record)
218 {
219 if ($record->is_admin)
220 {
221 return;
222 }
223
224 return parent::render_cell($record);
225 }
226 }
227
228 229 230 231 232
233 class UserColumn extends Column
234 {
235 236 237 238 239
240 private $user_cache;
241
242 243 244 245 246 247 248
249 private $resolved_user_names;
250
251 252 253
254 public function __construct(\Icybee\ManageBlock $manager, $id, array $options=[])
255 {
256 parent::__construct($manager, $id, $options + [
257
258 'title' => 'User',
259 'orderable' => true
260
261 ]);
262
263 $this->resolved_user_names = $this->resolve_user_names($manager->model);
264 }
265
266 private function resolve_user_names(Model $model)
267 {
268 global $core;
269
270 $query = $model->select("DISTINCT `{$this->id}`");
271
272 if ($model->has_scope('own'))
273 {
274 $query = $query->own;
275 }
276
277 if ($model->has_scope('similar_site'))
278 {
279 $query = $query->similar_site;
280 }
281
282 $users_keys = $query->all(\PDO::FETCH_COLUMN);
283
284 if (count($users_keys) < 2)
285 {
286 return;
287 }
288
289 return $core->models['users']
290 ->select('uid, IF((firstname != "" AND lastname != ""), CONCAT_WS(" ", firstname, lastname), username) name')
291 ->filter_by_uid($users_keys)
292 ->order('name')
293 ->pairs;
294 }
295
296 public function alter_query_with_filter(Query $query, $filter_value)
297 {
298 $query = parent::alter_query_with_filter($query, $filter_value);
299
300 if ($filter_value)
301 {
302 $query->and([ $this->id => $filter_value ]);
303 }
304
305 return $query;
306 }
307
308 public function alter_query_with_order(Query $query, $order_direction)
309 {
310 $keys = array_keys($this->resolved_user_names);
311
312 if ($order_direction < 0)
313 {
314 $keys = array_reverse($keys);
315 }
316
317 return $query->order($this->id, $keys);
318 }
319
320 321 322
323 public function alter_records(array $records)
324 {
325 global $core;
326
327 $records = parent::alter_records($records);
328 $keys = [];
329
330 foreach ($records as $record)
331 {
332 $keys[] = $record->{ $this->id };
333 }
334
335 if ($keys)
336 {
337 $keys = array_unique($keys, SORT_NUMERIC);
338
339 try
340 {
341 $this->user_cache = $core->models['users']->find($keys);
342 }
343 catch (RecordNotFound $e)
344 {
345 $this->user_cache = $e->records;
346 }
347 }
348
349 return $records;
350 }
351
352 public function get_options()
353 {
354 if (!$this->resolved_user_names)
355 {
356 return;
357 }
358
359 $options = [];
360
361 foreach ($this->resolved_user_names as $uid => $name)
362 {
363 $options["?uid=" . urlencode($uid)] = $name;
364 }
365
366 return $options;
367 }
368
369 public function render_cell($record)
370 {
371 $uid = $record->{ $this->id };
372 $user = $uid ? $this->user_cache[$uid] : null;
373
374 if (!$user)
375 {
376 return <<<EOT
377 <div class="alert alert-error undissmisable">Undefined user: {$uid}</div>
378 EOT;
379 }
380
381 return new FilterDecorator($record, $this->id, $this->is_filtering, $user ? $user->name : '');
382 }
383 }