1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\models; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\helpers\Url; |
7
|
|
|
use app\models\types\Media; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* This is the model class for table "template_background". |
11
|
|
|
* |
12
|
|
|
* @property int $id |
13
|
|
|
* @property string $webpath |
14
|
|
|
* @property ScreenTemplate[] $screenTemplates |
15
|
|
|
*/ |
16
|
|
|
class TemplateBackground extends \yii\db\ActiveRecord |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* {@inheritdoc} |
20
|
|
|
*/ |
21
|
|
|
public static function tableName() |
22
|
|
|
{ |
23
|
|
|
return 'template_background'; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
|
|
public function rules() |
30
|
|
|
{ |
31
|
|
|
return [ |
32
|
|
|
[['webpath'], 'required'], |
33
|
|
|
[['webpath'], 'string', 'max' => 256], |
34
|
|
|
]; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
|
|
public function attributeLabels() |
41
|
|
|
{ |
42
|
|
|
return [ |
43
|
|
|
'id' => Yii::t('app', 'ID'), |
44
|
|
|
'webpath' => Yii::t('app', 'Filepath'), |
45
|
|
|
'name' => Yii::t('app', 'Background'), |
46
|
|
|
]; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
public function afterDelete() |
53
|
|
|
{ |
54
|
|
|
if ($this->shouldDeleteFile()) { |
55
|
|
|
unlink($this->getRealFilepath()); |
56
|
|
|
} |
57
|
|
|
parent::afterDelete(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return \yii\db\ActiveQuery |
62
|
|
|
*/ |
63
|
|
|
public function getScreenTemplates() |
64
|
|
|
{ |
65
|
|
|
return $this->hasMany(ScreenTemplate::className(), ['background_id' => 'id']); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Returns Yii usable URL for this background. |
70
|
|
|
* |
71
|
|
|
* @return string url |
72
|
|
|
*/ |
73
|
|
|
public function getUri() |
74
|
|
|
{ |
75
|
|
|
return Url::to($this->webpath); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Search background name in file webpath. |
80
|
|
|
* |
81
|
|
|
* @return string name |
82
|
|
|
*/ |
83
|
|
|
public function getName() |
84
|
|
|
{ |
85
|
|
|
$parts = explode('/', $this->webpath); |
86
|
|
|
|
87
|
|
|
return $parts[count($parts) - 1]; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Count templatesBackghround currently using this background to decide on file deletion. |
92
|
|
|
* |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
|
|
public function shouldDeleteFile() |
96
|
|
|
{ |
97
|
|
|
return self::find()->where(['webpath' => $this->webpath])->count() < 1; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Extract real filepath from background webpath. |
102
|
|
|
* |
103
|
|
|
* @return string file path |
104
|
|
|
*/ |
105
|
|
|
public function getRealFilepath() |
106
|
|
|
{ |
107
|
|
|
return str_replace(Media::BASE_URI, '', $this->webpath); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|