Completed
Push — master ( 2867c5...18d669 )
by Nicolas
08:20 queued 04:11
created

FieldRelationship::formatCount()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
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
    public function getArray($name)
48
    {
49
        return array_filter(array_map(trim, explode(self::SEPARATOR, trim($this->get($name)))));
50
    }
51
52
    /**
53
     * @return bool
54
     *  True if the current field is required
55
     */
56
    public function isRequired()
57
    {
58
        return $this->is('required');
59
    }
60
61
    public static function getEntries(array $data)
62
    {
63
        return array_map(array('General', 'intval'), array_filter(array_map(trim, explode(self::SEPARATOR, $data['entries']))));
64
    }
65
66
    /**
67
     * @param string $fieldName
68
     * @param string $text
69
     */
70
    protected function createCheckbox($fieldName, $text) {
71
        $chk = Widget::Label();
72
        $chk->setAttribute('class', 'column');
73
        $attrs = null;
74
        if ($this->get($fieldName) == 'yes') {
75
            $attrs = array('checked' => 'checked');
76
        }
77
        $chk->appendChild(Widget::Input($this->createSettingsFieldName($fieldName), 'yes', 'checkbox', $attrs));
78
        $chk->setValue(__($text));
79
        return $chk;
80
    }
81
82
    /**
83
     * @param string $prefix
84
     * @param string $name
85
     * @param @optional bool $multiple
86
     */
87
    protected function createFieldName($prefix, $name, $multiple = false)
88
    {
89
        $name = "fields[$prefix][$name]";
90
        if ($multiple) {
91
            $name .= '[]';
92
        }
93
        return $name;
94
    }
95
96
    /**
97
     * @param string $name
98
     */
99
    protected function createSettingsFieldName($name, $multiple = false)
100
    {
101
        return $this->createFieldName($this->get('sortorder'), $name, $multiple);
102
    }
103
104
    /**
105
     * @param string $name
106
     */
107
    protected function createPublishFieldName($name, $multiple = false)
108
    {
109
        return $this->createFieldName($this->get('element_name'), $name, $multiple);
110
    }
111
112
    protected function getSelectedSectionsArray()
113
    {
114
        $selectedSections = $this->get('sections');
115
        if (!is_array($selectedSections)) {
116
            if (is_string($selectedSections) && strlen($selectedSections) > 0) {
117
                $selectedSections = explode(self::SEPARATOR, $selectedSections);
118
            }
119
            else {
120
                $selectedSections = array();
121
            }
122
        }
123
        return $selectedSections;
124
    }
125
126
    protected function createEntriesList($entries)
127
    {
128
        $wrap = new XMLElement('div');
129
        $wrapperClass = 'frame collapsible';
130
        if (count($entries) == 0) {
131
            $wrapperClass .= ' empty';
132
        }
133
        if (!$this->is('show_header')) {
134
            $wrapperClass .= ' no-header';
135
        }
136
        if ($this->orderable) {
137
            $wrapperClass .= ' orderable';
138
        }
139
        $wrap->setAttribute('class', $wrapperClass);
140
        
141
        $list = new XMLElement('ul');
142
        $list->setAttribute('class', '');
143
        if ($this->is('allow_collapse')) {
144
            $list->setAttribute('data-collapsible', '');
145
        }
146
        
147
        $wrap->appendChild($list);
148
        
149
        return $wrap;
150
    }
151
152
    /**
153
     * @param integer $count
154
     */
155
    final static protected function formatCount($count)
156
    {
157
        if ($count == 0) {
158
            return __('No item');
159
        } else if ($count == 1) {
160
            return __('1 item');
161
        }
162
        return __('%s items', array($count));
163
    }
164
}
165