Commit e5abaee8 authored by cornernote's avatar cornernote

change namespace again

parent 02c356ab
<?php
namespace cornernote\softdelete;
use yii\base\Behavior;
use yii\base\Event;
use yii\db\ActiveRecord;
/**
* Class SoftDelete
*
* @package vendor\vyants\softdelete
* @author Vladimir Yants <vladimir.yants@gmail.com>
* @property ActiveRecord $owner
*/
class SoftDelete extends Behavior
{
/**
* @var string delete time attribute
*/
public $timeAttribute = false;
/**
* @var string status attribute
*/
public $statusAttribute = "status";
/**
* @var string deleted status attribute
*/
public $deletedValue = -1;
/**
* @var string active status attribute
*/
public $activeValue = 1;
/**
* Удалить софтделитом. Возращает true/false в зависимости от того, успешно ли
* @return bool
*/
public function softDelete() {
if($this->timeAttribute) {
$attributes[0] = $this->timeAttribute;
$this->owner->$attributes[0] = time();
}
$attributes[1] = $this->statusAttribute;
$this->owner->$attributes[1] = $this->deletedValue;
// save record
return $this->owner->save(false, $attributes);
}
/**
* Restore soft-deleted record. Возращает true/false в зависимости от того, успешно ли
* @return bool
*/
public function restore() {
if($this->timeAttribute) {
$attributes[0] = $this->timeAttribute;
$this->owner->$attributes[0] = null;
}
$attributes[1] = $this->statusAttribute;
$this->owner->$attributes[1] = $this->activeValue;
// save record
return $this->owner->save(false, $attributes);
}
/**
* Force delete from database. Возращает true/false в зависимости от того, успешно ли
* @return bool
*/
public function forceDelete() {
// store model so that we can detach the behavior and delete as normal
$model = $this->owner;
$this->detach();
$result = $model->delete();
$this->attach($model);
return $result;
}
}
\ No newline at end of file
<?php
namespace cornernote\behaviors;
use yii\base\Event;
use yii\db\BaseActiveRecord;
use yii\db\Expression;
use yii\behaviors\AttributeBehavior;
/**
* SoftDeleteBehavior
*
* @usage:
* ```
* public function behaviors() {
* return [
* 'softDelete' => ['class' => 'cornernote\behaviors\SoftDeleteBehavior',
* 'attribute' => 'delete_time',
* 'value' => new Expression('NOW()'),
* ],
* ];
* }
* ```
*
* @property BaseActiveRecord $owner
*
* @author cornernote <cornernote@gmail.com>
* @author amnah <amnah.dev@gmail.com>
*/
class SoftDeleteBehavior extends AttributeBehavior
{
/**
* @var string SoftDelete attribute
*/
public $deletedAtAttribute = 'deleted_at';
/**
* @var callable|Expression The expression that will be used for generating the timestamp.
* This can be either an anonymous function that returns the timestamp value,
* or an [[Expression]] object representing a DB expression (e.g. `new Expression('NOW()')`).
* If not set, it will use the value of `time()` to set the attributes.
*/
public $value;
/**
* @inheritdoc
*/
public function events()
{
return [BaseActiveRecord::EVENT_BEFORE_DELETE => 'doDeleteTimestamp'];
}
/**
* Set the attribute with the current timestamp to mark as deleted
*
* @param Event $event
*/
public function doDeleteTimestamp($event)
{
// remove and mark as invalid to prevent real deletion
$this->remove();
$event->isValid = false;
}
/**
* Remove (aka soft-delete) record
*/
public function remove()
{
// evaluate timestamp and set attribute
$timestamp = $this->evaluateAttributes();
$attribute = $this->attribute;
$this->owner->$attribute = $timestamp;
// save record
$this->owner->save(false, [$attribute]);
}
/**
* Restore soft-deleted record
*/
public function restore()
{
// mark attribute as null
$attribute = $this->attribute;
$this->owner->$attribute = null;
// save record
$this->owner->save(false, [$attribute]);
}
/**
* Delete record from database regardless of the $safeMode attribute
*/
public function forceDelete()
{
// store model so that we can detach the behavior and delete as normal
$model = $this->owner;
$this->detach();
$model->delete();
}
/**
* @inheritdoc
*/
protected function getValue($event)
{
if ($this->value instanceof Expression) {
return $this->value;
} else {
return $this->value !== null ? call_user_func($this->value, $event) : time();
}
}
}
\ No newline at end of file
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {
"cornernote\\softdelete\\": "" "cornernote\\": ""
} }
} }
} }
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