This repository has been archived by the owner on Feb 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
ETrashBinBehavior.php
145 lines (141 loc) · 3.2 KB
/
ETrashBinBehavior.php
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
<?php
/**
* ETrashBinBehavior class file.
*
* @author Veaceslav Medvedev <slavcopost@gmail.com>
* @link http://code.google.com/p/yiiext/
* @license http://www.opensource.org/licenses/mit-license.php
*/
/**
* ETrashBinBehavior allows you to remove the model in the trash bin and restore them when need.
*
* @property CActiveRecord $owner
* @method CActiveRecord getOwner()
*
* @author Veaceslav Medvedev <slavcopost@gmail.com>
* @version 0.4
* @package yiiext.behaviors.model.trashBin
* @since 1.1.4
*/
class ETrashBinBehavior extends CActiveRecordBehavior
{
/**
* @var string The name of the table where data stored.
* Defaults to "removed".
*/
public $trashFlagField = 'removed';
/**
* @var mixed The value to set for removed model.
* Default is 1.
*/
public $removedFlag=1;
/**
* @var mixed The value to set for restored model.
* Default is 0.
*/
public $restoredFlag=0;
/**
* @var bool If except removed model in find results.
* Default is false.
*/
public $findRemoved=false;
/**
* @var bool The flag to disable filter removed models in the next query.
*/
protected $_withRemoved=false;
public function events()
{
$events = array();
if (!$this->findRemoved) {
$events['onBeforeFind'] = 'beforeFind';
$events['onBeforeCount'] = 'beforeCount';
}
return $events;
}
/**
* Set value for trash field.
*
* @param mixed $value
* @return CActiveRecord
*/
public function setTrashFlag($value)
{
$owner=$this->getOwner();
$owner->setAttribute($this->trashFlagField, $value==$this->removedFlag?$this->removedFlag:$this->restoredFlag);
return $owner;
}
/**
* Remove model in trash bin.
*
* @return CActiveRecord
*/
public function remove()
{
return $this->setTrashFlag($this->removedFlag);
}
/**
* Restore removed model from trash bin.
*
* @return CActiveRecord
*/
public function restore()
{
return $this->setTrashFlag($this->restoredFlag);
}
/**
* Check if model is removed in trash bin.
*
* @return bool
*/
public function getIsRemoved()
{
return $this->getOwner()->getAttribute($this->trashFlagField)==$this->removedFlag;
}
/**
* Disable excepting removed models for next search.
*
* @return CActiveRecord
*/
public function withRemoved()
{
$this->_withRemoved=true;
return $this->getOwner();
}
/**
* Add condition to query for filter removed models.
*
* @return CActiveRecord
* @since 1.1.4
*/
public function filterRemoved()
{
$owner=$this->getOwner();
if(!$this->_withRemoved) {
$criteria=$owner->getDbCriteria();
$column = $owner->getDbConnection()->quoteColumnName($owner->getTableAlias().'.'.$this->trashFlagField);
$criteria->addCondition($column.'!='.CDbCriteria::PARAM_PREFIX.CDbCriteria::$paramCount);
$criteria->params[CDbCriteria::PARAM_PREFIX.CDbCriteria::$paramCount++]=$this->removedFlag;
}
$this->_withRemoved=false;
return $owner;
}
/**
* Add condition before find, for except removed models.
*
* @param CEvent
*/
public function beforeFind($event)
{
$this->filterRemoved();
}
/**
* Add condition before count, for except removed models.
*
* @param CEvent
* @since 1.1.14
*/
public function beforeCount($event)
{
$this->filterRemoved();
}
}