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>
|
|
|
|
|
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>
|
|
|
|
|
50
|
|
|
<li><a class="nav-link" href="{{ route("backend.'.$this->module->getModuleName().'.index") }}">All '.str_plural($this->module->getModelName()).'</a></li>
|
|
|
|
|
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()))));
|
|
|
|
|
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';
|
|
|
|
|
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
|
|
|
|
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.