Completed
Pull Request — master (#47)
by
unknown
02:43
created

Entity::save()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 2
Metric Value
c 3
b 1
f 2
dl 0
loc 34
rs 8.8571
cc 3
eloc 22
nc 3
nop 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: VITALYIEGOROV
5
 * Date: 09.12.15
6
 * Time: 12:10
7
 */
8
namespace samsoncms\api;
9
10
/**
11
 * SamsonCMS Entity that has relation to specific navigation
12
 * and though has additional fields.
13
 *
14
 * @package samsoncms\api
15
 */
16
class Entity extends Material
17
{
18
19
    /** @var array Collection of navigation identifiers */
20
    protected static $navigationIDs = array();
21
22
    /** @var array Collection of localized additional fields identifiers */
23
    protected static $fieldIDs = array();
24
25
    /** @var array Collection of additional fields value column names */
26
    protected static $fieldValueColumns = array();
27
28
    /**
29
     * Override default entity saving
30
     */
31
    public function save()
32
    {
33
        // Format url
34
        $this->Url = str_replace(' ', '-', utf8_translit($this->Url));
35
36
        parent::save();
37
38
        $relationEntity = CMS::MATERIAL_FIELD_RELATION_ENTITY;
39
        foreach (static::$fieldIDs as $fieldID => $fieldName) {
40
            $type = static::$fieldValueColumns[$fieldID];
41
42
            // If material field relation exists use it or create new
43
            $materialField = null;
44
            if ($this->query
45
                ->entity($relationEntity)
46
                ->where(Field::F_PRIMARY, $fieldID)
47
                ->where(Material::F_PRIMARY, $this->id)
48
                ->first($materialField)
49
            ) {
50
                $materialField->$type = $this->$fieldName;
51
                $materialField->save();
52
            } else {
53
54
                /** @var \samson\activerecord\materialfield $materialfield */
55
                $materialField = new $relationEntity();
56
                $materialField->Active = 1;
57
                $materialField->MaterialID = $this->id;
58
                $materialField->FieldID = $fieldID;
59
                $materialField->$type = $this->$fieldName;
60
                $materialField->save();
61
            }
62
        }
63
        $this->attachTo(static::$navigationIDs);
64
    }
65
66
    /**
67
     * Add entity structure relation
68
     * @param integer|array $structureID Structure identifier or their collection
69
     */
70
    public function attachTo($structureID)
71
    {
72
        $relationEntity = CMS::MATERIAL_NAVIGATION_RELATION_ENTITY;
73
74
        foreach (is_array($structureID) ? $structureID : array($structureID) as $structureID) {
75
            // Check if we do not have this relation already
76
            if ($this->query
77
                    ->entity($relationEntity)
78
                    ->where(NavigationMaterial::F_PRIMARY, $structureID)
79
                    ->where(self::F_PRIMARY, $this->id)
80
                    ->count() === 0
81
            ) {
82
                /** @var \samson\activerecord\structurematerial $structureMaterial */
83
                $structureMaterial = new $relationEntity();
84
                $structureMaterial->Active = 1;
85
                $structureMaterial->MaterialID = $this->id;
86
                $structureMaterial->StructureID = $structureID;
87
                $structureMaterial->save();
88
            }
89
        }
90
    }
91
}
92