Completed
Push — master ( e4ab2b...e8d46a )
by Franco
24s queued 18s
created

DMSDocumentTaxonomyExtension::getAllTagsMap()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 13
nc 3
nop 0
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(
0 ignored issues
show
Unused Code introduced by
The property $many_many is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
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