-
-
Notifications
You must be signed in to change notification settings - Fork 159
/
table_relationships.php
executable file
·406 lines (370 loc) · 11.1 KB
/
table_relationships.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
<?php
/*------------------------------------------------------------------------------
$Id$
AbanteCart, Ideal OpenSource Ecommerce Solution
http://www.AbanteCart.com
Copyright © 2011-2015 Belavier Commerce LLC
This source file is subject to Open Software License (OSL 3.0)
License details is bundled with this package in the file LICENSE.txt.
It is also available at this URL:
<http://www.opensource.org/licenses/OSL-3.0>
UPGRADE NOTE:
Do not edit or add to this file if you wish to upgrade AbanteCart to newer
versions in the future. If you wish to customize AbanteCart for your
needs please refer to http://www.AbanteCart.com for more information.
------------------------------------------------------------------------------*/
if (! defined ( 'DIR_CORE' ) || !IS_ADMIN) {
header ( 'Location: static_pages/' );
}
class ModelToolTableRelationships extends Model {
private $tables_data = array();
private $sections = array();
public function __construct($registry ) {
parent::__construct($registry);
$this->_load_tables_configs();
}
private function _load_tables_configs(){
$this->sections['resource_library'] = array(
'id' => 'resource_id',
'relation_ids' => array ( 'type_id' ),
'children' => array(
'resource_descriptions' => array(
'id' => null,
'relation_ids' => array('resource_id', 'language_id')
),
'resource_map' => array(
'id' => null,
'relation_ids' => array( 'resource_id' )
),
),
);
$this->sections['resource_types'] = array(
'id' => 'type_id',
'children' => array(
'resource_library' => $this->sections['resource_library']
)
);
$this->sections['categories'] = array(
'id' => 'category_id',
'children' => array(
'category_descriptions' => array(
'id' => null,
'relation_ids' => array('category_id','language_id')
),
'categories_to_stores' => array(
'id' => null,
'relation_ids' => array('category_id','store_id')
),
/* Special case, no matching field name */
'resource_map' => array(
'id' => 'resource_id',
'on_insert_no_id' => true,
'relation_ids' => array( 'resource_id', 'object_id' ),
'special_relation' => array( 'object_name' => 'categories', 'object_id' => 'category_id'),
'children' => array(
/* Note: Issue to connect to resource_library automaticaly */
'resource_library' => $this->sections['resource_library']
)
),
),
);
$this->sections['products'] = array(
'id' => 'product_id',
'children' => array(
'product_descriptions' => array(
'id' => null,
'relation_ids' => array('product_id','language_id')
),
'products_featured' => array(
'id' => null,
'relation_ids' => array('product_id')
),
'products_related' => array(
'id' => null,
'relation_ids' => array('product_id', 'related_id')
),
'products_to_categories' => array(
'id' => null,
'relation_ids' => array('product_id', 'category_id')
),
'products_to_downloads' => array(
'id' => null,
'relation_ids' => array('product_id', 'download_id')
),
'products_to_stores' => array(
'id' => null,
'relation_ids' => array('product_id', 'store_id')
),
'product_discounts' => array(
'id' => 'product_discount_id',
'relation_ids' => array('product_id')
),
'product_options' => array(
'id' => 'product_option_id',
'relation_ids' => array('product_id'),
'children' =>array(
'product_option_descriptions' => array(
'id' => null,
'relation_ids' => array('product_id', 'language_id', 'product_option_id')
),
'product_option_values' => array(
'id' => 'product_option_value_id',
'relation_ids' => array('product_id', 'product_option_id'),
'children' => array(
'product_option_value_descriptions' => array(
'id' => null,
'relation_ids' => array('product_id', 'language_id', 'product_option_value_id')
),
)
),
)
),
'product_specials' => array(
'id' => 'product_special_id',
'relation_ids' => array('product_id')
),
/*
* All 3 columns of the table are primary keys
* So we can't update it. Only insert or delete
*/
'product_tags' => array(
'id' => null,
'relation_ids' => array('product_id', 'tag', 'language_id')
),
/* Special case, no matching field name */
'resource_map' => array(
'id' => 'resource_id',
'relation_ids' => array( 'resource_id', 'object_id' ),
'special_relation' => array( 'object_name' => 'products', 'object_id' => 'product_id'),
'children' => array(
'resource_library' => $this->sections['resource_library']
)
),
'reviews' => array(
'id' => null,
'relation_ids' => array('product_id', 'review_id', 'customer_id')
)
)
);
$this->sections['orders'] = array(
'id' => 'order_id',
'children' => array(
'order_downloads' => array(
'id' => null,
'relation_ids' => array('order_id', 'order_download_id')
),
'order_history' => array(
'id' => null,
'relation_ids' => array('order_id', 'order_history_id')
),
'order_options' => array(
'id' => null,
'relation_ids' => array('order_id', 'order_option_id')
),
'order_products' => array(
'id' => null,
'relation_ids' => array('order_id', 'order_product_id')
),
'order_totals' => array(
'id' => null,
'relation_ids' => array('order_id', 'order_total_id')
),
)
);
$this->sections['manufacturers'] = array(
'id' => 'manufacturer_id',
'children' => array(
'manufacturers_to_stores' => array(
'id' => null,
'relation_ids' => array('manufacturer_id', 'store_id')
),
/* Special case, no matching field name */
'resource_map' => array(
'id' => 'resource_id',
'relation_ids' => array( 'resource_id', 'object_id' ),
'special_relation' => array( 'object_name' => 'manufacturers', 'object_id' => 'manufacturer_id'),
'children' => array(
'resource_library' => $this->sections['resource_library']
)
),
)
);
$this->sections['customers'] = array(
'id' => 'customer_id',
'children' => array(
'addresses' => array(
'id' => null,
'relation_ids' => array('customer_id', 'address_id')
),
'reviews' => array(
'id' => null,
'relation_ids' => array('customer_id', 'review_id', 'product_id')
)
),
);
$this->sections['blocks'] = array(
'id' => 'block_id',
'children' => array(
'block_layouts' => array(
'id' => 'instance_id',
'relation_ids' => array('block_id', 'instance_id', 'layout_id', 'parent_instance_id', 'custom_block_id'),
'children' => array(
'layouts' => array(
'id' => null,
'switch_to_id' => 'layout_id',
'relation_ids' => array('layout_id')
)
)
),
'block_templates' => array(
'id' => null,
'relation_ids' => array('block_id', 'parent_block_id')
),
'custom_blocks' => array(
'id' => 'custom_block_id',
'relation_ids' => array('block_id'),
'children' => array(
'block_descriptions' => array(
'id' => null,
'relation_ids' => array('custom_block_id', 'block_description_id')
)
)
)
)
);
$this->sections['downloads'] = array(
'id' => 'download_id',
'children' => array(
'download_descriptions' => array(
'id' => null,
'relation_ids' => array('download_id', 'language_id')
)
)
);
// Only "add as it is". Update is left for future enhancements.
$this->sections['url_aliases'] = array(
'id' => 'url_alias_id'
);
}
/**
* check if requested table exist and return its array
* @param string $table_name
* @param array $search_in_input
* @return null|array
*/
public function find_table_cfg ( $table_name, $search_in_input = array() ) {
if ( empty($table_name) ) {
return null;
}
if (count($search_in_input) <= 0) {
$search_section = $this->sections;
}else{
$search_section = $search_in_input['children'];
}
foreach ($search_section as $sectionName => $section){
if ($table_name == $sectionName) {
return $section;
} else {
if ( is_array($section['children']) && count($section['children']) > 0 ) {
$found_arr = $this->find_table_cfg($table_name, $section);
if ($found_arr) {
return $found_arr;
}
}
}
}
return null;
}
/**
* @param array $sections
* @return bool
*/
public function setSections($sections = array()){
if ( !empty($sections) )
{
$this->sections = $sections;
return true;
}
return false;
}
/**
* @return array
*/
public function getSections(){
return $this->sections;
}
private function _build_table_relationship() {
//NOTE: Not used see below in _build_relathionship
$cache_file = 'tables.key.relationship';
if($this->cache){
$this->tables_data = $this->cache->get($cache_file);
}
if (!$this->tables_data) {
$sql = "SELECT table_name, column_name, extra
FROM information_schema.columns
WHERE table_schema='" . DB_DATABASE . "' group by table_name";
$load_sql = $this->db->query($sql);
$tables = $load_sql->rows;
foreach ($tables as $table) {
//get primary keys
$pkeys = array();
$sql = "SHOW INDEX FROM " . $table['table_name'] . "
WHERE Key_name = 'PRIMARY'";
$primary_query = $this->db->query($sql);
foreach($primary_query->rows as $value) {
$pkeys[] = $value['Column_name'];
}
$table_name = $table['table_name'];
$this->tables_data[$table_name] = array();
if ( $table['extra'] == 'auto_increment' ) {
$this->tables_data[$table_name]['id'] = $table['column_name'];
} else {
$this->tables_data[$table_name]['id'] = NULL;
}
$this->tables_data[$table_name]['relation_ids'] = $pkeys;
}
$this->_build_relathionship();
$this->_apply_special_cases();
if($this->cache){
$this->cache->set($cache_file, $table_data);
}
}
}
private function _build_relathionship() {
//Need o connect tables with relationship IDs.
//NOT IMPLEMENTED. Issue with logic to detect id and content. Slow and unreliable solution.
// Looking for better solution to automate or us TRUE relational DB with Postgres ot InnoDB with mysql
}
private function _apply_special_cases() {
//Need o connect tables with relationship IDs.
//NOT IMPLEMENTED. Issue with logic to detect id and content. Slow and unreliable solution.
// Looking for better solution to automate or us TRUE relational DB with Postgres ot InnoDB with mysql
}
/**
* @param string $table_name
* @return null|array
*/
public function get_table_cfg ( $table_name ) {
//NOTE: DO not use. Not implemented yet.
if ( empty($table_name) ) {
return null;
}
if (!$this->tables_data ) {
$this->_build_table_relationship();
}
if( $this->tables_data[DB_PREFIX.$table_name] ) {
return $this->tables_data[DB_PREFIX.$table_name];
} else {
return null;
}
}
/**
* @param string $table_name
* @return array
*/
public function get_table_columns ( $table_name ) {
$sql = 'SHOW COLUMNS FROM `' . $this->db->escape(DB_PREFIX.$table_name) . '` FROM `' . DB_DATABASE . '`';
$results = $this->db->query($sql);
return $results->rows;
}
}