|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
class EcommerceProductVariationTaskDeleteVariations extends BuildTask |
|
|
|
|
|
|
4
|
|
|
{ |
|
5
|
|
|
protected $verbose = true; |
|
6
|
|
|
|
|
7
|
|
|
public static function create_link($product) |
|
8
|
|
|
{ |
|
9
|
|
|
if (is_numeric($product)) { |
|
10
|
|
|
$product = Product::get()->byID($product); |
|
11
|
|
|
} elseif ($product instanceof Product) { |
|
|
|
|
|
|
12
|
|
|
//do nothing |
|
13
|
|
|
} |
|
14
|
|
|
if ($product) { |
|
15
|
|
|
return "dev/tasks/EcommerceProductVariationTaskDeleteVariations/?productid=".$product->ID."&live=1&silent=1"; |
|
16
|
|
|
} |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
protected $title = "Deletes all the variations and associated data from a product"; |
|
20
|
|
|
|
|
21
|
|
|
protected $description = "CAREFUL: the developer will need to supply the ID as a get variable (?productid=XXX) as well as a test / live flag (?live=1, default is test) for the product and variations will be deleted without keeping a history."; |
|
22
|
|
|
|
|
23
|
|
|
public function run($request) |
|
|
|
|
|
|
24
|
|
|
{ |
|
25
|
|
|
$productVariationArrayID = array(); |
|
26
|
|
|
if (empty($_GET["silent"])) { |
|
27
|
|
|
$this->verbose = true; |
|
28
|
|
|
} else { |
|
29
|
|
|
$this->verbose = intval($_GET["silent"]) == 1 ? false : true; |
|
30
|
|
|
} |
|
31
|
|
|
if (empty($_GET["productid"])) { |
|
32
|
|
|
$productID = 0; |
|
33
|
|
|
} elseif ($_GET["productid"] == 'all') { |
|
34
|
|
|
$productID = -1; |
|
35
|
|
|
} else { |
|
36
|
|
|
$productID = intval($_GET["productid"]); |
|
37
|
|
|
} |
|
38
|
|
View Code Duplication |
if (empty($_GET["live"])) { |
|
|
|
|
|
|
39
|
|
|
$live = false; |
|
40
|
|
|
} else { |
|
41
|
|
|
$live = intval($_GET["live"]) == 1 ? true : false; |
|
42
|
|
|
} |
|
43
|
|
View Code Duplication |
if ($live) { |
|
|
|
|
|
|
44
|
|
|
if ($this->verbose) { |
|
45
|
|
|
DB::alteration_message("this is a live task", "deleted"); |
|
46
|
|
|
} |
|
47
|
|
|
} else { |
|
48
|
|
|
if ($this->verbose) { |
|
49
|
|
|
DB::alteration_message("this is a test only. If you add a live=1 get variable then you can make it for real ;-)", "created"); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
if ($productID == -1) { |
|
53
|
|
|
$products = Product::get(); |
|
54
|
|
|
} else { |
|
55
|
|
|
$products = null; |
|
56
|
|
|
$product = Product::get()->byID($productID); |
|
57
|
|
|
if ($product) { |
|
58
|
|
|
$products= new ArrayList(); |
|
59
|
|
|
$products->push($product); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
if ($products && $products->count()) { |
|
63
|
|
|
foreach ($products as $product) { |
|
64
|
|
|
$productID = $product->ID; |
|
65
|
|
|
if ($products->count()) { |
|
66
|
|
|
if ($this->verbose) { |
|
67
|
|
|
DB::alteration_message("Deleting variations for ".$product->Title, "deleted"); |
|
68
|
|
|
} |
|
69
|
|
|
$variations = ProductVariation::get()->filter(array("ProductID" => $productID))->limit(100); |
|
70
|
|
|
if ($variations->count()) { |
|
71
|
|
|
if ($this->verbose) { |
|
72
|
|
|
DB::alteration_message("PRE DELETE COUNT: ".$variations->count()); |
|
73
|
|
|
} |
|
74
|
|
|
foreach ($variations as $variation) { |
|
75
|
|
|
if ($this->verbose) { |
|
76
|
|
|
DB::alteration_message(" Deleting Variation: ".$variation->Title(), "deleted"); |
|
77
|
|
|
} |
|
78
|
|
|
if ($live) { |
|
79
|
|
|
$variation->delete(); |
|
80
|
|
|
} |
|
81
|
|
|
$productVariationArrayID[$variation->ID] = $variation->ID; |
|
82
|
|
|
} |
|
83
|
|
|
$variations = ProductVariation::get()->filter(array("ProductID" => $productID))->limit(100); |
|
84
|
|
|
if ($live) { |
|
85
|
|
View Code Duplication |
if ($variations->count()) { |
|
|
|
|
|
|
86
|
|
|
if ($this->verbose) { |
|
87
|
|
|
DB::alteration_message("POST DELETE COUNT: ".$variations->count()); |
|
88
|
|
|
} |
|
89
|
|
|
} else { |
|
90
|
|
|
if ($this->verbose) { |
|
91
|
|
|
DB::alteration_message("All variations have been deleted: ", "created"); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
} else { |
|
95
|
|
|
if ($this->verbose) { |
|
96
|
|
|
DB::alteration_message("This was a test only", "created"); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
} else { |
|
100
|
|
|
if ($this->verbose) { |
|
101
|
|
|
DB::alteration_message("There are no variations to delete", "created"); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
if ($this->verbose) { |
|
105
|
|
|
DB::alteration_message("Starting cleanup", "created"); |
|
106
|
|
|
} |
|
107
|
|
|
if ($live) { |
|
108
|
|
|
$sql = "
|
|
109
|
|
|
DELETE
|
|
110
|
|
|
FROM \"Product_VariationAttributes\"
|
|
111
|
|
|
WHERE \"ProductID\" = ".$productID; |
|
112
|
|
|
if ($this->verbose) { |
|
113
|
|
|
DB::alteration_message("<pre>RUNNING<br />".$sql."</pre>"); |
|
114
|
|
|
} |
|
115
|
|
|
DB::query($sql); |
|
116
|
|
|
$sql = "
|
|
117
|
|
|
DELETE \"ProductVariation_AttributeValues\"
|
|
118
|
|
|
FROM \"ProductVariation_AttributeValues\"
|
|
119
|
|
|
LEFT JOIN \"ProductVariation\"
|
|
120
|
|
|
ON \"ProductVariation_AttributeValues\".\"ProductVariationID\" = \"ProductVariation\".\"ID\"
|
|
121
|
|
|
WHERE \"ProductVariation\".\"ID\" IS NULL"; |
|
122
|
|
|
if ($this->verbose) { |
|
123
|
|
|
DB::alteration_message("<pre>RUNNING<br />".$sql."</pre>"); |
|
124
|
|
|
} |
|
125
|
|
|
DB::query($sql); |
|
126
|
|
|
} else { |
|
127
|
|
|
$sql = "
|
|
128
|
|
|
SELECT COUNT(Product_VariationAttributes.ID)
|
|
129
|
|
|
FROM \"Product_VariationAttributes\"
|
|
130
|
|
|
WHERE \"ProductID\" = ".$productID; |
|
131
|
|
|
if ($this->verbose) { |
|
132
|
|
|
DB::alteration_message("<pre>RUNNING<br />".$sql."</pre>"); |
|
133
|
|
|
} |
|
134
|
|
|
$result = DB::query($sql); |
|
135
|
|
|
if ($this->verbose) { |
|
136
|
|
|
DB::alteration_message("Would have deleted ".$result->value()." rows"); |
|
137
|
|
|
} |
|
138
|
|
|
$sql = "
|
|
139
|
|
|
SELECT COUNT (\"ProductVariation_AttributeValues\".\"ID\")
|
|
140
|
|
|
FROM \"ProductVariation_AttributeValues\"
|
|
141
|
|
|
LEFT JOIN \"ProductVariation\"
|
|
142
|
|
|
ON \"ProductVariation_AttributeValues\".\"ProductVariationID\" = \"ProductVariation\".\"ID\"
|
|
143
|
|
|
WHERE
|
|
144
|
|
|
\"ProductVariation\".\"ID\" IS NULL OR
|
|
145
|
|
|
\"ProductVariation\".\"ID\" IN(".implode(",", $productVariationArrayID).") "; |
|
146
|
|
|
if ($this->verbose) { |
|
147
|
|
|
DB::alteration_message("<pre>RUNNING<br />".$sql."</pre>"); |
|
148
|
|
|
} |
|
149
|
|
|
$result = DB::query($sql); |
|
150
|
|
|
if ($this->verbose) { |
|
151
|
|
|
DB::alteration_message("Would have deleted ".$result->value()." rows"); |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
} else { |
|
157
|
|
|
if ($this->verbose) { |
|
158
|
|
|
DB::alteration_message("Product does not exist. You can set the product by adding it productid=XXX as a GET variable. You can also add <i>all</i> to delete ALL product Variations.", "deleted"); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
DB::alteration_message("Completed", "created"); |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
class EcommerceProductVariationTaskDeleteVariations_EXT extends Extension |
|
|
|
|
|
|
166
|
|
|
{ |
|
167
|
|
|
private static $allowed_actions = array( |
|
|
|
|
|
|
168
|
|
|
"ecommerceproductvariationtaskdeletevariations" => true |
|
169
|
|
|
); |
|
170
|
|
|
|
|
171
|
|
|
//NOTE THAT updateEcommerceDevMenuConfig adds to Config options |
|
172
|
|
|
//but you can als have: updateEcommerceDevMenuDebugActions |
|
173
|
|
|
public function updateEcommerceDevMenuRegularMaintenance($buildTasks) |
|
174
|
|
|
{ |
|
175
|
|
|
$buildTasks[] = "ecommerceproductvariationtaskdeletevariations"; |
|
176
|
|
|
return $buildTasks; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
public function ecommerceproductvariationtaskdeletevariations($request) |
|
180
|
|
|
{ |
|
181
|
|
|
$this->owner->runTask("EcommerceProductVariationTaskDeleteVariations", $request); |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|