From bbfc6c6eb5dc507b5589e1b5aa5304b45a034b7e Mon Sep 17 00:00:00 2001 From: Frank Schubert Date: Tue, 28 Jun 2022 13:25:37 +0200 Subject: [PATCH] AddessLink WIP --- Layout/default/Address/Form.php | 165 +++++++++++++++++++ Layout/default/Address/Index.php | 1 + application/Address/Address.php | 38 +++++ application/Address/AddressController.php | 26 ++- application/Address/AddressModel.php | 2 +- application/AddressLink/AddressLink.php | 32 ++++ application/AddressLink/AddressLinkModel.php | 131 +++++++++++++++ 7 files changed, 393 insertions(+), 2 deletions(-) create mode 100644 application/AddressLink/AddressLink.php create mode 100644 application/AddressLink/AddressLinkModel.php diff --git a/Layout/default/Address/Form.php b/Layout/default/Address/Form.php index e3d90dec8..f39af477d 100644 --- a/Layout/default/Address/Form.php +++ b/Layout/default/Address/Form.php @@ -256,6 +256,71 @@ +

Verknüpfte Adressen

+
+ +
+ +
@@ -344,6 +409,106 @@ return true; }); + + + + + /* + * link autocomplete + */ + + $('.link-autocomplete').autoComplete(); + $('.link-autocomplete').keydown(function() { + if(event.keyCode == 13) { + event.preventDefault(); + return false; + } + }); + + /* + * Links autocomplete + */ + $('.link-autocomplete').on("autocomplete.select", function(evt, item) { + autocompleteSelect(evt,item); + }); + + function autocompleteSelect(evt, item) { + if(item && item.value === 0) { + $('.link-autocomplete').autoComplete('set', null); + return; + } + + var match = evt.currentTarget.id.match(/links_(\d+)_address_id/); + var link_num = match[1]; + if(!link_num) { + console.log("Couldn't find selected Address"); + } + addLink(Number(link_num) + 1); + } + + function addLink(linknum) { + if(!linknum) { + console.log("no linknum"); + return false; + } + + if($("#links_" + linknum + "_address_id").length) { + console.log("gibs scho"); + return false; + } + + var new_link = ''; + + $('#link-container').append(new_link); + //$('#links_' + linknum + '_address_id').autocomplete(); + $('#links_' + linknum + '_address_id').autoComplete(); + $('#links_' + linknum + '_address_id').keydown(function() { + if(event.keyCode == 13) { + event.preventDefault(); + return false; + } + }); + + $('#links_' + linknum + '_address_id').on("autocomplete.select", function(evt, item) { + autocompleteSelect(evt,item); + }); + } + + function clearNewLink(linknum) { + if(!$("#link-" + linknum).length) { + return; + + } + + // only remove is not the only one + if($('#link-container').find('div.row').length > 1) { + $("#link-" + linknum).remove(); + } + + } + + + + + + \ No newline at end of file diff --git a/Layout/default/Address/Index.php b/Layout/default/Address/Index.php index 6d06432ca..cdce20d66 100644 --- a/Layout/default/Address/Index.php +++ b/Layout/default/Address/Index.php @@ -58,6 +58,7 @@ +
diff --git a/application/Address/Address.php b/application/Address/Address.php index 806fa6983..1db2506d3 100644 --- a/application/Address/Address.php +++ b/application/Address/Address.php @@ -4,6 +4,7 @@ class Address extends mfBaseModel { protected $forcestr = ['street','company','zip','phone','fax','mobile','note']; private $parent; private $childaddresses; + private $links; private $types; private $attributes; private $permissions; @@ -128,6 +129,19 @@ class Address extends mfBaseModel { return $spin; } + public function deleteLinks() { + $links = $this->getProperty("links"); + //var_dump($links);exit; + if(is_array($links) && count($links)) { + foreach($links as $type => $linktypes) { + //var_dump($type, $linktypes);exit; + foreach($linktypes as $link) { + //var_dump($link);exit; + $link->delete(); + } + } + } + } public function getProperty($name) { if($this->$name == null) { @@ -166,6 +180,30 @@ class Address extends mfBaseModel { $this->childaddresses = AddressModel::search(['parent' => $this->id]); return $this->childaddresses; } + /* + if($name == "links_to") { + $links = AddressLinkModel::search(['address_id' => $this->id]); + foreach($links as $link) { + if(!array_key_exists($link->type, $this->links_to)) { + $this->links_to[$link->type] = []; + } + $this->links_to[$link->type][] = $link->linked_to; + } + return $this->links_to; + }*/ + + if($name == "links") { + $links = AddressLinkModel::search(['origin_address_id' => $this->id]); + //var_dump($links);exit; + foreach($links as $link) { + if(!array_key_exists($link->type, $this->links)) { + $this->links[$link->type] = []; + } + $this->links[$link->type][] = $link; + //var_dump($this->links);exit; + } + return $this->links; + } $classname = ucfirst($name); $idfield = $name."_id"; diff --git a/application/Address/AddressController.php b/application/Address/AddressController.php index 951afb2ec..affa50a93 100644 --- a/application/Address/AddressController.php +++ b/application/Address/AddressController.php @@ -104,7 +104,7 @@ class AddressController extends mfBaseController { protected function saveAction() { $r = $this->request; $id = $r->id; - //var_dump($r);exit; + //var_dump($r->get());exit; if(is_numeric($id) && $id > 0) { $mode = "edit"; $address = new Address($id); @@ -228,6 +228,24 @@ class AddressController extends mfBaseController { } } + + $address->deleteLinks(); + //var_dump($r->links);exit; + if(is_array($r->links) && count($r->links)) { + //var_dump($r->links);exit; + foreach($r->links as $linknum => $link) { + if(!$link['type'] || !$link['address_id']) { + continue; + } + $l = AddressLinkModel::create([ + 'origin_address_id' => $new_id, + 'type' => $link['type'], + 'address_id' => $link['address_id'] + ]); + $l->save(); + } + } + $this->layout()->setFlash("Adresse erfolgreich gespeichert.", "success"); $this->redirect("Address", "Edit", ['id' => $new_id]); } @@ -287,6 +305,12 @@ class AddressController extends mfBaseController { $role = "billing"; $po = 0; } + if($this->request->role == "techcontact") { + $role = "techcontact"; + $po = 0; + } + + $this->log->debug(print_r($this->request->get(),true)); $addresses = []; diff --git a/application/Address/AddressModel.php b/application/Address/AddressModel.php index 60fd32f74..079ab5609 100644 --- a/application/Address/AddressModel.php +++ b/application/Address/AddressModel.php @@ -66,7 +66,7 @@ class AddressModel { $res = $db->select("Address", "*", "$where ORDER BY company, lastname, firstname, zip, city LIMIT 1"); if($db->num_rows($res)) { $data = $db->fetch_object($res); - $item = new Voicenumber($data); + $item = new Address($data); if($item->id) { return $item; } else { diff --git a/application/AddressLink/AddressLink.php b/application/AddressLink/AddressLink.php new file mode 100644 index 000000000..97c29b8bb --- /dev/null +++ b/application/AddressLink/AddressLink.php @@ -0,0 +1,32 @@ +$name == null) { + + if(!$this->id) { + return null; + } + + if($name == "origin") { + $this->origin = new Address($this->origin_address_id); + return $this->origin; + } + + $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; + } +} \ No newline at end of file diff --git a/application/AddressLink/AddressLinkModel.php b/application/AddressLink/AddressLinkModel.php new file mode 100644 index 000000000..a91f55612 --- /dev/null +++ b/application/AddressLink/AddressLinkModel.php @@ -0,0 +1,131 @@ + $value) { + if(property_exists(get_called_class(), $field)) { + $model->$field = $value; + } + } + + $me = new User(); + $me->loadMe(); + + if(!is_numeric($model->create_by) && !$model->create_by) { + $model->create_by = $me->id; + } + if(!is_numeric($model->edit_by) && !$model->edit_by) { + $model->edit_by = $me->id; + } + + return $model; + } + + public static function getFirst($filter = null) { + $db = FronkDB::singleton(); + + $where = self::getSqlFilter($filter); + $res = $db->select("AddressLink", "*", "$where ORDER BY address_id,`create`,origin_address_idLIMIT 1"); + if($db->num_rows($res)) { + $data = $db->fetch_object($res); + $item = new AddressLink($data); + if($item->id) { + return $item; + } else { + return null; + } + } + return null; + } + + public static function getAll() { + $items = []; + + $db = FronkDB::singleton(); + + $res = $db->select("AddressLink", "*", "1=1 ORDER BY address_id,`create`,origin_address_id"); + if($db->num_rows($res)) { + while($data = $db->fetch_object($res)) { + $items[] = new AddressLink($data); + } + } + return $items; + + } + + public static function count($filter) { + $db = FronkDB::singleton(); + + $where = self::getSqlFilter($filter); + $res = $db->select("AddressLink", "COUNT(*) as cnt", "$where"); + + //$res = $db->query($sql); + if($db->num_rows($res)) { + $data = $db->fetch_object($res); + return $data->cnt; + } + return 0; + } + + + public static function search($filter, $limit = false) { + $items = []; + $db = FronkDB::singleton(); + + $where = self::getSqlFilter($filter); + + $sql = "SELECT * FROM AddressLink WHERE $where ORDER BY address_id,`create`,origin_address_id"; + + if(is_array($limit) && count($limit)) { + if(is_numeric($limit['start']) && is_numeric($limit['count'])) { + $sql .= " LIMIT ".$limit['start'].", ".$limit['count']; + } elseif(is_numeric($count)) { + $sql .= " LIMIT ".$limit['count']; + } + } + + mfLoghandler::singleton()->debug($sql); + + $res = $db->query($sql); + if($db->num_rows($res)) { + while($data = $db->fetch_object($res)) { + $items[] = new AddressLink($data); + } + } + + return $items; + } + + private function getSqlFilter($filter) { + $where = "1=1 "; + + //var_dump($filter);exit; + if(array_key_exists("address_id", $filter)) { + $address_id = $filter["address_id"]; + if(is_numeric($address_id)) { + $where .= " AND address_id=$address_id"; + } + } + + if(array_key_exists("origin_address_id", $filter)) { + $origin_address_id = $filter["origin_address_id"]; + if(is_numeric($origin_address_id)) { + $where .= " AND origin_address_id=$origin_address_id"; + } + } + + return $where; + } + +} \ No newline at end of file