Added Products
This commit is contained in:
134
application/Product/ProductController.php
Normal file
134
application/Product/ProductController.php
Normal file
@@ -0,0 +1,134 @@
|
||||
<?php
|
||||
|
||||
class ProductController extends mfBaseController {
|
||||
|
||||
protected function init() {
|
||||
$this->needlogin=true;
|
||||
$me = new User();
|
||||
$me->loadMe();
|
||||
$this->me = $me;
|
||||
$this->layout()->set("me",$me);
|
||||
/*
|
||||
if(!$me->isAdmin()) {
|
||||
$this->redirect("Dashboard");
|
||||
}*/
|
||||
}
|
||||
|
||||
protected function indexAction() {
|
||||
$this->layout()->set("products", ProductModel::getAll());
|
||||
}
|
||||
|
||||
protected function addAction() {
|
||||
$this->layout()->setTemplate("Product/Form");
|
||||
$this->layout()->set("productgroups", ProductgroupModel::getAll());
|
||||
$this->layout()->set("producttechs", ProducttechModel::getAll());
|
||||
|
||||
}
|
||||
|
||||
protected function editAction() {
|
||||
$product_id = $this->request->id;
|
||||
$product = new Product($product_id);
|
||||
if(!$product->id) {
|
||||
$this->layout()->setFlash("Produkt nicht gefunden.", "error");
|
||||
$this->redirect("Product");
|
||||
}
|
||||
|
||||
$this->layout()->set("product", $product);
|
||||
|
||||
return $this->addAction();
|
||||
}
|
||||
|
||||
protected function saveAction() {
|
||||
$r = $this->request;
|
||||
//var_dump($r);exit;
|
||||
$id = $r->id;
|
||||
if(is_numeric($id) && $id > 0) {
|
||||
$mode = "edit";
|
||||
$product = new Product($id);
|
||||
if(!$product->id) {
|
||||
$this->layout()->setFlash("Produkt nicht gefunden", "error");
|
||||
$this->redirect("Produkt");
|
||||
}
|
||||
} else {
|
||||
$id = false;
|
||||
$mode = "add";
|
||||
}
|
||||
|
||||
$data = [];
|
||||
$data['name'] = $r->name;
|
||||
$data['description'] = $r->description;
|
||||
$data['external'] = ($r->external == 1) ? "1" : "0";
|
||||
$data['price_nne'] = Layout::commaToDot($r->price_nne);
|
||||
$data['price_nbe'] = Layout::commaToDot($r->price_nbe);
|
||||
$data['price'] = Layout::commaToDot($r->price);
|
||||
$data['billing_period'] = $r->billing_period;
|
||||
$data['note'] = $r->note;
|
||||
|
||||
if(is_numeric($r->producttech_id)) {
|
||||
$data['producttech_id'] = $r->producttech_id;
|
||||
}
|
||||
if(is_numeric($r->productgroup_id)) {
|
||||
$data['productgroup_id'] = $r->productgroup_id;
|
||||
}
|
||||
|
||||
$data['edit_by'] = 1;
|
||||
|
||||
if($mode == "add") {
|
||||
$data['create_by'] = 1;
|
||||
$product = ProductModel::create($data);
|
||||
} else {
|
||||
$product->update($data);
|
||||
}
|
||||
|
||||
//var_dump($address);exit;
|
||||
|
||||
$new_id = $product->save();
|
||||
if(!$new_id) {
|
||||
$this->layout()->setFlash("Fehler beim Speichern", "error");
|
||||
$this->layout()->set("product", $product);
|
||||
return $this->add();
|
||||
}
|
||||
|
||||
// create new product group and tech
|
||||
|
||||
if($r->productgroup_id == "new") {
|
||||
$ng = [];
|
||||
$ng['name'] = $r->productgroup_new_name;
|
||||
$ng['description'] = $r->productgroup_new_description;
|
||||
$ng['note'] = $r->productgroup_new_note;
|
||||
$group = ProductgroupModel::create($ng);
|
||||
$group_id = $group->save();
|
||||
$product->productgroup_id = $group_id;
|
||||
$product->save();
|
||||
}
|
||||
|
||||
if($r->producttech_id == "new") {
|
||||
$nt = [];
|
||||
$nt['name'] = $r->producttech_new_name;
|
||||
$nt['code'] = $r->producttech_new_code;
|
||||
$nt['description'] = $r->producttech_new_description;
|
||||
$nt['note'] = $r->producttech_new_note;
|
||||
$tech = ProducttechModel::create($nt);
|
||||
$tech_id = $tech->save();
|
||||
$product->producttech_id = $tech_id;
|
||||
$product->save();
|
||||
}
|
||||
|
||||
$this->layout()->setFlash("Produkt erfolgreich gespeichert.", "success");
|
||||
$this->redirect("Product", "Edit", ['id' => $new_id]);
|
||||
}
|
||||
|
||||
protected function delete() {
|
||||
$id = $this->request->id;
|
||||
|
||||
$product = new Product($id);
|
||||
if(!$product->id || $product->id != $id) {
|
||||
$this->layout()->setFlash("Produkt nicht gefunden.", "error");
|
||||
$this->redirect("Product");
|
||||
}
|
||||
|
||||
// check if Product is unused
|
||||
$product->delete();
|
||||
$this->redirect("Product");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user