mirror of
https://github.com/Icinga/icingaweb2.git
synced 2025-07-25 23:04:51 +02:00
RepositoryForm: Add more flexibility when interacting with the repository
This commit is contained in:
parent
5b4de83970
commit
460c06e922
@ -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
|
* The data of the entry to pre-populate the form with when in mode insert or update
|
||||||
*
|
*
|
||||||
* @var type
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $data;
|
protected $data;
|
||||||
|
|
||||||
@ -186,6 +186,59 @@ abstract class RepositoryForm extends Form
|
|||||||
return $this;
|
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
|
* Create and add elements to this form
|
||||||
*
|
*
|
||||||
@ -240,11 +293,7 @@ abstract class RepositoryForm extends Form
|
|||||||
{
|
{
|
||||||
$data = $this->getData();
|
$data = $this->getData();
|
||||||
if ($data === null) {
|
if ($data === null) {
|
||||||
$row = $this->repository
|
$row = $this->fetchEntry();
|
||||||
->select()
|
|
||||||
->from($this->getBaseTable())
|
|
||||||
->applyFilter($this->createFilter())
|
|
||||||
->fetchRow();
|
|
||||||
if ($row === false) {
|
if ($row === false) {
|
||||||
throw new NotFoundError('Entry "%s" not found', $this->getIdentifier());
|
throw new NotFoundError('Entry "%s" not found', $this->getIdentifier());
|
||||||
}
|
}
|
||||||
@ -264,12 +313,7 @@ abstract class RepositoryForm extends Form
|
|||||||
*/
|
*/
|
||||||
protected function onDeleteRequest()
|
protected function onDeleteRequest()
|
||||||
{
|
{
|
||||||
$count = $this->repository
|
if (! $this->entryExists()) {
|
||||||
->select()
|
|
||||||
->from($this->getBaseTable())
|
|
||||||
->addFilter($this->createFilter())
|
|
||||||
->count();
|
|
||||||
if ($count === 0) {
|
|
||||||
throw new NotFoundError('Entry "%s" not found', $this->getIdentifier());
|
throw new NotFoundError('Entry "%s" not found', $this->getIdentifier());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -298,7 +342,7 @@ abstract class RepositoryForm extends Form
|
|||||||
protected function onInsertSuccess()
|
protected function onInsertSuccess()
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$this->repository->insert($this->getBaseTable(), $this->getValues());
|
$this->insertEntry();
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
Notification::error($this->getInsertMessage(false));
|
Notification::error($this->getInsertMessage(false));
|
||||||
$this->error($e->getMessage());
|
$this->error($e->getMessage());
|
||||||
@ -317,7 +361,7 @@ abstract class RepositoryForm extends Form
|
|||||||
protected function onUpdateSuccess()
|
protected function onUpdateSuccess()
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$this->repository->update($this->getBaseTable(), $this->getValues(), $this->createFilter());
|
$this->updateEntry();
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
Notification::error($this->getUpdateMessage(false));
|
Notification::error($this->getUpdateMessage(false));
|
||||||
$this->error($e->getMessage());
|
$this->error($e->getMessage());
|
||||||
@ -336,7 +380,7 @@ abstract class RepositoryForm extends Form
|
|||||||
protected function onDeleteSuccess()
|
protected function onDeleteSuccess()
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$this->repository->delete($this->getBaseTable(), $this->createFilter());
|
$this->deleteEntry();
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
Notification::error($this->getDeleteMessage(false));
|
Notification::error($this->getDeleteMessage(false));
|
||||||
$this->error($e->getMessage());
|
$this->error($e->getMessage());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user