|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Bluz PHP Team |
|
4
|
|
|
* @link https://github.com/bluzphp/skeleton |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @namespace |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace Application\Media; |
|
11
|
|
|
|
|
12
|
|
|
use Application\Users; |
|
13
|
|
|
use Bluz\Proxy\Auth; |
|
14
|
|
|
use Bluz\Validator\Traits\Validator; |
|
15
|
|
|
use Bluz\Validator\Validator as v; |
|
16
|
|
|
use Image\Thumbnail; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Media Row |
|
20
|
|
|
* |
|
21
|
|
|
* @category Application |
|
22
|
|
|
* @package Media |
|
23
|
|
|
* |
|
24
|
|
|
* @property integer $id |
|
25
|
|
|
* @property integer $userId |
|
26
|
|
|
* @property string $title |
|
27
|
|
|
* @property string $module |
|
28
|
|
|
* @property string $type |
|
29
|
|
|
* @property string $file |
|
30
|
|
|
* @property string $preview |
|
31
|
|
|
* @property integer $size |
|
32
|
|
|
* @property string $created |
|
33
|
|
|
* @property string $updated |
|
34
|
|
|
*/ |
|
35
|
|
|
class Row extends \Bluz\Db\Row |
|
36
|
|
|
{ |
|
37
|
|
|
use Validator; |
|
38
|
|
|
|
|
39
|
|
|
const THUMB_HEIGHT = 196; |
|
40
|
|
|
const THUMB_WIDTH = 196; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* {@inheritdoc} |
|
44
|
|
|
*/ |
|
45
|
1 |
|
protected function beforeSave() |
|
46
|
|
|
{ |
|
47
|
1 |
|
$this->addValidator( |
|
48
|
1 |
|
'title', |
|
49
|
1 |
|
v::required()->latinNumeric(' -_.') |
|
50
|
|
|
); |
|
51
|
1 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* __insert |
|
55
|
|
|
* |
|
56
|
|
|
* @return void |
|
57
|
|
|
*/ |
|
58
|
1 |
|
protected function beforeInsert() |
|
59
|
|
|
{ |
|
60
|
1 |
|
$this->created = gmdate('Y-m-d H:i:s'); |
|
61
|
|
|
|
|
62
|
|
|
// set default module |
|
63
|
1 |
|
if (!$this->module) { |
|
64
|
1 |
|
$this->module = 'users'; |
|
65
|
|
|
} |
|
66
|
|
|
// set user ID |
|
67
|
1 |
View Code Duplication |
if ($user = Auth::getIdentity()) { |
|
|
|
|
|
|
68
|
1 |
|
$this->userId = $user->id; |
|
|
|
|
|
|
69
|
|
|
} else { |
|
70
|
|
|
$this->userId = Users\Table::SYSTEM_USER; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
// create preview |
|
74
|
|
|
// set full path |
|
75
|
1 |
|
$image = new Thumbnail(PATH_PUBLIC .'/'. $this->file); |
|
76
|
1 |
|
$image->setHeight(self::THUMB_HEIGHT); |
|
77
|
1 |
|
$image->setWidth(self::THUMB_WIDTH); |
|
78
|
1 |
|
$preview = $image->generate(); |
|
79
|
|
|
// crop full path |
|
80
|
1 |
|
$preview = substr($preview, strlen(PATH_PUBLIC) + 1); |
|
81
|
1 |
|
$this->preview = $preview; |
|
82
|
1 |
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* __update |
|
86
|
|
|
* |
|
87
|
|
|
* @return void |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function beforeUpdate() |
|
90
|
|
|
{ |
|
91
|
|
|
$this->updated = gmdate('Y-m-d H:i:s'); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* postDelete |
|
96
|
|
|
* |
|
97
|
|
|
* @return void |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function afterDelete() |
|
100
|
|
|
{ |
|
101
|
|
View Code Duplication |
if (is_file(PATH_PUBLIC .'/'. $this->file)) { |
|
|
|
|
|
|
102
|
|
|
@unlink(PATH_PUBLIC .'/'. $this->file); |
|
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
View Code Duplication |
if (is_file(PATH_PUBLIC .'/'. $this->preview)) { |
|
|
|
|
|
|
105
|
|
|
@unlink(PATH_PUBLIC .'/'. $this->preview); |
|
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.