1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Icybee package. |
5
|
|
|
* |
6
|
|
|
* (c) Olivier Laviale <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Icybee\Modules\Users\Block\ManageBlock; |
13
|
|
|
|
14
|
|
|
use ICanBoogie\ActiveRecord\Model; |
15
|
|
|
use ICanBoogie\ActiveRecord\Query; |
16
|
|
|
use ICanBoogie\ActiveRecord\RecordNotFound; |
17
|
|
|
use Icybee\Block\ManageBlock; |
18
|
|
|
use Icybee\Block\ManageBlock\Column; |
19
|
|
|
use Icybee\Block\ManageBlock\FilterDecorator; |
20
|
|
|
use Icybee\Modules\Users\User; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Representation of a _user_ column. |
24
|
|
|
* |
25
|
|
|
* This column can be used to represent the users associated to records. |
26
|
|
|
*/ |
27
|
|
|
class UserColumn extends Column |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* The users associated with the records, indexed by their identifier. |
31
|
|
|
* |
32
|
|
|
* @var User[] |
33
|
|
|
*/ |
34
|
|
|
private $user_cache; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The names of the users associated with the records, indexed by their identifier. |
38
|
|
|
* |
39
|
|
|
* @var array |
40
|
|
|
* |
41
|
|
|
* @see resolved_user_names() |
42
|
|
|
*/ |
43
|
|
|
private $resolved_user_names; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Initializes the {@link $resolved_user_names} property. |
47
|
|
|
* |
48
|
|
|
* @inheritdoc |
49
|
|
|
*/ |
50
|
|
|
public function __construct(ManageBlock $manager, $id, array $options = []) |
51
|
|
|
{ |
52
|
|
|
parent::__construct($manager, $id, $options + [ |
53
|
|
|
|
54
|
|
|
'title' => 'User', |
55
|
|
|
'orderable' => true |
56
|
|
|
|
57
|
|
|
]); |
58
|
|
|
|
59
|
|
|
$this->resolved_user_names = $this->resolve_user_names($manager->model); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
private function resolve_user_names(Model $model) |
63
|
|
|
{ |
64
|
|
|
$query = $model->select("DISTINCT `{$this->id}`"); |
65
|
|
|
|
66
|
|
|
if ($model->has_scope('own')) |
67
|
|
|
{ |
68
|
|
|
$query = $query->own; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if ($model->has_scope('similar_site')) |
72
|
|
|
{ |
73
|
|
|
$query = $query->similar_site; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$users_keys = $query->all(\PDO::FETCH_COLUMN); |
77
|
|
|
|
78
|
|
|
if (count($users_keys) < 2) |
79
|
|
|
{ |
80
|
|
|
return null; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return \ICanBoogie\app() |
84
|
|
|
->models['users'] |
85
|
|
|
->select('uid, IF((firstname != "" AND lastname != ""), CONCAT_WS(" ", firstname, lastname), username) name') |
86
|
|
|
->filter_by_uid($users_keys) |
87
|
|
|
->order('name') |
88
|
|
|
->pairs; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function alter_query_with_value(Query $query, $value) |
92
|
|
|
{ |
93
|
|
|
$query = parent::alter_query_with_value($query, $value); |
94
|
|
|
|
95
|
|
|
if ($value) |
96
|
|
|
{ |
97
|
|
|
$query->and([ $this->id => $value ]); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $query; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function alter_query_with_order(Query $query, $order_direction) |
104
|
|
|
{ |
105
|
|
|
$keys = array_keys($this->resolved_user_names); |
106
|
|
|
|
107
|
|
|
if ($order_direction < 0) |
108
|
|
|
{ |
109
|
|
|
$keys = array_reverse($keys); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $query->order($this->id, $keys); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Includes the users associated with the records. |
117
|
|
|
* |
118
|
|
|
* @param User[] $records |
119
|
|
|
* |
120
|
|
|
* @inheritdoc |
121
|
|
|
*/ |
122
|
|
|
public function alter_records(array &$records) |
123
|
|
|
{ |
124
|
|
|
parent::alter_records($records); |
125
|
|
|
$keys = []; |
126
|
|
|
|
127
|
|
|
foreach ($records as $record) |
128
|
|
|
{ |
129
|
|
|
$keys[] = $record->{ $this->id }; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
if ($keys) |
133
|
|
|
{ |
134
|
|
|
$keys = array_unique($keys, SORT_NUMERIC); |
135
|
|
|
|
136
|
|
|
try |
137
|
|
|
{ |
138
|
|
|
$this->user_cache = \ICanBoogie\app()->models['users']->find($keys); |
139
|
|
|
} |
140
|
|
|
catch (RecordNotFound $e) |
141
|
|
|
{ |
142
|
|
|
$this->user_cache = $e->records; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function get_options() |
148
|
|
|
{ |
149
|
|
|
if (!$this->resolved_user_names) |
150
|
|
|
{ |
151
|
|
|
return null; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$options = []; |
155
|
|
|
|
156
|
|
|
foreach ($this->resolved_user_names as $uid => $name) |
157
|
|
|
{ |
158
|
|
|
$options["?uid=" . urlencode($uid)] = $name; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $options; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param User $record |
166
|
|
|
* |
167
|
|
|
* @inheritdoc |
168
|
|
|
*/ |
169
|
|
|
public function render_cell($record) |
170
|
|
|
{ |
171
|
|
|
$uid = $record->{ $this->id }; |
172
|
|
|
$user = $uid ? $this->user_cache[$uid] : null; |
173
|
|
|
|
174
|
|
|
if (!$user) |
175
|
|
|
{ |
176
|
|
|
return <<<EOT |
177
|
|
|
<div class="alert alert-danger">Undefined user: {$uid}</div> |
178
|
|
|
EOT; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return new FilterDecorator($record, $this->id, $this->is_filtering, $user ? $user->name : ''); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|