ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
RelationsTable.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
29use ilObjUser;
30use ilStr;
31use ilUserUtil;
32use ilBuddyList;
35use ilLanguage;
36use ILIAS\UI\Factory as UIFactory;
37use ilUIService;
39use Closure;
41
53{
57 public function __construct(
58 private readonly UIFactory $create,
59 private readonly ilLanguage $lng,
60 private readonly ilUIService $ui_service,
61 private readonly Http $http,
62 private readonly Closure $link_to_profile,
63 ) {
64 }
65
70 public static function data(array $filter = []): array
71 {
72 $relations = ilBuddyList::getInstanceByGlobalUser()->getRelations();
73
74 $state_filter = (string) ($filter['state'] ?? '');
76
77 if ($state_filter) {
78 $relations = $relations->filter(static fn(ilBuddySystemRelation $relation): bool => (
79 $state_factory
80 ->getTableFilterStateMapper($relation->getState())
81 ->filterMatchesRelation($state_filter, $relation)
82 ));
83 }
84
85 $public_names = ilUserUtil::getNamePresentation($relations->getKeys(), false, false, '', false, true, false);
87 $logins = ilUserUtil::getNamePresentation($relations->getKeys(), false, false, '', false, false, false);
88
89 $logins = array_map(static function (string $value): string {
90 $matches = null;
91 preg_match_all('/\[([^\[]+?)\]/', $value, $matches);
92 return $matches[1][\count($matches[1]) - 1] ?? '';
93 }, $logins);
94
95 $public_name_query = (string) ($filter['name'] ?? '');
96 if ($public_name_query) {
97 $relations = $relations->filter(self::filter($public_name_query, $relations, $public_names, $logins));
98 }
99
100 $data = [];
101 foreach ($relations->toArray() as $user_id => $relation) {
102 $txt = $state_factory->getTableFilterStateMapper($relation->getState())->text($relation);
103 $data[] = [
104 'user_id' => $user_id,
105 'public_name' => $public_names[$user_id],
106 'login' => $logins[$user_id],
107 'state' => $relation->getState(),
108 'target_states' => $relation->getCurrentPossibleTargetStates()->toArray(),
109 'state_text' => $txt,
110 ];
111 }
112
113 return $data;
114 }
115
121 public function build(array $multi_actions, string $target_url, callable $action): array
122 {
123 $filter = $this->filterComponent($target_url);
124 $data = static::data($this->ui_service->filter()->getData($filter) ?: []);
125 $single_actions = $this->actions($data, $action);
126
127 $components = [
128 $filter
129 ];
130 $components[] = $this->create
131 ->table()
132 ->data(
133 new TableRows($data, array_keys($single_actions), $this->link_to_profile),
134 $this->lng->txt('buddy_tbl_title_relations'),
135 [
136 'public_name' => $this->create->table()->column()->text($this->lng->txt('name')),
137 'login' => $this->create->table()->column()->text($this->lng->txt('login')),
138 'state_text' => $this->create
139 ->table()->column()
140 ->text($this->lng->txt('buddy_tbl_state_actions_col_label')),
141 ]
142 )
143 ->withId('buddy_relations_table')
144 ->withRequest($this->http->request())
145 ->withRange(new Range(0, 50))
146 ->withActions(
147 array_merge($single_actions, $multi_actions)
148 );
149
150 return $components;
151 }
152
160 private static function filter(string $public_name_query, ilBuddySystemArrayCollection $relations, array $public_names, array $logins): Closure
161 {
162 $in_string = static fn(string $needle, string $haystack): bool => ilStr::strpos(
163 ilStr::strtolower($haystack),
164 ilStr::strtolower($needle),
165 0
166 ) !== false;
167
168 return self::pipe($relations->getKey(...), static fn(int $user_id): bool => (
169 $in_string($public_name_query, $public_names[$user_id]) ||
170 $in_string($public_name_query, $logins[$user_id])
172 }
173
174 private static function pipe(Closure $a, Closure $b): Closure
175 {
176 return static fn($x) => $b($a($x));
177 }
178
185 private function actions(array $data, callable $action): array
186 {
187 $actions = [];
188 foreach ($data as $row) {
189 foreach ($row['target_states'] as $state) {
190 $actions[$row['state'] . '->' . $state] = $action(
191 'single',
192 'buddy_bs_act_btn_txt_' . $row['state']->getSnakeName() . '_to_' . $state->getSnakeName(),
193 $state->getAction()
194 );
195 }
196 }
197
198 return $actions;
199 }
200
201 private function filterComponent(string $target_url): Filter
202 {
204 $options = array_merge(...array_map(
205 static fn($m): array => $m->optionsForState(),
206 array_map(
207 $state_factory->getTableFilterStateMapper(...),
208 array_filter($state_factory->getValidStates(), static fn($s): bool => !$s->isInitial())
209 )
210 ));
211
212 $fields = [
213 'state' => $this->create->input()->field()->select($this->lng->txt('buddy_tbl_filter_state'), $options),
214 'name' => $this->create->input()->field()->text($this->lng->txt('name')),
215 ];
216
217 return $this->ui_service->filter()->standard(
218 'contact-filter',
219 $target_url,
220 $fields,
221 array_map(static fn(): bool => true, $fields),
222 true,
223 true
224 );
225 }
226}
$relation
$components
@phpstan-type RelationRecord array{ user_id: int, public_name: string, login: string,...
build(array $multi_actions, string $target_url, callable $action)
__construct(private readonly UIFactory $create, private readonly ilLanguage $lng, private readonly ilUIService $ui_service, private readonly Http $http, private readonly Closure $link_to_profile,)
static filter(string $public_name_query, ilBuddySystemArrayCollection $relations, array $public_names, array $logins)
@template A
@phpstan-import-type RelationRecord from \ILIAS\Contact\BuddySystem\Tables\RelationsTable
Definition: TableRows.php:34
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
A simple class to express a naive range of whole positive numbers.
Definition: Range.php:29
Class ilBuddyList.
static getInstanceByGlobalUser(?ilObjUser $user=null)
Class ilBuddySystemArrayCollection A generic array based collection class.
Class ilBuddySystemRelation.
language handling
User class.
static _lookupActive(int $a_usr_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: class.ilStr.php:20
Filter service.
Class ilUserUtil.
static getNamePresentation( $a_user_id, bool $a_user_image=false, bool $a_profile_link=false, string $a_profile_back_link='', bool $a_force_first_lastname=false, bool $a_omit_login=false, bool $a_sortable=true, bool $a_return_data_array=false, $a_ctrl_path=null)
Default behaviour is:
$http
Definition: deliver.php:30
Interface GlobalHttpState.
A component is the most general form of an entity in the UI.
Definition: Component.php:28
This describes a standard filter.
Definition: Standard.php:27
Interface ilBuddySystemRelationState.
static http()
Fetches the global http state from ILIAS.
filter(string $filter_id, array $class_path, string $cmd, bool $activated=true, bool $expanded=true)
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
global $lng
Definition: privfeed.php:31