Passed
Push — master ( df28ec...dcb770 )
by Nikita
06:01 queued 48s
created

Entity::attachTo()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 21
rs 9.0534
cc 4
eloc 13
nc 3
nop 1
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
use samsoncms\api\query\FieldNavigation;
11
use samsonframework\orm\QueryInterface;
12
13
/**
14
 * SamsonCMS Entity that has relation to specific navigation
15
 * and though has additional fields.
16
 *
17
 * @package samsoncms\api
18
 */
19
class Entity extends Material
20
{
21
22
    /** @var array Collection of navigation identifiers */
23
    protected static $navigationIDs = array();
24
25
    /** @var array Collection of localized additional fields identifiers */
26
    protected static $fieldIDs = array();
27
28
    /** @var array Collection of additional fields value column names */
29
    protected static $fieldValueColumns = array();
30
31
    /**
32
     * Override default entity saving
33
     */
34
    public function save()
35
    {
36
        parent::save();
37
38
        $relationEntity = CMS::MATERIAL_FIELD_RELATION_ENTITY;
39
        foreach (static::$fieldIDs as $fieldID => $fieldName) {
40
            /** @var \samson\activerecord\materialfield $materialfield */
41
            $materialField = new $relationEntity();
42
            $materialField->Active = 1;
43
            $materialField->MaterialID = $this->id;
44
            $materialField->FieldID = $fieldID;
45
            $type = static::$fieldValueColumns[$fieldID];
46
            $materialField->$type = $this->$fieldName;
47
            $materialField->save();
48
        }
49
        $this->attachTo(static::$navigationIDs);
50
    }
51
52
    /**
53
     * Add entity structure relation
54
     * @param integer|array $structureID Structure identifier or their collection
55
     */
56
    public function attachTo($structureID)
57
    {
58
        $relationEntity = CMS::MATERIAL_NAVIGATION_RELATION_ENTITY;
59
60
        foreach (is_array($structureID) ? $structureID : array($structureID) as $structureID) {
61
            // Check if we do not have this relation already
62
            if ($this->query
63
                    ->entity($relationEntity)
64
                    ->where(NavigationMaterial::F_PRIMARY, $structureID)
65
                    ->where(self::F_PRIMARY, $this->id)
66
                    ->count() === 0
67
            ) {
68
                /** @var \samson\activerecord\structurematerial $structureMaterial */
69
                $structureMaterial = new $relationEntity();
70
                $structureMaterial->Active = 1;
71
                $structureMaterial->MaterialID = $this->id;
72
                $structureMaterial->StructureID = $structureID;
73
                $structureMaterial->save();
74
            }
75
        }
76
    }
77
}
78