mirror of
https://github.com/openvk/chandler.git
synced 2025-03-31 21:43:59 +03:00
DBEntity.php with logs
This commit is contained in:
parent
b46c33d5b8
commit
1dc9ba16e2
1 changed files with 43 additions and 10 deletions
|
@ -4,6 +4,8 @@ use Chandler\Database\DatabaseConnection;
|
||||||
use Nette\Database\Table\Selection;
|
use Nette\Database\Table\Selection;
|
||||||
use Nette\Database\Table\ActiveRow;
|
use Nette\Database\Table\ActiveRow;
|
||||||
use Nette\InvalidStateException as ISE;
|
use Nette\InvalidStateException as ISE;
|
||||||
|
use openvk\Web\Models\Repositories\CurrentUser;
|
||||||
|
use openvk\Web\Models\Repositories\Logs;
|
||||||
|
|
||||||
|
|
||||||
abstract class DBEntity
|
abstract class DBEntity
|
||||||
|
@ -11,7 +13,8 @@ abstract class DBEntity
|
||||||
protected $record;
|
protected $record;
|
||||||
protected $changes;
|
protected $changes;
|
||||||
protected $deleted;
|
protected $deleted;
|
||||||
|
protected $user;
|
||||||
|
|
||||||
protected $tableName;
|
protected $tableName;
|
||||||
|
|
||||||
function __construct(?ActiveRow $row = NULL)
|
function __construct(?ActiveRow $row = NULL)
|
||||||
|
@ -70,9 +73,14 @@ abstract class DBEntity
|
||||||
|
|
||||||
function delete(bool $softly = true): void
|
function delete(bool $softly = true): void
|
||||||
{
|
{
|
||||||
|
$user = CurrentUser::i()->getUser();
|
||||||
|
$user_id = is_null($user) ? (int) OPENVK_ROOT_CONF["openvk"]["preferences"]["support"]["adminAccount"] : $user->getId();
|
||||||
|
|
||||||
if(is_null($this->record))
|
if(is_null($this->record))
|
||||||
throw new ISE("Can't delete a model, that hasn't been flushed to DB. Have you forgotten to call save() first?");
|
throw new ISE("Can't delete a model, that hasn't been flushed to DB. Have you forgotten to call save() first?");
|
||||||
|
|
||||||
|
(new Logs)->create($user_id, $this->getTable()->getName(), get_class($this), 2, $this->record->toArray(), $this->changes);
|
||||||
|
|
||||||
if($softly) {
|
if($softly) {
|
||||||
$this->record = $this->getTable()->where("id", $this->record->id)->update(["deleted" => true]);
|
$this->record = $this->getTable()->where("id", $this->record->id)->update(["deleted" => true]);
|
||||||
} else {
|
} else {
|
||||||
|
@ -85,23 +93,48 @@ abstract class DBEntity
|
||||||
{
|
{
|
||||||
if(is_null($this->record))
|
if(is_null($this->record))
|
||||||
throw new ISE("Can't undelete a model, that hasn't been flushed to DB. Have you forgotten to call save() first?");
|
throw new ISE("Can't undelete a model, that hasn't been flushed to DB. Have you forgotten to call save() first?");
|
||||||
|
|
||||||
|
$user = CurrentUser::i()->getUser();
|
||||||
|
$user_id = is_null($user) ? (int) OPENVK_ROOT_CONF["openvk"]["preferences"]["support"]["adminAccount"] : $user->getId();
|
||||||
|
|
||||||
|
(new Logs)->create($user_id, $this->getTable()->getName(), get_class($this), 3, $this->record->toArray(), ["deleted" => false]);
|
||||||
|
|
||||||
$this->getTable()->where("id", $this->record->id)->update(["deleted" => false]);
|
$this->getTable()->where("id", $this->record->id)->update(["deleted" => false]);
|
||||||
}
|
}
|
||||||
|
|
||||||
function save(): void
|
function save(?bool $log = true): void
|
||||||
{
|
{
|
||||||
|
if ($log) {
|
||||||
|
$user = CurrentUser::i();
|
||||||
|
$user_id = is_null($user) ? (int)OPENVK_ROOT_CONF["openvk"]["preferences"]["support"]["adminAccount"] : $user->getUser()->getId();
|
||||||
|
}
|
||||||
|
|
||||||
if(is_null($this->record)) {
|
if(is_null($this->record)) {
|
||||||
$this->record = $this->getTable()->insert($this->changes);
|
$this->record = $this->getTable()->insert($this->changes);
|
||||||
} else if($this->deleted) {
|
|
||||||
$this->record = $this->getTable()->insert((array) $this->record);
|
if ($log && $this->getTable()->getName() !== "logs") {
|
||||||
|
(new Logs)->create($user_id, $this->getTable()->getName(), get_class($this), 0, $this->record->toArray(), $this->changes);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
$this->getTable()->get($this->record->id)->update($this->changes);
|
if ($log && $this->getTable()->getName() !== "logs") {
|
||||||
$this->record = $this->getTable()->get($this->record->id);
|
(new Logs)->create($user_id, $this->getTable()->getName(), get_class($this), 1, $this->record->toArray(), $this->changes);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->deleted) {
|
||||||
|
$this->record = $this->getTable()->insert((array)$this->record);
|
||||||
|
} else {
|
||||||
|
$this->getTable()->get($this->record->id)->update($this->changes);
|
||||||
|
$this->record = $this->getTable()->get($this->record->id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->changes = [];
|
$this->changes = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getTableName(): string
|
||||||
|
{
|
||||||
|
return $this->getTable()->getName();
|
||||||
|
}
|
||||||
|
|
||||||
use \Nette\SmartObject;
|
use \Nette\SmartObject;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue