33
44use Illuminate \Support \Str ;
55use Laraveldaily \Quickadmin \Cache \QuickCache ;
6- use Laraveldaily \Quickadmin \Models \Crud ;
6+ use Laraveldaily \Quickadmin \Models \Menu ;
77
88class ControllerBuilder
99{
@@ -21,6 +21,7 @@ class ControllerBuilder
2121 private $ fields ;
2222 private $ relationships ;
2323 private $ files ;
24+ private $ enum ;
2425
2526 /**
2627 * Build our controller file
@@ -29,11 +30,22 @@ public function build()
2930 {
3031 $ cache = new QuickCache ();
3132 $ cached = $ cache ->get ('fieldsinfo ' );
32- $ this ->template = __DIR__ . DIRECTORY_SEPARATOR .'.. ' . DIRECTORY_SEPARATOR .'Templates ' . DIRECTORY_SEPARATOR .'controller ' ;
33+ $ this ->template = __DIR__ . DIRECTORY_SEPARATOR . '.. ' . DIRECTORY_SEPARATOR . 'Templates ' . DIRECTORY_SEPARATOR . 'controller ' ;
3334 $ this ->name = $ cached ['name ' ];
3435 $ this ->fields = $ cached ['fields ' ];
3536 $ this ->relationships = $ cached ['relationships ' ];
3637 $ this ->files = $ cached ['files ' ];
38+ $ this ->enum = $ cached ['enum ' ];
39+ $ this ->names ();
40+ $ template = (string ) $ this ->loadTemplate ();
41+ $ template = $ this ->buildParts ($ template );
42+ $ this ->publish ($ template );
43+ }
44+
45+ public function buildCustom ($ name )
46+ {
47+ $ this ->template = __DIR__ . DIRECTORY_SEPARATOR . '.. ' . DIRECTORY_SEPARATOR . 'Templates ' . DIRECTORY_SEPARATOR . 'customController ' ;
48+ $ this ->name = $ name ;
3749 $ this ->names ();
3850 $ template = (string ) $ this ->loadTemplate ();
3951 $ template = $ this ->buildParts ($ template );
@@ -71,7 +83,8 @@ private function buildParts($template)
7183 '$RELATIONSHIP_COMPACT_EDIT$ ' ,
7284 '$RELATIONSHIP_NAMESPACES$ ' ,
7385 '$FILETRAIT$ ' ,
74- '$FILESAVING$ '
86+ '$FILESAVING$ ' ,
87+ '$ENUM$ ' ,
7588 ], [
7689 $ this ->namespace ,
7790 $ this ->modelName ,
@@ -86,7 +99,8 @@ private function buildParts($template)
8699 $ this ->compactEditBuilder (),
87100 $ this ->relationshipsNamespaces (),
88101 $ this ->files > 0 ? 'use App\Http\Controllers\Traits\FileUploadTrait; ' : '' ,
89- $ this ->files > 0 ? '$this->saveFiles($request); ' : ''
102+ $ this ->files > 0 ? '$this->saveFiles($request); ' : '' ,
103+ $ this ->enum > 0 ? $ this ->enum () : '' ,
90104 ], $ template );
91105
92106 return $ template ;
@@ -122,13 +136,13 @@ public function relationshipsNamespaces()
122136 if ($ this ->relationships == 0 ) {
123137 return '' ;
124138 } else {
125- $ cruds = Crud ::all ()->keyBy ('id ' );
139+ $ menus = Menu ::all ()->keyBy ('id ' );
126140 $ relationships = '' ;
127141 $ first = true ;
128142 foreach ($ this ->fields as $ field ) {
129143 if ($ field ->type == 'relationship ' ) {
130- $ crud = $ cruds [$ field ->relationship_id ];
131- $ relationships .= 'use App \\' . ucfirst (Str::camel ($ crud ->name )) . "; \r\n" ;
144+ $ menu = $ menus [$ field ->relationship_id ];
145+ $ relationships .= 'use App \\' . ucfirst (Str::camel ($ menu ->name )) . "; \r\n" ;
132146 }
133147 }
134148
@@ -145,7 +159,7 @@ public function relationshipsBuilder()
145159 if ($ this ->relationships == 0 ) {
146160 return '' ;
147161 } else {
148- $ cruds = Crud ::all ()->keyBy ('id ' );
162+ $ menus = Menu ::all ()->keyBy ('id ' );
149163 $ relationships = '' ;
150164 $ first = true ;
151165 foreach ($ this ->fields as $ field ) {
@@ -154,11 +168,11 @@ public function relationshipsBuilder()
154168 if (!$ first ) {
155169 $ relationships .= ' ' ;
156170 }
157- $ crud = $ cruds [$ field ->relationship_id ];
171+ $ menu = $ menus [$ field ->relationship_id ];
158172 $ relationships .= '$ '
159173 . $ field ->relationship_name
160174 . ' = '
161- . ucfirst (Str::camel ($ crud ->name ))
175+ . ucfirst (Str::camel ($ menu ->name ))
162176 . '::lists(" '
163177 . $ field ->relationship_field
164178 . '", "id"); '
@@ -176,21 +190,40 @@ public function relationshipsBuilder()
176190 */
177191 public function compactBuilder ()
178192 {
179- if ($ this ->relationships == 0 ) {
193+ if ($ this ->relationships == 0 && $ this -> enum == 0 ) {
180194 return '' ;
181195 } else {
182196 $ compact = ', compact($RELATIONS$) ' ;
183- $ first = true ;
184- foreach ($ this ->fields as $ field ) {
185- if ($ field ->type == 'relationship ' ) {
186- $ toReplace = '' ;
187- if ($ first != true ) {
188- $ toReplace .= ', ' ;
189- } else {
190- $ first = false ;
197+ if ($ this ->relationships > 0 ) {
198+ $ first = true ;
199+ foreach ($ this ->fields as $ field ) {
200+ if ($ field ->type == 'relationship ' ) {
201+ $ toReplace = '' ;
202+ if ($ first != true ) {
203+ $ toReplace .= ', ' ;
204+ } else {
205+ $ first = false ;
206+ }
207+ $ toReplace .= '" ' . $ field ->relationship_name . '"$RELATIONS$ ' ;
208+ $ compact = str_replace ('$RELATIONS$ ' , $ toReplace , $ compact );
209+ }
210+ }
211+ }
212+ if ($ this ->enum > 0 ) {
213+ if (!isset ($ first )) {
214+ $ first = true ;
215+ }
216+ foreach ($ this ->fields as $ field ) {
217+ if ($ field ->type == 'enum ' ) {
218+ $ toReplace = '' ;
219+ if ($ first != true ) {
220+ $ toReplace .= ', ' ;
221+ } else {
222+ $ first = false ;
223+ }
224+ $ toReplace .= '" ' . $ field ->title . '"$RELATIONS$ ' ;
225+ $ compact = str_replace ('$RELATIONS$ ' , $ toReplace , $ compact );
191226 }
192- $ toReplace .= '" ' . $ field ->relationship_name . '"$RELATIONS$ ' ;
193- $ compact = str_replace ('$RELATIONS$ ' , $ toReplace , $ compact );
194227 }
195228 }
196229 $ compact = str_replace ('$RELATIONS$ ' , '' , $ compact );
@@ -205,13 +238,22 @@ public function compactBuilder()
205238 */
206239 public function compactEditBuilder ()
207240 {
208- if ($ this ->relationships == 0 ) {
241+ if ($ this ->relationships == 0 && $ this -> enum == 0 ) {
209242 return '' ;
210243 } else {
211244 $ compact = '' ;
212- foreach ($ this ->fields as $ field ) {
213- if ($ field ->type == 'relationship ' ) {
214- $ compact .= ', " ' . $ field ->relationship_name . '" ' ;
245+ if ($ this ->relationships > 0 ) {
246+ foreach ($ this ->fields as $ field ) {
247+ if ($ field ->type == 'relationship ' ) {
248+ $ compact .= ', " ' . $ field ->relationship_name . '" ' ;
249+ }
250+ }
251+ }
252+ if ($ this ->enum > 0 ) {
253+ foreach ($ this ->fields as $ field ) {
254+ if ($ field ->type == 'enum ' ) {
255+ $ compact .= ', " ' . $ field ->title . '" ' ;
256+ }
215257 }
216258 }
217259
@@ -239,11 +281,24 @@ private function names()
239281 */
240282 private function publish ($ template )
241283 {
242- if (!file_exists (app_path ('Http ' . DIRECTORY_SEPARATOR .'Controllers ' . DIRECTORY_SEPARATOR .'Admin ' ))) {
243- mkdir (app_path ('Http ' . DIRECTORY_SEPARATOR .'Controllers ' . DIRECTORY_SEPARATOR .'Admin ' ));
244- chmod (app_path ('Http ' . DIRECTORY_SEPARATOR .'Controllers ' . DIRECTORY_SEPARATOR .'Admin ' ), 0777 );
284+ if (!file_exists (app_path ('Http ' . DIRECTORY_SEPARATOR . 'Controllers ' . DIRECTORY_SEPARATOR . 'Admin ' ))) {
285+ mkdir (app_path ('Http ' . DIRECTORY_SEPARATOR . 'Controllers ' . DIRECTORY_SEPARATOR . 'Admin ' ));
286+ chmod (app_path ('Http ' . DIRECTORY_SEPARATOR . 'Controllers ' . DIRECTORY_SEPARATOR . 'Admin ' ), 0777 );
245287 }
246- file_put_contents (app_path ('Http ' . DIRECTORY_SEPARATOR .'Controllers ' . DIRECTORY_SEPARATOR .'Admin ' . DIRECTORY_SEPARATOR . $ this ->fileName ), $ template );
288+ file_put_contents (app_path ('Http ' . DIRECTORY_SEPARATOR . 'Controllers ' . DIRECTORY_SEPARATOR . 'Admin ' . DIRECTORY_SEPARATOR . $ this ->fileName ),
289+ $ template );
290+ }
291+
292+ public function enum ()
293+ {
294+ $ return = "\r\n" ;
295+ foreach ($ this ->fields as $ field ) {
296+ if ($ field ->type == 'enum ' ) {
297+ $ return .= ' $ ' . $ field ->title . ' = ' . $ this ->modelName . '::$ ' . $ field ->title . "; \r\n" ;
298+ }
299+ }
300+
301+ return $ return ;
247302 }
248303
249304}
0 commit comments