123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355 |
- <?php
- namespace common\behaviors;
- use yii\base\Behavior;
- use yii\base\InvalidConfigException;
- use yii\base\ModelEvent;
- use yii\db\BaseActiveRecord;
- class SoftDeleteBehavior extends Behavior
- {
-
- const EVENT_BEFORE_SOFT_DELETE = 'beforeSoftDelete';
-
- const EVENT_AFTER_SOFT_DELETE = 'afterSoftDelete';
-
- const EVENT_BEFORE_RESTORE = 'beforeRestore';
-
- const EVENT_AFTER_RESTORE = 'afterRestore';
-
- public $softDeleteAttributeValues = [
- 'isDeleted' => true
- ];
-
- public $restoreAttributeValues;
-
- public $invokeDeleteEvents = true;
-
- public $allowDeleteCallback;
-
- public $deleteFallbackException = 'yii\db\IntegrityException';
-
- private $_replaceRegularDelete = false;
-
- public function getReplaceRegularDelete()
- {
- return $this->_replaceRegularDelete;
- }
-
- public function setReplaceRegularDelete($replaceRegularDelete)
- {
- $this->_replaceRegularDelete = $replaceRegularDelete;
- if (is_object($this->owner)) {
- $owner = $this->owner;
- $this->detach();
- $this->attach($owner);
- }
- }
-
- public function softDelete()
- {
- if ($this->isDeleteAllowed()) {
- return $this->owner->delete();
- }
- if ($this->invokeDeleteEvents && !$this->owner->beforeDelete()) {
- return false;
- }
- $result = $this->softDeleteInternal();
- if ($this->invokeDeleteEvents) {
- $this->owner->afterDelete();
- }
- return $result;
- }
-
- protected function softDeleteInternal()
- {
- $result = false;
- if ($this->beforeSoftDelete()) {
- $attributes = [];
- foreach ($this->softDeleteAttributeValues as $attribute => $value) {
- if (!is_scalar($value) && is_callable($value)) {
- $value = call_user_func($value, $this->owner);
- }
- $attributes[$attribute] = $value;
- }
- $result = $this->owner->updateAttributes($attributes);
- $this->afterSoftDelete();
- }
- return $result;
- }
-
- public function beforeSoftDelete()
- {
- if (method_exists($this->owner, 'beforeSoftDelete')) {
- if (!$this->owner->beforeSoftDelete()) {
- return false;
- }
- }
- $event = new ModelEvent();
- $this->owner->trigger(self::EVENT_BEFORE_SOFT_DELETE, $event);
- return $event->isValid;
- }
-
- public function afterSoftDelete()
- {
- if (method_exists($this->owner, 'afterSoftDelete')) {
- $this->owner->afterSoftDelete();
- }
- $this->owner->trigger(self::EVENT_AFTER_SOFT_DELETE);
- }
-
- protected function isDeleteAllowed()
- {
- if ($this->allowDeleteCallback === null) {
- return false;
- }
- return call_user_func($this->allowDeleteCallback, $this->owner);
- }
-
-
- public function restore()
- {
- $result = false;
- if ($this->beforeRestore()) {
- $result = $this->restoreInternal();
- $this->afterRestore();
- }
- return $result;
- }
-
- protected function restoreInternal()
- {
- $restoreAttributeValues = $this->restoreAttributeValues;
- if ($restoreAttributeValues === null) {
- foreach ($this->softDeleteAttributeValues as $name => $value) {
- if (is_bool($value) || $value === 1 || $value === 0) {
- $restoreValue = !$value;
- } elseif (is_int($value)) {
- if ($value === 1) {
- $restoreValue = 0;
- } elseif ($value === 0) {
- $restoreValue = 1;
- } else {
- $restoreValue = $value + 1;
- }
- } else {
- throw new InvalidConfigException('Unable to automatically determine restore attribute values, "' . get_class($this) . '::restoreAttributeValues" should be explicitly set.');
- }
- $restoreAttributeValues[$name] = $restoreValue;
- }
- }
- $attributes = [];
- foreach ($restoreAttributeValues as $attribute => $value) {
- if (!is_scalar($value) && is_callable($value)) {
- $value = call_user_func($value, $this->owner);
- }
- $attributes[$attribute] = $value;
- }
- return $this->owner->updateAttributes($attributes);
- }
-
- public function beforeRestore()
- {
- if (method_exists($this->owner, 'beforeRestore')) {
- if (!$this->owner->beforeRestore()) {
- return false;
- }
- }
- $event = new ModelEvent();
- $this->owner->trigger(self::EVENT_BEFORE_RESTORE, $event);
- return $event->isValid;
- }
-
- public function afterRestore()
- {
- if (method_exists($this->owner, 'afterRestore')) {
- $this->owner->afterRestore();
- }
- $this->owner->trigger(self::EVENT_AFTER_RESTORE);
- }
-
- public function safeDelete()
- {
- try {
- $transaction = $this->beginTransaction();
- $result = $this->owner->delete();
- if (isset($transaction)) {
- $transaction->commit();
- }
- } catch (\Exception $exception) {
- if (isset($transaction)) {
- $transaction->rollback();
- }
- $fallbackExceptionClass = $this->deleteFallbackException;
- if ($exception instanceof $fallbackExceptionClass) {
- $result = $this->softDeleteInternal();
- } else {
- throw $exception;
- }
- }
- return $result;
- }
-
- private function beginTransaction()
- {
- $db = $this->owner->getDb();
- if ($db->hasMethod('beginTransaction')) {
- return $db->beginTransaction();
- }
- return null;
- }
-
-
- public function events()
- {
- if ($this->getReplaceRegularDelete()) {
- return [
- BaseActiveRecord::EVENT_BEFORE_DELETE => 'beforeDelete',
- ];
- } else {
- return [];
- }
- }
-
- public function beforeDelete($event)
- {
- if (!$this->isDeleteAllowed()) {
- $this->softDeleteInternal();
- $event->isValid = false;
- }
- }
- }
|