Passed
Push — main ( 3e1577...e2b5f1 )
by Stefan
02:05
created

FormImage::setDefault()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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