]> git.agnieray.net Git - galette.git/commitdiff
Propose existing types on document form, not only system ones
authorJohan Cwiklinski <johan@x-tnd.be>
Sun, 14 Apr 2024 05:39:39 +0000 (07:39 +0200)
committerJohan Cwiklinski <johan@x-tnd.be>
Sun, 14 Apr 2024 05:39:39 +0000 (07:39 +0200)
closes #1813

galette/lib/Galette/Entity/Document.php
galette/templates/default/pages/document_form.html.twig
tests/Galette/Entity/tests/units/Document.php

index 1a8f4aacac11b712ad52796139fee8b8cf30d117..ff7c179809fda6a8de442102f7933e6863fc9683 100644 (file)
@@ -495,6 +495,29 @@ class Document implements FileInterface
         return $this->getSystemTypes($translated)[$type] ?? _T($type);
     }
 
+    /**
+     * Get all known types
+     *
+     * @return array<string,string>
+     *
+     * @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
      *
index 47c4fb0bf3b8ea67ea52dd69eacd331798aa387b..6fb0c4eea39033c5cacf0c86ee5a000e4cb0cf84 100644 (file)
@@ -50,8 +50,8 @@
                     <i class="jsonly displaynone dropdown icon" aria-hidden="true"></i>
                     <div class="jsonly displaynone default text">{{ _T("Choose or enter your own...") }}</div>
                     <div class="jsonly displaynone menu">
-                        {% for doc_type in document.getSystemTypes(false) %}
-                            <div class="item" data-value="{{ doc_type }}">{{ document.getSystemType(doc_type) }}</div>
+                        {% for doc_type, tdoc_type in document.getTypes() %}
+                            <div class="item" data-value="{{ doc_type }}">{{ tdoc_type }}</div>
                         {% endfor %}
                     </div>
                 </div>
index 7ae9b9371d477ebca56d10f660cf20dcee0f2904..438cfc4c2adbdedf94f7948156bddfe93ee1b9b8 100644 (file)
@@ -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());
+    }
 }