Passed
Branch master (d88b3b)
by Prateek
03:56
created

View::tabContents()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 36
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 26
c 0
b 0
f 0
nc 6
nop 1
dl 0
loc 36
rs 8.5706
1
<?php
2
namespace Prateekkarki\Laragen\Generators\Backend;
3
4
use Prateekkarki\Laragen\Generators\BaseGenerator;
5
use Prateekkarki\Laragen\Generators\GeneratorInterface;
6
use Prateekkarki\Laragen\Models\Module;
7
use Illuminate\Support\Str;
8
9
class View extends BaseGenerator implements GeneratorInterface
10
{    
11
    protected static $initializeFlag = 0;
12
    protected $generatedFiles = [];
13
14
    public function generate()
15
    {
16
17
        $viewsToBeGenerated = ['index', 'create', 'edit'];
18
19
        foreach ($viewsToBeGenerated as $view) {
20
            $viewTemplate = $this->buildTemplate('backend/views/'.$view, [
21
                '{{headings}}' 			 => $this->getHeadings($this->module),
22
                '{{displayFields}}'      => $this->getDisplayFields($this->module, $this->module->getModelNameLowercase()),
23
                '{{modelNameLowercase}}' => Str::singular($this->module->getModuleName()),
24
                '{{modelName}}'          => $this->module->getModelName(),
25
                '{{moduleName}}'         => $this->module->getModuleName(),
26
                '{{tabLinks}}'           => $this->getTabLinks(),
27
                '{{tabContents}}'        => $this->tabContents($view),
28
            ]);
29
30
            $fullFilePath = $this->getPath("resources/views/backend/".$this->module->getModuleName())."/{$view}.blade.php";
31
            file_put_contents($fullFilePath, $viewTemplate);
32
            $this->generatedFiles[] = $fullFilePath;
33
        }
34
35
        $mainMenuFile = $this->getPath("resources/views/backend/includes/")."main_menu.blade.php";
36
37
        if (self::$initializeFlag++ == 0) {
38
            $this->initializeFiles([
39
                $mainMenuFile => "backend/views/includes/main_menu",
40
            ]);
41
        }
42
43
        $this->insertIntoFile(
44
            $mainMenuFile,
45
            '{{-- Main Menu --}}',
46
			"\n".'<li class="nav-item dropdown">
47
                    <a href="#" class="nav-link has-dropdown" data-toggle="dropdown"><i class="fas fa-columns"></i> <span> '.str_plural($this->module->getModelName()).' </span></a>
0 ignored issues
show
Deprecated Code introduced by
The function str_plural() has been deprecated: Str::plural() should be used directly instead. Will be removed in Laravel 5.9. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

47
                    <a href="#" class="nav-link has-dropdown" data-toggle="dropdown"><i class="fas fa-columns"></i> <span> './** @scrutinizer ignore-deprecated */ str_plural($this->module->getModelName()).' </span></a>

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

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

Loading history...
48
                    <ul class="dropdown-menu">
49
                        <li><a class="nav-link" href="{{ route("backend.'.$this->module->getModuleName().'.create") }}"> Add new '.str_plural($this->module->getModelName()).'</a></li>
0 ignored issues
show
Deprecated Code introduced by
The function str_plural() has been deprecated: Str::plural() should be used directly instead. Will be removed in Laravel 5.9. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

49
                        <li><a class="nav-link" href="{{ route("backend.'.$this->module->getModuleName().'.create") }}"> Add new './** @scrutinizer ignore-deprecated */ str_plural($this->module->getModelName()).'</a></li>

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

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

Loading history...
50
                        <li><a class="nav-link" href="{{ route("backend.'.$this->module->getModuleName().'.index") }}">All '.str_plural($this->module->getModelName()).'</a></li>
0 ignored issues
show
Deprecated Code introduced by
The function str_plural() has been deprecated: Str::plural() should be used directly instead. Will be removed in Laravel 5.9. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

50
                        <li><a class="nav-link" href="{{ route("backend.'.$this->module->getModuleName().'.index") }}">All './** @scrutinizer ignore-deprecated */ str_plural($this->module->getModelName()).'</a></li>

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

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

Loading history...
51
                    </ul>
52
                </li>'
53
		);
54
        return $this->generatedFiles;
55
    }
56
57
    public function getHeadings($module, $simple = false)
58
    {
59
        $columns = $module->getDisplayColumns();
60
        $headings = "";
61
        foreach ($columns as $column) {
62
            $headings .= 
63
                $simple ? 
64
                "<th>" . $column->getDisplay() . "</th>" : 
65
                "<th> 
66
                    <a href=\"{{ route('backend." . $this->module->getModuleName() . ".index') }}?sort=" . $column->getColumn() . "&sort_dir={{ request()->input('sort_dir')=='asc' ? 'desc' : 'asc' }}\">" . $column->getDisplay() . " 
67
                        @if(request()->input('sort')=='" . $column->getColumn() . "')
68
                            {!! request()->input('sort_dir')=='asc' ? '<i class=\"fas fa-arrow-down\"></i>' : '<i class=\"fas fa-arrow-up\"></i>' !!}
69
                        @endif
70
                    </a>
71
                </th>";
72
        }
73
        return $headings;
74
    }
75
76
    public function getTabLinks()
77
    {
78
        $tabs = $this->module->getTabTitles();
79
        $data = "";
80
        foreach ($tabs as $key => $tab) {
81
            $activeClass = ($key==0) ? 'active' : '';
82
            $data .= '<li class="nav-item">'.PHP_EOL.$this->getTabs(7);
83
            $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);
84
            $data .= '</li>';
85
            $data .= ($tab!==last($tabs)) ? PHP_EOL . $this->getTabs(6) : '';
86
        }
87
        return $data;
88
    }
