function run($request) { $tables = array("ProductGroup", "ProductGroup_Live", "Product", "Product_Live"); if (class_exists("ProductVariation")) { $tables[] = "ProductVariation"; } //todo: make list based on buyables rather than hard-coded. foreach ($tables as $tableName) { $classErrorCount = 0; $removeCount = 0; $updateClassCount = 0; $rowCount = DB::query("SELECT COUNT(\"ImageID\") FROM \"{$tableName}\" WHERE ImageID > 0;")->value(); DB::alteration_message("<h2><strong>CHECKING {$tableName} ( {$rowCount} records ):</strong></h2>"); $rows = DB::query("SELECT \"ImageID\", \"{$tableName}\".\"ID\" FROM \"{$tableName}\" WHERE ImageID > 0;"); if ($rows) { foreach ($rows as $row) { $remove = false; $classErrorCount += DB::query("\r\n\t\t\t\t\t\tSELECT COUNT (\"File\".\"ID\")\r\n\t\t\t\t\t\tFROM \"File\"\r\n\t\t\t\t\t\tWHERE\r\n\t\t\t\t\t\t\t\"File\".\"ID\" = " . $row["ImageID"] . "\r\n\t\t\t\t\t\t\tAND (\r\n\t\t\t\t\t\t\t \"ClassName\" = 'Image' OR\r\n\t\t\t\t\t\t\t \"ClassName\" = 'ProductVariation_Image' OR\r\n\t\t\t\t\t\t\t \"ClassName\" = ''\r\n\t\t\t\t\t\t\t);\r\n\t\t\t\t\t")->value(); DB::query("\r\n\t\t\t\t\t\tUPDATE \"File\"\r\n\t\t\t\t\t\tSET \"ClassName\" = 'Product_Image'\r\n\t\t\t\t\t\tWHERE\r\n\t\t\t\t\t\t\t\"File\".\"ID\" = " . $row["ImageID"] . "\r\n\t\t\t\t\t\t\tAND (\r\n\t\t\t\t\t\t\t \"ClassName\" = 'Image' OR\r\n\t\t\t\t\t\t\t \"ClassName\" = 'ProductVariation_Image' OR\r\n\t\t\t\t\t\t\t \"ClassName\" = ''\r\n\t\t\t\t\t\t\t);\r\n\t\t\t\t\t"); $image = Product_Image::get()->byID($row["ImageID"]); if (!$image) { $remove = true; } elseif (!$image->getTag()) { $remove = true; } if ($remove) { $removeCount++; DB::query("UPDATE \"{$tableName}\" SET \"ImageID\" = 0 WHERE \"{$tableName}\".\"ID\" = " . $row["ID"] . " AND \"{$tableName}\".\"ImageID\" = " . $row["ImageID"] . ";"); } elseif (!is_a($image, Object::getCustomClass("Product_Image"))) { $updateClassCount++; $image = $image->newClassInstance("Product_Image"); $image - write(); } } } if ($classErrorCount) { DB::alteration_message("<strong>{$tableName}:</strong> there were {$classErrorCount} files with the wrong class names. These have been fixed.", "deleted"); } else { DB::alteration_message("<strong>{$tableName}:</strong> there were no files with the wrong class names. ", "created"); } if ($removeCount) { DB::alteration_message("<strong>{$tableName}:</strong> Removed {$removeCount} image(s) from products and variations because they do not exist in the file-system or database", "deleted"); } else { DB::alteration_message("<strong>{$tableName}:</strong> All product images are accounted for", "created"); } if ($updateClassCount) { DB::alteration_message("<strong>{$tableName}:</strong> {$removeCount} image(s) did not match the requirement 'instanceOF Product_Image', this has been corrected.", "deleted"); } else { DB::alteration_message("<strong>{$tableName}:</strong> All product images instancesOF Product_Image", "created"); } } }