-
Notifications
You must be signed in to change notification settings - Fork 5
/
EStatusBehavior.php
166 lines (147 loc) · 3.86 KB
/
EStatusBehavior.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
/**
* Status behavior for models.
*
* @author Veaceslav Medvedev <[email protected]>
* @link https://github.com/yiiext/status-behavior
* @version 0.6
*/
class EStatusBehavior extends CActiveRecordBehavior
{
/**
* @var string the name of the table field where data is stored.
* Required to set on init behavior. No default.
*/
public $statusField = null;
/**
* @var array the possible statuses.
* Default: draft, published, archived.
* @see setStatuses
*/
public $statuses = array('draft', 'published', 'archived');
protected $_status = null;
protected $_statusText = 'unknown';
/**
* Checks required properties and attaches the behavior object to the component.
*
* @param CActiveRecord $owner owner model.
* @throws CException if required properties are not set.
*/
public function attach($owner)
{
// Check required var statusField.
if (!is_string($this->statusField) || empty($this->statusField)) {
throw new CException(self::t(
'yii',
'Property "{class}.{property}" is not defined.',
array('{class}' => get_class($this), '{property}' => 'statusField')
));
}
parent::attach($owner);
}
/**
* @return string status value.
* @see getStatus
*/
public function __toString()
{
return $this->getStatus();
}
/**
* Init valid statuses values.
*
* @param array $statuses valid values for status.
* @return CActiveRecord owner model.
*/
public function setStatuses($statuses)
{
$this->statuses = is_array($statuses) && !empty($statuses) ? $statuses :
array('draft', 'published', 'archived');
return $this->getOwner();
}
/**
* @return string status value.
*/
public function getStatus()
{
return $this->_status;
}
/**
* @return string status text.
*/
public function getStatusText()
{
return $this->_statusText;
}
/**
* Set status for model.
*
* @param string $status status value or status text for model.
* @return CActiveRecord owner model.
* @throws CException if status is invalid.
*/
public function setStatus($status)
{
if (isset($this->statuses[$status])) {
$this->_status = $status;
} else {
if (($this->_status = array_search($status, $this->statuses)) === false) {
throw new CException(Yii::t(
'yiiext',
'Status "{status}" is not allowed.',
array('{status}' => $status)
));
}
}
$this->_statusText = $this->statuses[$this->_status];
$this->getOwner()->setAttribute($this->statusField, $this->_status);
return $this->getOwner();
}
/**
* Save status. Will save only status attribute for model.
*
* @return boolean whether the saving succeeds.
*/
public function saveStatus()
{
return $this->getOwner()->save(true, array($this->statusField));
}
/**
* Load status after find model.
*
* @param CEvent $event the event parameter
*/
public function afterFind($event)
{
$this->_status = $this->getOwner()->getAttribute($this->statusField);
$this->_statusText = isset($this->statuses[$this->_status]) ? $this->statuses[$this->_status] : 'unknown';
parent::afterFind($event);
}
/**
* Has status.
*
* @param string|array $statuses the possible statuses.
* @return bool
*/
public function hasStatus($statuses = array())
{
if (!is_array($statuses)) {
$statuses = explode(',', $statuses);
}
return in_array($this->getOwner()->getAttribute($this->statusField), $statuses);
}
/**
* Named scope.
*
* @param string|integer $status status.
* @return CActiveRecord owner model.
*/
public function status($status)
{
$criteria = $this->getOwner()->getDbCriteria();
$column = $this->getOwner()->getDbConnection()->quoteColumnName($this->getOwner()->getTableAlias().'.'.$this->statusField);
$criteria->addCondition($column.'='.CDbCriteria::PARAM_PREFIX.CDbCriteria::$paramCount);
$criteria->params[CDbCriteria::PARAM_PREFIX.CDbCriteria::$paramCount++]=$status;
return $this->getOwner();
}
}