From 1c6acca834e04e3ca4e627d946a0d3bcecb9eb48 Mon Sep 17 00:00:00 2001 From: Frank Schubert Date: Thu, 24 Jun 2021 20:07:02 +0200 Subject: [PATCH] Finished Addressattribute --- Layout/default/Address/Form.php | 14 +- application/Address/Address.php | 13 +- application/Address/AddressController.php | 18 ++- .../Addressattribute/Addressattribute.php | 5 + .../AddressattributeModel.php | 124 ++++++++++++++++++ 5 files changed, 169 insertions(+), 5 deletions(-) create mode 100644 application/Addressattribute/Addressattribute.php create mode 100644 application/Addressattribute/AddressattributeModel.php diff --git a/Layout/default/Address/Form.php b/Layout/default/Address/Form.php index 23464d699..4b5629659 100644 --- a/Layout/default/Address/Form.php +++ b/Layout/default/Address/Form.php @@ -25,7 +25,7 @@
-

id) ? "Person/Firma bearbeiten" : "Neue Person/Firma"?>

+

id) ? "Person/Firma bearbeiten" : "Neue Person/Firma"?>

">
@@ -141,6 +141,18 @@
+ +

Zusatzdaten

+
+
+
+ +
+ +
+
+
+
diff --git a/application/Address/Address.php b/application/Address/Address.php index 73de8e2ac..46a98a5bc 100644 --- a/application/Address/Address.php +++ b/application/Address/Address.php @@ -3,7 +3,7 @@ class Address extends mfBaseModel { protected $forcestr = ['company','zip','phone','fax','mobile','note']; private $types; - + private $attributes; public function getFullName() { // Assumes "Firma1 Firma2" or "firstname lastname" as readable form @@ -44,6 +44,17 @@ class Address extends mfBaseModel { return $this->types; } + if($name == "attributes") { + $attribs = AddressattributeModel::search(['address_id' => $this->id]); + if(count($attribs)) { + $this->attributes = []; + foreach($attribs as $a) { + $this->attributes[$a->name] = $a; + } + } + return $this->attributes; + } + $classname = ucfirst($name); $idfield = $name."_id"; $this->$name = new $classname($this->$idfield); diff --git a/application/Address/AddressController.php b/application/Address/AddressController.php index 3b06b4efb..9d1e4ba07 100644 --- a/application/Address/AddressController.php +++ b/application/Address/AddressController.php @@ -42,7 +42,7 @@ class AddressController extends mfBaseController { protected function saveAction() { $r = $this->request; $id = $r->id; - + //var_dump($r); if(is_numeric($id) && $id > 0) { $mode = "edit"; $address = new Address($id); @@ -94,7 +94,7 @@ class AddressController extends mfBaseController { foreach($address->types as $existing_type) { //var_dump($existing_type); //var_dump($new_types); - echo $existing_type->type; + //echo $existing_type->type; if(!in_array($existing_type->type, $new_types)) { $existing_type->delete(); } else { @@ -108,7 +108,19 @@ class AddressController extends mfBaseController { $type_object = AddresstypeModel::create(['address_id' => $address->id, 'type' => $type]); $type_object->save(); $address->types[$type] = $type_object; - + } + } + + $attribs = $r->attributes; + //var_dump($attribs);exit; + if(is_array($attribs) && count($attribs)) { + foreach($attribs as $atttrib => $value) { + $aa = AddressattributeModel::getFirst(["address_id" => $new_id, "name" => $attrib]); + if(!$aa) { + $aa = AddressattributeModel::create(["address_id" => $new_id, "name" => $attrib]); + } + $aa->value = $value; + $aa->save(); } } diff --git a/application/Addressattribute/Addressattribute.php b/application/Addressattribute/Addressattribute.php new file mode 100644 index 000000000..cf12fae9e --- /dev/null +++ b/application/Addressattribute/Addressattribute.php @@ -0,0 +1,5 @@ + $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($idOrFilter) { + if(!is_numeric($id) || !$id) { + throw new Exception("Invalid number", 400); + } + $item = []; + $db = FronkDB::singleton(); + + $res = $db->select("Addressattribute", "*", "id=$id LIMIT 1"); + if($db->num_rows($res)) { + $data = $db->fetch_object($res); + $item = new Addressattribute($data); + } + return $item; + } + + public static function getAll($filter = false) { + $items = []; + $db = FronkDB::singleton(); + + $res = $db->select("Addressattribute", "*", "1=1 ORDER BY address_id ASC, name"); + if($db->num_rows($res)) { + while($data = $db->fetch_object($res)) { + $items[] = new Addressattribute($data); + } + } + return $items; + } + + public static function getFirst($filter) { + $db = FronkDB::singleton(); + $where = self::getSqlFilter($filter); + $sql = "SELECT Addressattribute.* FROM Addressattribute + WHERE $where + ORDER BY address_id ASC, name"; + //var_dump($sql); + $res = $db->query($sql); + if($db->num_rows($res)) { + $data = $db->fetch_object($res); + $item = new Addressattribute($data); + if($item->id) { + return $item; + } else { + return null; + } + } else { + return null; + } + return $items; + } + + public static function search($filter) { + $items = []; + $db = FronkDB::singleton(); + + $where = self::getSqlFilter($filter); + $sql = "SELECT Addressattribute.* FROM Addressattribute + WHERE $where + ORDER BY address_id ASC, name"; + //var_dump($sql); + $res = $db->query($sql); + if($db->num_rows($res)) { + while($data = $db->fetch_object($res)) { + $items[] = new Addressattribute($data); + } + } + return $items; + } + + private function getSqlFilter($filter) { + $where = "1=1 "; + + if(array_key_exists("address_id", $filter)) { + $address_id = $filter['address_id']; + if(!is_numeric($address_id) || !$address_id) { + return false; + } + + $where .= " AND address_id=$address_id"; + } + + if(array_key_exists("name", $filter)) { + $name = FronkDB::singleton()->escape($filter['name']); + if($name) { + $where .= " AND `name`='$name'"; + } + } + + return $where; + } +} \ No newline at end of file