Completed
Push — dev ( 6f87eb...29b8e3 )
by Nicolas
01:54
created

FieldRelationship   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
wmc 19
lcom 2
cbo 0
dl 0
loc 130
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getInt() 0 4 1
A is() 0 4 1
A isRequired() 0 4 1
A getEntries() 0 4 1
A createCheckbox() 0 11 2
A createFieldName() 0 8 2
A createSettingsFieldName() 0 4 1
A createPublishFieldName() 0 4 1
A getSelectedSectionsArray() 0 13 4
B createEntriesList() 0 25 5
1
<?php
2
/**
3
 * Copyright: Deux Huit Huit 2017
4
 * LICENCE: MIT https://deuxhuithuit.mit-license.org
5
 */
6
7
if (!defined('__IN_SYMPHONY__')) die('<h2>Symphony Error</h2><p>You cannot directly access this file</p>');
8
9
require_once(TOOLKIT . '/class.field.php');
10
11
/**
12
 *
13
 * Base Field class that will represent a relationship between entries
14
 * @author Deux Huit Huit
15
 *
16
 */
17
class FieldRelationship extends Field
18
{
19
    /**
20
     * 
21
     * Separator char for values
22
     *  @var string
23
     */
24
    const SEPARATOR = ',';
25
26
    protected $orderable = false;
27
28
    /**
29
     * @param string $name
30
     */
31
    public function getInt($name)
32
    {
33
        return General::intval($this->get($name));
34
    }
35
36
    /**
37
     * Check if a given property is == 'yes'
38
     * @param string $name
39
     * @return bool
40
     *  True if the current field's value is 'yes'
41
     */
42
    public function is($name)
43
    {
44
        return $this->get($name) == 'yes';
45
    }
46
47
    /**
48
     * @return bool
49
     *  True if the current field is required
50
     */
51
    public function isRequired()
52
    {
53
        return $this->is('required');
54
    }
55
56
    public static function getEntries(array $data)
57
    {
58
        return array_map(array('General', 'intval'), array_filter(array_map(trim, explode(self::SEPARATOR, $data['entries']))));
59
    }
60
61
    /**
62
     * @param string $fieldName
63
     * @param string $text
64
     */
65
    protected function createCheckbox($fieldName, $text) {
66
        $chk = Widget::Label();
67
        $chk->setAttribute('class', 'column');
68
        $attrs = null;
69
        if ($this->get($fieldName) == 'yes') {
70
            $attrs = array('checked' => 'checked');
71
        }
72
        $chk->appendChild(Widget::Input($this->createSettingsFieldName($fieldName), 'yes', 'checkbox', $attrs));
73
        $chk->setValue(__($text));
74
        return $chk;
75
    }
76
77
    /**
78
     * @param string $prefix
79
     * @param string $name
80
     * @param @optional bool $multiple
81
     */
82
    protected function createFieldName($prefix, $name, $multiple = false)
83
    {
84
        $name = "fields[$prefix][$name]";
85
        if ($multiple) {
86
            $name .= '[]';
87
        }
88
        return $name;
89
    }
90
91
    /**
92
     * @param string $name
93
     */
94
    protected function createSettingsFieldName($name, $multiple = false)
95
    {
96
        return $this->createFieldName($this->get('sortorder'), $name, $multiple);
97
    }
98
99
    /**
100
     * @param string $name
101
     */
102
    protected function createPublishFieldName($name, $multiple = false)
103
    {
104
        return $this->createFieldName($this->get('element_name'), $name, $multiple);
105
    }
106
107
    protected function getSelectedSectionsArray()
108
    {
109
        $selectedSections = $this->get('sections');
110
        if (!is_array($selectedSections)) {
111
            if (is_string($selectedSections) && strlen($selectedSections) > 0) {
112
                $selectedSections = explode(self::SEPARATOR, $selectedSections);
113
            }
114
            else {
115
                $selectedSections = array();
116
            }
117
        }
118
        return $selectedSections;
119
    }
120
121
    protected function createEntriesList($entries)
122
    {
123
        $wrap = new XMLElement('div');
124
        $wrapperClass = 'frame collapsible';
125
        if (count($entries) == 0) {
126
            $wrapperClass .= ' empty';
127
        }
128
        if (!$this->is('show_header')) {
129
            $wrapperClass .= ' no-header';
130
        }
131
        if ($this->orderable) {
132
            $wrapperClass .= ' orderable';
133
        }
134
        $wrap->setAttribute('class', $wrapperClass);
135
        
136
        $list = new XMLElement('ul');
137
        $list->setAttribute('class', '');
138
        if ($this->is('allow_collapse')) {
139
            $list->setAttribute('data-collapsible', '');
140
        }
141
        
142
        $wrap->appendChild($list);
143
        
144
        return $wrap;
145
    }
146
}
147