1 <?php
2
3 namespace Scaffolder\Compilers\View;
4
5 use Illuminate\Support\Facades\File;
6 use Scaffolder\Compilers\AbstractViewCompiler;
7 use Scaffolder\Support\Contracts\ScaffolderThemeExtensionInterface;
8 use Scaffolder\Support\FileToCompile;
9 use Scaffolder\Support\PathParser;
10 use stdClass;
11
12 class IndexViewCompiler extends AbstractViewCompiler
13 {
14 15 16 17 18 19 20 21 22 23 24 25 26 27
28 public function compile($stub, $modelName, $modelData, stdClass $scaffolderConfig, $hash, ScaffolderThemeExtensionInterface $themeExtension, array $extensions, $extra = null)
29 {
30 if (File::exists(base_path('scaffolder-config/cache/view_index_' . $hash . self::CACHE_EXT)))
31 {
32 return $this->store($modelName, $scaffolderConfig, '', new FileToCompile(true, $hash));
33 }
34 else
35 {
36 $this->stub = $stub;
37
38 $this->replaceClassName($modelName)
39 ->replaceBreadcrumb($modelName, $modelData->modelLabel)
40 ->addDatatableFields($modelName, $modelData)
41 ->setTableHeaders($modelData)
42 ->replaceRoutePrefix($scaffolderConfig->routing->prefix);
43
44 $this->stub = $themeExtension->runAfterIndexViewIsCompiled($this->stub, $modelData, $scaffolderConfig);
45
46 foreach ($extensions as $extension)
47 {
48 $this->stub = $extension->runAfterIndexViewIsCompiled($this->stub, $modelData, $scaffolderConfig);
49 }
50
51 return $this->store($modelName, $scaffolderConfig, $this->stub, new FileToCompile(false, $hash));
52 }
53 }
54
55 56 57 58 59 60 61 62 63 64
65 protected function store($modelName, stdClass $scaffolderConfig, $compiled, FileToCompile $fileToCompile)
66 {
67 $path = PathParser::parse($scaffolderConfig->paths->views) . strtolower($modelName) . '/index.blade.php';
68
69
70 if ($fileToCompile->cached)
71 {
72 File::copy(base_path('scaffolder-config/cache/view_index_' . $fileToCompile->hash . self::CACHE_EXT), $path);
73 }
74 else
75 {
76 File::put(base_path('scaffolder-config/cache/view_index_' . $fileToCompile->hash . self::CACHE_EXT), $compiled);
77 File::copy(base_path('scaffolder-config/cache/view_index_' . $fileToCompile->hash . self::CACHE_EXT), $path);
78 }
79
80 return $path;
81 }
82
83 84 85 86 87 88 89 90
91 private function addDatatableFields($modelName, $modelData)
92 {
93 $fields = '';
94 $firstIteration = true;
95
96 foreach ($modelData->fields as $field)
97 {
98 if ($field->hideInListings === false)
99 {
100 if ($firstIteration)
101 {
102 $fields .= sprintf("{ data: '%s', name: '%s' }," . PHP_EOL, $field->name, $field->name);
103 $firstIteration = false;
104 }
105 else
106 {
107 $fields .= sprintf($this->tab(4) . "{ data: '%s', name: '%s' }," . PHP_EOL, $field->name, $field->name);
108 }
109 }
110 }
111
112 $this->stub = str_replace('{{datatable_fields}}', $fields, $this->stub);
113 $this->stub = str_replace('{{datatable_url}}', ucfirst($modelName), $this->stub);
114
115 return $this;
116 }
117
118 119 120 121 122 123 124
125 private function setTableHeaders($modelData)
126 {
127 $fields = '';
128 $firstIteration = true;
129
130 foreach ($modelData->fields as $field)
131 {
132 if ($field->hideInListings === false)
133 {
134 if ($firstIteration)
135 {
136 $fields .= sprintf("<th>%s</th>" . PHP_EOL, ucfirst($field->name));
137 $firstIteration = false;
138
139 }
140 else
141 {
142 $fields .= sprintf($this->tab(3) . "<th>%s</th>" . PHP_EOL, ucfirst($field->name));
143 }
144 }
145 }
146
147 $this->stub = str_replace('{{table_headers}}', $fields, $this->stub);
148
149 return $this;
150 }
151
152 153 154 155 156 157 158
159 private function replaceRoutePrefix($prefix)
160 {
161 $this->stub = str_replace('{{route_prefix}}', $prefix, $this->stub);
162
163 return $this;
164 }
165 }
166