Completed
Push — master ( 7761d4...144654 )
by Song
02:35
created

Editable   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 227
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 227
rs 10
c 0
b 0
f 0
wmc 17
lcom 1
cbo 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A initOptions() 0 4 1
A addOptions() 0 4 1
A setResource() 0 4 1
A buildEditable() 0 6 1
A text() 0 3 1
A textarea() 0 3 1
A select() 0 13 2
A date() 0 4 1
A datetime() 0 4 1
A year() 0 4 1
A month() 0 4 1
A day() 0 4 1
A combodate() 0 13 1
B html() 0 30 2
1
<?php
2
3
namespace Encore\Admin\Grid;
4
5
use Encore\Admin\Facades\Admin;
6
7
class Editable
8
{
9
    /**
10
     * Class of form element.
11
     *
12
     * @var mixed|string
13
     */
14
    protected $class = '';
15
16
    /**
17
     * @var array
18
     */
19
    protected $arguments = [];
20
21
    /**
22
     * Form element name.
23
     *
24
     * @var string
25
     */
26
    protected $name = '';
27
28
    /**
29
     * Type of editable.
30
     *
31
     * @var string
32
     */
33
    protected $type = '';
34
35
    /**
36
     * Script for editable action.
37
     *
38
     * @var string
39
     */
40
    protected $script = '';
41
42
    /**
43
     * Resource url.
44
     *
45
     * @var string
46
     */
47
    protected $resource = '';
48
49
    /**
50
     * Options of editable function.
51
     *
52
     * @var array
53
     */
54
    protected $options = [];
55
56
    /**
57
     * Generate a new Editable instance.
58
     *
59
     * @param string $name
60
     * @param array  $arguments
61
     */
62
    public function __construct($name, $arguments = [])
63
    {
64
        $this->name = $name;
65
        $this->class = str_replace('.', '_', $name);
66
        $this->arguments = $arguments;
67
68
        $this->initOptions();
69
    }
70
71
    /**
72
     * Initialize options for editable.
73
     */
74
    public function initOptions()
75
    {
76
        $this->options['name'] = $this->name;
77
    }
78
79
    /**
80
     * Add options for editable.
81
     *
82
     * @param array $options
83
     */
84
    public function addOptions($options = [])
85
    {
86
        $this->options = array_merge($this->options, $options);
87
    }
88
89
    /**
90
     * Set resource url.
91
     *
92
     * @param string $resource
93
     */
94
    public function setResource($resource = '')
95
    {
96
        $this->resource = $resource;
97
    }
98
99
    protected function buildEditable(array $arguments = [])
100
    {
101
        $this->type = array_get($arguments, 0, 'text');
102
103
        call_user_func_array([$this, $this->type], array_slice($arguments, 1));
104
    }
105
106
    /**
107
     * Text type editable.
108
     */
109
    public function text()
110
    {
111
    }
112
113
    /**
114
     * Textarea type editable.
115
     */
116
    public function textarea()
117
    {
118
    }
119
120
    /**
121
     * Select type editable.
122
     *
123
     * @param array $options
124
     */
125
    public function select($options = [])
126
    {
127
        $source = [];
128
129
        foreach ($options as $key => $value) {
130
            $source[] = [
131
                'value' => $key,
132
                'text'  => $value,
133
            ];
134
        }
135
136
        $this->addOptions(['source' => $source]);
137
    }
138
139
    /**
140
     * Date type editable.
141
     */
142
    public function date()
143
    {
144
        $this->combodate();
145
    }
146
147
    /**
148
     * Datetime type editable.
149
     */
150
    public function datetime()
151
    {
152
        $this->combodate('YYYY-MM-DD HH:mm:ss');
153
    }
154
155
    /**
156
     * Year type editable.
157
     */
158
    public function year()
159
    {
160
        $this->combodate('YYYY');
161
    }
162
163
    /**
164
     * Month type editable.
165
     */
166
    public function month()
167
    {
168
        $this->combodate('MM');
169
    }
170
171
    /**
172
     * Day type editable.
173
     */
174
    public function day()
175
    {
176
        $this->combodate('DD');
177
    }
178
179
    /**
180
     * Combodate type editable.
181
     *
182
     * @param string $format
183
     */
184
    public function combodate($format = 'YYYY-MM-DD')
185
    {
186
        $this->type = 'combodate';
187
188
        $this->addOptions([
189
            'format'        => $format,
190
            'viewformat'    => $format,
191
            'template'      => $format,
192
            'combodate'     => [
193
                'maxYear' => 2035,
194
            ],
195
        ]);
196
    }
197
198
    /**
199
     * Build html for editable.
200
     *
201
     * @return string
202
     */
203
    public function html()
204
    {
205
        $this->buildEditable($this->arguments);
206
207
        $options = json_encode($this->options);
208
209
        $this->script = <<<EOT
210
211
\$('.{$this->class}-editable').editable($options);
212
213
EOT;
214
215
        Admin::script($this->script);
216
217
        $attributes = [
218
            'href'       => '#',
219
            'class'      => "{$this->class}-editable",
220
            'data-type'  => $this->type,
221
            'data-pk'    => '{pk}',
222
            'data-url'   => "/{$this->resource}/{pk}",
223
            'data-value' => '{$value}',
224
        ];
225
226
        $html = [];
227
        foreach ($attributes as $name => $attribute) {
228
            $html[] = "$name=\"$attribute\"";
229
        }
230
231
        return '<a '.implode(' ', $html).'>{$value}</a>';
232
    }
233
}
234