Passed
Push — master ( 37ba64...7e87e7 )
by Alexey
05:29
created

Image::beforeSave()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 2
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Item image
5
 *
6
 * @author Alexey Krupskiy <[email protected]>
7
 * @link http://inji.ru/
8
 * @copyright 2016 Alexey Krupskiy
9
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
10
 */
11
12
namespace Ecommerce\Item;
13
14
class Image extends \Model {
15
16
    public static $objectName = 'Фото товара';
17
    public static $labels = [
18
        'file_id' => 'Изображение',
19
        'item_id' => 'Товар',
20
        'name' => 'Название',
21
        'description' => 'Описание',
22
    ];
23
    public static $cols = [
24
        'file_id' => ['type' => 'image'],
25
        'item_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'item'],
26
        'name' => ['type' => 'text'],
27
        'description' => ['type' => 'html'],
28
        'weight' => ['type' => 'number'],
29
    ];
30
31
    public static function relations() {
32
        return [
33
            'item' => [
34
                'col' => 'item_id',
35
                'model' => 'Ecommerce\Item'
36
            ],
37
            'file' => [
38
                'col' => 'file_id',
39
                'model' => 'Files\File'
40
            ]
41
        ];
42
    }
43
44
    public static $dataManagers = [
45
        'manager' => [
46
            'name' => 'Фото товара',
47
            'cols' => [
48
                'file_id', 'name'
49
            ]
50
        ]
51
    ];
52
    public static $forms = [
53
        'manager' => [
54
            'map' => [
55
                ['name'],
56
                ['item_id', 'file_id'],
57
                ['description']
58
            ]
59
        ]
60
    ];
61
62
    public function beforeSave() {
63
        if ($this->file_id && !$this->item->image_file_id) {
64
            $item = $this->item;
65
            $item->image_file_id = $this->file_id;
66
            $item->save();
67
        }
68
    }
69
70
    public function beforeDelete() {
71
        if ($this->file) {
72
            $this->file->delete();
73
        }
74
    }
75
}