1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://flipboxfactory.com/software/patron/license |
6
|
|
|
* @link https://www.flipboxfactory.com/software/patron/ |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\organizations\events\handlers; |
10
|
|
|
|
11
|
|
|
use Craft; |
12
|
|
|
use craft\elements\User; |
13
|
|
|
use craft\events\SetElementTableAttributeHtmlEvent; |
14
|
|
|
use craft\helpers\Html; |
15
|
|
|
use flipbox\organizations\behaviors\OrganizationsAssociatedToUserBehavior; |
16
|
|
|
use flipbox\organizations\Organizations; |
17
|
|
|
use flipbox\organizations\records\UserAssociation; |
18
|
|
|
use flipbox\organizations\records\UserType; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Flipbox Factory <[email protected]> |
22
|
|
|
* @since 1.0.0 |
23
|
|
|
*/ |
24
|
|
|
class SetOrganizationUserElementTableAttributeHtml |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @param SetElementTableAttributeHtmlEvent $event |
28
|
|
|
* @throws \Twig\Error\LoaderError |
29
|
|
|
* @throws \Twig\Error\RuntimeError |
30
|
|
|
* @throws \Twig\Error\SyntaxError |
31
|
|
|
*/ |
32
|
|
|
public static function handle(SetElementTableAttributeHtmlEvent $event) |
33
|
|
|
{ |
34
|
|
|
if (!in_array($event->attribute, ['state', 'types', 'edit'], true)) { |
35
|
|
|
return; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
if ($event->attribute === 'edit') { |
39
|
|
|
$event->html = '<span class="edit-association icon settings"></span>'; |
40
|
|
|
return; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** @var User|OrganizationsAssociatedToUserBehavior $element */ |
44
|
|
|
$element = $event->sender; |
45
|
|
|
|
46
|
|
|
/** @var UserAssociation $association */ |
47
|
|
|
$association = $element->getOrganizations()->findOne( |
|
|
|
|
48
|
|
|
Craft::$app->getRequest()->getParam('organization') |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
switch ($event->attribute) { |
52
|
|
|
case 'state': |
53
|
|
|
$params = [ |
54
|
|
|
'indicator' => '', |
55
|
|
|
'label' => 'N/A' |
56
|
|
|
]; |
57
|
|
|
|
58
|
|
|
$state = Organizations::getInstance()->getSettings()->getUserStates(); |
59
|
|
|
|
60
|
|
|
if ($association) { |
61
|
|
|
$params = [ |
62
|
|
|
'indicator' => $association->state, |
63
|
|
|
'label' => $state[$association->state] ?? 'N/A' |
64
|
|
|
]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$event->html = Html::encodeParams( |
68
|
|
|
'<span class="user-state status {indicator}"></span>{label}', |
69
|
|
|
$params |
70
|
|
|
); |
71
|
|
|
break; |
72
|
|
|
|
73
|
|
|
case 'types': |
74
|
|
|
$types = $association ? $association->getTypes() : []; |
75
|
|
|
|
76
|
|
|
$html = $label = []; |
77
|
|
|
foreach ($types->getCollection() as $type) { |
78
|
|
|
$label[] = $type->name; |
79
|
|
|
$html[] = self::icon($type); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$event->html = Html::encodeParams( |
83
|
|
|
'<span class="user-types-icons" data-label="' . implode(', ', $label) . '">' . implode( |
84
|
|
|
'', |
85
|
|
|
$html |
86
|
|
|
) . '</span>', |
87
|
|
|
[] |
88
|
|
|
); |
89
|
|
|
|
90
|
|
|
break; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param UserType $type |
96
|
|
|
* @return string |
97
|
|
|
* @throws \Twig\Error\LoaderError |
98
|
|
|
* @throws \Twig\Error\RuntimeError |
99
|
|
|
* @throws \Twig\Error\SyntaxError |
100
|
|
|
*/ |
101
|
|
|
private static function icon(UserType $type) |
102
|
|
|
{ |
103
|
|
|
return '<span class="foo" data-label="' . $type->name . '">' . Craft::$app->getView()->renderTemplate( |
104
|
|
|
"organizations/_includes/icon.svg", |
105
|
|
|
[ |
106
|
|
|
'label' => $type->name |
107
|
|
|
] |
108
|
|
|
) . '</span>'; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: