-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
89 lines (67 loc) · 3.06 KB
/
index.php
File metadata and controls
89 lines (67 loc) · 3.06 KB
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
<?php
function prepareSqlQuery($sqlQuery){
global $modx;
return $modx->query($sqlQuery);
}
function extractResultsFromQueryObject($sqlQueryResult, $itemsToRetreive, $multiDimensionRetreive=false){
global $modx;
$results = array();
if (is_object($sqlQueryResult)) {
while ($row = $sqlQueryResult->fetch(PDO::FETCH_ASSOC)) {
if($multiDimensionRetreive){
foreach($itemsToRetreive as $key => $value){
array_push($results, $row[$key], $row[$value]);
}
} else {
foreach($itemsToRetreive as $itemToRetreive){
array_push($results, $row[$itemToRetreive]);
}
}
}
}
return $results;
}
$prepareSqlQueryOfRetreiveCategories = prepareSqlQuery('SELECT modx_site_content.id FROM `modx_site_content` WHERE modx_site_content.class_key = "msCategory"');
$categoriesIds = extractResultsFromQueryObject($prepareSqlQueryOfRetreiveCategories, array('id'));
if(count($categoriesIds) <= 0){
return 'Have no categories';
}
foreach($categoriesIds as $categoryId){
$productsQueryObject = prepareSqlQuery('SELECT modx_site_content.id FROM `modx_site_content` WHERE modx_site_content.class_key = "msProduct" AND modx_site_content.parent = "'.$categoryId.'"');
$productsIds = extractResultsFromQueryObject($productsQueryObject, array('id'));
if(count($productsIds) <= 0) continue;
$productsIds = implode(",", $productsIds);
$distinctProductOptionsSqlQuery = prepareSqlQuery('SELECT DISTINCT modx_ms2_product_options.key FROM `modx_ms2_product_options` WHERE modx_ms2_product_options.product_id IN ('.$productsIds.")");
$distinctProductOptions = extractResultsFromQueryObject($distinctProductOptionsSqlQuery, array('key'));
if(count($distinctProductOptions) <= 0) continue;
$uniqueOptions = "";
$uniqueOptionsLength = count($distinctProductOptions);
foreach ($distinctProductOptions as $idx => $value) {
if($idx >= $uniqueOptionsLength - 1){
$uniqueOptions .= "'$value'";
} else{
$uniqueOptions .= "'$value',";
}
}
$uniqueOptionsIdsSqlQuery = prepareSqlQuery('SELECT modx_ms2_options.id,modx_ms2_options.key FROM `modx_ms2_options` WHERE modx_ms2_options.key IN ('.$uniqueOptions.')');
$uniqueOptionsIds = extractResultsFromQueryObject($uniqueOptionsIdsSqlQuery, array('id'));
if(count($uniqueOptionsIds) <= 0) continue;
/* #################################### ##################################### */
foreach($uniqueOptionsIds as $idx => $uniqueOptionId){
if (!$cop = $modx->getObject('msCategoryOption', array('option_id' => $uniqueOptionId, 'category_id' => $categoryId))) {
$table = $modx->getTableName('msCategoryOption');
$sql = "INSERT INTO {$table} (`option_id`,`category_id`,`active`) VALUES ({$uniqueOptionId}, {$categoryId}, 1);";
$stmt = $modx->prepare($sql);
$stmt->execute();
} else {
$q = $modx->newQuery('msCategoryOption');
$q->command('UPDATE');
$q->where(array('option_id' => $uniqueOptionId, 'category_id' => $categoryId));
$q->set(array('active' => 1));
$q->prepare();
$q->stmt->execute();
}
}
/* #################################### ##################################### */
echo 'Done';
}