Completed
Branch master (9ffc9e)
by Pierre-Henry
35:33
created

Checksort::jQueryDocumentReady()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 4
nop 0
dl 0
loc 22
rs 8.9197
c 0
b 0
f 0
1
<?php
2
/**
3
 * File has been modified by Pierre-Henry Soria
4
 */
5
6
namespace PFBC\Element;
7
8
class Checksort extends Sort
9
{
10
    protected $attributes = array('type' => 'checkbox');
11
    protected $inline;
12
    protected $maxheight;
13
14
    public function jQueryDocumentReady()
15
    {
16
        parent::jQueryDocumentReady();
17
        if (!empty($this->inline)) {
18
            echo 'jQuery("#', $this->attributes['id'], ' .pfbc-checkbox:last").css("margin-right", "0");';
19
        } else {
20
            echo 'jQuery("#', $this->attributes['id'], ' .pfbc-checkbox:last").css({ "padding-bottom": "0", "border-bottom": "none" });';
21
        }
22
23
        if (!empty($this->maxheight) && is_numeric($this->maxheight)) {
24
            echo <<<JS
25
var checkboxes = jQuery("#{$this->attributes['id']} .pfbc-checkboxes");
26
if(checkboxes.outerHeight() > {$this->maxheight}) {
27
    checkboxes.css({
28
        "height": "{$this->maxheight}px",
29
        "overflow": "auto",
30
        "overflow-x": "hidden"
31
    });
32
}
33
JS;
34
        }
35
    }
36
37
    public function render()
38
    {
39
        if (isset($this->attributes['value'])) {
40
            if (!is_array($this->attributes['value'])) {
41
                $this->attributes['value'] = array($this->attributes['value']);
42
            }
43
        } else {
44
            $this->attributes['value'] = array();
45
        }
46
47
        if (substr($this->attributes['name'], -2) !== '[]') {
48
            $this->attributes['name'] .= '[]';
49
        }
50
51
        $count = 0;
52
        $existing = "";
53
        echo '<div id="', $this->attributes['id'], '"><div class="pfbc-checkboxes">';
54
        foreach ($this->options as $value => $text) {
55
            $value = $this->getOptionValue($value);
56
            echo '<div class="pfbc-checkbox"><table cellpadding="0" cellspacing="0"><tr><td valign="top"><input id="', $this->attributes['id'], "-", $count, '"', $this->getAttributes(array("id", "value", "checked", "name", "onclick")), ' value="', $this->filter($value), '"';
0 ignored issues
show
Bug introduced by
It seems like $value defined by $this->getOptionValue($value) on line 55 can also be of type boolean; however, PFBC\Base::filter() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
57
            if (in_array($value, $this->attributes['value'])) {
58
                echo ' checked="checked"';
59
            }
60
61
            echo ' onclick="updateChecksort(this, \'', $this->filter($text), '\');"/></td><td><label for="', $this->attributes['id'], "-", $count, '">', $text, '</label></td></tr></table></div>';
62
63
            if (in_array($value, $this->attributes['value'])) {
64
                $existing .= '<li id="' . $this->attributes['id'] . "-sort-" . $count . '" class="ui-state-default"><input type="hidden" name="' . $this->attributes['name'] . '" value="' . $value . '"/>' . $text . '</li>';
65
            }
66
67
            ++$count;
68
        }
69
        echo '</div>';
70
71
        if (!empty($this->inline)) {
72
            echo '<div style="clear: both;"></div>';
73
        }
74
75
        echo '<ul>', $existing, '</ul></div>';
76
    }
77
78
    public function renderJS()
79
    {
80
        echo <<<JS
81
if(typeof updateChecksort !== 'function') {
82
    function updateChecksort(element, text) {
83
        var position = element.id.lastIndexOf("-");
84
        var id = element.id.substr(0, position);
85
        var index = element.id.substr(position + 1);
86
        if(element.checked)
87
            jQuery("#" + id + " ul").append('<li id="' + id + '-sort-' + index + '" class="ui-state-default"><input type="hidden" name="{$this->attributes['name']}" value="' + element.value + '"/>' + text + '</li>');
88
        else
89
            jQuery("#" + id + "-sort-" + index).remove();
90
    }
91
}
92
JS;
93
    }
94
95
    public function renderCSS()
96
    {
97
        if (!empty($this->inline)) {
98
            echo '#', $this->attributes['id'], ' .pfbc-checkbox { float: left; margin-right: 0.5em; }';
99
        } else {
100
            echo '#', $this->attributes['id'], ' .pfbc-checkbox { padding: 0.5em 0; border-bottom: 1px solid #f4f4f4; }';
101
        }
102
103
        parent::renderCSS();
104
    }
105
}
106