|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the Zikula package. |
| 7 | + * |
| 8 | + * Copyright Zikula Foundation - https://ziku.la/ |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view the LICENSE |
| 11 | + * file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Zikula\Component\SortableColumns; |
| 15 | + |
| 16 | +use Doctrine\Common\Collections\ArrayCollection; |
| 17 | +use InvalidArgumentException; |
| 18 | +use Symfony\Component\HttpFoundation\Request; |
| 19 | +use Symfony\Component\Routing\RouterInterface; |
| 20 | + |
| 21 | +/** |
| 22 | + * Class SortableColumns |
| 23 | + * |
| 24 | + * SortableColumns is a zikula component to help manage data table column headings that can be clicked to sort the data. |
| 25 | + * The collection is an ArrayCollection of Zikula\Component\SortableColumns\Column objects. |
| 26 | + * Use the ::generateSortableColumns method to create an array of attributes (url, css class) indexed by column name |
| 27 | + * which can be used in the generation of table headings/links. |
| 28 | + */ |
| 29 | +class SortableColumns |
| 30 | +{ |
| 31 | + /** |
| 32 | + * @var RouterInterface |
| 33 | + */ |
| 34 | + private $router; |
| 35 | + |
| 36 | + /** |
| 37 | + * The route name string to generate urls for column headers |
| 38 | + * @var string |
| 39 | + */ |
| 40 | + private $routeName; |
| 41 | + |
| 42 | + /** |
| 43 | + * A collection of Columns to manage |
| 44 | + * @var ArrayCollection |
| 45 | + */ |
| 46 | + private $columnCollection; |
| 47 | + |
| 48 | + /** |
| 49 | + * The default column (if unset, the first column add is used) |
| 50 | + * @var Column |
| 51 | + */ |
| 52 | + private $defaultColumn; |
| 53 | + |
| 54 | + /** |
| 55 | + * The column used to sort the data |
| 56 | + * @var Column |
| 57 | + */ |
| 58 | + private $sortColumn; |
| 59 | + |
| 60 | + /** |
| 61 | + * The direction to sorted (constant from Column class) |
| 62 | + * @var string |
| 63 | + */ |
| 64 | + private $sortDirection = Column::DIRECTION_ASCENDING; |
| 65 | + |
| 66 | + /** |
| 67 | + * The name of the html field that holds the selected orderBy field (default: `sort-field`) |
| 68 | + * @var string |
| 69 | + */ |
| 70 | + private $sortFieldName; |
| 71 | + |
| 72 | + /** |
| 73 | + * The name of the html field that holds the selected orderBy direction (default: `sort-direction`) |
| 74 | + * @var string |
| 75 | + */ |
| 76 | + private $directionFieldName; |
| 77 | + |
| 78 | + /** |
| 79 | + * Additional url parameters that must be included in the generated urls |
| 80 | + * @var array |
| 81 | + */ |
| 82 | + private $additionalUrlParameters = []; |
| 83 | + |
| 84 | + public function __construct( |
| 85 | + RouterInterface $router, |
| 86 | + string $routeName, |
| 87 | + string $sortFieldName = 'sort-field', |
| 88 | + string $directionFieldName = 'sort-direction' |
| 89 | + ) { |
| 90 | + $this->router = $router; |
| 91 | + $this->routeName = $routeName; |
| 92 | + $this->sortFieldName = $sortFieldName; |
| 93 | + $this->directionFieldName = $directionFieldName; |
| 94 | + $this->columnCollection = new ArrayCollection(); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Create an array of column definitions indexed by column name |
| 99 | + * <code> |
| 100 | + * ['a' => |
| 101 | + * ['url' => '/foo?sort-direction=ASC&sort-field=a', |
| 102 | + * 'class' => 'z-order-unsorted' |
| 103 | + * ], |
| 104 | + * ] |
| 105 | + * </code> |
| 106 | + */ |
| 107 | + public function generateSortableColumns(): array |
| 108 | + { |
| 109 | + $resultArray = []; |
| 110 | + /** @var Column $column */ |
| 111 | + foreach ($this->columnCollection as $column) { |
| 112 | + $this->additionalUrlParameters[$this->directionFieldName] = $column->isSortColumn() ? $column->getReverseSortDirection() : $column->getCurrentSortDirection(); |
| 113 | + $this->additionalUrlParameters[$this->sortFieldName] = $column->getName(); |
| 114 | + $resultArray[$column->getName()] = [ |
| 115 | + 'url' => $this->router->generate($this->routeName, $this->additionalUrlParameters), |
| 116 | + 'class' => $column->getCssClassString(), |
| 117 | + ]; |
| 118 | + } |
| 119 | + |
| 120 | + return $resultArray; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Add one column. |
| 125 | + */ |
| 126 | + public function addColumn(Column $column): void |
| 127 | + { |
| 128 | + $this->columnCollection->set($column->getName(), $column); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Shortcut to add an array of columns. |
| 133 | + */ |
| 134 | + public function addColumns(array $columns = []): void |
| 135 | + { |
| 136 | + foreach ($columns as $column) { |
| 137 | + if ($column instanceof Column) { |
| 138 | + $this->addColumn($column); |
| 139 | + } else { |
| 140 | + throw new InvalidArgumentException('Columns must be an instance of \Zikula\Component\SortableColumns\Column.'); |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + public function removeColumn(string $name): void |
| 146 | + { |
| 147 | + $this->columnCollection->remove($name); |
| 148 | + } |
| 149 | + |
| 150 | + public function getColumn(?string $name): ?Column |
| 151 | + { |
| 152 | + return $this->columnCollection->get($name); |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Set the column to sort by and the sort direction. |
| 157 | + */ |
| 158 | + public function setOrderBy(Column $sortColumn = null, string $sortDirection = null): void |
| 159 | + { |
| 160 | + $sortColumn = $sortColumn ?: $this->getDefaultColumn(); |
| 161 | + if (null === $sortColumn) { |
| 162 | + return; |
| 163 | + } |
| 164 | + $sortDirection = $sortDirection ?: Column::DIRECTION_ASCENDING; |
| 165 | + $this->setSortDirection($sortDirection); |
| 166 | + $this->setSortColumn($sortColumn); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Shortcut to set OrderBy using the Request object. |
| 171 | + */ |
| 172 | + public function setOrderByFromRequest(Request $request): void |
| 173 | + { |
| 174 | + if (null === $this->getDefaultColumn()) { |
| 175 | + return; |
| 176 | + } |
| 177 | + $sortColumnName = $request->get($this->sortFieldName, $this->getDefaultColumn()->getName()); |
| 178 | + $sortDirection = $request->get($this->directionFieldName, Column::DIRECTION_ASCENDING); |
| 179 | + $this->setOrderBy($this->getColumn($sortColumnName), $sortDirection); |
| 180 | + } |
| 181 | + |
| 182 | + public function getSortColumn(): ?Column |
| 183 | + { |
| 184 | + return $this->sortColumn ?? $this->getDefaultColumn(); |
| 185 | + } |
| 186 | + |
| 187 | + private function setSortColumn(Column $sortColumn): void |
| 188 | + { |
| 189 | + if ($this->columnCollection->contains($sortColumn)) { |
| 190 | + $this->sortColumn = $sortColumn; |
| 191 | + $sortColumn->setSortColumn(true); |
| 192 | + $sortColumn->setCurrentSortDirection($this->getSortDirection()); |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + public function getSortDirection(): string |
| 197 | + { |
| 198 | + return $this->sortDirection; |
| 199 | + } |
| 200 | + |
| 201 | + private function setSortDirection(string $sortDirection): void |
| 202 | + { |
| 203 | + if (in_array($sortDirection, [Column::DIRECTION_ASCENDING, Column::DIRECTION_DESCENDING], true)) { |
| 204 | + $this->sortDirection = $sortDirection; |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + public function getDefaultColumn(): ?Column |
| 209 | + { |
| 210 | + if (!empty($this->defaultColumn)) { |
| 211 | + return $this->defaultColumn; |
| 212 | + } |
| 213 | + |
| 214 | + return $this->columnCollection->first(); |
| 215 | + } |
| 216 | + |
| 217 | + public function setDefaultColumn(Column $defaultColumn): void |
| 218 | + { |
| 219 | + $this->defaultColumn = $defaultColumn; |
| 220 | + } |
| 221 | + |
| 222 | + public function getAdditionalUrlParameters(): array |
| 223 | + { |
| 224 | + return $this->additionalUrlParameters; |
| 225 | + } |
| 226 | + |
| 227 | + public function setAdditionalUrlParameters(array $additionalUrlParameters = []): void |
| 228 | + { |
| 229 | + $this->additionalUrlParameters = $additionalUrlParameters; |
| 230 | + } |
| 231 | +} |
0 commit comments