|
| 1 | +<?php |
| 2 | + |
| 3 | +/* Icinga for Kubernetes Web | (c) 2025 Icinga GmbH | AGPLv3 */ |
| 4 | + |
| 5 | +namespace Icinga\Module\Kubernetes\Web; |
| 6 | + |
| 7 | +use Icinga\Exception\Http\HttpNotFoundException; |
| 8 | +use Icinga\Module\Kubernetes\Common\Auth; |
| 9 | +use Icinga\Module\Kubernetes\Model\Favorite; |
| 10 | +use ipl\Html\Form; |
| 11 | +use ipl\Sql\Connection; |
| 12 | +use ipl\Sql\Expression; |
| 13 | +use ipl\Sql\Select; |
| 14 | +use ipl\Stdlib\Filter; |
| 15 | +use ipl\Web\Common\CsrfCounterMeasure; |
| 16 | +use LogicException; |
| 17 | +use Ramsey\Uuid\Uuid; |
| 18 | + |
| 19 | +class MoveFavoriteForm extends Form |
| 20 | +{ |
| 21 | + use CsrfCounterMeasure; |
| 22 | + |
| 23 | + protected $defaultAttributes = ['hidden' => true]; |
| 24 | + |
| 25 | + protected $method = 'POST'; |
| 26 | + |
| 27 | + /** @var Connection */ |
| 28 | + protected $db; |
| 29 | + |
| 30 | + /** @var string */ |
| 31 | + protected $kind; |
| 32 | + |
| 33 | + /** |
| 34 | + * Create a new MoveFavoriteForm |
| 35 | + * |
| 36 | + * @param ?Connection $db |
| 37 | + */ |
| 38 | + public function __construct(Connection $db = null) |
| 39 | + { |
| 40 | + $this->db = $db; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Get the kind |
| 45 | + * |
| 46 | + * @return string |
| 47 | + */ |
| 48 | + public function getKind(): string |
| 49 | + { |
| 50 | + if ($this->kind === null) { |
| 51 | + throw new LogicException('The form must be successfully submitted first'); |
| 52 | + } |
| 53 | + |
| 54 | + return $this->kind; |
| 55 | + } |
| 56 | + |
| 57 | + protected function assemble(): void |
| 58 | + { |
| 59 | + $this->addElement('hidden', 'uuid', ['required' => true]); |
| 60 | + $this->addElement('hidden', 'priority', ['required' => true]); |
| 61 | + } |
| 62 | + |
| 63 | + protected function onSuccess(): void |
| 64 | + { |
| 65 | + $favoriteUuid = Uuid::fromString($this->getValue('uuid'))->getBytes(); |
| 66 | + $newPriority = $this->getValue('priority'); |
| 67 | + |
| 68 | + /** @var ?Favorite $favorite */ |
| 69 | + $favorite = Favorite::on($this->db) |
| 70 | + ->columns(['kind', 'priority']) |
| 71 | + ->filter(Filter::all( |
| 72 | + Filter::equal('resource_uuid', $favoriteUuid), |
| 73 | + Filter::equal('username', Auth::getInstance()->getUser()->getUsername()) |
| 74 | + )) |
| 75 | + ->first(); |
| 76 | + if ($favorite === null) { |
| 77 | + throw new HttpNotFoundException('Favorite not found'); |
| 78 | + } |
| 79 | + |
| 80 | + $transactionStarted = ! $this->db->inTransaction(); |
| 81 | + if ($transactionStarted) { |
| 82 | + $this->db->beginTransaction(); |
| 83 | + } |
| 84 | + |
| 85 | + $this->kind = $favorite->kind; |
| 86 | + |
| 87 | + // Free up the current priority used by the favorite in question |
| 88 | + $this->db->update('favorite', ['priority' => null], ['resource_uuid = ?' => $favoriteUuid]); |
| 89 | + |
| 90 | + // Update the priorities of the favorites that are affected by the move |
| 91 | + if ($newPriority < $favorite->priority) { |
| 92 | + $affectedFavorites = $this->db->select( |
| 93 | + (new Select()) |
| 94 | + ->columns('resource_uuid') |
| 95 | + ->from('favorite') |
| 96 | + ->where([ |
| 97 | + 'kind = ?' => $favorite->kind, |
| 98 | + 'priority >= ?' => $newPriority, |
| 99 | + 'priority < ?' => $favorite->priority |
| 100 | + ]) |
| 101 | + ->orderBy('priority', SORT_DESC) |
| 102 | + ); |
| 103 | + foreach ($affectedFavorites as $affectedFavorite) { |
| 104 | + $this->db->update( |
| 105 | + 'favorite', |
| 106 | + ['priority' => new Expression('priority + 1')], |
| 107 | + ['resource_uuid = ?' => $affectedFavorite->resource_uuid] |
| 108 | + ); |
| 109 | + } |
| 110 | + } elseif ($newPriority > $favorite->priority) { |
| 111 | + $affectedFavorites = $this->db->select( |
| 112 | + (new Select()) |
| 113 | + ->columns('resource_uuid') |
| 114 | + ->from('favorite') |
| 115 | + ->where([ |
| 116 | + 'kind = ?' => $favorite->kind, |
| 117 | + 'priority > ?' => $favorite->priority, |
| 118 | + 'priority <= ?' => $newPriority |
| 119 | + ]) |
| 120 | + ->orderBy('priority ASC') |
| 121 | + ); |
| 122 | + foreach ($affectedFavorites as $affectedFavorite) { |
| 123 | + $this->db->update( |
| 124 | + 'favorite', |
| 125 | + ['priority' => new Expression('priority - 1')], |
| 126 | + ['resource_uuid = ?' => $affectedFavorite->resource_uuid] |
| 127 | + ); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + // Now insert the favorite at the new priority |
| 132 | + $this->db->update('favorite', ['priority' => $newPriority], ['resource_uuid = ?' => $favoriteUuid]); |
| 133 | + |
| 134 | + if ($transactionStarted) { |
| 135 | + $this->db->commitTransaction(); |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments