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 samson\activerecord\StructureMaterial; |
11
|
|
|
use samsonframework\orm\DatabaseInterface; |
12
|
|
|
use samsonframework\orm\QueryInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* SamsonCMS Entity that has relation to specific navigation |
16
|
|
|
* and though has additional fields. |
17
|
|
|
* |
18
|
|
|
* @package samsoncms\api |
19
|
|
|
*/ |
20
|
|
|
class Entity extends Material |
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
|
|
|
/** @var array Collection of localized additional fields identifiers */ |
32
|
|
|
protected static $localizedFieldIDs = array(); |
33
|
|
|
|
34
|
|
|
/** @var string Locale */ |
35
|
|
|
protected $locale; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Entity constructor. |
39
|
|
|
* |
40
|
|
|
* @param null|string $locale Locale |
41
|
|
|
* @param null|DatabaseInterface $database |
42
|
|
|
* @param null|QueryInterface $query |
43
|
|
|
*/ |
44
|
|
|
public function __construct($locale = null, $database = null, $query = null) |
45
|
|
|
{ |
46
|
|
|
$this->locale = $locale; |
47
|
|
|
|
48
|
|
|
parent::__construct($database, $query); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Override default entity saving |
53
|
|
|
*/ |
54
|
|
|
public function save() |
55
|
|
|
{ |
56
|
|
|
// Format url |
57
|
|
|
$this->Url = str_replace(' ', '-', utf8_translit($this->Url)); |
58
|
|
|
|
59
|
|
|
parent::save(); |
60
|
|
|
|
61
|
|
|
$relationEntity = CMS::MATERIAL_FIELD_RELATION_ENTITY; |
62
|
|
|
foreach (static::$fieldIDs as $fieldID => $fieldName) { |
63
|
|
|
$type = static::$fieldValueColumns[$fieldID]; |
64
|
|
|
|
65
|
|
|
// If material field relation exists use it or create new |
66
|
|
|
$materialField = null; |
67
|
|
|
if (!$this->query |
68
|
|
|
->entity($relationEntity) |
69
|
|
|
->where(Field::F_PRIMARY, $fieldID) |
70
|
|
|
->where(Material::F_PRIMARY, $this->id) |
71
|
|
|
->first($materialField) |
72
|
|
|
) { |
73
|
|
|
/** @var Field $materialfield */ |
74
|
|
|
$materialField = new $relationEntity(); |
75
|
|
|
$materialField->Active = 1; |
76
|
|
|
$materialField->MaterialID = $this->id; |
77
|
|
|
$materialField->FieldID = $fieldID; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// Set locale only if this field is localized |
81
|
|
|
if (array_key_exists($fieldID, static::$localizedFieldIDs)) { |
82
|
|
|
$materialField->locale = $this->locale; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$materialField->$type = $this->$fieldName; |
86
|
|
|
$materialField->save(); |
87
|
|
|
} |
88
|
|
|
$this->attachTo(static::$navigationIDs); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Add entity structure relation |
93
|
|
|
* @param integer|array $structureID Structure identifier or their collection |
94
|
|
|
*/ |
95
|
|
|
public function attachTo($structureID) |
96
|
|
|
{ |
97
|
|
|
$relationEntity = CMS::MATERIAL_NAVIGATION_RELATION_ENTITY; |
98
|
|
|
|
99
|
|
|
foreach (is_array($structureID) ? $structureID : array($structureID) as $structureID) { |
100
|
|
|
// Check if we do not have this relation already |
101
|
|
|
if ($this->query |
102
|
|
|
->entity($relationEntity) |
103
|
|
|
->where(NavigationMaterial::F_PRIMARY, $structureID) |
104
|
|
|
->where(self::F_PRIMARY, $this->id) |
105
|
|
|
->count() === 0 |
106
|
|
|
) { |
107
|
|
|
/** @var StructureMaterial $structureMaterial */ |
108
|
|
|
$structureMaterial = new $relationEntity(); |
109
|
|
|
$structureMaterial->Active = 1; |
|
|
|
|
110
|
|
|
$structureMaterial->MaterialID = $this->id; |
111
|
|
|
$structureMaterial->StructureID = $structureID; |
112
|
|
|
$structureMaterial->save(); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.