11<?php
2-
32namespace App \Http \Controllers ;
4- use Illuminate \Support \Facades \Route ;
53
64use Illuminate \Http \Request ;
7-
8-
95use App \Models \LinkType ;
106use App \Models \Link ;
11-
127use App \Models \Button ;
13- use micro \FormFactory ;
14-
15- use DB ;
8+ use Illuminate \Support \Facades \Route ;
169
1710class LinkTypeViewController extends Controller
1811{
19-
20-
21- public function getParamForm ($ typeid , $ linkId = 0 )
12+ public function getParamForm ($ typename , $ linkId = 0 )
2213 {
23- $ linkType = LinkType::select ('params ' , 'typename ' )->where ('id ' , $ typeid )->First ();
24-
25-
26- $ data ['params ' ] = '' ;
27- $ data ['link_title ' ] = '' ;
28- $ data ['link_url ' ] = '' ;
29- $ data ['button_id ' ] = 0 ;
30-
14+ $ data = [
15+ 'link_title ' => '' ,
16+ 'link_url ' => '' ,
17+ 'button_id ' => 0 ,
18+ 'buttons ' => [],
19+ ];
20+
3121 if ($ linkId ) {
3222 $ link = Link::find ($ linkId );
33- $ data [ ' params ' ] = json_decode ( $ link[ ' type_params ' ]) ;
23+ $ typename = $ link-> type ?? ' predefined ' ;
3424 $ data ['link_title ' ] = $ link ->title ;
3525 $ data ['link_url ' ] = $ link ->link ;
36- if (Route::currentRouteName () != 'showButtons ' ) {$ data ['button_id ' ] = $ link ->button_id ;}
26+ if (Route::currentRouteName () != 'showButtons ' ) {
27+ $ data ['button_id ' ] = $ link ->button_id ;
28+ }
29+
30+ // Check if type_params is not empty and is a valid JSON string
31+ if (!empty ($ link ->type_params ) && is_string ($ link ->type_params )) {
32+ // Decode the JSON string into an associative array
33+ $ typeParams = json_decode ($ link ->type_params , true );
34+ if (is_array ($ typeParams )) {
35+ // Merge the associative array into $data
36+ $ data = array_merge ($ data , $ typeParams );
37+ }
38+ }
3739 }
38-
39- if (!empty ($ linkType ) && $ linkType ->typename === 'predefined ' ) {
40- // get buttons list if showing predefined form
40+ if ($ typename === 'predefined ' ) {
4141 $ buttons = Button::select ()->orderBy ('name ' , 'asc ' )->get ();
4242 foreach ($ buttons as $ btn ) {
4343 $ data ['buttons ' ][] = [
4444 'name ' => $ btn ->name ,
4545 'title ' => $ btn ->alt ,
4646 'exclude ' => $ btn ->exclude ,
47- 'selected ' => (is_object ( $ data [ ' params ' ] ) && $ data [ ' params ' ]-> button === $ btn ->name )
47+ 'selected ' => ($ linkId && isset ( $ link ) && $ link -> button_id == $ btn ->id ),
4848 ];
4949 }
50- //echo "<pre>"; print_r($data['params']); exit;
51-
52- }
53- return view ('components.pageitems. ' . $ linkType ->typename .'-form ' , $ data );
54-
55- $ jsonForm = FormFactory::jsonForm ();
56- try {
57- $ json = $ linkType ->params ;
58- } catch (\Throwable $ th ) {
59- //throw $th;
60- }
61-
62-
63- // dynamiclly create params for predefined website to fill a select list with available buttons
64- if (!empty ($ linkType ) && $ linkType ->typename === 'predefined ' ) {
65- $ buttons = Button::select ('name ' )->orderBy ('name ' , 'asc ' )->get ();
66- $ pdParams [] = ['tag ' => 'select ' , 'name ' => 'button ' , 'id ' => 'button ' ];
67- foreach ($ buttons as $ btn ) {
68- $ pdParams [0 ]['value ' ][] = [
69- 'tag ' =>'option ' ,
70- 'label ' => ucwords ($ btn ->name ),
71- 'value ' => $ btn ->name
72- ];
73-
74- }
75- $ pdParams [] = ['tag ' => 'input ' , 'name ' => 'link_title ' , 'id ' => 'link_title ' , 'name ' => 'link_title ' , 'tip ' => 'Leave blank for default title ' ];
76- $ pdParams [] = ['tag ' => 'input ' , 'name ' => 'link_url ' , 'id ' => 'link_url ' , 'name ' => 'link_url ' , 'tip ' => 'Enter the url address for this site. ' ];
77-
78- $ json = json_encode ($ pdParams );
50+ return view ('components.pageitems.predefined-form ' , $ data );
7951 }
80-
81- if (empty ($ json )) {
82- $ json =
83- <<<EOS
84- [{
85- "tag": "input",
86- "id": "link_title",
87- "for": "link_title",
88- "label": "Link Title *",
89- "type": "text",
90- "name": "link_title",
91- "class": "form-control",
92- "tip": "Enter a title for this link",
93- "required": "required"
94- },
95- {
96- "tag": "input",
97- "id": "link",
98- "for": "link",
99- "label": "Link Address *",
100- "type": "text",
101- "name": "link_title",
102- "class": "form-control",
103- "tip": "Enter the website address",
104- "required": "required"
105- }
106- ]
107- EOS ;
108- }
109-
110-
111- if ($ linkId ) {
112-
113- $ link = Link::find ($ linkId );
114- }
115-
116-
117- // cleanup json
118- $ params = json_decode ($ json , true );
119- $ idx = 0 ;
120- foreach ($ params as $ p ) {
121- if (!array_key_exists ('for ' , $ p ))
122- $ params [$ idx ]['for ' ] = $ p ['name ' ];
123-
124- if (!array_key_exists ('label ' , $ p ))
125- $ params [$ idx ]['label ' ] = ucwords (preg_replace ('/[^a-zA-Z0-9-]/ ' , ' ' , $ p ['name ' ]));
126-
127- if (!array_key_exists ('label ' , $ p ) || !str_contains ($ p ['class ' ], 'form-control ' )) {
128- $ params [$ idx ]['class ' ] = " form-control " ;
129- }
130-
131- // get existing values if any
132- if ($ link ) {
133- $ typeParams = json_decode ($ link ['type_params ' ]);
134-
135-
136- //echo "<pre>";
137- // print_r($typeParams);
138- //print_r($params[$idx]);
139- //echo "</pre>";
140-
141- if ($ typeParams && property_exists ($ typeParams , $ params [$ idx ]['name ' ])) {
142- if (key_exists ('value ' , $ params [$ idx ]) && is_array ($ params [$ idx ]['value ' ])) {
143-
144- $ optIdx = 0 ;
145- foreach ($ params [$ idx ]['value ' ] as $ option ) {
146- //echo $option['value']."<br />";
147- //echo $typeParams->{$params[$idx]['name']};
148- if ($ option ['value ' ] == $ typeParams ->{$ params [$ idx ]['name ' ]}) {
149- $ params [$ idx ]['value ' ][$ optIdx ]['selected ' ] = true ;
150- break ;
151- }
152- //echo $key ." = ".$value;
153- $ optIdx ++;
154- }
155- } else {
156- $ params [$ idx ]['value ' ] = $ typeParams ->{$ params [$ idx ]['name ' ]};
157- }
158- }
159-
160- }
161-
162- $ idx ++;
163- }
164- $ json = json_encode ($ params );
165-
166-
167- echo $ jsonForm ->render ($ json );
52+
53+ return view ($ typename . '.form ' , $ data );
16854 }
169- }
55+ }
0 commit comments