Passed
Push — main ( 4485e3...ffe0c0 )
by Stefan
02:12
created

FormStarRate::setSubmitTitle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace SKien\Formgenerator;
5
6
/**
7
 * Element for rating input with stars.
8
 *
9
 * This element can be used to select rating between 1 ... n stars. It is based
10
 * on radio-input + CSS and uses UTF-8 Symbols for the stars - no Javascript
11
 * and images are needed!
12
 *
13
 * The titles of the stars are displayed as tooltip and can be set
14
 * - by calling the `setTitles()` method
15
 * - within the XML definition
16
 * - in the configuration
17
 *
18
 * (Method/XML overwrites config overwrites default)
19
 *
20
 * The count of titles also results in the number of stars.
21
 *
22
 * Default setting is 5 Stars (*'terrible', 'not good', 'average', 'good', 'verry good'*)
23
 *
24
 * By default the submitted value is an integer between 1 ... n (representing the count of stars)
25
 * wich can be changed to title of the selected star using `setSubmitTitle()` method.
26
 *
27
 * @SKienImage FormStarRate.png
28
 *
29
 * @package Formgenerator
30
 * @author Stefanius <[email protected]>
31
 * @copyright MIT License - see the LICENSE file for details
32
 */
33
class FormStarRate extends FormInput
34
{
35
    /** default count of stars */
36
    protected const STAR_COUNT = 5;
37
    /** default titles for the stars */
38
    protected const STAR_TITELS = ['terrible', 'not good', 'average', 'good', 'verry good'];
39
40
    /** @var array titles for the stars (from 0 ... n-1) */
41
    protected ?array $aTitles = null;
42
    /** @var bool $bSubmitTitle submit the title of the selected star instead of index */
43
    protected bool $bSubmitTitle = false;
44
45
    /**
46
     * Create a star rating element.
47
     * @param string $strName   name AND id of the element
48
     * @param int $wFlags    (default: 0)
49
     */
50
    public function __construct(string $strName, int $wFlags = 0)
51
    {
52
        $this->oFlags = new FormFlags($wFlags);
53
        $this->strName = $strName;
54
    }
55
56
    /**
57
     * {@inheritDoc}
58
     * @see \SKien\Formgenerator\FormElement::fromXML()
59
     * @internal
60
     */
61
    static public function fromXML(\DOMElement $oXMLElement, FormCollection $oFormParent) : ?FormElement
62
    {
63
        $strName = self::getAttribString($oXMLElement, 'name', '');
64
        $wFlags = self::getAttribFlags($oXMLElement);
65
        $oFormElement = new self($strName, $wFlags);
66
        $oFormParent->add($oFormElement);
67
        $oFormElement->readAdditionalXML($oXMLElement);
68
        return $oFormElement;
69
    }
70
71
    /**
72
     * {@inheritDoc}
73
     * @see \SKien\Formgenerator\FormElement::readAdditionalXML()
74
     * @internal
75
     */
76
    public function readAdditionalXML(\DOMElement $oXMLElement) : void
77
    {
78
        parent::readAdditionalXML($oXMLElement);
79
        $aTitles = $oXMLElement->getElementsByTagName('title');
80
        if ($aTitles->length > 0) {
81
            $this->aTitles = [];
82
            $i = 0;
83
            foreach ($aTitles as $oTitle) {
84
                $this->aTitles[$i++] = $oTitle->nodeValue;
85
            }
86
        }
87
        $this->bSubmitTitle = self::getAttribBool($oXMLElement, 'submittitle', false);
88
    }
89
90
    /**
91
     * We 'ask' the configuration for count and titles.
92
     * $this->oFG is not available until the parent is set!
93
     */
94
    protected function onParentSet() : void
95
    {
96
        if ($this->aTitles === null) {
97
            $this->aTitles = $this->oFG->getConfig()->getArray('StarRate.Titles', self::STAR_TITELS);
98
        }
99
    }
100
101
    /**
102
     * Build the HTML-markup for the radio group.
103
     * @return string
104
     * @internal l
105
     */
106
    public function getHTML() : string
107
    {
108
        $this->processFlags();
109
110
        $strStyle = '';
111
        if ($this->oFlags->isSet(FormFlags::ALIGN_CENTER)) {
112
            $strStyle = 'text-align: center;';
113
        } else if ($this->oFlags->isSet(FormFlags::ALIGN_RIGHT)) {
114
            $strStyle = 'text-align: right;';
115
        }
116
117
        $strHTML = $this->buildContainerDiv($strStyle) . PHP_EOL;
118
        $strHTML .= '    <div class="starrate">' . PHP_EOL;
119
120
        $iSelect = intval($this->oFG->getData()->getValue($this->strName));
121
122
        if (is_array($this->aTitles)) {
123
            $iStarCount = count($this->aTitles);
124
            for ($iStar = $iStarCount; $iStar > 0; $iStar--) {
125
                $strInputClassCSS = ($iStar == $iStarCount) ? ' class="best"' : '';
126
                $strId = $this->strName . 'Star' . $iStar;
127
                $strTitle = ($this->aTitles !== null && isset($this->aTitles[$iStar - 1])) ? $this->aTitles[$iStar - 1] : $iStar . ' Star';
128
                $strValue = ($this->bSubmitTitle) ? $strTitle : $iStar;
129
130
                $strHTML .= '        <input' . $strInputClassCSS;
131
                $strHTML .= ' type="radio" id="' . $strId . '"';
132
                $strHTML .= ($iSelect == $iStar) ? ' checked' : '';
133
                $strHTML .= ' name="' . $this->strName . '"';
134
                $strHTML .= $this->buildAttributes();
135
                $strHTML .= ' value="' . $strValue . '"/>' . PHP_EOL;
136
137
                $strHTML .= '        <label for="' . $strId . '"';
138
                $strHTML .= ' id="' . $strId . 'Label"';
139
                $strHTML .= ' title="' . $strTitle . '"></label>' . PHP_EOL;
140
            }
141
            $strHTML .= '    </div>' . PHP_EOL;
142
143
            $strHTML .= '</div>' . PHP_EOL;
144
        }
145
146
        return $strHTML;
147
    }
148
149
    /**
150
     * Set the titles for the stars.
151
     * The count of titles also results in the number of stars!
152
     * @param array $aTitles    [0]: lowest [n-1]: highest
153
     */
154
    public function setTitles(array $aTitles) : void
155
    {
156
        $this->aTitles = $aTitles;
157
    }
158
159
    /**
160
     * Submit the title of the selected star instead of index.
161
     * @param bool $bSet
162
     */
163
    public function setSubmitTitle(bool $bSet = true) : void
164
    {
165
        $this->bSubmitTitle = $bSet;
166
    }
167
}
168