View::tabContents()   B
last analyzed

Complexity

Conditions 7
Paths 6

Size

Total Lines 38
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 7
eloc 27
nc 6
nop 1
dl 0
loc 38
rs 8.5546
c 1
b 1
f 0
1
<?php
2
namespace Prateekkarki\Laragen\Generators\Backend;
3
4
use Illuminate\Support\Str;
5
use Prateekkarki\Laragen\Models\LaragenOptions;
6
use Prateekkarki\Laragen\Generators\BaseGenerator;
7
use Prateekkarki\Laragen\Generators\GeneratorInterface;
8
9
class View extends BaseGenerator implements GeneratorInterface
10
{
11
    protected static $initializeFlag = 0;
12
    protected $generatedFiles = [];
13
14
// ToDo:
15
//     private static $destination = "laragen/app/Http/Controllers/Backend";
16
//     private static $namespace  = "Laragen\App\Http\Controllers\Backend";
17
//     private static $template  = "backend/Request";
18
// self::$template,
19
//             '{{namespace}}'     => self::$namespace,
20
// $this->getPath(self::$destination."/")
21
22
    public function generate()
23
    {
24
25
        $viewsToBeGenerated = ['index', 'create', 'edit'];
26
27
        foreach ($viewsToBeGenerated as $view) {
28
            $viewTemplate = $this->buildTemplate('backend/views/'.$view, [
29
                '{{headings}}' 			 => $this->getHeadings($this->module),
30
                '{{displayFields}}'      => $this->getDisplayFields($this->module, $this->module->getModelNameLowercase()),
31
                '{{modelNameLowercase}}' => Str::singular($this->module->getModuleName()),
32
                '{{modelName}}'          => $this->module->getModelName(),
33
                '{{moduleName}}'         => $this->module->getModuleName(),
34
                '{{tabLinks}}'           => $this->getTabLinks(),
35
                '{{tabContents}}'        => $this->tabContents($view),
36
            ]);
37
38
            $fullFilePath = $this->getPath("resources/views/backend/".$this->module->getModuleName())."/{$view}.blade.php";
39
            file_put_contents($fullFilePath, $viewTemplate);
40
            $this->generatedFiles[] = $fullFilePath;
41
        }
42
43
        $mainMenuFile = $this->getPath("resources/views/backend/includes/")."main_menu.blade.php";
44
45
        if (self::$initializeFlag++ == 0) {
46
            $this->initializeFiles([
47
                $mainMenuFile => "backend/views/includes/main_menu",
48
            ]);
49
        }
50
51
        $this->insertIntoFile(
52
            $mainMenuFile,
53
            '{{-- Main Menu --}}',
54
			"\n".'<li class="nav-item dropdown">
55
                    <a href="#" class="nav-link has-dropdown" data-toggle="dropdown"><i class="fas fa-columns"></i> <span> '.Str::plural($this->module->getModelName()).' </span></a>
56
                    <ul class="dropdown-menu">
57
                        <li><a class="nav-link" href="{{ route("backend.'.$this->module->getModuleName().'.create") }}"> Add new '.Str::plural($this->module->getModelName()).'</a></li>
58
                        <li><a class="nav-link" href="{{ route("backend.'.$this->module->getModuleName().'.index") }}">All '.Str::plural($this->module->getModelName()).'</a></li>
59
                    </ul>
60
                </li>'
61
		);
62
        return $this->generatedFiles;
63
    }
64
65
    public function getHeadings($module, $simple = false)
66
    {
67
        $columns = $module->getDisplayColumns();
68
        $headings = "";
69
        foreach ($columns as $column) {
70
            $headings .=
71
                $simple ?
72
                "<th>".$column->getDisplay()."</th>" : "<th>
73
                    <a href=\"{{ route('backend." . $this->module->getModuleName().".index') }}?sort=".$column->getColumn()."&sort_dir={{ request()->input('sort_dir')=='asc' ? 'desc' : 'asc' }}\">".$column->getDisplay()."
74
                        @if(request()->input('sort')=='" . $column->getColumn()."')
75
                            {!! request()->input('sort_dir')=='asc' ? '<i class=\"fas fa-arrow-down\"></i>' : '<i class=\"fas fa-arrow-up\"></i>' !!}
76
                        @endif
77
                    </a>
78
                </th>";
79
        }
80
        return $headings;
81
    }
82
83
    public function getTabLinks()
84
    {
85
        $tabs = $this->module->getTabTitles();
86
        $data = "";
87
        foreach ($tabs as $key => $tab) {
88
            $activeClass = ($key == 0) ? 'active' : '';
89
            $data .= '<li class="nav-item">'.PHP_EOL.$this->getTabs(7);
90
            $data .= '<a class="nav-link '.$activeClass.'" id="base-tab'.$key.'" data-toggle="tab" aria-controls="tab'.$key.'" href="#tab'.$key.'" aria-expanded="true">'.$tab.'</a>'.PHP_EOL.$this->getTabs(6);
91
            $data .= '</li>';
92
            $data .= ($tab !== last($tabs)) ? PHP_EOL . $this->getTabs(6) : '';
93
        }
94
        return $data;
95
    }
96
97
    public function getDisplayFields($module, $model)
98
    {
99
        $columns = $module->getDisplayColumns();
100
        $data = "";
101
        foreach ($columns as $column) {
102
            $data .= "<td> {{ $".$model."->".$column->getColumn()." }}</td>".PHP_EOL;
103
        }
104
        return $data;
105
    }
106
107
    public function buildFormElement($page, $type)
108
    {
109
        $displayColumn = $type->getRelatedModule() == 'users' ? 'name' : 'title';
110
        if (($type->hasPivot() || $type->isParent()) && $type->getRelatedModule() != 'users') {
111
            $relatedModule = LaragenOptions::getInstance()->getModule($type->getRelatedModule());
112
            $displayColumn = $relatedModule->getDisplayColumns()[0]->getColumn();
113
        }
114
        $formElement = $this->buildTemplate('backend/views/formelements/'.$page.'/'.$type->getFormType(), [
115
            '{{key}}'                       => $type->getColumnKey(),
116
            '{{column}}'                    => $type->getColumn(),
117
            '{{label}}'                     => $type->getDisplay(),
118
            '{{options}}'                   => $type->getFormOptions(),
119
            '{{relatedModule}}'             => $type->getRelatedModule(),
120
            '{{relatedModelLowercase}}'     => $type->getRelatedModelLowercase(),
121
            '{{relatedModelDisplayColumn}}' => $displayColumn,
122
            '{{modelNameLowercase}}'        => $this->module->getModelNameLowercase(),
123
            '{{moduleName}}'                => $this->module->getModuleName(),
124
        ]);
125
        return  "@if(auth()->user()->can('view_".$this->module->getModuleName()."_".$type->getColumnKey()."') || auth()->user()->can('edit_".$this->module->getModuleName()."_".$type->getColumnKey()."') )".PHP_EOL.$formElement.PHP_EOL."@endif".PHP_EOL.PHP_EOL;
126
    }
127
128
129
    public function buildMultiple($page, $type)
130
    {
131
        $displayColumn = $type->getRelatedModule() == 'users' ? 'name' : 'title';
0 ignored issues
show
Unused Code introduced by
The assignment to $displayColumn is dead and can be removed.
Loading history...
132
        $relatedModule = LaragenOptions::getInstance()->getModule($this->module->getModelNameLowercase()."_".$type->getColumn());
133
        $displayColumn = $relatedModule->getDisplayColumns()[0]->getColumn();
134
        $formElement = $this->buildTemplate('backend/views/formelements/'.$page.'/'.$type->getFormType(), [
135
            '{{key}}'                       => $type->getColumn(),
136
            '{{label}}'                     => $type->getDisplay(),
137
            '{{relatedModule}}'             => $type->getRelatedModule(),
138
            '{{headings}}'                  => $this->getHeadings($relatedModule, true),
139
            '{{displayFields}}'             => $this->getDisplayFields($relatedModule, $type->getRelatedModelLowercase()),
140
            '{{relatedModelLowercase}}'     => $type->getRelatedModelLowercase(),
141
            '{{relatedModelDisplayColumn}}' => $displayColumn,
142
            '{{modelNameLowercase}}'        => $this->module->getModelNameLowercase(),
143
            '{{modulename}}'                => $this->module->getModuleName(),
144
        ]).PHP_EOL;
145
        return $formElement;
146
    }
147
148
    public function tabContents($page = "create")
149
    {
150
        if (!in_array($page, ['create', 'edit'])) {
151
            return "";
152
        }
153
        $tabs = $this->module->getTabs();
154
        $tabTitles = $this->module->getTabTitles();
155
        $viewTemplate = '<div class="tab-content px-1 pt-1">'.PHP_EOL;
156
        foreach ($tabs as $key => $tab) {
157
            $activeClass = ($key == 0) ? 'active' : '';
158
            $viewTemplate .= $this->getTabs(6).'<div role="tabpanel" class="tab-pane '.$activeClass.'" id="tab'.$key.'" aria-expanded="true" aria-labelledby="base-tab'.$key.'">'.PHP_EOL;
159
            $viewTemplate .= $this->getTabs(7).'<div class="row mt-4 mb-4">'.PHP_EOL;
160
            $viewTemplate .= $this->getTabs(8).'<div class="col">'.PHP_EOL;
161
162
163
            $typeTemplate = "";
164
            if (is_string($tab) && !in_array($tab, ['hasFile', 'hasImage', 'Seo'])) {
165
                $types = $this->module->getColumnsData();
166
                $type = $types[Str::plural(strtolower(Str::snake($tab)))];
167
                $typeTemplate .= $this->buildMultiple($page, $type);
168
            } else {
169
                foreach ($this->module->getFilteredColumns($tab) as $type) {
170
                    $typeTemplate .= $this->buildFormElement($page, $type);
171
                }
172
            }
173
174
            $viewTemplate .= $this->getTabs(9)."@include('backend.".$this->module->getModuleName().".".$page.".form_parts.".strtolower(Str::title($tabTitles[$key]))."')".PHP_EOL;
175
176
            $fullFilePath = $this->getPath("resources/views/backend/".$this->module->getModuleName()."/".$page."/form_parts/").strtolower(Str::title($tabTitles[$key])).".blade.php";
177
            file_put_contents($fullFilePath, $typeTemplate);
178
            $this->generatedFiles[] = $fullFilePath;
179
180
            $viewTemplate .= $this->getTabs(8).'</div>'.PHP_EOL;
181
            $viewTemplate .= $this->getTabs(7).'</div>'.PHP_EOL;
182
            $viewTemplate .= $this->getTabs(6).'</div>'.PHP_EOL;
183
        }
184
        $viewTemplate .= $this->getTabs(5).'</div>'.PHP_EOL;
185
        return $viewTemplate;
186
    }
187
}
188