89
90
    public function getDisplayFields($module, $model)
91
    {
92
        $columns = $module->getDisplayColumns();
93
        $data = "";
94
        foreach ($columns as $column) {
95
            $data .= "<td> {{ $" . $model . "->" . $column->getColumn() . " }}</td>" . PHP_EOL;
96
        }
97
        return $data;
98
    }
99
100
    public function buildFormElement($page, $type)
101
    {
102
        $displayColumn = $type->getRelatedModule() == 'users' ? 'name' : 'title';
103
        if (($type->hasPivot() || $type->isParent()) && $type->getRelatedModule() != 'users') {
104
            $relatedModule = app('laragen')->getModule(Str::plural(strtolower(Str::snake($type->getRelatedModel()))));
0 ignored issues
show
Bug introduced by
The method getModule() does not exist on Illuminate\Contracts\Foundation\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

104
            $relatedModule = app('laragen')->/** @scrutinizer ignore-call */ getModule(Str::plural(strtolower(Str::snake($type->getRelatedModel()))));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
105
            $displayColumn = $relatedModule->getDisplayColumns()[0]->getColumn();
106
        }
107
        $formElement = $this->buildTemplate('backend/views/formelements/' . $page . '/' . $type->getFormType(), [
108
            '{{key}}'                       => $type->getColumnKey(),
109
            '{{column}}'                    => $type->getColumn(),
110
            '{{label}}'                     => $type->getDisplay(),
111
            '{{options}}'                   => $type->getFormOptions(),
112
            '{{relatedModule}}'             => $type->getRelatedModule(),
113
            '{{relatedModelLowercase}}'     => $type->getRelatedModelLowercase(),
114
            '{{relatedModelDisplayColumn}}' => $displayColumn,
115
            '{{modelNameLowercase}}'        => $this->module->getModelNameLowercase(),
116
            '{{moduleName}}'                => $this->module->getModuleName(),
117
        ]);
118
        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;
119
    }
120
121
122
    public function buildMultiple($page, $type)
123
    {
124
        $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...
125
        $relatedModule = app('laragen')->getModule($this->module->getModelNameLowercase()."_".$type->getColumn());
126
        $displayColumn = $relatedModule->getDisplayColumns()[0]->getColumn();
127
        $formElement = $this->buildTemplate('backend/views/formelements/' . $page . '/' . $type->getFormType(), [
128
            '{{key}}'                       => $type->getColumn(),
129
            '{{label}}'                     => $type->getDisplay(),
130
            '{{relatedModule}}'             => $type->getRelatedModule(),
131
            '{{headings}}'                  => $this->getHeadings($relatedModule, true),
132
            '{{displayFields}}'             => $this->getDisplayFields($relatedModule, $type->getRelatedModelLowercase()),
133
            '{{relatedModelLowercase}}'     => $type->getRelatedModelLowercase(),
134
            '{{relatedModelDisplayColumn}}' => $displayColumn,
135
            '{{modelNameLowercase}}'        => $this->module->getModelNameLowercase(),
136
            '{{modulename}}'                => $this->module->getModuleName(),
137
        ]) . PHP_EOL;
138
        return $formElement;
139
    }
140
141
    public function tabContents($page = "create")
142
    {
143
        if (!in_array($page, ['create', 'edit'])) return "";
144
        $tabs = $this->module->getTabs();
145
        $tabTitles = $this->module->getTabTitles();
146
        $viewTemplate ='<div class="tab-content px-1 pt-1">'.PHP_EOL;
147
        foreach ($tabs as $key => $tab) {
148
            $activeClass = ($key == 0) ? 'active' : '';
149
            $viewTemplate .= $this->getTabs(6).'<div role="tabpanel" class="tab-pane '.$activeClass.'" id="tab'.$key.'" aria-expanded="true" aria-labelledby="base-tab'.$key.'">'.PHP_EOL;
150
            $viewTemplate .= $this->getTabs(7).'<div class="row mt-4 mb-4">'.PHP_EOL;
151
            $viewTemplate .= $this->getTabs(8).'<div class="col">'.PHP_EOL;
152
153
154
            $typeTemplate = "";
155
            if(is_string($tab)&&!in_array($tab, ['hasFile', 'hasImage', 'Seo'])){
156
                $types = $this->module->getColumnsData();
157
                $type = $types[Str::plural(strtolower(Str::snake($tab)))];
158
                $typeTemplate .= $this->buildMultiple($page, $type);
159
            }else{
160
                foreach ($this->module->getFilteredColumns($tab) as $type) {
161
                    $typeTemplate .= $this->buildFormElement($page, $type);
162
                }
163
            }
164
            
165
            $viewTemplate .= $this->getTabs(9)."@include('backend.".$this->module->getModuleName().".".$page.".form_parts.". strtolower(Str::title($tabTitles[$key])) ."')".PHP_EOL;
166
            
167
            $fullFilePath = $this->getPath("resources/views/backend/".$this->module->getModuleName()."/".$page."/form_parts/").strtolower(Str::title($tabTitles[$key])).".blade.php";
168
            file_put_contents($fullFilePath, $typeTemplate);
169
            $this->generatedFiles[] = $fullFilePath;
170
171
            $viewTemplate .= $this->getTabs(8).'</div>'.PHP_EOL;
172
            $viewTemplate .= $this->getTabs(7).'</div>'.PHP_EOL;
173
            $viewTemplate .= $this->getTabs(6).'</div>'.PHP_EOL;
174
        }
175
        $viewTemplate .= $this->getTabs(5).'</div>'.PHP_EOL;
176
        return $viewTemplate;
177
    }
178
}
179