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\DataOption;
|
7
|
|
|
use Prateekkarki\Laragen\Models\Module;
|
8
|
|
|
|
9
|
|
|
class View extends BaseGenerator implements GeneratorInterface
|
10
|
|
|
{
|
11
|
|
|
protected static $initializeFlag = 0;
|
12
|
|
|
|
13
|
|
|
public function generate()
|
14
|
|
|
{
|
15
|
|
|
|
16
|
|
|
$viewsToBeGenerated = ['index', 'create', 'edit'];
|
17
|
|
|
|
18
|
|
|
$generatedFiles = [];
|
19
|
|
|
|
20
|
|
|
foreach ($viewsToBeGenerated as $view) {
|
21
|
|
|
$viewTemplate = $this->buildTemplate('backend/views/' . $view, [
|
22
|
|
|
'{{headings}}' => $this->getHeadings(),
|
23
|
|
|
'{{moduleDisplayName}}' => $this->module->getModuleDisplayName(),
|
24
|
|
|
'{{modelNameLowercase}}' => str_singular($this->module->getModuleName()),
|
|
|
|
|
25
|
|
|
'{{modelName}}' => $this->module->getModelName(),
|
26
|
|
|
'{{moduleName}}' => $this->module->getModuleName()
|
27
|
|
|
]);
|
28
|
|
|
|
29
|
|
|
$fullFilePath = $this->getPath("resources/views/backend/" . $this->module->getModuleName()) . "/{$view}.blade.php";
|
30
|
|
|
file_put_contents($fullFilePath, $viewTemplate);
|
31
|
|
|
$generatedFiles[] = $fullFilePath;
|
32
|
|
|
}
|
33
|
|
|
|
34
|
|
|
$mainMenuFile = $this->getPath("resources/views/backend/includes/")."main_menu.blade.php";
|
35
|
|
|
|
36
|
|
|
if(self::$initializeFlag++ == 0){
|
37
|
|
|
$this->initializeFiles([
|
38
|
|
|
$mainMenuFile => "backend/views/includes/main_menu",
|
39
|
|
|
]);
|
40
|
|
|
}
|
41
|
|
|
|
42
|
|
|
|
43
|
|
|
|
44
|
|
|
$this->insertIntoFile(
|
45
|
|
|
$mainMenuFile,
|
46
|
|
|
'{{-- Main Menu --}}',
|
47
|
|
|
"\n".'<li class="nav-item dropdown">
|
48
|
|
|
<a href="#" class="nav-link has-dropdown" data-toggle="dropdown"><i class="fas fa-columns"></i> <span> '.str_plural($this->module->getModelName()).' </span></a>
|
|
|
|
|
49
|
|
|
<ul class="dropdown-menu">
|
50
|
|
|
<li><a class="nav-link" href="{{ route("backend.'.$this->module->getModuleName().'.create") }}"> Add new '.str_plural($this->module->getModelName()).'</a></li>
|
|
|
|
|
51
|
|
|
<li><a class="nav-link" href="{{ route("backend.'.$this->module->getModuleName().'.index") }}">All '.str_plural($this->module->getModelName()).'</a></li>
|
|
|
|
|
52
|
|
|
</ul>
|
53
|
|
|
</li>'
|
54
|
|
|
);
|
55
|
|
|
$generatedFiles = array_merge($generatedFiles, $this->formGenerateCreate());
|
56
|
|
|
return $generatedFiles;
|
57
|
|
|
}
|
58
|
|
|
|
59
|
|
|
public function getHeadings(){
|
60
|
|
|
$columns = $this->module->getBackendColumnTitles();
|
61
|
|
|
$headings= "";
|
62
|
|
|
foreach ($columns as $column) {
|
63
|
|
|
$headings .= "<th>".$column."</th>";
|
64
|
|
|
}
|
65
|
|
|
return $headings;
|
66
|
|
|
}
|
67
|
|
|
|
68
|
|
|
public function formGenerateCreate()
|
69
|
|
|
{
|
70
|
|
|
$viewTemplate = '';
|
71
|
|
|
foreach($this->module->getData() as $column => $options){
|
72
|
|
|
$columnOptions = new DataOption($column, $options);
|
73
|
|
|
$type = $columnOptions->getType();
|
74
|
|
|
$viewTemplate .= $this->buildTemplate('backend/views/formelements/'.$type, [
|
75
|
|
|
'{{key}}' => $column,
|
76
|
|
|
'{{display}}' => $columnOptions->getDisplay(),
|
77
|
|
|
'{{options}}' => $columnOptions->getFormOptions(),
|
78
|
|
|
'{{parentModule}}' => $columnOptions->getParentModule(),
|
79
|
|
|
'{{parentModuleSinglular}}' => str_singular($columnOptions->getParentModule()),
|
|
|
|
|
80
|
|
|
'{{parentDisplay}}' => $this->getParentDisplay($columnOptions->getParentModule()),
|
81
|
|
|
'{{modelNameLowercase}}' => $this->module->getModelNameLowercase()
|
82
|
|
|
]);
|
83
|
|
|
}
|
84
|
|
|
|
85
|
|
|
$formTemplate = $this->buildTemplate('backend/views/formelements/_form', [
|
86
|
|
|
'{{createElements}}' => $viewTemplate,
|
87
|
|
|
]);
|
88
|
|
|
|
89
|
|
|
$formFilePath = $this->getPath("resources/views/backend/" . $this->module->getModuleName()) . "/_form.blade.php";
|
90
|
|
|
file_put_contents($formFilePath, $formTemplate);
|
91
|
|
|
|
92
|
|
|
$generatedFiles[] = $formFilePath;
|
|
|
|
|
93
|
|
|
return $generatedFiles;
|
94
|
|
|
|
95
|
|
|
}
|
96
|
|
|
|
97
|
|
|
public function getParentDisplay($parentModule)
|
98
|
|
|
{
|
99
|
|
|
$modules = config('laragen.modules');
|
100
|
|
|
$displayColumn = "";
|
101
|
|
|
if(isset($modules[$parentModule])){
|
102
|
|
|
$module = $modules[$parentModule];
|
103
|
|
|
$module = new Module($parentModule, $module);
|
104
|
|
|
$displayColumn = $module->getDisplayColumn();
|
105
|
|
|
}
|
106
|
|
|
|
107
|
|
|
if ($parentModule=='users'){
|
108
|
|
|
$displayColumn = 'name';
|
109
|
|
|
}
|
110
|
|
|
return $displayColumn;
|
111
|
|
|
}
|
112
|
|
|
}
|
113
|
|
|
|
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.