FormImage::fromXML()   A
last analyzed

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
 * The image can be bound to an input field that contains the filename (-&gt; `src`)
10
 * of the image. This field can be readonly or hidden.
11
 * If an image is bound to an input field, there is also the posibilty to set a default
12
 * image, that is displayed, when the bounded field is empty.
13
 *
14
 * > <b>Example:</b><br/>
15
 *   The image is bounded to the field 'strPortrait' that contains the path of a
16
 *   portrait comming from a databaase record. For an empty/new record or even if
17
 *   no portrait has been selected so far, an specified default image will be displayed
18
 *   (e.g. an placeholder)
19
 *
20
 *
21
 *  An example for a bounded image can be found in ColumnForm.php and ColumnFormXML.php in
22
 *  the example directory.
23
 *
24
 * @package Formgenerator
25
 * @author Stefanius <[email protected]>
26
 * @copyright MIT License - see the LICENSE file for details
27
 */
28
class FormImage extends FormElement
29
{
30
    /** standard delete image */
31
    const IMG_DELETE            = 1;
32
    /** standard search image */
33
    const IMG_SEARCH            = 2;
34
    /** standard search image */
35
    const IMG_BROWSE            = 3;
36
    /** standard image for date picker */
37
    const IMG_DATE_PICKER       = 4;
38
    /** standard image for time picker */
39
    const IMG_TIME_PICKER       = 5;
40
    /** standard image for dtu insert (DTU: Date-Time-User) */
41
    const IMG_DTU               = 6;
42
43
    /** @var string|int image to display     */
44
    protected $img;
45
    /** @var string CSS styles     */
46
    protected string $strStyle;
47
    /** @var string image is bound to this input element    */
48
    protected string $strBoundTo = '';
49
    /** @var string image to use, if no image set    */
50
    protected string $strDefaultImg = '';
51
52
    /**
53
     * Create image element.
54
     * @param string $strName       Name of the image
55
     * @param string|int $img       image to display or index to a standard image
56
     * @param string $strOnClick    JS onclick() handler
57
     * @param int $wFlags           any combination of FormFlag constants
58
     * @param string $strStyle      CSS style (default '')
59
     */
60
    public function __construct(string $strName, $img, string $strOnClick, int $wFlags = 0, string $strStyle = '')
61
    {
62
        parent::__construct($wFlags);
63
        $this->strName = $strName;
64
        $this->img = $img;
65
        $this->addAttribute('onclick', $strOnClick);
66
67
        if (strlen($strStyle) > 0) {
68
            $this->parseStyle($strStyle);
69
        }
70
    }
71
72
    /**
73
     * {@inheritDoc}
74
     * @see \SKien\Formgenerator\FormElement::fromXML()
75
     * @internal
76
     */
77
    static public function fromXML(\DOMElement $oXMLElement, FormCollection $oFormParent) : ?FormElement
78
    {
79
        $strName = self::getAttribString($oXMLElement, 'name');
80
        $image = self::getAttribString($oXMLElement, 'image');
81
        $strConstName = 'self::' . strtoupper($image);
82
        if (defined($strConstName)) {
83
            $image = constant($strConstName);
84
        }
85
        $wFlags = self::getAttribFlags($oXMLElement);
86
        $oFormElement = new self($strName, $image, '', $wFlags);
87
        $oFormParent->add($oFormElement);
88
        $oFormElement->readAdditionalXML($oXMLElement);
89
        return $oFormElement;
90
    }
91
92
    /**
93
     * {@inheritDoc}
94
     * @see \SKien\Formgenerator\FormElement::readAdditionalXML()
95
     * @internal
96
     */
97
    public function readAdditionalXML(\DOMElement $oXMLElement) : void
98
    {
99
        parent::readAdditionalXML($oXMLElement);
100
        if (self::hasAttrib($oXMLElement, 'bindto')) {
101
            $this->bindTo(self::getAttribString($oXMLElement, 'bindto'));
102
        }
103
        if (self::hasAttrib($oXMLElement, 'default')) {
104
            $this->setDefault(self::getAttribString($oXMLElement, 'default'));
105
        }
106
    }
107
108
    /**
109
     * Bind the image to an input field that contains the imagepath.
110
     * @param string $strBoundTo    Name of the input element
111
     */
112
    public function bindTo(string $strBoundTo) : void
113
    {
114
        $this->strBoundTo = $strBoundTo;
115
    }
116
117
    /**
118
     * Set a default image, if no image set or the bounded input contains no data.
119
     * @param string $strDefault
120
     */
121
    public function setDefault(string $strDefault) : void
122
    {
123
        $this->strDefaultImg = $strDefault;
124
    }
125
126
    /**
127
     * Build the HTML-notation for the image.
128
     * @return string
129
     * @internal l
130
     */
131
    public function getHTML() : string
132
    {
133
        if (!isset($this->aStyle['vertical-align'])) {
134
            $this->addStyle('vertical-align', 'text-bottom');
135
        }
136
        $strStyle = '';
137
        if ($this->oFlags->isSet(FormFlags::ALIGN_CENTER)) {
138
            $strStyle = 'text-align: center;';
139
        } else if ($this->oFlags->isSet(FormFlags::ALIGN_RIGHT)) {
140
            $strStyle = 'text-align: right;';
141
        }
142
        $strHTML = $this->buildContainerDiv($strStyle);
143
144
        $strImg = $this->getImg();
145
        $strAlt = 'Image';
146
        $strHTML .= '<img src="' . $strImg . '" alt="' . $strAlt . '"';
147
        if (!empty($this->strName)) {
148
            $strHTML .= ' id="' . $this->strName . '"';
149
        }
150
        $strHTML .= $this->buildStyle();
151
        $strHTML .= $this->buildClass();
152
        $strHTML .= $this->buildID();
153
        $strHTML .= $this->buildAttributes();
154
        $strHTML .= '></div>' . PHP_EOL;
155
156
        return $strHTML;
157
    }
158
159
    /**
160
     * Get  the image to display.
161
     * Can be <ul>
162
     * <li> the image contained in the value of a bounded input field </li>
163
     * <li> a standard image specified by number </li>
164
     * <li> the image specified by the img property </li></ul>
165
     * @return string
166
     */
167
    protected function getImg() : string
168
    {
169
        $strImg = '';
170
        if (strlen($this->strDefaultImg) > 0) {
171
            $this->addAttribute('data-default', $this->strDefaultImg);
172
        }
173
174
        if (strlen($this->strBoundTo) > 0) {
175
            $this->addAttribute('data-bound-to', $this->strBoundTo);
176
            $strImg = $this->oFG->getData()->getValue($this->strBoundTo);
177
        } else if (is_numeric($this->img)) {
178
            [$strImg, $strTitle] = $this->oFG->getStdImage(intval($this->img));
179
            if (strlen($strTitle) > 0) {
180
                $this->addAttribute('title', $strTitle);
181
            }
182
        } else {
183
            $strImg = $this->img;
184
        }
185
186
        if (strlen($strImg) == 0) {
187
            $strImg = $this->strDefaultImg;
188
        }
189
        return $strImg;
190
    }
191
}
192