Skip to content

Commit

Permalink
Revert "WIP land remaining TODO multiline"
Browse files Browse the repository at this point in the history
This reverts commit ff1bc41.
  • Loading branch information
mvorisek committed Feb 14, 2024
1 parent ff1bc41 commit a984f1b
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions src/Form/Control/Multiline.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Atk4\Data\Model;
use Atk4\Data\Persistence;
use Atk4\Data\ValidationException;
use Atk4\Ui\Demos\WrappedId;
use Atk4\Ui\Exception;
use Atk4\Ui\Form;
use Atk4\Ui\HtmlTemplate;
Expand Down Expand Up @@ -338,14 +337,8 @@ public function saveRows(): self
{
$model = $this->model;

// delete remaining IDs
// TODO this is dangerous, the deleted row IDs should be passed from UI
$modelCloned = clone $model;
$idsToDelete = array_filter(array_column($this->rowData, $model->idField), static fn ($v) => $v !== null);
$modelCloned->addCondition($modelCloned->idField, 'not in', $idsToDelete);
foreach ($modelCloned as $entity) {
$entity->delete();
}
// collects existing IDs
$currentIds = array_column($model->export(), $model->idField);

foreach ($this->rowData as $row) {
$entity = $row[$model->idField] !== null
Expand All @@ -364,6 +357,16 @@ public function saveRows(): self
if (!$entity->isLoaded() || $entity->getDirtyRef() !== []) {
$entity->save();
}

$k = array_search($entity->getId(), $currentIds, true);
if ($k !== false) {
unset($currentIds[$k]);
}
}

// delete removed IDs
foreach ($currentIds as $id) {
$model->delete($id);
}

return $this;
Expand Down Expand Up @@ -559,7 +562,7 @@ protected function getLookupProps(Field $field): array
public function setLookupOptionValue(Field $field, string $value): void
{
$model = $field->getReference()->refModel($this->model);
$entity = $model->tryLoadBy($field->getReference()->getTheirFieldName($model), new WrappedId(ctype_digit($value) ? (int) $value : $value));
$entity = $model->tryLoadBy($field->getReference()->getTheirFieldName($model), $value);
if ($entity !== null) {
$option = ['key' => $value, 'text' => $entity->get($model->titleField), 'value' => $value];
foreach ($this->fieldDefs as $key => $component) {
Expand Down Expand Up @@ -619,11 +622,7 @@ protected function getFieldItems(Field $field, ?int $limit = 10): array
$model->setLimit($limit);

foreach ($model as $item) {
$k = $item->get($field->getReference()->getTheirFieldName($model));
if ($k instanceof WrappedId) {
$k = $k->getId();
}
$items[$k] = $item->get($model->titleField);
$items[$item->get($field->getReference()->getTheirFieldName($model))] = $item->get($model->titleField);
}
}

Expand Down

0 comments on commit a984f1b

Please sign in to comment.