-
Notifications
You must be signed in to change notification settings - Fork 3
/
ActiveRecord.php
225 lines (195 loc) · 5.44 KB
/
ActiveRecord.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
namespace kato;
use kato\modules\media\models\ContentMedia;
use kato\modules\media\models\Media;
use yii\helpers\Inflector;
use ReflectionClass;
use yii\helpers\Url;
class ActiveRecord extends \yii\db\ActiveRecord
{
/**
* Attached Content Media, by type
* @return static
*/
public function getContentMedia()
{
return $this->hasMany(ContentMedia::className(), ['content_id' => 'id'])
->where('content_type = :type', [':type' => $this->className()]);
}
/**
* Relate Media
* Usage: $model->media();
* @param null $type
* @return static
*/
public function getMedia($type = null)
{
$media = $this->hasMany(Media::className(), ['id' => 'media_id']);
if ($type !== null) {
$media->where('media_type = :type', [':type' => $type]);
}
$media->via('contentMedia');
return $media;
}
/**
* Actions to be taken before saving the record.
* @param bool $insert
* @return bool whether the record can be saved
*/
public function beforeSave($insert)
{
if (parent::beforeSave($insert)) {
if (isset(\Yii::$app->user)) {
$user_id = \Yii::$app->user->id;
if ($this->isNewRecord) {
if ($this->hasAttribute('created_by'))
$this->created_by = $user_id;
} else {
if ($this->hasAttribute('updated_by'))
$this->updated_by = $user_id;
}
}
return true;
}
return false;
}
/**
* This method is invoked before deleting a record.
* The default implementation raises the [[EVENT_BEFORE_DELETE]] event.
*/
public function beforeDelete()
{
if (parent::beforeDelete()) {
if ($this->media) {
foreach ($this->media as $media) {
$media->delete();
}
}
return true;
}
return false;
}
/**
* Return basic select options for the record.
* @param string $key
* @param string $value
* @param array $where
* @return array
*/
public static function getSelectOptions($key = 'id', $value = 'title', $where = [])
{
$parents = self::find();
if (!empty($where)) {
$parents->where($where);
}
return \yii\helpers\ArrayHelper::map($parents->all(), $key, $value);
}
/**
* Return last row inserted
* @return mixed
*/
public function getLastRow()
{
return static::find()
->orderBy('id DESC')
->one();
}
/**
* Returns lists of status available
* @return array
*/
public function listStatus()
{
$data = [];
// create a reflection class to get constants
$refl = new ReflectionClass(get_called_class());
$constants = $refl->getConstants();
// check for status constants (e.g., STATUS_ACTIVE)
foreach ($constants as $constantName => $constantValue) {
// add prettified name to dropdown
if (strpos($constantName, "STATUS_") === 0) {
$prettyName = str_replace("STATUS_", "", $constantName);
$prettyName = Inflector::humanize(strtolower($prettyName));
$data[$constantValue] = $prettyName;
}
}
return $data;
}
/**
* Returns status label
* @return bool
*/
public function getStatusLabel()
{
if ($status =$this->listStatus()) {
return $status[$this->status];
}
return false;
}
/**
* Returns permalink to model
* @return string
*/
public function getPermalink() {
return Url::to(['view', 'id' => $this->id]);
}
/**
* Returns types available
* @param $type
* @return array
*/
public function listTypes($type)
{
$data = [];
// create a reflection class to get constants
$refl = new ReflectionClass(get_called_class());
$constants = $refl->getConstants();
foreach ($constants as $constantName => $constantValue) {
// add prettified name to dropdown
if (strpos($constantName, $type) === 0) {
$prettyName = preg_replace('/' . $type . '/', "", $constantName, 1);
$prettyName = Inflector::humanize(strtolower($prettyName));
$data[$constantValue] = trim(ucwords($prettyName));
}
}
return $data;
}
/**
* Returns type label
* @param $type
* @param $constId
* @return bool
*/
public function getTypeLabel($type, $constId)
{
if (!is_null($type)) {
$array = $this->listTypes($constId);
if (isset($array[$type])) {
return $array[$type];
}
}
return false;
}
/**
* return array of listing order
* @return array
*/
public function getListingOrderArray()
{
$count = static::find()
->count();
$array = [];
for ($i = 1; $i <= $count; $i++) {
$array[$i] = $i;
}
return $array;
}
public function softDelete()
{
if ($this->hasAttribute('deleted')) {
$this->deleted = 1;
return $this->save(false);
}
return false;
}
}