Completed
Push — master ( 9a8993...02f155 )
by Song
06:07 queued 03:47
created

Editable::time()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Grid\Displayers;
4
5
use Encore\Admin\Admin;
6
7
class Editable extends AbstractDisplayer
8
{
9
    /**
10
     * @var array
11
     */
12
    protected $arguments = [];
13
14
    /**
15
     * Type of editable.
16
     *
17
     * @var string
18
     */
19
    protected $type = '';
20
21
    /**
22
     * Options of editable function.
23
     *
24
     * @var array
25
     */
26
    protected $options = [
27
        'emptytext'  => '<i class="fa fa-pencil"></i>',
28
    ];
29
30
    /**
31
     * @var array
32
     */
33
    protected $attributes = [];
34
35
    /**
36
     * Add options for editable.
37
     *
38
     * @param array $options
39
     */
40
    public function addOptions($options = [])
41
    {
42
        $this->options = array_merge($this->options, $options);
43
    }
44
45
    /**
46
     * Add attributes for editable.
47
     *
48
     * @param array $attributes
49
     */
50
    public function addAttributes($attributes = [])
51
    {
52
        $this->attributes = array_merge($this->attributes, $attributes);
53
    }
54
55
    /**
56
     * Text type editable.
57
     */
58
    public function text()
59
    {
60
    }
61
62
    /**
63
     * Textarea type editable.
64
     */
65
    public function textarea()
66
    {
67
    }
68
69
    /**
70
     * Select type editable.
71
     *
72
     * @param array|\Closure $options
73
     */
74
    public function select($options = [])
75
    {
76
        $useClosure = false;
77
78
        if ($options instanceof \Closure) {
79
            $useClosure = true;
80
            $options = $options->call($this, $this->row);
81
        }
82
83
        $source = [];
84
85
        foreach ($options as $value => $text) {
86
            $source[] = compact('value', 'text');
87
        }
88
89
        if ($useClosure) {
90
            $this->addAttributes(['data-source' => json_encode($source)]);
91
        } else {
92
            $this->addOptions(compact('source'));
93
        }
94
    }
95
96
    /**
97
     * Date type editable.
98
     */
99
    public function date()
100
    {
101
        $this->combodate();
102
    }
103
104
    /**
105
     * Datetime type editable.
106
     */
107
    public function datetime()
108
    {
109
        $this->combodate('YYYY-MM-DD HH:mm:ss');
110
    }
111
112
    /**
113
     * Year type editable.
114
     */
115
    public function year()
116
    {
117
        $this->combodate('YYYY');
118
    }
119
120
    /**
121
     * Month type editable.
122
     */
123
    public function month()
124
    {
125
        $this->combodate('MM');
126
    }
127
128
    /**
129
     * Day type editable.
130
     */
131
    public function day()
132
    {
133
        $this->combodate('DD');
134
    }
135
    
136
    /**
137
     * Time type editable.
138
     */
139
    public function time()
140
    {
141
        $this->combodate('HH:mm:ss');
142
    }
143
144
    /**
145
     * Combodate type editable.
146
     *
147
     * @param string $format
148
     */
149
    public function combodate($format = 'YYYY-MM-DD')
150
    {
151
        $this->type = 'combodate';
152
153
        $this->addOptions([
154
            'format'     => $format,
155
            'viewformat' => $format,
156
            'template'   => $format,
157
            'combodate'  => [
158
                'maxYear' => 2035,
159
            ],
160
        ]);
161
    }
162
163
    protected function buildEditableOptions(array $arguments = [])
164
    {
165
        $this->type = array_get($arguments, 0, 'text');
0 ignored issues
show
Deprecated Code introduced by
The function array_get() has been deprecated with message: Arr::get() should be used directly instead. Will be removed in Laravel 5.9.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
166
167
        call_user_func_array([$this, $this->type], array_slice($arguments, 1));
168
    }
169
170
    public function display()
171
    {
172
        $this->options['name'] = $column = $this->column->getName();
173
174
        $class = 'grid-editable-'.str_replace(['.', '#', '[', ']'], '-', $column);
175
176
        $this->buildEditableOptions(func_get_args());
177
178
        $options = json_encode($this->options);
179
180
        Admin::script("$('.$class').editable($options);");
181
182
        $this->value = htmlentities($this->value);
183
184
        $attributes = [
185
            'href'       => '#',
186
            'class'      => "$class",
187
            'data-type'  => $this->type,
188
            'data-pk'    => "{$this->getKey()}",
189
            'data-url'   => "{$this->grid->resource()}/{$this->getKey()}",
190
            'data-value' => "{$this->value}",
191
        ];
192
193
        if (!empty($this->attributes)) {
194
            $attributes = array_merge($attributes, $this->attributes);
195
        }
196
197
        $attributes = collect($attributes)->map(function ($attribute, $name) {
198
            return "$name='$attribute'";
199
        })->implode(' ');
200
201
        $html = $this->type === 'select' ? '' : $this->value;
202
203
        return "<a $attributes>{$html}</a>";
204
    }
205
}
206