Completed
Pull Request — master (#541)
by Richard
07:06
created

DateSelect::render()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 40
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 28
nc 2
nop 0
dl 0
loc 40
rs 8.8571
c 0
b 0
f 0
ccs 27
cts 27
cp 1
crap 2
1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
namespace Xoops\Form;
13
14
/**
15
 * DateSelect - date entry element with calendar popup
16
 *
17
 * @category  Xoops\Form\DateSelect
18
 * @package   Xoops\Form
19
 * @author    Kazumi Ono <[email protected]>
20
 * @copyright 2001-2016 XOOPS Project (http://xoops.org)
21
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
22
 * @link      http://xoops.org
23
 */
24
class DateSelect extends Text
25
{
26
    /**
27
     * __construct
28
     *
29
     * @param string|array      $caption caption or array of all attributes
30
     * @param string            $name    name
31
     * @param integer|\DateTime $value   unix timestamp or DateTime object
32
     */
33 4
    public function __construct($caption, $name = null, $value = null)
34
    {
35 4
        if (is_array($caption)) {
36 3
            parent::__construct($caption);
37 3
            $this->setIfNotSet('size', 15);
38 3
            $this->setIfNotSet('value', 0);
39 3
            $this->set('value', \Xoops\Core\Locale\Time::cleanTime($this->get('value', null)));
40
        } else {
41 2
            parent::__construct([]);
42 2
            $this->setCaption($caption);
43 2
            $this->setName($name);
44 2
            $this->set('size', 15);
45 2
            $this->setValue(\Xoops\Core\Locale\Time::cleanTime($value));
46
        }
47 4
    }
48
49
    /**
50
     * render
51
     *
52
     * @return string rendered form element
53
     */
54 2
    public function render()
55
    {
56 2
        $xoops = \Xoops::getInstance();
57
58 2
        $display_value = \Xoops\Core\Locale\Time::formatDate($this->getValue(false));
59
60 2
        $dataList = $this->isDatalist();
61 2
        if (!empty($dataList)) {
62
            $this->add('list', 'list_' . $this->getName());
63
        }
64
65 2
        $this->themeDecorateElement();
66 2
        $this->suppressRender(['value']);
67 2
        $attributes = $this->renderAttributeString();
68
69 2
        $xoops->theme()->addBaseStylesheetAssets('@jqueryuicss');
70 2
        $xoops->theme()->addBaseScriptAssets('@jqueryui');
71 2
        \Xoops\Core\Locale\Time::localizeDatePicker();
72
73 2
        $xoops->theme()->addScript(
74 2
            '',
75 2
            '',
76 2
            ' $(function() { $( "#' . $this->get('id') . '" ).datepicker({' .
77 2
            'showOn: "focus", changeYear: true, constrainInput: false ' .
78 2
            ' }); }); '
79
        );
80
81 2
        $this->set('class', 'form-control');
82 2
        $ret = '<div class="input-group">';
83 2
        $ret .= '<input ' . $attributes . ' value="' . $display_value . '" ' . $this->getExtra() .' >';
84 2
        $ret .= '<span class="input-group-btn">';
85 2
        $ret .= '<button class="btn btn-default" type="button" ';
86 2
        $ret .= 'data-toggle="tooltip" data-placement="left" title="' . \XoopsLocale::A_SELECT . '" ';
87 2
        $ret .= 'onclick="$( \'#' . $this->get('id') . '\' ).datepicker( \'show\' );"> ';
88 2
        $ret .= '<span class="glyphicon glyphicon-calendar" aria-hidden="true"></span></button>';
89 2
        $ret .= '</span></div>';
90
91 2
        return $ret;
92
            //'<input ' . $attributes . 'value="' . $display_value . '" ' . $this->getExtra() .' >';
93
    }
94
}
95