Completed
Push — master ( 798e92...ea2dad )
by Yaro
04:12 queued 26s
created

Wysiwyg::getEditFormValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Yaro\Jarboe\Table\Fields;
4
5
use Illuminate\Http\Request;
6
use Yaro\Jarboe\Table\Fields\Traits\Orderable;
7
use Yaro\Jarboe\Table\Fields\Traits\Translatable;
8
9
class Wysiwyg extends AbstractField
10
{
11
    use Orderable;
12
    use Translatable;
13
14
    const SUMMERNOTE = 'summernote';
15
    const TINYMCE = 'tinymce';
16
17
    protected $type = self::SUMMERNOTE;
18
    protected $allowedTypes = [
19
        self::SUMMERNOTE,
20
        self::TINYMCE,
21
    ];
22
    private $options = [];
23
24 2
    public function value(Request $request)
25
    {
26 2
        $value = parent::value($request);
27
28 2
        return is_array($value) ? $value : (string) $value;
29
    }
30
31 3
    public function type(string $type)
32
    {
33 3
        if (in_array($type, $this->allowedTypes)) {
34 2
            $this->type = $type;
35
        }
36
37 3
        return $this;
38
    }
39
40 8
    public function getType()
41
    {
42 8
        return $this->type;
43
    }
44
45 1
    public function options(array $options)
46
    {
47 1
        $this->options = $options;
48
49 1
        return $this;
50
    }
51
52 1
    public function getOptions(): array
53
    {
54 1
        if ($this->options) {
55 1
            return $this->options;
56
        }
57
58 1
        switch ($this->getType()) {
59 1
            case self::SUMMERNOTE:
60 1
                return $this->summernoteDefaultOptions();
61 1
            case self::TINYMCE:
62 1
                return $this->tinymceDefaultOptions();
63
        }
64
    }
65
66 2 View Code Duplication
    public function getListValue($model)
67
    {
68 2
        $template = 'list';
69 2
        if ($this->isTranslatable()) {
70 1
            $template .= '_translatable';
71
        }
72
73 2
        return view(sprintf('jarboe::crud.fields.wysiwyg.%s.%s', $this->getType(), $template), [
74 2
            'model' => $model,
75 2
            'field' => $this,
76
        ]);
77
    }
78
79 1
    public function getEditFormValue($model)
80
    {
81 1
        $template = $this->isReadonly() ? 'readonly' : 'edit';
82
83 1
        return view(sprintf('jarboe::crud.fields.wysiwyg.%s.%s', $this->getType(), $template), [
84 1
            'model' => $model,
85 1
            'field' => $this,
86
        ]);
87
    }
88
89 1
    public function getCreateFormValue()
90
    {
91 1
        return view(sprintf('jarboe::crud.fields.wysiwyg.%s.create', $this->getType()), [
92 1
            'field' => $this,
93
        ]);
94
    }
95
96 1
    private function summernoteDefaultOptions(): array
97
    {
98
        return [
99 1
            'height' => 200,
100
            'codemirror' => [
101
                'theme' => 'monokai',
102
            ],
103
            'toolbar' => [
104
                ['style', ['style']],
105
                ['font', ['bold', 'italic', 'underline', 'clear']],
106
                ['fontname', ['fontname']],
107
                ['color', ['color']],
108
                ['para', ['ul', 'ol', 'paragraph']],
109
                ['height', ['height']],
110
                ['table', ['table']],
111
                ['insert', ['link', 'picture', 'hr']],
112
                ['view', ['codeview', 'fullscreen']],
113
            ],
114
        ];
115
    }
116
117 1
    private function tinymceDefaultOptions(): array
118
    {
119
        return [
120 1
            'plugins' => 'code table lists autoresize link',
121
            'toolbar' => 'undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | fontsizeselect | code | table | link',
122
        ];
123
    }
124
}
125