Completed
Pull Request — master (#2905)
by
unknown
03:06
created

Editable::select()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 1
dl 0
loc 17
rs 9.7
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
     * Add options for editable.
32
     *
33
     * @param array $options
34
     */
35
    public function addOptions($options = [])
36
    {
37
        $this->options = array_merge($this->options, $options);
38
    }
39
40
    /**
41
     * Text type editable.
42
     */
43
    public function text()
44
    {
45
    }
46
47
    /**
48
     * Textarea type editable.
49
     */
50
    public function textarea()
51
    {
52
    }
53
54
    /**
55
     * Select type editable.
56
     *
57
     * @param array $options
58
     */
59
    public function select($options = [])
60
    {
61
        if ($options instanceof \Closure) {
62
            $options = $options->call($this, $this->row);
63
        }
64
65
        $source = [];
66
67
        foreach ($options as $key => $value) {
68
            $source[] = [
69
                'value' => $key,
70
                'text'  => $value,
71
            ];
72
        }
73
74
        $this->addOptions(['source' => $source]);
75
    }
76
77
    /**
78
     * Date type editable.
79
     */
80
    public function date()
81
    {
82
        $this->combodate();
83
    }
84
85
    /**
86
     * Datetime type editable.
87
     */
88
    public function datetime()
89
    {
90
        $this->combodate('YYYY-MM-DD HH:mm:ss');
91
    }
92
93
    /**
94
     * Year type editable.
95
     */
96
    public function year()
97
    {
98
        $this->combodate('YYYY');
99
    }
100
101
    /**
102
     * Month type editable.
103
     */
104
    public function month()
105
    {
106
        $this->combodate('MM');
107
    }
108
109
    /**
110
     * Day type editable.
111
     */
112
    public function day()
113
    {
114
        $this->combodate('DD');
115
    }
116
117
    /**
118
     * Combodate type editable.
119
     *
120
     * @param string $format
121
     */
122
    public function combodate($format = 'YYYY-MM-DD')
123
    {
124
        $this->type = 'combodate';
125
126
        $this->addOptions([
127
            'format'     => $format,
128
            'viewformat' => $format,
129
            'template'   => $format,
130
            'combodate'  => [
131
                'maxYear' => 2035,
132
            ],
133
        ]);
134
    }
135
136
    protected function buildEditableOptions(array $arguments = [])
137
    {
138
        $this->type = array_get($arguments, 0, 'text');
139
140
        call_user_func_array([$this, $this->type], array_slice($arguments, 1));
141
    }
142
143
    public function display()
144
    {
145
        $this->options['name'] = $column = $this->column->getName();
146
147
        $class = 'grid-editable-'.str_replace(['.', '#', '[', ']'], '-', $column);
148
149
        $this->buildEditableOptions(func_get_args());
150
151
        $options = json_encode($this->options);
152
153
        Admin::script("$('.$class').editable($options);");
154
155
        $attributes = [
156
            'href'       => '#',
157
            'class'      => "$class",
158
            'data-type'  => $this->type,
159
            'data-pk'    => "{$this->getKey()}",
160
            'data-url'   => "{$this->grid->resource()}/{$this->getKey()}",
161
            'data-value' => "{$this->value}",
162
        ];
163
164
        $attributes = collect($attributes)->map(function ($attribute, $name) {
165
            return "$name='$attribute'";
166
        })->implode(' ');
167
168
        $html = $this->type === 'select' ? '' : $this->value;
169
170
        return "<a $attributes>{$html}</a>";
171
    }
172
}
173