Commit 31905914 authored by Tino Goratsch's avatar Tino Goratsch

Extended the ModelSaveEvent with the ability to determine, whether the saved...

Extended the ModelSaveEvent with the ability to determine, whether the saved result is actually new or an existing result has been saved
parent cabc83fd
......@@ -56,7 +56,8 @@ abstract class AbstractModel
{
$filteredData = $this->prepareData($rawData);
$this->dispatchBeforeSaveEvent($this->repository, $entryId, $filteredData, $rawData);
$isNewEntry = $entryId === null;
$this->dispatchBeforeSaveEvent($this->repository, $entryId, $filteredData, $rawData, $isNewEntry);
if ($entryId === null) {
$result = $this->repository->insert($filteredData);
......@@ -68,7 +69,7 @@ abstract class AbstractModel
$result = $this->repository->update($filteredData, $entryId);
}
$this->dispatchAfterSaveEvent($this->repository, $entryId, $filteredData, $rawData);
$this->dispatchAfterSaveEvent($this->repository, $entryId, $filteredData, $rawData, $isNewEntry);
return $result;
}
......@@ -78,17 +79,20 @@ abstract class AbstractModel
* @param int|null|array $entryId
* @param array $filteredData
* @param array $rawData
* @param bool $isNewEntry
*/
protected function dispatchBeforeSaveEvent(
AbstractRepository $repository,
$entryId,
array $filteredData,
array $rawData
array $rawData,
$isNewEntry
) {
$this->dispatchEvent('core.model.before_save', $entryId, $filteredData, $rawData);
$this->dispatchEvent('core.model.before_save', $entryId, $isNewEntry, $filteredData, $rawData);
$this->dispatchEvent(
static::EVENT_PREFIX . '.model.' . $repository::TABLE_NAME . '.before_save',
$entryId,
$isNewEntry,
$filteredData,
$rawData
);
......@@ -97,14 +101,15 @@ abstract class AbstractModel
/**
* @param string $eventName
* @param int|null|array $entryId
* @param bool $isNewEntry
* @param array $filteredData
* @param array $rawData
*/
protected function dispatchEvent($eventName, $entryId, array $filteredData = [], array $rawData = [])
protected function dispatchEvent($eventName, $entryId, $isNewEntry, array $filteredData = [], array $rawData = [])
{
$this->eventDispatcher->dispatch(
$eventName,
new ModelSaveEvent(static::EVENT_PREFIX, $filteredData, $rawData, $entryId)
new ModelSaveEvent(static::EVENT_PREFIX, $filteredData, $rawData, $entryId, $isNewEntry)
);
}
......@@ -127,17 +132,20 @@ abstract class AbstractModel
* @param int|null|array $entryId
* @param array $filteredData
* @param array $rawData
* @param bool $isNewEntry
*/
protected function dispatchAfterSaveEvent(
AbstractRepository $repository,
$entryId,
array $filteredData,
array $rawData
array $rawData,
$isNewEntry
) {
$this->dispatchEvent('core.model.after_save', $entryId, $filteredData, $rawData);
$this->dispatchEvent('core.model.after_save', $entryId, $isNewEntry, $filteredData, $rawData);
$this->dispatchEvent(
static::EVENT_PREFIX . '.model.' . $repository::TABLE_NAME . '.after_save',
$entryId,
$isNewEntry,
$filteredData,
$rawData
);
......@@ -155,9 +163,9 @@ abstract class AbstractModel
$entryId = [$entryId];
}
$this->dispatchEvent('core.model.before_delete', $entryId);
$this->dispatchEvent('core.model.before_delete', $entryId, false);
$this->dispatchEvent(
static::EVENT_PREFIX . '.model.' . $repository::TABLE_NAME . '.before_delete', $entryId
static::EVENT_PREFIX . '.model.' . $repository::TABLE_NAME . '.before_delete', $entryId, false
);
$affectedRows = 0;
......@@ -165,9 +173,9 @@ abstract class AbstractModel
$affectedRows += (int)$this->repository->delete($item);
}
$this->dispatchEvent('core.model.before_delete', $entryId);
$this->dispatchEvent('core.model.before_delete', $entryId, false);
$this->dispatchEvent(
static::EVENT_PREFIX . '.model.' . $repository::TABLE_NAME . '.after_delete', $entryId
static::EVENT_PREFIX . '.model.' . $repository::TABLE_NAME . '.after_delete', $entryId, false
);
return $affectedRows;
......
......@@ -61,7 +61,9 @@ abstract class AbstractNestedSetModel extends AbstractModel
{
$filteredData = $this->prepareData($rawData);
$this->dispatchBeforeSaveEvent($this->repository, $entryId, $filteredData, $rawData);
$isNewEntry = $entryId === null;
$this->dispatchBeforeSaveEvent($this->repository, $entryId, $filteredData, $rawData, $isNewEntry);
if ($entryId === null) {
$result = $this->insertOperation->execute($filteredData, $rawData['parent_id']);
......@@ -78,7 +80,7 @@ abstract class AbstractNestedSetModel extends AbstractModel
);
}
$this->dispatchAfterSaveEvent($this->repository, $entryId, $filteredData, $rawData);
$this->dispatchAfterSaveEvent($this->repository, $entryId, $filteredData, $rawData, $isNewEntry);
return $result;
}
......@@ -95,9 +97,9 @@ abstract class AbstractNestedSetModel extends AbstractModel
$entryId = [$entryId];
}
$this->dispatchEvent('core.model.before_delete', $entryId);
$this->dispatchEvent('core.model.before_delete', $entryId, false);
$this->dispatchEvent(
static::EVENT_PREFIX . '.model.' . $repository::TABLE_NAME . '.before_delete', $entryId
static::EVENT_PREFIX . '.model.' . $repository::TABLE_NAME . '.before_delete', $entryId, false
);
$affectedRows = 0;
......@@ -105,9 +107,9 @@ abstract class AbstractNestedSetModel extends AbstractModel
$affectedRows += (int)$this->deleteOperation->execute($item);
}
$this->dispatchEvent('core.model.before_delete', $entryId);
$this->dispatchEvent('core.model.before_delete', $entryId, false);
$this->dispatchEvent(
static::EVENT_PREFIX . '.model.' . $repository::TABLE_NAME . '.after_delete', $entryId
static::EVENT_PREFIX . '.model.' . $repository::TABLE_NAME . '.after_delete', $entryId, false
);
return $affectedRows;
......
......@@ -27,6 +27,10 @@ class ModelSaveEvent extends Event
* @var array
*/
private $rawData;
/**
* @var bool
*/
private $isNewEntry;
/**
* ModelSaveEvent constructor.
......@@ -34,13 +38,15 @@ class ModelSaveEvent extends Event
* @param array $filteredData
* @param array $rawData
* @param int|null|array $entryId
* @param bool $isNewEntry
*/
public function __construct($moduleName, array $filteredData, array $rawData, $entryId)
public function __construct($moduleName, array $filteredData, array $rawData, $entryId, $isNewEntry)
{
$this->moduleName = $moduleName;
$this->filteredData = $filteredData;
$this->rawData = $rawData;
$this->entryId = $entryId;
$this->isNewEntry = $isNewEntry;
}
/**
......@@ -82,4 +88,12 @@ class ModelSaveEvent extends Event
{
return count($this->filteredData) === 0 && is_array($this->entryId);
}
/**
* @return bool
*/
public function isIsNewEntry()
{
return $this->isNewEntry;
}
}
......@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
## [Unreleased] - 20016-xx-xx
### Added
- Extended the ajax-form jQuery plugin with the ability to gracefully handle failed AJAX requests
- Extended the ModelSaveEvent with the ability to determine, whether the saved result is actually new or an existing result has been saved
### Changed
- Reworked and improved the form handling a little bit
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment