• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

contributte / datagrid / 8230471191

11 Mar 2024 09:25AM UTC coverage: 34.102%. First build
8230471191

Pull #1060

github

radimvaculik
Fix phpstan
Pull Request #1060: [7.x] Next

117 of 435 new or added lines in 54 files covered. (26.9%)

1124 of 3296 relevant lines covered (34.1%)

0.34 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Column/MultiAction.php
1
<?php declare(strict_types = 1);
2

3
namespace Contributte\Datagrid\Column;
4

5
use Contributte\Datagrid\Datagrid;
6
use Contributte\Datagrid\Exception\DatagridException;
7
use Contributte\Datagrid\Row;
8
use Contributte\Datagrid\Traits\TButtonCaret;
9
use Contributte\Datagrid\Traits\TButtonClass;
10
use Contributte\Datagrid\Traits\TButtonIcon;
11
use Contributte\Datagrid\Traits\TButtonText;
12
use Contributte\Datagrid\Traits\TButtonTitle;
13
use Contributte\Datagrid\Traits\TButtonTryAddIcon;
14
use Contributte\Datagrid\Traits\TLink;
15
use Nette\Utils\Html;
16

17
class MultiAction extends Column
18
{
19

20
        use TButtonTryAddIcon;
21
        use TButtonIcon;
22
        use TButtonClass;
23
        use TButtonTitle;
24
        use TButtonText;
25
        use TButtonCaret;
26
        use TLink;
27

28
        protected Datagrid $grid;
29

30
        protected string $name;
31

32
        protected array $actions = [];
33

34
        /** @var array|callable[] */
35
        private array $rowConditions = [];
36

37
        public function __construct(Datagrid $grid, string $key, string $name)
38
        {
39
                parent::__construct($grid, $key, '', $name);
×
40

41
                $this->setTemplate(__DIR__ . '/../templates/column_multi_action.latte');
×
42
        }
43

44
        public function renderButton(): Html
45
        {
46
                $button = Html::el('button')
×
47
                        ->setAttribute('type', 'button')
×
NEW
48
                        ->data('bs-toggle', 'dropdown');
×
49

50
                $this->tryAddIcon($button, $this->getIcon(), $this->getName());
×
51

52
                $button->addText($this->grid->getTranslator()->translate($this->name));
×
53

54
                if ($this->hasCaret()) {
×
55
                        $button->addHtml('&nbsp;');
×
56
                        $button->addHtml('<i class="caret"></i>');
×
57
                }
58

59
                if ($this->getTitle() !== null) {
×
60
                        $button->setAttribute(
×
61
                                'title',
×
62
                                $this->grid->getTranslator()->translate($this->getTitle())
×
63
                        );
64
                }
65

66
                if ($this->getClass() !== '') {
×
67
                        $button->setAttribute('class', $this->getClass() . ' dropdown-toggle');
×
68
                }
69

70
                return $button;
×
71
        }
72

73
        /**
74
         * @return static
75
         */
76
        public function addAction(
77
                string $key,
78
                string $name,
79
                ?string $href = null,
80
                ?array $params = null
81
        ): self
82
        {
83
                if (isset($this->actions[$key])) {
×
NEW
84
                        throw new DatagridException(
×
85
                                sprintf('There is already action at key [%s] defined for MultiAction.', $key)
×
86
                        );
87
                }
88

NEW
89
                $href ??= $key;
×
90

91
                if ($params === null) {
×
92
                        $params = [$this->grid->getPrimaryKey()];
×
93
                }
94

95
                $action = new Action($this->grid, $key, $href, $name, $params);
×
96

97
                $action->setClass('dropdown-item datagrid-multiaction-dropdown-item');
×
98

99
                $this->actions[$key] = $action;
×
100

101
                return $this;
×
102
        }
103

104
        /**
105
         * @return array<Action>
106
         */
107
        public function getActions(): array
108
        {
109
                return $this->actions;
×
110
        }
111

112
        public function getAction(string $key): Action
113
        {
114
                if (!isset($this->actions[$key])) {
×
NEW
115
                        throw new DatagridException(
×
116
                                sprintf('There is no action at key [%s] defined for MultiAction.', $key)
×
117
                        );
118
                }
119

120
                return $this->actions[$key];
×
121
        }
122

123
        /**
124
         * Column can have variables that will be passed to custom template scope
125
         */
126
        public function getTemplateVariables(): array
127
        {
128
                return array_merge($this->templateVariables, [
×
129
                        'multiAction' => $this,
×
130
                ]);
131
        }
132

133
        public function setRowCondition(
134
                string $actionKey,
135
                callable $rowCondition
136
        ): void
137
        {
138
                $this->rowConditions[$actionKey] = $rowCondition;
×
139
        }
140

141
        public function testRowCondition(string $actionKey, Row $row): bool
142
        {
143
                if (!isset($this->rowConditions[$actionKey])) {
×
144
                        return true;
×
145
                }
146

147
                return (bool) call_user_func($this->rowConditions[$actionKey], $row->getItem());
×
148
        }
149

150
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc