From 460c06e922b5f78eaabcd0295d1c0f16e64fdfb9 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Fri, 3 Feb 2017 12:20:24 +0100 Subject: [PATCH] RepositoryForm: Add more flexibility when interacting with the repository --- application/forms/RepositoryForm.php | 74 ++++++++++++++++++++++------ 1 file changed, 59 insertions(+), 15 deletions(-) diff --git a/application/forms/RepositoryForm.php b/application/forms/RepositoryForm.php index 1485d98b2..fa3ffe5b7 100644 --- a/application/forms/RepositoryForm.php +++ b/application/forms/RepositoryForm.php @@ -61,7 +61,7 @@ abstract class RepositoryForm extends Form /** * The data of the entry to pre-populate the form with when in mode insert or update * - * @var type + * @var array */ protected $data; @@ -186,6 +186,59 @@ abstract class RepositoryForm extends Form return $this; } + /** + * Fetch and return the entry to pre-populate the form with when in mode update + * + * @return false|object + */ + protected function fetchEntry() + { + return $this->repository + ->select() + ->from($this->getBaseTable()) + ->applyFilter($this->createFilter()) + ->fetchRow(); + } + + /** + * Return whether the entry supposed to be removed exists + * + * @return bool + */ + protected function entryExists() + { + $count = $this->repository + ->select() + ->from($this->getBaseTable()) + ->addFilter($this->createFilter()) + ->count(); + return $count > 0; + } + + /** + * Insert the new entry + */ + protected function insertEntry() + { + $this->repository->insert($this->getBaseTable(), $this->getValues()); + } + + /** + * Update the entry + */ + protected function updateEntry() + { + $this->repository->update($this->getBaseTable(), $this->getValues(), $this->createFilter()); + } + + /** + * Delete the entry + */ + protected function deleteEntry() + { + $this->repository->delete($this->getBaseTable(), $this->createFilter()); + } + /** * Create and add elements to this form * @@ -240,11 +293,7 @@ abstract class RepositoryForm extends Form { $data = $this->getData(); if ($data === null) { - $row = $this->repository - ->select() - ->from($this->getBaseTable()) - ->applyFilter($this->createFilter()) - ->fetchRow(); + $row = $this->fetchEntry(); if ($row === false) { throw new NotFoundError('Entry "%s" not found', $this->getIdentifier()); } @@ -264,12 +313,7 @@ abstract class RepositoryForm extends Form */ protected function onDeleteRequest() { - $count = $this->repository - ->select() - ->from($this->getBaseTable()) - ->addFilter($this->createFilter()) - ->count(); - if ($count === 0) { + if (! $this->entryExists()) { throw new NotFoundError('Entry "%s" not found', $this->getIdentifier()); } } @@ -298,7 +342,7 @@ abstract class RepositoryForm extends Form protected function onInsertSuccess() { try { - $this->repository->insert($this->getBaseTable(), $this->getValues()); + $this->insertEntry(); } catch (Exception $e) { Notification::error($this->getInsertMessage(false)); $this->error($e->getMessage()); @@ -317,7 +361,7 @@ abstract class RepositoryForm extends Form protected function onUpdateSuccess() { try { - $this->repository->update($this->getBaseTable(), $this->getValues(), $this->createFilter()); + $this->updateEntry(); } catch (Exception $e) { Notification::error($this->getUpdateMessage(false)); $this->error($e->getMessage()); @@ -336,7 +380,7 @@ abstract class RepositoryForm extends Form protected function onDeleteSuccess() { try { - $this->repository->delete($this->getBaseTable(), $this->createFilter()); + $this->deleteEntry(); } catch (Exception $e) { Notification::error($this->getDeleteMessage(false)); $this->error($e->getMessage());