Commit a8f371b9 authored by Nicola Tomassoni's avatar Nicola Tomassoni

Digisin release

parent efa5f6bf
......@@ -10,4 +10,5 @@ build/
vendor/
# ignore temporary files
*~
\ No newline at end of file
*~
/nbproject/private/
\ No newline at end of file
{
"name": "cornernote/yii2-softdelete",
"name": "digisin/yii2-softdelete",
"description": "Soft delete behavior for Yii2.",
"keywords": ["yii2", "soft delete"],
"type": "yii2-behavior",
......@@ -8,6 +8,10 @@
{
"name": "Brett O'Donnell",
"email": "cornernote@gmail.com"
},
{
"name": "Nicola Tomassoni",
"email": "nicola@digisin.it"
}
],
"require": {
......@@ -20,7 +24,7 @@
},
"autoload": {
"psr-4": {
"cornernote\\softdelete\\": "src"
"digisin\\softdelete\\": "src"
}
}
}
include.path=${php.global.include.path}
php.version=PHP_56
source.encoding=UTF-8
src.dir=.
tags.asp=false
tags.short=false
web.root=.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://www.netbeans.org/ns/project/1">
<type>org.netbeans.modules.php.project</type>
<configuration>
<data xmlns="http://www.netbeans.org/ns/php-project/1">
<name>yii2-softdelete</name>
</data>
</configuration>
</project>
<?php
/**
* @author Brett O'Donnell <cornernote@gmail.com>
* @copyright 2015 Mr PHP
......@@ -6,7 +7,7 @@
* @license BSD-3-Clause https://raw.github.com/cornernote/yii2-softdelete/master/LICENSE.md
*/
namespace cornernote\softdelete;
namespace digisin\softdelete;
use yii\base\Behavior;
use yii\base\Event;
......@@ -31,90 +32,84 @@ use yii\db\Expression;
*
* @property BaseActiveRecord $owner
*/
class SoftDeleteBehavior extends Behavior
{
/**
* @var string SoftDelete attribute
*/
public $attribute = 'deleted_at';
class SoftDeleteBehavior extends Behavior {
/**
* @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;
/**
* @var string SoftDelete attribute
*/
public $attribute = 'deleted_at';
/**
* @inheritdoc
*/
public function events()
{
return [BaseActiveRecord::EVENT_BEFORE_DELETE => 'softDeleteEvent'];
}
/**
* @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;
/**
* Set the attribute with the current timestamp to mark as deleted
*
* @param Event $event
*/
public function softDeleteEvent($event)
{
// remove and mark as invalid to prevent real deletion
$this->softDelete();
$event->isValid = false;
}
/**
* @inheritdoc
*/
public function events() {
return [BaseActiveRecord::EVENT_BEFORE_DELETE => 'softDeleteEvent'];
}
/**
* Soft delete record
*/
public function softDelete()
{
// set attribute with evaluated timestamp
$attribute = $this->attribute;
$this->owner->$attribute = $this->getValue(null);
// save record
$this->owner->save(false, [$attribute]);
}
/**
* Set the attribute with the current timestamp to mark as deleted
*
* @param Event $event
*/
public function softDeleteEvent($event) {
// remove and mark as invalid to prevent real deletion
$this->softDelete();
$event->isValid = false;
}
/**
* Restore record
*/
public function unDelete()
{
// set attribute as null
$attribute = $this->attribute;
$this->owner->$attribute = null;
// save record
$this->owner->save(false, [$attribute]);
}
/**
* Soft delete record
*/
public function softDelete() {
// set attribute with evaluated timestamp
$attribute = $this->attribute;
$this->owner->$attribute = $this->getValue(null);
// save record
$this->owner->save(false, [$attribute]);
}
/**
* Delete record from database regardless of the $safeMode attribute
*/
public function hardDelete()
{
// store model so that we can detach the behavior
$model = $this->owner;
$this->detach();
// delete as normal
$model->delete();
}
/**
* Restore record
*/
public function unDelete() {
// set 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 hardDelete() {
// store model so that we can detach the behavior
$model = $this->owner;
$this->detach();
// delete as normal
$model->delete();
}
/**
* Evaluate the timestamp to be saved.
*
* @param Event|null $event the event that triggers the current attribute updating.
* @return mixed the attribute value
*/
protected function getValue($event)
{
if ($this->value instanceof Expression) {
return $this->value;
} else {
return $this->value !== null ? call_user_func($this->value, $event) : time();
}
/**
* Evaluate the timestamp to be saved.
*
* @param Event|null $event the event that triggers the current attribute updating.
* @return mixed the attribute value
*/
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
}
<?php
namespace cornernote\softdelete;
namespace digisin\softdelete;
use yii\base\Behavior;
use yii\db\ActiveQuery;
......@@ -25,37 +25,34 @@ use yii\db\ActiveRecord;
*
* @author cornernote <cornernote@gmail.com>
*/
class SoftDeleteQueryBehavior extends Behavior
{
/**
* @var string SoftDelete attribute
*/
public $attribute = 'deleted_at';
/**
* @return static
*/
public function deleted()
{
return $this->owner->andWhere($this->tableName() . '.' . $this->attribute . ' IS NOT NULL');
}
/**
* @return static
*/
public function notDeleted()
{
return $this->owner->andWhere($this->tableName() . '.' . $this->attribute . ' IS NULL');
}
/**
* @return string
*/
protected function tableName()
{
/** @var ActiveRecord $modelClass */
$modelClass = $this->owner->modelClass;
return $modelClass::tableName();
}
class SoftDeleteQueryBehavior extends Behavior {
/**
* @var string SoftDelete attribute
*/
public $attribute = 'deleted_at';
/**
* @return static
*/
public function deleted() {
return $this->owner->andWhere($this->tableName() . '.' . $this->attribute . ' IS NOT NULL');
}
/**
* @return static
*/
public function notDeleted() {
return $this->owner->andWhere($this->tableName() . '.' . $this->attribute . ' IS NULL');
}
/**
* @return string
*/
protected function tableName() {
/** @var ActiveRecord $modelClass */
$modelClass = $this->owner->modelClass;
return $modelClass::tableName();
}
}
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