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\UserType; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Flipbox Factory <[email protected]> |
21
|
|
|
* @since 1.0.0 |
22
|
|
|
*/ |
23
|
|
|
class SetOrganizationUserElementTableAttributeHtml |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @param SetElementTableAttributeHtmlEvent $event |
27
|
|
|
* @throws \Twig\Error\LoaderError |
28
|
|
|
* @throws \Twig\Error\RuntimeError |
29
|
|
|
* @throws \Twig\Error\SyntaxError |
30
|
|
|
*/ |
31
|
|
|
public static function handle(SetElementTableAttributeHtmlEvent $event) |
32
|
|
|
{ |
33
|
|
|
if (!in_array($event->attribute, ['state', 'types', 'edit'], true)) { |
34
|
|
|
return; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if ($event->attribute === 'edit') { |
38
|
|
|
$event->html = '<span class="edit-association icon settings"></span>'; |
39
|
|
|
return; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** @var User|OrganizationsAssociatedToUserBehavior $element */ |
43
|
|
|
$element = $event->sender; |
44
|
|
|
|
45
|
|
|
$association = $element->getOrganizationManager()->findOne( |
|
|
|
|
46
|
|
|
Craft::$app->getRequest()->getParam('organization') |
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
switch ($event->attribute) { |
50
|
|
|
case 'state': |
51
|
|
|
$params = [ |
52
|
|
|
'indicator' => '', |
53
|
|
|
'label' => 'N/A' |
54
|
|
|
]; |
55
|
|
|
|
56
|
|
|
$state = Organizations::getInstance()->getSettings()->getUserStates(); |
57
|
|
|
|
58
|
|
|
if ($association) { |
59
|
|
|
$params = [ |
60
|
|
|
'indicator' => $association->state, |
61
|
|
|
'label' => $state[$association->state] ?? 'N/A' |
62
|
|
|
]; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$event->html = Html::encodeParams( |
66
|
|
|
'<span class="user-state status {indicator}"></span>{label}', |
67
|
|
|
$params |
68
|
|
|
); |
69
|
|
|
break; |
70
|
|
|
|
71
|
|
|
case 'types': |
72
|
|
|
$types = $association ? $association->types : []; |
73
|
|
|
|
74
|
|
|
$html = $label = []; |
75
|
|
|
foreach ($types as $type) { |
76
|
|
|
$label[] = $type->name; |
77
|
|
|
$html[] = self::icon($type); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$event->html = Html::encodeParams( |
81
|
|
|
'<span class="user-types-icons" data-label="' . implode(', ', $label) . '">' . implode( |
82
|
|
|
'', |
83
|
|
|
$html |
84
|
|
|
) . '</span>', |
85
|
|
|
[] |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
break; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param UserType $type |
94
|
|
|
* @return string |
95
|
|
|
* @throws \Twig\Error\LoaderError |
96
|
|
|
* @throws \Twig\Error\RuntimeError |
97
|
|
|
* @throws \Twig\Error\SyntaxError |
98
|
|
|
*/ |
99
|
|
|
private static function icon(UserType $type) |
100
|
|
|
{ |
101
|
|
|
return '<span class="foo" data-label="' . $type->name . '">' . Craft::$app->getView()->renderTemplate( |
102
|
|
|
"organizations/_includes/icon.svg", |
103
|
|
|
[ |
104
|
|
|
'label' => $type->name |
105
|
|
|
] |
106
|
|
|
) . '</span>'; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
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: