PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.16.8.1
GiveWP – Donation Plugin and Fundraising Platform v4.16.8.1
4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 All 254 releases
give / src / Framework / ListTable / Concerns / Columns.php

Columns.php in GiveWP – Donation Plugin and Fundraising Platform 4.16.8.1, at src/Framework/ListTable/Concerns/Columns.php

212 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Give\Framework\ListTable\Concerns;
4
5 use Give\Framework\ListTable\Exceptions\ColumnIdCollisionException;
6 use Give\Framework\ListTable\Exceptions\ReferenceColumnNotFoundException;
7 use Give\Framework\ListTable\ModelColumn;
8
9 /**
10 * @since 2.24.0
11 */
12 trait Columns
13 {
14 /**
15 * @var ModelColumn[]
16 */
17 private $columns = [];
18
19 /**
20 * Add List Table column
21 *
22 * @since 2.24.0
23 *
24 * @param ModelColumn $column
25 *
26 * @return self
27 * @throws ColumnIdCollisionException
28 */
29 public function addColumn(ModelColumn $column): self
30 {
31 $columnId = $column::getId();
32
33 if (isset($this->columns[$columnId])) {
34 throw new ColumnIdCollisionException($columnId);
35 }
36
37 $this->columns[$columnId] = $column;
38 $this->setColumnVisibility($columnId);
39
40 return $this;
41 }
42
43 /**
44 * Add List Table columns
45 *
46 * @since 2.24.0
47 *
48 * @param ModelColumn ...$columns
49 *
50 * @return void
51 * @throws ColumnIdCollisionException
52 */
53 public function addColumns(...$columns)
54 {
55 foreach ($columns as $column) {
56 $this->addColumn($column);
57 }
58 }
59
60 /**
61 * Remove List Table column
62 *
63 * @since 2.24.0
64 *
65 * @return self
66 * @throws ReferenceColumnNotFoundException
67 */
68 public function removeColumn(string $columnId): self
69 {
70 if ( ! isset($this->columns[$columnId])) {
71 throw new ReferenceColumnNotFoundException($columnId);
72 }
73
74 unset($this->columns[$columnId]);
75
76 return $this;
77 }
78
79 /**
80 * @since 4.3.0 set list table data on columns
81 * @since 2.24.0
82 *
83 * @return ModelColumn[]
84 */
85 public function getColumns(): array
86 {
87 foreach($this->columns as $column) {
88 if ($column->isUsingListTableData()) {
89 $column->setListTableData($this->getData());
90 }
91 }
92
93 return $this->columns;
94 }
95
96 /**
97 * @since 2.24.0
98 *
99 * @return array
100 */
101 public function getColumnsArray(): array
102 {
103 return array_map(static function (ModelColumn $column) {
104 return $column->toArray();
105 }, array_values($this->columns));
106 }
107
108 /**
109 * Add column before specific column
110 *
111 * @since 2.24.0
112 *
113 * @return self
114 * @throws ReferenceColumnNotFoundException
115 */
116 public function addColumnBefore(string $columnId, ModelColumn $column): self
117 {
118 if (is_int($index = $this->getColumnIndexById($columnId))) {
119 return $this->insertAtIndex($index, $column);
120 }
121
122 throw new ReferenceColumnNotFoundException($columnId);
123 }
124
125 /**
126 * Add column after specific column
127 *
128 * @since 2.24.0
129 *
130 * @return self
131 * @throws ReferenceColumnNotFoundException
132 */
133 public function addColumnAfter(string $columnId, ModelColumn $column): self
134 {
135 if (is_int($index = $this->getColumnIndexById($columnId))) {
136 return $this->insertAtIndex($index + 1, $column);
137 }
138
139 throw new ReferenceColumnNotFoundException($columnId);
140 }
141
142 /**
143 * Get registered column by column id
144 *
145 * @since 2.24.0
146 *
147 * @param string $columnId
148 *
149 * @return ModelColumn|null
150 */
151 public function getColumnById(string $columnId)
152 {
153 return $this->columns[$columnId] ?? null;
154 }
155
156 /**
157 * Get column position index
158 *
159 * @since 2.24.0
160 *
161 * @return int|false
162 */
163 public function getColumnIndexById(string $columnId)
164 {
165 return array_search($columnId, array_keys($this->columns), true);
166 }
167
168 /**
169 * @since 2.24.0
170 *
171 * @return self
172 */
173 protected function insertAtIndex(int $index, ModelColumn $column): self
174 {
175 $this->columns = array_merge(
176 array_splice($this->columns, 0, $index),
177 [$column::getId() => $column],
178 $this->columns
179 );
180
181 return $this;
182 }
183
184 /**
185 * @since 2.24.0
186 *
187 * @return self
188 */
189 public function setColumnVisibility($columnId, $isVisible = null): self
190 {
191 if (is_null($isVisible)) {
192 $isVisible = in_array($columnId, $this->getDefaultVisibleColumns(), true);
193 }
194
195 $this->getColumnById($columnId)->visible($isVisible);
196
197 return $this;
198 }
199
200 /**
201 * @since 2.24.0
202 *
203 * @return string[]
204 */
205 public function getSortColumnById(string $columnId): array
206 {
207 $column = $this->getColumnById($columnId);
208
209 return $column->getSortColumn() ?: ['id'];
210 }
211 }
212