Completed
Pull Request — master (#168)
by Franco
02:41
created

code/extensions/DMSDocumentTaxonomyExtension.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
class DMSDocumentTaxonomyExtension extends DataExtension
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
    private static $many_many = array(
6
        'Tags' => 'TaxonomyTerm'
7
    );
8
9
    /**
10
     * Push an autocomplete dropdown for the available tags in documents
11
     *
12
     * @param FieldList $fields
13
     */
14
    public function updateCMSFields(FieldList $fields)
15
    {
16
        $tags = $this->getAllTagsMap();
17
        $tagField = ListboxField::create('Tags', _t('DMSDocumentTaxonomyExtension.TAGS', 'Tags'))
18
            ->setMultiple(true)
19
            ->setSource($tags);
20
21
        if (empty($tags)) {
22
            $tagField->setAttribute('data-placeholder', _t('DMSDocumentTaxonomyExtension.NOTAGS', 'No tags found'));
23
        }
24
25
        $fields->insertAfter('Description', $tagField);
26
    }
27
28
    /**
29
     * Return an array of all the available tags that a document can use. Will return a list containing a taxonomy
30
     * term's entire hierarchy, e.g. "Photo > Attribute > Density > High"
31
     *
32
     * @return array
33
     */
34
    public function getAllTagsMap()
35
    {
36
        $tags = TaxonomyTerm::get()->filter(
37
            'Type.Name:ExactMatch',
38
            Config::inst()->get('DMSTaxonomyTypeExtension', 'default_record_name')
39
        );
40
41
        $map = array();
42
        foreach ($tags as $tag) {
43
            $nameParts = array($tag->Name);
44
            $currentTag = $tag;
45
46
            while ($currentTag->Parent() && $currentTag->Parent()->exists()) {
47
                array_unshift($nameParts, $currentTag->Parent()->Name);
48
                $currentTag = $currentTag->Parent();
49
            }
50
51
            $map[$tag->ID] = implode(' > ', $nameParts);
52
        }
53
        return $map;
54
    }
55
}
56