1
1
<?php
2
-
3
2
namespace App \Http \Controllers ;
4
- use Illuminate \Support \Facades \Route ;
5
3
6
4
use Illuminate \Http \Request ;
7
-
8
-
9
5
use App \Models \LinkType ;
10
6
use App \Models \Link ;
11
-
12
7
use App \Models \Button ;
13
- use micro \FormFactory ;
14
-
15
- use DB ;
8
+ use Illuminate \Support \Facades \Route ;
16
9
17
10
class LinkTypeViewController extends Controller
18
11
{
19
-
20
-
21
- public function getParamForm ($ typeid , $ linkId = 0 )
12
+ public function getParamForm ($ typename , $ linkId = 0 )
22
13
{
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
+
31
21
if ($ linkId ) {
32
22
$ link = Link::find ($ linkId );
33
- $ data [ ' params ' ] = json_decode ( $ link[ ' type_params ' ]) ;
23
+ $ typename = $ link-> type ?? ' predefined ' ;
34
24
$ data ['link_title ' ] = $ link ->title ;
35
25
$ 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
+ }
37
39
}
38
-
39
- if (!empty ($ linkType ) && $ linkType ->typename === 'predefined ' ) {
40
- // get buttons list if showing predefined form
40
+ if ($ typename === 'predefined ' ) {
41
41
$ buttons = Button::select ()->orderBy ('name ' , 'asc ' )->get ();
42
42
foreach ($ buttons as $ btn ) {
43
43
$ data ['buttons ' ][] = [
44
44
'name ' => $ btn ->name ,
45
45
'title ' => $ btn ->alt ,
46
46
'exclude ' => $ btn ->exclude ,
47
- 'selected ' => (is_object ( $ data [ ' params ' ] ) && $ data [ ' params ' ]-> button === $ btn ->name )
47
+ 'selected ' => ($ linkId && isset ( $ link ) && $ link -> button_id == $ btn ->id ),
48
48
];
49
49
}
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 );
79
51
}
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 );
168
54
}
169
- }
55
+ }
0 commit comments