Products can now be cloned

This commit is contained in:
Frank Schubert
2024-02-21 15:40:59 +01:00
parent 0c706f6a0c
commit de39dd6946
5 changed files with 108 additions and 1 deletions

View File

@@ -108,6 +108,46 @@ class ProductController extends mfBaseController {
return $this->addAction();
}
protected function copyAction() {
if(!$this->me->is(["Admin"])) {
$this->redirect("Dashboard");
}
$id = $this->request->id;
if(!is_numeric($id) || $id < 1) {
$this->layout()->setFlash("Produkt nicht gefunden", "error");
$this->redirect("Product");
}
$product = new Product($id);
if(!$product->id) {
$this->layout()->setFlash("Produkt nicht gefunden", "error");
$this->redirect("Product");
}
$copy = clone($product);
if(!$copy->save()) {
$this->layout()->setFlash("Kopie konnte nicht gespeichert werden", "error");
$this->redirect("Product");
}
foreach($product->attributes as $attrib) {
$new_attrib = clone($attrib);
$new_attrib->product_id = $copy->id;
$new_attrib->save();
}
foreach(ProductNetworkModel::search(['product_id' => $product->id]) as $pn) {
$new_pn = clone($pn);
$new_pn->product_id = $copy->id;
$new_pn->save();
}
$this->layout()->setFlash("Kopie erfolgreich erstellt.", "success");
$this->redirect("Product", "edit", ["id" => $copy->id]);
}
protected function saveAction() {
if(!$this->me->is(["Admin"])) {
$this->redirect("Dashboard");