Maslosoft Mangan API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
<?php
/**
* This software package is licensed under AGPL or Commercial license.
*
* @package maslosoft/mangan
* @licence AGPL or Commercial
* @copyright Copyright (c) Piotr Masełkowski <pmaselkowski@gmail.com>
* @copyright Copyright (c) Maslosoft
* @copyright Copyright (c) Others as mentioned in code
* @link https://maslosoft.com/mangan/
*/
namespace Maslosoft\Mangan\Traits\Model;
use Exception;
use Maslosoft\Mangan\EntityManager;
use Maslosoft\Mangan\Events\Event;
use Maslosoft\Mangan\Events\RestoreEvent;
use Maslosoft\Mangan\Events\TrashEvent;
use Maslosoft\Mangan\Finder;
use Maslosoft\Mangan\Helpers\PkManager;
use Maslosoft\Mangan\Interfaces\TrashInterface;
use Maslosoft\Mangan\Meta\ManganMeta;
use Maslosoft\Mangan\Model\Trash;
use Maslosoft\Mangan\ScenarioManager;
use Maslosoft\Mangan\Validator;
/**
* Uswe this trait to make model trashable
*
* @author Piotr
*/
trait TrashableTrait
{
/**
* Move to trash. Validation will be performed
* before trashing with `trash` (TrashInterface::ScenarioTrash) scenario.
*
*
* @see TrashInterface::ScenarioTrash
* @param boolean $runValidation whether to perform validation before saving the record.
* If the validation fails, the record will not be saved to database.
*
* @return boolean
* @Ignored
*/
public function trash($runValidation = true)
{
ScenarioManager::setScenario($this, TrashInterface::ScenarioTrash);
$validator = new Validator($this);
if (!$runValidation || $validator->validate())
{
$eventBefore = new TrashEvent($this);
if (Event::hasHandler($this, TrashInterface::EventBeforeTrash))
{
if (!Event::valid($this, TrashInterface::EventBeforeTrash, $eventBefore))
{
return false;
}
}
$meta = ManganMeta::create($this);
$trash = $eventBefore->getTrash();
if (empty($trash))
{
$trash = new Trash();
}
$trash->name = (string) $this;
$trash->data = $this;
$trash->type = isset($meta->type()->label) ? $meta->type()->label : get_class($this);
if (!$trash->save())
{
return false;
}
$eventAfter = new TrashEvent($this);
$eventAfter->setTrash($trash);
if (!Event::valid($this, TrashInterface::EventAfterTrash, $eventAfter))
{
return false;
}
// Use deleteOne, to avoid beforeDelete event,
// which should be raised only when really removing document:
// when emtying trash
$em = new EntityManager($this);
return $em->deleteOne(PkManager::prepareFromModel($this));
}
return false;
}
/**
* Restore trashed item
* @return boolean
* @throws Exception
* @Ignored
*/
public function restore()
{
if (!$this instanceof TrashInterface)
{
// When trying to restore normal document instead of trash item
throw new Exception(sprintf('Restore can be performed only on `%s` instance', TrashInterface::class));
}
$em = new EntityManager($this->data);
// Set scenario to `restore` for model, which is just about to be restored
ScenarioManager::setScenario($this->data, TrashInterface::ScenarioRestore);
if (!Event::valid($this->data, TrashInterface::EventBeforeRestore))
{
return false;
}
$saved = $em->save();
if (!$saved)
{
return false;
}
$finder = new Finder($this->data);
$model = $finder->find(PkManager::prepareFromModel($this->data));
if (empty($model))
{
return false;
}
$eventAfter = new RestoreEvent();
$eventAfter->setTrashed($this->data);
$eventAfter->setTrash($this);
if (!Event::valid($model, TrashInterface::EventAfterRestore, $eventAfter))
{
return false;
}
$trashEm = new EntityManager($this);
$this->data = null;
// Use deleteOne, to avoid beforeDelete event,
// which should be raised only when really removing document:
// when emptying trash
return $trashEm->deleteOne(PkManager::prepareFromModel($this));
}
abstract public function __toString();
}
API documentation generated by ApiGen