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 samsonframework\orm\DatabaseInterface; |
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
|
|
|
/** @var array Collection of navigation identifiers */ |
22
|
|
|
protected static $navigationIDs = array(); |
23
|
|
|
|
24
|
|
|
/** @var array Collection of localized additional fields identifiers */ |
25
|
|
|
protected static $fieldIDs = array(); |
26
|
|
|
|
27
|
|
|
/** @var array Collection of additional fields value column names */ |
28
|
|
|
protected static $fieldValueColumns = array(); |
29
|
|
|
|
30
|
|
|
/** @var string Locale */ |
31
|
|
|
protected $locale; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Entity constructor. |
35
|
|
|
* |
36
|
|
|
* @param null|string $locale Locale |
37
|
|
|
* @param null|DatabaseInterface $database |
38
|
|
|
* @param null|QueryInterface $query |
39
|
|
|
*/ |
40
|
|
|
public function __construct($locale = null, $database = null, $query = null) |
41
|
|
|
{ |
42
|
|
|
$this->locale = $locale; |
43
|
|
|
|
44
|
|
|
parent::__construct($database, $query); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Override default entity saving |
49
|
|
|
*/ |
50
|
|
|
public function save() |
51
|
|
|
{ |
52
|
|
|
// Format url |
53
|
|
|
$this->Url = str_replace(' ', '-', utf8_translit($this->Url)); |
54
|
|
|
|
55
|
|
|
parent::save(); |
56
|
|
|
|
57
|
|
|
$relationEntity = CMS::MATERIAL_FIELD_RELATION_ENTITY; |
58
|
|
|
foreach (static::$fieldIDs as $fieldID => $fieldName) { |
59
|
|
|
$type = static::$fieldValueColumns[$fieldID]; |
60
|
|
|
|
61
|
|
|
// If material field relation exists use it or create new |
62
|
|
|
$materialField = null; |
63
|
|
|
if ($this->query |
64
|
|
|
->entity($relationEntity) |
65
|
|
|
->where(Field::F_PRIMARY, $fieldID) |
66
|
|
|
->where(Material::F_PRIMARY, $this->id) |
67
|
|
|
->first($materialField) |
68
|
|
|
) { |
69
|
|
|
$materialField->$type = $this->$fieldName; |
70
|
|
|
$materialField->save(); |
71
|
|
|
} else { |
72
|
|
|
|
73
|
|
|
/** @var \samson\activerecord\materialfield $materialfield */ |
74
|
|
|
$materialField = new $relationEntity(); |
75
|
|
|
$materialField->Active = 1; |
76
|
|
|
$materialField->MaterialID = $this->id; |
77
|
|
|
$materialField->FieldID = $fieldID; |
78
|
|
|
$materialField->$type = $this->$fieldName; |
79
|
|
|
$materialField->save(); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
$this->attachTo(static::$navigationIDs); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Add entity structure relation |
87
|
|
|
* @param integer|array $structureID Structure identifier or their collection |
88
|
|
|
*/ |
89
|
|
|
public function attachTo($structureID) |
90
|
|
|
{ |
91
|
|
|
$relationEntity = CMS::MATERIAL_NAVIGATION_RELATION_ENTITY; |
92
|
|
|
|
93
|
|
|
foreach (is_array($structureID) ? $structureID : array($structureID) as $structureID) { |
94
|
|
|
// Check if we do not have this relation already |
95
|
|
|
if ($this->query |
96
|
|
|
->entity($relationEntity) |
97
|
|
|
->where(NavigationMaterial::F_PRIMARY, $structureID) |
98
|
|
|
->where(self::F_PRIMARY, $this->id) |
99
|
|
|
->count() === 0 |
100
|
|
|
) { |
101
|
|
|
/** @var \samson\activerecord\structurematerial $structureMaterial */ |
102
|
|
|
$structureMaterial = new $relationEntity(); |
103
|
|
|
$structureMaterial->Active = 1; |
104
|
|
|
$structureMaterial->MaterialID = $this->id; |
105
|
|
|
$structureMaterial->StructureID = $structureID; |
106
|
|
|
$structureMaterial->save(); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|