From: Johan Cwiklinski Date: Sun, 14 Apr 2024 05:39:39 +0000 (+0200) Subject: Propose existing types on document form, not only system ones X-Git-Url: https://git.agnieray.net/?a=commitdiff_plain;h=f7cb86fad14e3fdc19b5540230c1bba832bbe1cc;p=galette.git Propose existing types on document form, not only system ones closes #1813 --- diff --git a/galette/lib/Galette/Entity/Document.php b/galette/lib/Galette/Entity/Document.php index 1a8f4aaca..ff7c17980 100644 --- a/galette/lib/Galette/Entity/Document.php +++ b/galette/lib/Galette/Entity/Document.php @@ -495,6 +495,29 @@ class Document implements FileInterface return $this->getSystemTypes($translated)[$type] ?? _T($type); } + /** + * Get all known types + * + * @return array + * + * @throws Throwable + */ + public function getTypes(): array + { + $types = $this->getSystemTypes(); + + $select = $this->zdb->select(self::TABLE); + $select->quantifier('DISTINCT'); + $select->where->notIn('type', array_keys($this->getSystemTypes(false))); + $results = $this->zdb->execute($select); + + foreach ($results as $r) { + $types[$r->type] = $r->type; + } + + return $types; + } + /** * Handle files * diff --git a/galette/templates/default/pages/document_form.html.twig b/galette/templates/default/pages/document_form.html.twig index 47c4fb0bf..6fb0c4eea 100644 --- a/galette/templates/default/pages/document_form.html.twig +++ b/galette/templates/default/pages/document_form.html.twig @@ -50,8 +50,8 @@
{{ _T("Choose or enter your own...") }}
diff --git a/tests/Galette/Entity/tests/units/Document.php b/tests/Galette/Entity/tests/units/Document.php index 7ae9b9371..438cfc4c2 100644 --- a/tests/Galette/Entity/tests/units/Document.php +++ b/tests/Galette/Entity/tests/units/Document.php @@ -305,4 +305,38 @@ class Document extends GaletteTestCase $tlist = $document->getTypedList(); $this->assertCount(1, $tlist); } + + /** + * Test getTypes + * + * @return void + */ + public function testGetTypes(): void + { + $document = new \Galette\Entity\Document($this->zdb); + + //per default, retrieve ony system types + $list_types = $document->getSystemTypes(); + $this->assertSame($list_types, $document->getTypes()); + + //create a new type + $document = $this->getDocumentInstance(); + $_FILES['document_file'] = [ + 'error' => UPLOAD_ERR_OK, + 'name' => 'afile.pdf', + 'tmp_name' => '/tmp/afile.pdf', + 'size' => 4096 + ]; + $post = [ + 'document_type' => 'An other document type', + 'comment' => '', + 'visible' => \Galette\Entity\FieldsConfig::STAFF + ]; + + $this->assertTrue($document->store($post, $_FILES)); + + //check new type is present from getTypes() method + $list_types['An other document type'] = 'An other document type'; + $this->assertSame($list_types, $document->getTypes()); + } }