Passed
Push — main ( 2edd53...9e1454 )
by Stefan
02:35
created

FormImage::fromXML()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 2
dl 0
loc 13
rs 9.9332
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace SKien\Formgenerator;
5
6
/**
7
 * Element to display image inside of a form.
8
 *
9
 * @package Formgenerator
10
 * @author Stefanius <[email protected]>
11
 * @copyright MIT License - see the LICENSE file for details
12
 */
13
class FormImage extends FormElement
14
{
15
    /** standard delete image */
16
    const IMG_DELETE            = 1;
17
    /** standard search image */
18
    const IMG_SEARCH            = 2;
19
    /** standard search image */
20
    const IMG_BROWSE            = 3;
21
    /** standard image for date picker */
22
    const IMG_DATE_PICKER       = 4;
23
    /** standard image for time picker */
24
    const IMG_TIME_PICKER       = 5;
25
    /** standard image for dtu insert (DTU: Date-Time-User) */
26
    const IMG_DTU               = 6;
27
    
28
    /** @var string|int image to display     */
29
    protected $img;
30
    /** @var string CSS styles     */
31
    protected string $strStyle;
32
    /** @var string image is bound to this input element    */
33
    protected string $strBoundTo = '';
34
    /** @var string image to use, if no image set    */
35
    protected string $strDefaultImg = '';
36
    
37
    /**
38
     * Create image element. 
39
     * @param string $strName
40
     * @param string|int $img       image to display or index to a standard image
41
     * @param string $strOnClick    JS onclick() handler
42
     * @param int $wFlags       
43
     * @param string $strStyle      CSS style (default '')
44
     */
45
    public function __construct(string $strName, $img, string $strOnClick, int $wFlags = 0, string $strStyle = '')
46
    {
47
        parent::__construct($wFlags);
48
        $this->strName = $strName;
49
        $this->img = $img;
50
        $this->addAttribute('onclick', $strOnClick);
51
52
        if (strlen($strStyle) > 0) {
53
            $this->parseStyle($strStyle);
54
        }
55
        if (!isset($this->aStyle['vertical-align'])) {
56
            $this->addStyle('vertical-align', 'text-bottom');
57
        }
58
    }
59
    
60
    /**
61
     * {@inheritDoc}
62
     * @see \SKien\Formgenerator\FormElement::fromXML()
63
     */
64
    static public function fromXML(\DOMElement $oXMLElement, FormCollection $oFormParent) : ?FormElement
65
    {
66
        $strName = self::getAttribString($oXMLElement, 'name', '');
67
        $image = self::getAttribString($oXMLElement, 'image', '');
68
        $strConstName = 'self::' . strtoupper($image);
69
        if (defined($strConstName)) {
70
            $image = constant($strConstName);
71
        }
72
        $wFlags = self::getAttribFlags($oXMLElement);
73
        $oFormElement = new self($strName, $image, '', $wFlags);
74
        $oFormParent->add($oFormElement);
75
        $oFormElement->readAdditionalXML($oXMLElement);
76
        return $oFormElement;
77
    }
78
    
79
    /**
80
     * {@inheritDoc}
81
     * @see \SKien\Formgenerator\FormElement::readAdditionalXML()
82
     */
83
    public function readAdditionalXML(\DOMElement $oXMLElement) : void
84
    {
85
        parent::readAdditionalXML($oXMLElement);
86
        if (($strBindTo = self::getAttribString($oXMLElement, 'bindto')) !== null) {
87
            $this->bindTo($strBindTo);
88
        }
89
        if (($strDefault = self::getAttribString($oXMLElement, 'default')) !== null) {
90
            $this->setDefault($strDefault);
91
        }
92
    }
93
    
94
    /**
95
     * Bind the image to an input field that contains the imagepath.
96
     * @param string $strBoundTo
97
     */
98
    public function bindTo(string $strBoundTo) : void
99
    {
100
        $this->strBoundTo = $strBoundTo;
101
    }
102
    
103
    /**
104
     * Set a default image, if no image set or the bounded input contains no data.
105
     * @param string $strDefault
106
     */
107
    public function setDefault(string $strDefault) : void
108
    {
109
        $this->strDefaultImg = $strDefault;
110
    }
111
    
112
    /**
113
     * Build the HTML-notation for the image.
114
     * @return string
115
     */
116
    public function getHTML() : string
117
    {
118
        $strStyle = '';
119
        if ($this->oFlags->isSet(FormFlags::ALIGN_CENTER)) {
120
            $strStyle = 'text-align: center;';
121
        } else if ($this->oFlags->isSet(FormFlags::ALIGN_RIGHT)) {
122
            $strStyle = 'text-align: right;';
123
        }
124
        $strHTML = $this->buildContainerDiv($strStyle);
125
        
126
        $strImg = $this->getImg();
127
        $strAlt = 'Image'; 
128
        $strHTML .= '<img src="' . $strImg . '" alt="' . $strAlt . '"';
129
        if (!empty($this->strName)) {
130
            $strHTML .= ' id="' . $this->strName . '"';
131
        }
132
        $strHTML .= $this->buildStyle();
133
        $strHTML .= $this->buildClass();
134
        $strHTML .= $this->buildID();
135
        $strHTML .= $this->buildAttributes();
136
        $strHTML .= '></div>' . PHP_EOL;
137
        
138
        return $strHTML;
139
    }
140
    
141
    /**
142
     * Get  the image to display.
143
     * Can be <ul>
144
     * <li> the image contained in the value of a bounded input field </li>
145
     * <li> a standard image specified by number </li>
146
     * <li> the image specified by the img property </li></ul>  
147
     * @return string
148
     */
149
    protected function getImg() : string
150
    {
151
        $strImg = '';
152
        if (strlen($this->strDefaultImg) > 0) {
153
            $this->addAttribute('data-default', $this->strDefaultImg);
154
        }
155
        
156
        if (strlen($this->strBoundTo) > 0) {
157
            $this->addAttribute('data-bound-to', $this->strBoundTo);
158
            $strImg = $this->oFG->getData()->getValue($this->strBoundTo);
159
        } else if (is_numeric($this->img)) {
160
            [$strImg, $strTitle] = $this->oFG->getStdImage(intval($this->img));
161
            if (strlen($strTitle) > 0) {
162
                $this->addAttribute('title', $strTitle);
163
            }
164
        } else {
165
            $strImg = $this->img;
166
        }
167
        
168
        if (strlen($strImg) == 0) {
169
            $strImg = $this->strDefaultImg;
170
        }
171
        return $strImg;
172
    }
173
}
174