Test Failed
Push — master ( 357bf5...71cdad )
by Russell
03:53
created

ExternalTreeDropdownField   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 4
eloc 9
c 2
b 1
f 1
dl 0
loc 45
rs 10
1
<?php
2
3
use SilverStripe\Forms\TreeDropdownField;
4
use SilverStripe\ORM\DataObject;
5
use SilverStripe\Core\Convert;
6
7
class ExternalTreeDropdownField extends TreeDropdownField
8
{
9
    /**
10
     * Should children be selectable when this tree is shown
11
     *
12
     * @var boolean
13
     */
14
    protected $showChildren;
15
16
    /**
17
     * @param string $name the field name
18
     * @param string $title the field label
19
     * @param string $souceClass the class to display in the tree, must have the "Hierachy" extension.
20
     * @param string $keyField to field on the source class to save as the field value (default ID).
21
     * @param string $labelField the field name to show as the human-readable value on the tree (default Title).
22
     * @param boolean $showChildren whether children should be selectable
23
     */
24
    public function __construct($name, $title = null, $sourceObject = 'Group', $keyField = 'ID', $labelField = 'Title', $showChildren=true)
25
    {
26
        parent::__construct($name, $title, $sourceObject, $keyField, $labelField);
27
        $this->showChildren = $showChildren;
28
    }
29
30
    /**
31
     * Override to allow for compound IDs
32
     *
33
     * @param mixed $ID
34
     */
35
    public function setTreeBaseID($ID)
36
    {
37
        $this->baseID = $ID;
38
    }
39
40
    /**
41
     * Get the object where the $keyField is equal to a certain value
42
     *
43
     * @param string|int $key
44
     * @return DataObject
45
     */
46
    protected function objectForKey($key)
47
    {
48
        if ($this->keyField == 'ID') {
49
            return ExternalContent::getDataObjectFor($key);
50
        } else {
51
            return DataObject::get_one($this->sourceObject, "\"{$this->keyField}\" = '" . Convert::raw2sql($key) . "'");
52
        }
53
    }
54
}
55