1
|
|
|
<?php |
2
|
|
|
namespace samsoncms\api; |
3
|
|
|
|
4
|
|
|
use samson\activerecord\dbRelation; |
5
|
|
|
use samson\activerecord\field; |
|
|
|
|
6
|
|
|
use samson\activerecord\materialfield; |
7
|
|
|
use samson\activerecord\structure; |
8
|
|
|
use samson\activerecord\structurefield; |
9
|
|
|
use samson\activerecord\structurematerial; |
10
|
|
|
use samson\activerecord\TableRelation; |
11
|
|
|
use samson\activerecord\material; |
|
|
|
|
12
|
|
|
use samson\core\CompressableService; |
13
|
|
|
use samson\activerecord\dbRecord; |
14
|
|
|
use samson\activerecord\dbMySQLConnector; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* SamsonCMS API |
18
|
|
|
* @package samsoncms\api |
19
|
|
|
*/ |
20
|
|
|
class CMS extends CompressableService |
21
|
|
|
{ |
22
|
|
|
/** Identifier */ |
23
|
|
|
protected $id = 'cmsapi'; |
24
|
|
|
|
25
|
|
|
/** @var string Database table names prefix */ |
26
|
|
|
public $tablePrefix = ''; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Read SQL file with variables placeholders pasting |
30
|
|
|
* @param string $filePath SQL file for reading |
31
|
|
|
* @param string $prefix Prefix for addition |
32
|
|
|
* @return string SQL command text |
33
|
|
|
*/ |
34
|
|
|
public function readSQL($filePath, $prefix = '') |
35
|
|
|
{ |
36
|
|
|
$sql = ''; |
37
|
|
|
|
38
|
|
|
// Build path to SQL folder |
39
|
|
|
if (file_exists($filePath)) { |
40
|
|
|
// Replace prefix |
41
|
|
|
$sql = str_replace('@prefix', $prefix, file_get_contents($filePath)); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $sql; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @see ModuleConnector::prepare() |
49
|
|
|
*/ |
50
|
|
|
public function prepare() |
51
|
|
|
{ |
52
|
|
|
// Perform SQL table creation |
53
|
|
|
foreach (scandir(__DIR__.'/../sql/') as $file) { |
54
|
|
|
db()->query($this->readSQL($file)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// Initiate migration mechanism |
58
|
|
|
db()->migration(get_class($this), array($this, 'migrator')); |
59
|
|
|
|
60
|
|
|
// Define permanent table relations |
61
|
|
|
new TableRelation('material', 'user', 'UserID', 0, 'user_id'); |
62
|
|
|
new TableRelation('material', 'gallery', 'MaterialID', TableRelation::T_ONE_TO_MANY); |
63
|
|
|
new TableRelation('material', 'materialfield', 'MaterialID', TableRelation::T_ONE_TO_MANY); |
64
|
|
|
new TableRelation('material', 'field', 'materialfield.FieldID', TableRelation::T_ONE_TO_MANY); |
65
|
|
|
new TableRelation('material', 'structurematerial', 'MaterialID', TableRelation::T_ONE_TO_MANY); |
66
|
|
|
new TableRelation('material', 'structure', 'structurematerial.StructureID', TableRelation::T_ONE_TO_MANY); |
67
|
|
|
new TableRelation('materialfield', 'field', 'FieldID'); |
68
|
|
|
new TableRelation('materialfield', 'material', 'MaterialID'); |
69
|
|
|
new TableRelation('structurematerial', 'structure', 'StructureID'); |
70
|
|
|
new TableRelation('structurematerial', 'materialfield', 'MaterialID', TableRelation::T_ONE_TO_MANY); |
71
|
|
|
new TableRelation('structurematerial', 'material', 'MaterialID', TableRelation::T_ONE_TO_MANY); |
72
|
|
|
new TableRelation('structure', 'material', 'structurematerial.MaterialID', TableRelation::T_ONE_TO_MANY, null, 'manymaterials'); |
|
|
|
|
73
|
|
|
new TableRelation('structure', 'gallery', 'structurematerial.MaterialID', TableRelation::T_ONE_TO_MANY, null, 'manymaterials'); |
|
|
|
|
74
|
|
|
/*new TableRelation( 'structure', 'material', 'MaterialID' );*/ |
75
|
|
|
new TableRelation('structure', 'user', 'UserID', 0, 'user_id'); |
76
|
|
|
new TableRelation('structure', 'materialfield', 'material.MaterialID', TableRelation::T_ONE_TO_MANY, 'MaterialID', '_mf'); |
|
|
|
|
77
|
|
|
new TableRelation('structure', 'structurematerial', 'StructureID', TableRelation::T_ONE_TO_MANY); |
78
|
|
|
new TableRelation('related_materials', 'material', 'first_material', TableRelation::T_ONE_TO_MANY, 'MaterialID'); |
|
|
|
|
79
|
|
|
new TableRelation('related_materials', 'materialfield', 'first_material', TableRelation::T_ONE_TO_MANY, 'MaterialID'); |
|
|
|
|
80
|
|
|
new TableRelation('field', 'structurefield', 'FieldID'); |
81
|
|
|
new TableRelation('field', 'structure', 'structurefield.StructureID'); |
82
|
|
|
new TableRelation('structurefield', 'field', 'FieldID'); |
83
|
|
|
new TableRelation('structurefield', 'materialfield', 'FieldID'); |
84
|
|
|
new TableRelation('structurefield', 'material', 'materialfield.MaterialID'); |
85
|
|
|
new TableRelation('structure', 'structure_relation', 'StructureID', TableRelation::T_ONE_TO_MANY, 'parent_id', 'children_relations'); |
|
|
|
|
86
|
|
|
new TableRelation('structure', 'structure', 'children_relations.child_id', TableRelation::T_ONE_TO_MANY, 'StructureID', 'children'); |
|
|
|
|
87
|
|
|
new TableRelation('structure', 'structure_relation', 'StructureID', TableRelation::T_ONE_TO_MANY, 'child_id', 'parents_relations'); |
|
|
|
|
88
|
|
|
new TableRelation('structure', 'structure', 'parents_relations.parent_id', TableRelation::T_ONE_TO_MANY, 'StructureID', 'parents'); |
|
|
|
|
89
|
|
|
new TableRelation('structurematerial', 'structure_relation', 'StructureID', TableRelation::T_ONE_TO_MANY, 'parent_id'); |
|
|
|
|
90
|
|
|
new TableRelation('groupright', 'right', 'RightID', TableRelation::T_ONE_TO_MANY); |
91
|
|
|
//elapsed('CMS:prepare'); |
92
|
|
|
|
93
|
|
|
// Все прошло успешно |
94
|
|
|
return true && parent::prepare(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Handler for CMSAPI database version manipulating |
99
|
|
|
* @param string $to_version Version to switch to |
100
|
|
|
* @return string Current database version |
101
|
|
|
*/ |
102
|
|
|
public function migrator($to_version = null) |
103
|
|
|
{ |
104
|
|
|
// If something passed - change database version to it |
105
|
|
|
if (func_num_args()) { |
106
|
|
|
// Save current version to special db table |
107
|
|
|
db()->query("ALTER TABLE `" . dbMySQLConnector::$prefix . "cms_version` CHANGE `version` `version` VARCHAR( 15 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '" . $to_version . "';"); |
|
|
|
|
108
|
|
|
die('Database successfully migrated to [' . $to_version . ']'); |
109
|
|
|
} else { // Return current database version |
|
|
|
|
110
|
|
|
$version_row = db()->fetch('SHOW COLUMNS FROM `' . dbMySQLConnector::$prefix . 'cms_version`'); |
111
|
|
|
return $version_row[0]['Default']; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** @see \samson\core\CompressableExternalModule::afterCompress() */ |
116
|
|
|
public function afterCompress(& $obj = null, array & $code = null) |
117
|
|
|
{ |
118
|
|
|
// Fill additional fields data to material db request data for automatic altering material request |
119
|
|
|
self::$fields = array(); |
120
|
|
|
|
121
|
|
|
$t_name = '_mf'; |
122
|
|
|
|
123
|
|
|
// Save original material attributes |
124
|
|
|
self::$materialAttributes = &Material::$_attributes; |
125
|
|
|
|
126
|
|
|
// Copy original material table attributes |
127
|
|
|
Material::$_attributes = \samson\activerecord\material::$_attributes; |
128
|
|
|
Material::$_sql_select = \samson\activerecord\material::$_sql_select; |
129
|
|
|
Material::$_sql_from = \samson\activerecord\material::$_sql_from; |
130
|
|
|
Material::$_own_group = \samson\activerecord\material::$_own_group; |
131
|
|
|
Material::$_map = \samson\activerecord\material::$_map; |
132
|
|
|
|
133
|
|
|
// Perform db query to get all possible material fields |
134
|
|
|
if (dbQuery('field')->Active(1)->Name('', dbRelation::NOT_EQUAL)->exec($this->material_fields)) foreach ($this->material_fields as $db_field) { |
|
|
|
|
135
|
|
|
// Add additional field localization condition |
136
|
|
|
if ($db_field->local == 1) $equal = '((' . $t_name . '.FieldID = ' . $db_field->id . ')&&(' . $t_name . ".locale = '" . locale() . "'))"; |
|
|
|
|
137
|
|
|
else $equal = '((' . $t_name . '.FieldID = ' . $db_field->id . ')&&(' . $t_name . ".locale = ''))"; |
|
|
|
|
138
|
|
|
|
139
|
|
|
// Define field value DB column for storing data |
140
|
|
|
$v_col = 'Value'; |
141
|
|
|
// We must get data from other column for this type of field |
142
|
|
|
if ($db_field->Type == 7 || $db_field->Type == 3 || $db_field->Type == 10) { |
143
|
|
|
$v_col = 'numeric_value'; |
144
|
|
|
} else if ($db_field->Type == 6) { |
|
|
|
|
145
|
|
|
$v_col = 'key_value'; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
// Save additional field |
149
|
|
|
self::$fields[$db_field->Name] = "\n" . ' MAX(IF(' . $equal . ',' . $t_name . '.`' . $v_col . '`, NULL)) as `' . $db_field->Name . '`'; |
|
|
|
|
150
|
|
|
|
151
|
|
|
// Set additional object metadata |
152
|
|
|
Material::$_attributes[$db_field->Name] = $db_field->Name; |
153
|
|
|
Material::$_map[$db_field->Name] = dbMySQLConnector::$prefix . 'material.' . $db_field->Name; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
// Set additional object metadata |
157
|
|
|
Material::$_sql_select['this'] = ' STRAIGHT_JOIN ' . Material::$_sql_select['this']; |
158
|
|
|
if (sizeof(self::$fields)) { |
159
|
|
|
Material::$_sql_select['this'] .= ',' . implode(',', self::$fields); |
160
|
|
|
} |
161
|
|
|
Material::$_sql_from['this'] .= "\n" . 'LEFT JOIN ' . dbMySQLConnector::$prefix . 'materialfield as ' . $t_name . ' on ' . dbMySQLConnector::$prefix . 'material.MaterialID = ' . $t_name . '.MaterialID'; |
|
|
|
|
162
|
|
|
Material::$_own_group[] = dbMySQLConnector::$prefix . 'material.MaterialID'; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** @see \samson\core\ExternalModule::init() */ |
166
|
|
|
public function init(array $params = array()) |
167
|
|
|
{ |
168
|
|
|
// Change static class data |
169
|
|
|
$this->afterCompress(); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: