Finished Addressattribute

This commit is contained in:
Frank Schubert
2021-06-24 20:07:02 +02:00
parent 1b4226f762
commit 1c6acca834
5 changed files with 169 additions and 5 deletions

View File

@@ -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);

View File

@@ -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();
}
}

View File

@@ -0,0 +1,5 @@
<?php
class Addressattribute extends mfBaseModel {
}

View File

@@ -0,0 +1,124 @@
<?php
class AddressattributeModel {
public $address_id = null;
public $name = 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 Addressattribute();
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($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;
}
}