Completed
Push — master ( b2aa64...db9a1b )
by Alexey
05:01
created

File::beforeSave()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.2
cc 4
eloc 4
nc 2
nop 0
1
<?php
2
3
/**
4
 * File
5
 *
6
 * @author Alexey Krupskiy <[email protected]>
7
 * @link http://inji.ru/
8
 * @copyright 2015 Alexey Krupskiy
9
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
10
 */
11
12
namespace Files;
13
14
class File extends \Model
15
{
16
    public static $cols = [
17
        'code' => ['type' => 'text'],
18
        'type_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'type'],
19
        'folder_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'foler'],
20
        'upload_code' => ['type' => 'text'],
21
        'path' => ['type' => 'textarea'],
22
        'name' => ['type' => 'text'],
23
        'about' => ['type' => 'html'],
24
        'original_name' => ['type' => 'text'],
25
        'md5' => ['type' => 'text'],
26
        'date_create' => ['type' => 'dateTime'],
27
    ];
28
29
    public function beforeSave()
30
    {
31
        $path = $this->getRealPath();
32
        if (!$this->md5 && $this->path && file_exists($path)) {
33
            $this->md5 = md5_file($path);
34
        }
35
    }
36
37
    public function beforeDelete()
38
    {
39
        $path = $this->getRealPath();
40
        if (file_exists($path)) {
41
            unlink($path);
42
        }
43
    }
44
45
    public function getRealPath()
46
    {
47
        $sitePath = \App::$primary->path;
48
        return "{$sitePath}{$this->path}";
49
    }
50
51
    public static function relations()
52
    {
53
        return [
54
            'type' => [
55
                'model' => 'Files\Type',
56
                'col' => 'type_id'
57
            ],
58
            'folder' => [
59
                'model' => 'Files\Folder',
60
                'col' => 'folder_id'
61
            ],
62
        ];
63
    }
64
65
}
66