From 2286d47b693348f9488846bfabac9ae090b9b7a0 Mon Sep 17 00:00:00 2001 From: Paolo Date: Tue, 16 May 2017 18:52:38 +0200 Subject: [PATCH] Task #1353: First solution, to be refined --- .../libraries/OrganisationseinheitLib.php | 49 +++++++++++++++++++ .../Organisationseinheit_model.php | 31 ++++++++++-- application/widgets/vorlage_widget.php | 24 +++++++-- 3 files changed, 97 insertions(+), 7 deletions(-) diff --git a/application/libraries/OrganisationseinheitLib.php b/application/libraries/OrganisationseinheitLib.php index be66e2157..7f40b29dd 100644 --- a/application/libraries/OrganisationseinheitLib.php +++ b/application/libraries/OrganisationseinheitLib.php @@ -59,7 +59,56 @@ class OrganisationseinheitLib return $this->treeSearch($schema, $table, $select, $where, $orderby, $result->retval[0]->_ppk); } } + + return $result; + } + + public function treeSearchEntire($table, $alias, $fields, $where, $orderby, $oe_kurzbz, $entireTree = false) + { + $select = ""; + if (is_array($fields)) + { + for ($i = 0; $i < count($fields); $i++) + { + $select .= $fields[$i]; + if ($i != count($fields) - 1) + { + $select .= ", "; + } + } + } + else + { + $select = $fields; + } + $result = $this->ci->OrganisationseinheitModel->getOneLevel2($table, $alias, $select, $where, $orderby, $oe_kurzbz); + + if (hasData($result)) + { + if ($result->retval[0]->_pk != null && $result->retval[0]->_ppk != null) + { + $tmpResult = error(); + + if (($entireTree == false && $result->retval[0]->oe_kurzbz == null) || $entireTree === true) + { + $tmpResult = $this->treeSearchEntire($table, $alias, $select, $where, $orderby, $result->retval[0]->_ppk, $entireTree); + } + + if ($entireTree === true) + { + if (hasData($tmpResult)) + { + $result->retval = array_merge($result->retval, $tmpResult->retval); + } + } + } + else if ($result->retval[0]->_pk == null && $result->retval[0]->_ppk != null) + { + $result = $this->treeSearchEntire($table, $alias, $select, $where, $orderby, $result->retval[0]->_ppk, $entireTree); + } + } + return $result; } } \ No newline at end of file diff --git a/application/models/organisation/Organisationseinheit_model.php b/application/models/organisation/Organisationseinheit_model.php index ccab383cd..22a221c92 100755 --- a/application/models/organisation/Organisationseinheit_model.php +++ b/application/models/organisation/Organisationseinheit_model.php @@ -78,9 +78,34 @@ class Organisationseinheit_model extends DB_Model ) _joined_table ON (orgs._pk = _joined_table._pk) WHERE orgs._pk = ? ORDER BY %s"; - + $query = sprintf($query, $table, $fields, $schema, $table, $where, $orderby); - + return $this->execQuery($query, array($oe_kurzbz)); } -} + + public function getOneLevel2($table, $alias, $fields, $where, $orderby, $oe_kurzbz) + { + $query = "WITH RECURSIVE organizations(_pk, _ppk) AS + ( + SELECT o.oe_kurzbz, o.oe_parent_kurzbz + FROM public.tbl_organisationseinheit o + WHERE o.oe_parent_kurzbz IS NULL + UNION ALL + SELECT o.oe_kurzbz, o.oe_parent_kurzbz + FROM public.tbl_organisationseinheit o INNER JOIN organizations orgs ON (o.oe_parent_kurzbz = orgs._pk) + ) + SELECT orgs._pk, orgs._ppk, _joined_table.* + FROM organizations orgs LEFT JOIN ( + SELECT %s.oe_kurzbz as _pk, %s + FROM %s + WHERE %s + ) _joined_table ON (orgs._pk = _joined_table._pk) + WHERE orgs._pk = ? + ORDER BY %s"; + + $query = sprintf($query, $alias, $fields, $table, $where, $orderby); + + return $this->execQuery($query, array($oe_kurzbz)); + } +} \ No newline at end of file diff --git a/application/widgets/vorlage_widget.php b/application/widgets/vorlage_widget.php index a720222b1..a7664a122 100644 --- a/application/widgets/vorlage_widget.php +++ b/application/widgets/vorlage_widget.php @@ -4,13 +4,29 @@ class Vorlage_widget extends DropdownWidget { public function display($widgetData) { - $this->load->model('system/Vorlage_model', 'VorlageModel'); - $this->VorlageModel->addOrder('vorlage_kurzbz'); + // Loads + $this->load->library('OrganisationseinheitLib'); - $this->addSelectToModel($this->VorlageModel, 'vorlage_kurzbz', 'bezeichnung'); + $vorlage = $this->organisationseinheitlib->treeSearchEntire( + '( + SELECT v.vorlage_kurzbz, v.bezeichnung, vs.version, vs.oe_kurzbz, vs.aktiv, vs.subject, vs.text, v.mimetype + FROM tbl_vorlagestudiengang vs INNER JOIN tbl_vorlage v USING(vorlage_kurzbz) + ) templates', + 'templates', + array("templates.vorlage_kurzbz AS id", "UPPER(templates.oe_kurzbz) || ' - ' || templates.bezeichnung || ' - V' || templates.version AS description"), + 'templates.aktiv = TRUE + AND templates.subject IS NOT NULL + AND templates.text IS NOT NULL + AND templates.mimetype = \'text/html\'', + "description ASC", + 'eas', + true + ); + + array_pop($vorlage->retval); $this->setElementsArray( - $this->VorlageModel->loadWhere(array('mimetype' => 'text/html')), + $vorlage, true, 'Select a vorlage...', 'No vorlage found'