Attributes can be set in products now

This commit is contained in:
Frank Schubert
2021-07-06 14:56:57 +02:00
parent 002c2565eb
commit ad4ab58332
16 changed files with 893 additions and 14 deletions
+34 -1
View File
@@ -5,6 +5,36 @@ class Product extends mfBaseModel {
private $producttech;
private $sla;
private $networks;
private $attributes;
public function loadAttributes() {
if(!$this->producttech_id) {
return false;
}
$this->attributes = [];
// get tech attribs
$ptattribs = ProducttechAttributeModel::search(['producttech_id' => $this->producttech_id]);
// fill atrribs with existing values of product attribs
foreach($ptattribs as $pta) {
$attrib = ProductAttributeModel::getFirst(['product_id' => $this->id, 'producttechattribute_id' => $pta->id]);
if(!$attrib) {
$attrib = new ProductAttribute();
$attrib->product_id = $this->id;
$attrib->producttechattribute_id = $pta->id;
$attrib->value = $pta->value;
$attrib->note = $pta->note;
}
$attrib->name = $pta->name;
$attrib->displayname = $pta->displayname;
$attrib->description = $pta->description;
$this->attributes[$attrib->name] = $attrib;
}
return true;
}
public function getProperty($name) {
if($this->$name == null) {
@@ -18,7 +48,10 @@ class Product extends mfBaseModel {
return $this->networks;
}
if($name == "attributes") {
$this->loadAttributes();
return $this->attributes;
}
$classname = ucfirst($name);
$idfield = $name."_id";
+21 -3
View File
@@ -35,6 +35,8 @@ class ProductController extends mfBaseController {
$this->redirect("Product");
}
//var_dump($product->attributes);exit;
$this->layout()->set("product", $product);
return $this->addAction();
@@ -49,7 +51,7 @@ class ProductController extends mfBaseController {
$product = new Product($id);
if(!$product->id) {
$this->layout()->setFlash("Produkt nicht gefunden", "error");
$this->redirect("Produkt");
$this->redirect("Product");
}
} else {
$id = false;
@@ -79,10 +81,10 @@ class ProductController extends mfBaseController {
$data['productgroup_id'] = null;
}
$data['edit_by'] = 1;
$data['edit_by'] = $this->me->id;
if($mode == "add") {
$data['create_by'] = 1;
$data['create_by'] = $this->me->id;
$product = ProductModel::create($data);
} else {
$product->update($data);
@@ -141,6 +143,22 @@ class ProductController extends mfBaseController {
$product->save();
}
if(is_array($r->attributes) && count($r->attributes)) {
foreach($r->attributes as $pta_id => $attribute_value) {
$attrib = ProductAttributeModel::getFirst(['product_id' => $new_id, 'producttechattribute_id' => $pta_id]);
if(!$attrib) {
$attrib = ProductAttributeModel::create([
'product_id' => $new_id,
'producttechattribute_id' => $pta_id,
]);
//var_dump($attrib);exit;
}
$attrib->value = $attribute_value;
$attrib->edit_by = $this->me->id;
$attrib->save();
}
}
$this->layout()->setFlash("Produkt erfolgreich gespeichert.", "success");
$this->redirect("Product", "Edit", ['id' => $new_id]);
}
+1 -1
View File
@@ -94,7 +94,7 @@ class ProductModel {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$res = $db->select("Product", "*", "$where ORDER BY name, owner_id");
$res = $db->select("Product", "*", "$where ORDER BY producttech_id, name");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new Product($data);
@@ -0,0 +1,33 @@
<?php
class ProductAttribute extends mfBaseModel {
public function getProperty($name) {
if($this->$name == null) {
if($name == "networks") {
$this->networks = [];
$productNetworks = ProductNetworkModel::search(['product_id' => $this->id]);
foreach($productNetworks as $pnet) {
$this->networks[$pnet->network_id] = new Network($pnet->network_id);
}
return $this->networks;
}
$classname = ucfirst($name);
$idfield = $name."_id";
$this->$name = new $classname($this->$idfield);
if($this->$name->id) {
return $this->$name;
} else {
return null;
}
}
return $this->$name;
}
}
@@ -0,0 +1,18 @@
<?php
class ProductAttributeController 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");
}*/
}
}
@@ -0,0 +1,121 @@
<?php
class ProductAttributeModel {
public $product_id = null;
public $producttechattribute_id = null;
public $value = null;
public $create_by = null;
public $edit_by = null;
public $create = null;
public $edit = null;
public static function create(Array $data) {
$model = new ProductAttribute();
foreach($data as $field => $value) {
if(property_exists(get_called_class(), $field)) {
$model->$field = $value;
}
}
$me = new User();
$me->loadMe();
if($model->create_by === null) {
$model->create_by = $me->id;
}
if($model->edit_by === null) {
$model->edit_by = $me->id;
}
return $model;
}
public static function getOne($id) {
if(!is_numeric($id) || !$id) {
throw new Exception("Invalid number", 400);
}
$item = [];
$db = FronkDB::singleton();
$res = $db->select("ProductAttribute", "*", "id=$id LIMIT 1");
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new ProductAttribute($data);
}
return $item;
}
public static function getAll() {
$items = [];
$db = FronkDB::singleton();
$res = $db->select("ProductAttribute", "*", "1 = 1 ORDER BY product_id, producttechattribute_id");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[$data->id] = new ProductAttribute($data);
}
}
return $items;
}
public static function getFirst($filter) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$res = $db->select("ProductAttribute", "*", "$where ORDER BY product_id, producttechattribute_id");
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new ProductAttribute($data);
if($item->id) {
return $item;
} else {
return null;
}
}
return null;
}
public static function search($filter) {
$items = [];
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$res = $db->select("ProductAttribute", "*", "$where ORDER BY name");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[$data->id] = new ProductAttribute($data);
}
}
return $items;
}
private function getSqlFilter($filter) {
$where = "1=1 ";
$db = FronkDB::singleton();
if(array_key_exists("product_id", $filter)) {
$product_id = $filter['product_id'];
if(is_numeric($product_id)) {
$where .= " AND `product_id` = $product_id";
}
}
if(array_key_exists("producttechattribute_id", $filter)) {
$producttechattribute_id = $filter['producttechattribute_id'];
if(is_numeric($producttechattribute_id)) {
$where .= " AND `producttechattribute_id` = $producttechattribute_id";
}
}
//var_dump($filter, $where);exit;
return $where;
}
}
+27
View File
@@ -1,5 +1,32 @@
<?php
class Producttech extends mfBaseModel {
private $attributes;
public function getProperty($name) {
if($this->$name == null) {
if($name == "attributes") {
$this->attributes = [];
$producttechAttributes = ProducttechAttributeModel::search(['producttech_id' => $this->id]);
foreach($producttechAttributes as $pta) {
$this->attributes[$pta->id] = $pta;
}
return $this->attributes;
}
$classname = ucfirst($name);
$idfield = $name."_id";
$this->$name = new $classname($this->$idfield);
if($this->$name->id) {
return $this->$name;
} else {
return null;
}
}
return $this->$name;
}
}
@@ -0,0 +1,133 @@
<?php
class ProducttechController 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()->setTemplate("Producttech/Index");
$this->layout()->set("producttechs", ProducttechModel::getAll());
}
protected function addAction() {
$this->layout()->setTemplate("Producttech/Form");
}
protected function editAction() {
$tech_id = $this->request->id;
$tech = new Producttech($tech_id);
if(!$tech->id) {
$this->layout()->setFlash("Technologie nicht gefunden.", "error");
$this->redirect("Producttech");
}
$this->layout()->set("producttech", $tech);
return $this->addAction();
}
protected function saveAction() {
$r = $this->request;
//var_dump($r);
//var_dump($r->attributes);
//exit;
$id = $r->id;
if(is_numeric($id) && $id > 0) {
$mode = "edit";
$producttech = new Producttech($id);
if(!$producttech->id) {
$this->layout()->setFlash("Technologie nicht gefunden", "error");
$this->redirect("Producttech");
}
} else {
$id = false;
$mode = "add";
}
$data = [];
$data['name'] = $r->name;
$data['customer_type'] = $r->customer_type;
$data['description'] = $r->description;
$data['note'] = $r->note;
$data['edit_by'] = $this->me->id;
if($mode == "add") {
$data['create_by'] = $this->me->id;
$producttech = ProducttechModel::create($data);
} else {
$producttech->update($data);
}
//var_dump($address);exit;
$new_id = $producttech->save();
if(!$new_id) {
$this->layout()->setFlash("Fehler beim Speichern", "error");
$this->layout()->set("producttech", $producttech);
return $this->add();
}
// handle ProductAttributes
if(is_array($r->attributes) && count($r->attributes)) {
foreach($r->attributes as $aid => $attribute) {
if(!trim($attribute['name']) || !trim($attribute['displayname'])) {
continue;
}
$a = [];
$a['name'] = $attribute['name'];
$a['producttech_id'] = $new_id;
$a['displayname'] = $attribute['displayname'];
$a['value'] = $attribute['value'];
$a['description'] = $attribute['description'];
$a['note'] = $attribute['note'];
$a['edit_by'] = $this->me->id;
if($aid == "new") {
$attrib = ProducttechAttributeModel::create($a);
} elseif(is_numeric($aid) && $aid > 0) {
$attrib = new ProducttechAttribute($aid);
$attrib->update($a);
} else {
continue;
}
$attrib->save();
}
}
$this->layout()->setFlash("Technologie erfolgreich gespeichert.", "success");
$this->redirect("Producttech", "edit", ['id' => $new_id]);
}
protected function delete() {
$id = $this->request->id;
$tech = new Producttech($id);
if(!$tech->id || $tech->id != $id) {
$this->layout()->setFlash("Technologie nicht gefunden.", "error");
$this->redirect("Producttech");
}
if(ProductModel::search(["producttech_id" => $tech->id])) {
$this->layout()->setFlash("Technologie kann nicht gelöscht werden, da sie in Verwendung ist.", "error");
$this->redirect("Producttech");
}
// check if Product is unused
$tech->delete();
$this->redirect("Producttech");
}
}
@@ -0,0 +1,23 @@
<?php
class ProducttechAttribute extends mfBaseModel {
private $producttech;
public function getProperty($name) {
if($this->$name == null) {
$classname = ucfirst($name);
$idfield = $name."_id";
$this->$name = new $classname($this->$idfield);
if($this->$name->id) {
return $this->$name;
} else {
return null;
}
}
return $this->$name;
}
}
@@ -0,0 +1,37 @@
<?php
class ProducttechAttributeController 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 deleteAction() {
$id = $this->request->id;
$ta = new ProducttechAttribute($id);
if(!$ta->id || $ta->id != $id) {
$this->layout()->setFlash("Attribut nicht gefunden.", "error");
$this->redirect("Producttech");
}
/*if(ProductModel::search(["producttech_id" => $tech->id])) {
$this->layout()->setFlash("Technologie kann nicht gelöscht werden, da sie in Verwendung ist.", "error");
$this->redirect("Producttech");
}*/
$producttech_id = $ta->producttech_id;
// check if Product is unused
$ta->delete();
$this->redirect("Producttech", "edit", ['id' => $producttech_id]);
}
}
@@ -0,0 +1,132 @@
<?php
class ProducttechAttributeModel {
public $producttech_id = null;
public $name = null;
public $displayname = null;
public $value = null;
public $description = null;
public $note = null;
public $create_by = null;
public $edit_by = null;
public $create = null;
public $edit = null;
public static function create(Array $data) {
$model = new ProducttechAttribute();
foreach($data as $field => $value) {
if(property_exists(get_called_class(), $field)) {
$model->$field = $value;
}
}
$me = new User();
$me->loadMe();
if($model->create_by === null) {
$model->create_by = $me->id;
}
if($model->edit_by === null) {
$model->edit_by = $me->id;
}
return $model;
}
public static function getOne($id) {
if(!is_numeric($id) || !$id) {
throw new Exception("Invalid number", 400);
}
$item = [];
$db = FronkDB::singleton();
$res = $db->select("ProducttechAttribute", "*", "id=$id LIMIT 1");
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new ProducttechAttribute($data);
}
return $item;
}
public static function getAll() {
$items = [];
$db = FronkDB::singleton();
$res = $db->select("ProducttechAttribute", "*", "1 = 1 ORDER BY name");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[$data->id] = new ProducttechAttribute($data);
}
}
return $items;
}
public static function getFirst() {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$res = $db->select("ProducttechAttribute", "*", "$where ORDER BY name");
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new ProducttechAttribute($data);
if($item->id) {
return $item;
} else {
return null;
}
}
return null;
}
public static function search($filter) {
$items = [];
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$res = $db->select("ProducttechAttribute", "*", "$where ORDER BY name");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[$data->id] = new ProducttechAttribute($data);
}
}
return $items;
}
private function getSqlFilter($filter) {
$where = "1=1 ";
$db = FronkDB::singleton();
//var_dump($filter);exit;
if(array_key_exists("name", $filter)) {
$name = $db->escape($filter['name']);
if($name) {
$where .= " AND `name` like '$name'";
}
}
if(array_key_exists("nameLike", $filter)) {
$name = $db->escape($filter['nameLike']);
if($name) {
$where .= " AND `name` like '%$name%'";
}
}
if(array_key_exists("producttech_id", $filter)) {
$producttech_id = $db->escape($filter['producttech_id']);
if(is_numeric($producttech_id)) {
$where .= " AND `producttech_id` = $producttech_id";
}
}
//var_dump($filter, $where);exit;
return $where;
}
}