1
|
|
|
<?php |
2
|
|
|
namespace samsoncms\api; |
3
|
|
|
|
4
|
|
|
use samson\activerecord\structurefield; |
5
|
|
|
use samson\activerecord\structurematerial; |
6
|
|
|
use samson\activerecord\TableRelation; |
7
|
|
|
use samson\core\CompressableService; |
8
|
|
|
use samson\activerecord\dbMySQLConnector; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* SamsonCMS API |
12
|
|
|
* @package samsoncms\api |
13
|
|
|
*/ |
14
|
|
|
class CMS extends CompressableService |
15
|
|
|
{ |
16
|
|
|
/** Database entity name for relations between material and navigation */ |
17
|
|
|
const MATERIAL_NAVIGATION_RELATION_ENTITY = '\samson\activerecord\structurematerial'; |
18
|
|
|
/** Database entity name for relations between material and images */ |
19
|
|
|
const MATERIAL_IMAGES_RELATION_ENTITY = '\samson\activerecord\gallery'; |
20
|
|
|
/** Database entity name for relations between additional fields and navigation */ |
21
|
|
|
const FIELD_NAVIGATION_RELATION_ENTITY = '\samson\activerecord\structurefield'; |
22
|
|
|
/** Database entity name for relations between material and additional fields values */ |
23
|
|
|
const MATERIAL_FIELD_RELATION_ENTITY = '\samson\activerecord\materialfield'; |
24
|
|
|
|
25
|
|
|
/** Identifier */ |
26
|
|
|
protected $id = 'cmsapi2'; |
27
|
|
|
|
28
|
|
|
/** @var \samsonframework\orm\DatabaseInterface */ |
29
|
|
|
protected $database; |
30
|
|
|
|
31
|
|
|
/** @var string Database table names prefix */ |
32
|
|
|
public $tablePrefix = ''; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* CMS constructor. |
36
|
|
|
* @param null|string $path |
37
|
|
|
* @param null|string $vid |
38
|
|
|
* @param mixed|null $resources |
39
|
|
|
*/ |
40
|
|
|
public function __construct($path, $vid, $resources) |
41
|
|
|
{ |
42
|
|
|
// TODO: This should changed to normal DI |
43
|
|
|
$this->database = db(); |
|
|
|
|
44
|
|
|
|
45
|
|
|
parent::__construct($path, $vid, $resources); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
//[PHPCOMPRESSOR(remove,start)] |
49
|
|
|
/** |
50
|
|
|
* Read SQL file with variables placeholders pasting |
51
|
|
|
* @param string $filePath SQL file for reading |
52
|
|
|
* @param string $prefix Prefix for addition |
53
|
|
|
* @return string SQL command text |
54
|
|
|
*/ |
55
|
|
|
public function readSQL($filePath, $prefix = '') |
56
|
|
|
{ |
57
|
|
|
$sql = ''; |
58
|
|
|
|
59
|
|
|
// Build path to SQL folder |
60
|
|
|
if (file_exists($filePath)) { |
61
|
|
|
// Replace prefix |
62
|
|
|
$sql = str_replace('@prefix', $prefix, file_get_contents($filePath)); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $sql; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @see ModuleConnector::prepare() |
70
|
|
|
*/ |
71
|
|
|
public function prepare() |
72
|
|
|
{ |
73
|
|
|
// Perform this migration and execute only once |
74
|
|
|
if ($this->migrator() != 40) { |
75
|
|
|
// Perform SQL table creation |
76
|
|
|
$path = __DIR__ . '/../sql/'; |
77
|
|
|
foreach (array_slice(scandir($path), 2) as $file) { |
78
|
|
|
$this->database->execute($this->readSQL($path . $file, $this->tablePrefix)); |
79
|
|
|
} |
80
|
|
|
$this->migrator(40); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// Initiate migration mechanism |
84
|
|
|
$this->database->migration(get_class($this), array($this, 'migrator')); |
85
|
|
|
|
86
|
|
|
// Generate entities classes file |
87
|
|
|
$generator = new Generator($this->database); |
88
|
|
|
$file = md5($generator->entityHash()).'.php'; |
89
|
|
|
if ($this->cache_refresh($file)) { |
90
|
|
|
file_put_contents($file, '<?php '.$generator->createEntityClasses()); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
// Include entities file |
94
|
|
|
require($file); |
95
|
|
|
|
96
|
|
|
// Define permanent table relations |
97
|
|
|
new TableRelation('material', 'user', 'UserID', 0, 'user_id'); |
98
|
|
|
new TableRelation('material', 'gallery', 'MaterialID', TableRelation::T_ONE_TO_MANY); |
99
|
|
|
new TableRelation('material', 'materialfield', 'MaterialID', TableRelation::T_ONE_TO_MANY); |
100
|
|
|
new TableRelation('material', 'field', 'materialfield.FieldID', TableRelation::T_ONE_TO_MANY); |
101
|
|
|
new TableRelation('material', 'structurematerial', 'MaterialID', TableRelation::T_ONE_TO_MANY); |
102
|
|
|
new TableRelation('material', 'structure', 'structurematerial.StructureID', TableRelation::T_ONE_TO_MANY); |
103
|
|
|
new TableRelation('materialfield', 'field', 'FieldID'); |
104
|
|
|
new TableRelation('materialfield', 'material', 'MaterialID'); |
105
|
|
|
new TableRelation('structurematerial', 'structure', 'StructureID'); |
106
|
|
|
new TableRelation('structurematerial', 'materialfield', 'MaterialID', TableRelation::T_ONE_TO_MANY); |
107
|
|
|
new TableRelation('structurematerial', 'material', 'MaterialID', TableRelation::T_ONE_TO_MANY); |
108
|
|
|
new TableRelation('structure', 'material', 'structurematerial.MaterialID', TableRelation::T_ONE_TO_MANY, null, 'manymaterials'); |
109
|
|
|
new TableRelation('structure', 'gallery', 'structurematerial.MaterialID', TableRelation::T_ONE_TO_MANY, null, 'manymaterials'); |
110
|
|
|
/*new TableRelation( 'structure', 'material', 'MaterialID' );*/ |
111
|
|
|
new TableRelation('structure', 'user', 'UserID', 0, 'user_id'); |
112
|
|
|
new TableRelation('structure', 'materialfield', 'material.MaterialID', TableRelation::T_ONE_TO_MANY, 'MaterialID', '_mf'); |
113
|
|
|
new TableRelation('structure', 'structurematerial', 'StructureID', TableRelation::T_ONE_TO_MANY); |
114
|
|
|
new TableRelation('related_materials', 'material', 'first_material', TableRelation::T_ONE_TO_MANY, 'MaterialID'); |
115
|
|
|
new TableRelation('related_materials', 'materialfield', 'first_material', TableRelation::T_ONE_TO_MANY, 'MaterialID'); |
116
|
|
|
new TableRelation('field', 'structurefield', 'FieldID'); |
117
|
|
|
new TableRelation('field', 'structure', 'structurefield.StructureID'); |
118
|
|
|
new TableRelation('structurefield', 'field', 'FieldID'); |
119
|
|
|
new TableRelation('structurefield', 'materialfield', 'FieldID'); |
120
|
|
|
new TableRelation('structurefield', 'material', 'materialfield.MaterialID'); |
121
|
|
|
new TableRelation('structure', 'structure_relation', 'StructureID', TableRelation::T_ONE_TO_MANY, 'parent_id', 'children_relations'); |
122
|
|
|
new TableRelation('structure', 'structure', 'children_relations.child_id', TableRelation::T_ONE_TO_MANY, 'StructureID', 'children'); |
123
|
|
|
new TableRelation('structure', 'structure_relation', 'StructureID', TableRelation::T_ONE_TO_MANY, 'child_id', 'parents_relations'); |
124
|
|
|
new TableRelation('structure', 'structure', 'parents_relations.parent_id', TableRelation::T_ONE_TO_MANY, 'StructureID', 'parents'); |
125
|
|
|
new TableRelation('structurematerial', 'structure_relation', 'StructureID', TableRelation::T_ONE_TO_MANY, 'parent_id'); |
126
|
|
|
new TableRelation('groupright', 'right', 'RightID', TableRelation::T_ONE_TO_MANY); |
127
|
|
|
|
128
|
|
|
return parent::prepare(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Handler for CMSAPI database version manipulating |
133
|
|
|
* @param string $toVersion Version to switch to |
134
|
|
|
* @return string Current database version |
135
|
|
|
*/ |
136
|
|
|
public function migrator($toVersion = null) |
137
|
|
|
{ |
138
|
|
|
// If something passed - change database version to it |
139
|
|
|
if (func_num_args()) { |
140
|
|
|
// Save current version to special db table |
141
|
|
|
$this->database->execute( |
142
|
|
|
"ALTER TABLE `" . dbMySQLConnector::$prefix . "cms_version` |
143
|
|
|
CHANGE `version` `version` VARCHAR( 15 ) CHARACTER SET utf8 |
144
|
|
|
COLLATE utf8_general_ci NOT NULL DEFAULT '" . $toVersion . "';" |
145
|
|
|
); |
146
|
|
|
die('Database successfully migrated to [' . $toVersion . ']'); |
147
|
|
|
} else { // Return current database version |
148
|
|
|
$version_row = $this->database->fetch('SHOW COLUMNS FROM `' . dbMySQLConnector::$prefix . 'cms_version`'); |
149
|
|
|
if (isset($version_row[0]['Default'])) { |
150
|
|
|
return $version_row[0]['Default']; |
151
|
|
|
} else { |
152
|
|
|
return 0; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
//[PHPCOMPRESSOR(remove,end)] |
157
|
|
|
} |
158
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..