Completed
Push — master ( 5fc65a...affc4f )
by Song
02:25
created

InputFilter::render()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 33
rs 9.392
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Grid\Column;
4
5
use Encore\Admin\Admin;
6
use Encore\Admin\Grid\Model;
7
8
class InputFilter extends Filter
9
{
10
    /**
11
     * @var string
12
     */
13
    protected $type;
14
15
    /**
16
     * InputFilter constructor.
17
     *
18
     * @param string $type
19
     */
20
    public function __construct($type)
21
    {
22
        $this->type  = $type;
23
        $this->class = uniqid('column-filter-');
24
    }
25
26
    /**
27
     * Add a binding to the query.
28
     *
29
     * @param string $value
30
     *
31
     * @param Model|null $model
32
     */
33
    public function addBinding($value, Model $model)
34
    {
35
        if (empty($value)) {
36
            return;
37
        }
38
39
        if ($this->type == 'like') {
40
            $model->where($this->getColumnName(), 'like', "%{$value}%");
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<Encore\Admin\Grid\Model>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
41
            return;
42
        }
43
44
        if (in_array($this->type, ['date', 'time'])) {
45
            $method = 'where' . ucfirst($this->type);
46
            $model->{$method}($this->getColumnName(), $value);
47
            return;
48
        }
49
50
        $model->where($this->getColumnName(), $value);
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<Encore\Admin\Grid\Model>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
51
    }
52
53
    /**
54
     * Add script to page.
55
     *
56
     * @return void
57
     */
58
    protected function addScript()
59
    {
60
        $options = [
61
            'locale'           => config('app.locale'),
62
            'allowInputToggle' => true,
63
        ];
64
65 View Code Duplication
        if ($this->type == 'date') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
66
            $options['format'] = 'YYYY-MM-DD';
67
        } elseif ($this->type == 'time') {
68
            $options['format'] = 'HH:mm:ss';
69
        } elseif ($this->type == 'datetime') {
70
            $options['format'] = 'YYYY-MM-DD HH:mm:ss';
71
        } else {
72
            return;
73
        }
74
75
        $options = json_encode($options);
76
77
        Admin::script("$('.{$this->class}').datetimepicker($options);");
78
    }
79
80
    /**
81
     * Render this filter.
82
     *
83
     * @return string
84
     */
85
    public function render()
86
    {
87
        $script = <<<SCRIPT
88
$('.dropdown-menu input').click(function(e) {
89
    e.stopPropagation();
90
});
91
SCRIPT;
92
        Admin::script($script);
93
94
        $this->addScript();
95
96
        $active = empty($value) ? '' : 'text-yellow';
0 ignored issues
show
Bug introduced by
The variable $value seems to never exist, and therefore empty should always return true. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
97
98
        return <<<EOT
99
<span class="dropdown">
100
    <form action="{$this->getFormAction()}" pjax-container style="display: inline-block;">
101
    <a href="javascript:void(0);" class="dropdown-toggle {$active}" data-toggle="dropdown">
102
        <i class="fa fa-filter"></i>
103
    </a>
104
    <ul class="dropdown-menu" role="menu" style="padding: 10px;box-shadow: 0 2px 3px 0 rgba(0,0,0,.2);left: -70px;border-radius: 0;">
105
        <li>
106
            <input type="text" name="{$this->getColumnName()}" value="{$this->getFilterValue()}" class="form-control input-sm {$this->class}" autocomplete="off"/>
107
        </li>
108
        <li class="divider"></li>
109
        <li class="text-right">
110
            <button class="btn btn-sm btn-flat btn-primary column-filter-submit pull-left">{$this->trans('submit')}</button>
111
            <button class="btn btn-sm btn-flat btn-default column-filter-all">{$this->trans('reset')}</button>
112
        </li>
113
    </ul>
114
    </form>
115
</span>
116
EOT;
117
    }
118
}