FormSpin::getStyleText()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
namespace XoopsModules\Extcal\Form\Spin;
4
5
/**
6
 * FormSpin element  -  Spin button.
7
 *
8
 * You may not change or alter any portion of this comment or credits
9
 * of supporting developers from this source code or any supporting source code
10
 * which is considered copyrighted (c) material of the original comment or credit authors.
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
 *
15
 * @copyright       XOOPS Project (https://xoops.org)
16
 * @license         GNU GPL 2 (https://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
17
 *
18
 * @since           2.0.0
19
 *
20
 * @author          Jean-Jacques DELALANDRE <[email protected]>
21
 *
22
 * @version         FormSpin v 1.2
23
 */
24
25
\xoops_load('XoopsFormElement');
26
27
/**
28
 * A select field.
29
 *
30
 * @author      Jean-Jacques DELALANDRE <[email protected]>
31
 * @copyright   JJD http:xoops.kiolo.com
32
 */
33
34
/**
35
 * Class FormSpin.
36
 */
37
class FormSpin extends \XoopsFormElement
38
{
39
    /**
40
     * Value.
41
     *
42
     * @var int
43
     */
44
    protected $_value = 0;
45
    /**
46
     * Value minimum.
47
     *
48
     * @var int
49
     */
50
    protected $_min = 0;
51
    /**
52
     * Value maximum.
53
     *
54
     * @var int
55
     */
56
    protected $_max = 100;
57
    /**
58
     * Small increment.
59
     *
60
     * @var int
61
     */
62
    protected $_smallIncrement = 1;
63
    /**
64
     * Large increment.
65
     *
66
     * @var int
67
     */
68
    protected $_largeIncrement = 10;
69
    /**
70
     *  unite for information on value.
71
     *
72
     * @var string
73
     */
74
    protected $_unite = '';
75
    /**
76
     * Folder of arrow image.
77
     *
78
     * @var string
79
     */
80
    protected $_imgFolder = 'default';
81
    /**
82
     * size of input text in nb car.
83
     *
84
     * @var int
85
     */
86
    protected $_size = 2;
87
    /**
88
     *  minMaxVisible show buttons to go minimum and maximum.
89
     *
90
     * @var bool|int
91
     */
92
    protected $_minMaxVisible = true;
93
    /**
94
     *  tyleBordure ;  style CSS of frame control.
95
     *
96
     * @var string
97
     */
98
    protected $_styleBordure = 'color: #FFFFFF; background-color: #CCCCCC; line-height: 100%;border-width:1px; border-style: solid; border-color: #000000; margin-top: 0; margin-bottom: 0; padding: 0';
99
    /**
100
     *  tyleText : style CSS of input text.
101
     *
102
     * @var string
103
     */
104
    protected $_styleText = 'color: #000000; text-align: right; margin-left: 1; margin-right: 2; padding-right: 8';
105
    /**
106
     * Allow loading of javascript.
107
     *
108
     * @var bool
109
     */
110
    protected $_loadJS = true;
111
    /*---------------------------------------------------------------*/
112
113
    /**
114
     * Constructor.
115
     *
116
     * @param string $caption        Caption
117
     * @param string $name           "name" attribute
118
     * @param int    $value          Pre-selected value.
119
     * @param int    $min            value
120
     * @param int    $max            value
121
     * @param int    $smallIncrement Increment when click on button
122
     * @param int    $largeIncrement Increment when click on button
123
     * @param int    $size           Number caractere of inputtext
124
     * @param string $unite          of the value
125
     * @param string $imgFolder      of image gif for button
126
     * @param string $styleText      style CSs of text
127
     * @param string $styleBordure   style CSs of frame
128
     * @param bool   $minMaxVisible  show min and mas buttons
129
     */
130
    public function __construct(
131
        $caption,
132
        $name,
133
        $value = 0,
134
        $min = 0,
135
        $max = 100,
136
        $smallIncrement = 1,
137
        $largeIncrement = 10,
138
        $size = 5,
139
        $unite = '',
140
        $imgFolder = 'default',
141
        $styleText = '',
142
        $styleBordure = '',
143
        $minMaxVisible = true
144
    ) {
145
        $this->setName($name);
146
        $this->setCaption($caption);
147
        $this->setValue($value);
148
        $this->setMin($min);
149
        $this->setMax($max);
150
        $this->setSmallIncrement($smallIncrement);
151
        $this->setLargeIncrement($largeIncrement);
152
        $this->setSize($size);
153
        $this->setUnite($unite);
154
        $this->setImgFolder($imgFolder);
155
        $this->setStyleText($styleText);
156
        $this->setStyleBordure($styleBordure);
157
        $this->setMinMaxVisible($minMaxVisible);
158
    }
159
160
    /*-----------------------------------------------------------------*/
161
162
    /**
163
     * Get the values.
164
     */
165
    public function getValue()
166
    {
167
        return $this->_value;
168
    }
169
170
    /**
171
     * Set the value.
172
     *
173
     * @param int $value
174
     */
175
    public function setValue($value)
176
    {
177
        $this->_value = $value;
178
    }
179
180
    /*-----------------------------------------------------------------*/
181
182
    /**
183
     * Get the min value.
184
     */
185
    public function getMin()
186
    {
187
        return $this->_min;
188
    }
189
190
    /**
191
     * Set the min value.
192
     *
193
     * @param int $min
194
     */
195
    public function setMin($min)
196
    {
197
        $this->_min = (int)$min;
198
    }
199
200
    /*-----------------------------------------------------------------*/
201
202
    /**
203
     * Get the max value - must be more great then min.
204
     */
205
    public function getMax()
206
    {
207
        return $this->_max;
208
    }
209
210
    /**
211
     * Set the max value - must be more great then min.
212
     *
213
     * @param int $max
214
     */
215
    public function setMax($max)
216
    {
217
        $this->_max = (int)$max;
218
    }
219
220
    /*-----------------------------------------------------------------*/
221
222
    /**
223
     * Get the small increment when click a short time on up down nutton.
224
     */
225
    public function getSmallIncrement()
226
    {
227
        return $this->_smallIncrement;
228
    }
229
230
    /**
231
     * Set the small increment when click a short time on up down nutton
232
     * must be  " > 0 ".
233
     *
234
     * @param $smallIncrement
235
     *
236
     * @internal param int $value
237
     */
238
    public function setSmallIncrement($smallIncrement)
239
    {
240
        $this->_smallIncrement = (int)$smallIncrement;
241
        if (0 == $this->_smallIncrement) {
242
            $this->_smallIncrement = 1;
243
        }
244
    }
245
246
    /*-----------------------------------------------------------------*/
247
248
    /**
249
     * Get the large increment when click a long time on up down nutton.
250
     */
251
    public function getLargeIncrement()
252
    {
253
        return $this->_largeIncrement;
254
    }
255
256
    /**
257
     * Set the large increment when click a long time on up down nutton.
258
     *
259
     * @param int $largeIncrement
260
     */
261
    public function setLargeIncrement($largeIncrement)
262
    {
263
        $this->_largeIncrement = (int)$largeIncrement;
264
        if (0 == $this->_largeIncrement) {
265
            $this->_largeIncrement = 10;
266
        }
267
    }
268
269
    /*-----------------------------------------------------------------*/
270
271
    /**
272
     * Get the size in nb car of the input text for the value.
273
     */
274
    public function getSize()
275
    {
276
        return $this->_size;
277
    }
278
279
    /**
280
     * Set the size in nb car of the input text for the value
281
     * must be 2 car min.
282
     *
283
     * @param mixed $size
284
     */
285
    public function setSize($size)
286
    {
287
        $this->_size = $size;
288
        if (0 == $this->_size) {
289
            $this->_size = 2;
290
        }
291
    }
292
293
    /*-----------------------------------------------------------------*/
294
295
    /**
296
     * @return string
297
     */
298
    public function getImgFolder()
299
        /*
300
         * Get the shortname of the folder images
301
         */
302
    {
303
        return $this->_imgFolder;
304
    }
305
306
    /**
307
     * Set the shortname of the folder images.
308
     *
309
     * @param string $folder
310
     */
311
    public function setImgFolder($folder)
312
    {
313
        if ('' != $folder) {
314
            $this->_imgFolder = $folder;
315
        }
316
    }
317
318
    /*-----------------------------------------------------------------*/
319
320
    /**
321
     * Get the label of unites between value and buttons.
322
     */
323
    public function getUnite()
324
    {
325
        return $this->_unite;
326
    }
327
328
    /**
329
     * Set the label of unites between value and buttons.
330
     *
331
     * @param string $unite
332
     */
333
    public function setUnite($unite)
334
    {
335
        $this->_unite = $unite;
336
    }
337
338
    /*-----------------------------------------------------------------*/
339
340
    /**
341
     * Get the style CSS of the text.
342
     */
343
    public function getStyleText()
344
    {
345
        return $this->_styleText;
346
    }
347
348
    /**
349
     * Set the style CSS of the text.
350
     *
351
     * @param string $style
352
     */
353
    public function setStyleText($style)
354
    {
355
        if ('' != $style) {
356
            $this->_styleText = $style;
357
        }
358
    }
359
360
    /*-----------------------------------------------------------------*/
361
362
    /**
363
     * Get the style CSS of the frame.
364
     */
365
    public function getStyleBordure()
366
    {
367
        return $this->_styleBordure;
368
    }
369
370
    /**
371
     * Set the style CSS of the frame.
372
     *
373
     * @param string $style
374
     */
375
    public function setStyleBordure($style)
376
    {
377
        if ('' != $style) {
378
            $this->_styleBordure = $style;
379
        }
380
    }
381
382
    /*-----------------------------------------------------------------*/
383
384
    /**
385
     * Get MinMaxVisible : show the button to go min and max value.
386
     */
387
    public function getMinMaxVisible()
388
    {
389
        return $this->_minMaxVisible;
390
    }
391
392
    /**
393
     * Set  MinMaxVisible : show the button to go min and max value.
394
     *
395
     * @param bool $visible
396
     */
397
    public function setMinMaxVisible($visible)
398
    {
399
        $this->_minMaxVisible = $visible;
400
    }
401
402
    /**********************************************************************/
403
404
    /**
405
     * Prepare HTML for output.
406
     *
407
     * @return string HTML
408
     */
409
    public function render()
410
    {
411
        $sSpinFolder = $this->getFolder();
412
        $sFolderImg  = "{$sSpinFolder}/images/{$this->getImgFolder()}/";
413
414
        $prefixe  = $this->getName();
415
        $prefixe2 = 'spin' . $prefixe;
416
417
        $smallIncrement = $this->getSmallIncrement();
418
        $largeIncrement = $this->getLargeIncrement();
419
420
        /*----------------------------------------------*/
421
        $delai        = 200;
422
        $onMouseDown1 = "spinStart(\"{$prefixe}\", \"{$prefixe2}\",  {$smallIncrement},  {$largeIncrement}, {$delai}, \"{$sFolderImg}spinUp1.gif\");";
423
        $onMouseDown2 = "spinStart(\"{$prefixe}\", \"{$prefixe2}\", -{$smallIncrement}, -{$largeIncrement}, {$delai}, \"{$sFolderImg}spinDown1.gif\");";
424
425
        $onMouseUp = 'spinStop();';
426
        //----------------------------------------------------------------
427
        $styleBordure = $this->htmlAddAttribut('style', $this->getStyleBordure());
428
        $styleText    = $this->htmlAddAttribut('style', $this->getStyleText());
429
        $styleArrow   = 'style="display: table-cell;vertical-align: middle; text-align: center; line-height: 100%; font-size: 7 pt; margin-top: 0; margin-bottom: 0; padding: 0"';
430
        //----------------------------------------------------------------
431
        $t = [];
432
433
        if ($this->_loadJS) {
434
            $js  = $sSpinFolder . '/js/spin.js';
435
            $t[] = "<script src='{$js}' type='text/javascript'></script>";
436
        }
437
438
        $t[] = "<div STYLE='width:50px;'>";
439
        //$t[] = "<table border='0' width='8%' cellpadding='0' cellspacing='0'>";
440
        $t[] = "<table border='0' width='8%' cellpadding='0' cellspacing='0' {$styleBordure}>";
441
        $t[] = '  <tr>';
442
        //$t[] = "    <td width='60%'>{$Caption}</td>";
443
        $t[] = "    <td width='60%'>";
444
        $t[] = "      <INPUT TYPE='hidden' NAME='{$prefixe2}_min' VALUE='{$this->getMin()}'>";
445
        $t[] = "      <INPUT TYPE='hidden' NAME='{$prefixe2}_max' VALUE='{$this->getMax()}'>";
446
        $t[] = "      <INPUT TYPE='hidden' NAME='{$prefixe2}_smallIncrement' VALUE='{$this->_smallIncrement}'  style='text-align: right;'>";
447
        $t[] = "      <input type='text'  name='{$prefixe}' size='{$this->getSize()}' value='{$this->getValue()}' {$styleText}>";
448
        $t[] = '    </td>';
449
450
        $unite = $this->getUnite();
451
        if ('' != $unite) {
452
            $t[] = "    <td style='display: table-cell;vertical-align: middle; '>&nbsp;{$unite}&nbsp;</td>";
453
        }
454
        //-------------------------------------------------------
455
        if ($this->getMinMaxVisible()) {
456
            $onMouseDownMin = "spinSetValue(\"{$prefixe}\", \"{$prefixe2}\",  \"Min\", {$this->getMin()}, {$delai}, \"{$sFolderImg}spinMin1.gif\");";
457
            $t[]            = "    <td width='63%' align='center' {$styleArrow}>";
458
            $t[]            = "      <img border='0' name='{$prefixe2}_imgMin' src='{$sFolderImg}spinMin0.gif'   onmousedown='{$onMouseDownMin}'><br>";
459
            $t[]            = '    </td>';
460
        }
461
        //-------------------------------------------------------
462
        $t[] = "    <td width='63%' align='center' {$styleArrow}>";
463
464
        $t[] = "      <img border='0' name='{$prefixe2}_img0' src='{$sFolderImg}spinUp0.gif'   onmousedown='{$onMouseDown1}' onmouseup='{$onMouseUp}' onmouseout='{$onMouseUp}'><br>";
465
        $t[] = "      <img border='0' name='{$prefixe2}_img1' src='{$sFolderImg}spinDown0.gif' onmousedown='{$onMouseDown2}' onmouseup='{$onMouseUp}' onmouseout='{$onMouseUp}'>";
466
467
        $t[] = '    </td>';
468
469
        //-------------------------------------------------------
470
        if ($this->getMinMaxVisible()) {
471
            $onMouseDownMax = "spinSetValue(\"{$prefixe}\", \"{$prefixe2}\",  \"Max\", {$this->getMax()}, {$delai}, \"{$sFolderImg}spinMax1.gif\");";
472
            $t[]            = "    <td width='63%' align='center' {$styleArrow}>";
473
            $t[]            = "      <img border='0' name='{$prefixe2}_imgMax' src='{$sFolderImg}spinMax0.gif'   onmousedown='{$onMouseDownMax}'><br>";
474
            $t[]            = '    </td>';
475
        }
476
        //-------------------------------------------------------
477
478
        $t[] = '  </tr>';
479
        $t[] = '</table>' . "\n";
480
        $t[] = '</div>';
481
        //-------------------------------------------
482
        $html = \implode("\n", $t);
483
484
        return $html;
485
    }
486
487
    /**************************************************************************
488
     * calcul du dossier du composant
489
     *************************************************************************/
490
    public function getFolder()
491
    {
492
        $sSpinFolder = $GLOBALS['xoops']->url('modules/extcal/class/Form/Spin/');
493
494
        return $sSpinFolder;
495
    }
496
497
    /********************************************************************
498
     *
499
     ********************************************************************
500
     * @param        $attribut
501
     * @param        $value
502
     * @param string $default
503
     * @return string
504
     */
505
    public function htmlAddAttribut($attribut, $value, $default = '')
506
    {
507
        $r = '';
508
        if ('' == $value) {
509
            $value = $default;
510
        }
511
512
        if ('' != $value) {
513
            //            if (substr($value, 0, strlen($attribut)) != $attribut) {
514
            if (0 !== mb_strpos($value, $attribut)) {
515
                $r = "{$attribut}=\"{$value}\"";
516
            }
517
518
            return $r;
519
        }
520
521
        /*-----------------------------------------------*/ /*---          fin de la classe               ---*/
522
        /*-----------------------------------------------*/
523
    }
524
}
525