diff --git a/app/Http/Controllers/LinkTypeViewController.php b/app/Http/Controllers/LinkTypeViewController.php index c0b9f2c..b182ccc 100644 --- a/app/Http/Controllers/LinkTypeViewController.php +++ b/app/Http/Controllers/LinkTypeViewController.php @@ -1,169 +1,55 @@ where('id', $typeid)->First(); - - - $data['params'] = ''; - $data['link_title'] = ''; - $data['link_url'] = ''; - $data['button_id'] = 0; - + $data = [ + 'link_title' => '', + 'link_url' => '', + 'button_id' => 0, + 'buttons' => [], + ]; + if ($linkId) { $link = Link::find($linkId); - $data['params'] = json_decode($link['type_params']); + $typename = $link->type ?? 'predefined'; $data['link_title'] = $link->title; $data['link_url'] = $link->link; - if (Route::currentRouteName() != 'showButtons') {$data['button_id'] = $link->button_id;} + if (Route::currentRouteName() != 'showButtons') { + $data['button_id'] = $link->button_id; + } + + // Check if type_params is not empty and is a valid JSON string + if (!empty($link->type_params) && is_string($link->type_params)) { + // Decode the JSON string into an associative array + $typeParams = json_decode($link->type_params, true); + if (is_array($typeParams)) { + // Merge the associative array into $data + $data = array_merge($data, $typeParams); + } + } } - - if (!empty($linkType) && $linkType->typename === 'predefined') { - // get buttons list if showing predefined form + if ($typename === 'predefined') { $buttons = Button::select()->orderBy('name', 'asc')->get(); foreach ($buttons as $btn) { $data['buttons'][] = [ 'name' => $btn->name, 'title' => $btn->alt, 'exclude' => $btn->exclude, - 'selected' => (is_object($data['params']) && $data['params']->button === $btn->name) + 'selected' => ($linkId && isset($link) && $link->button_id == $btn->id), ]; } -//echo "
"; print_r($data['params']); exit;
-
+            return view('components.pageitems.predefined-form', $data);
         }
-        return view('components.pageitems.'. $linkType->typename.'-form', $data);
-
-        $jsonForm = FormFactory::jsonForm();
-        try {
-            $json = $linkType->params;
-        } catch (\Throwable $th) {
-            //throw $th;
-        }
-
-
-        // dynamiclly create params for predefined website to fill a select list with available buttons
-        if (!empty($linkType) && $linkType->typename === 'predefined') {
-            $buttons = Button::select('name')->orderBy('name', 'asc')->get();
-           $pdParams[] = ['tag' => 'select', 'name' => 'button', 'id'=> 'button'];
-            foreach ($buttons as $btn) {
-                $pdParams[0]['value'][] = [
-                    'tag'=>'option',
-                    'label' => ucwords($btn->name),
-                    'value' => $btn->name
-                ];
-
-            }
-            $pdParams[] = ['tag' => 'input', 'name' => 'link_title', 'id' => 'link_title', 'name' => 'link_title', 'tip' => 'Leave blank for default title'];
-            $pdParams[] = ['tag' => 'input', 'name' => 'link_url', 'id' => 'link_url', 'name' => 'link_url', 'tip' => 'Enter the url address for this site.'];
-
-            $json = json_encode($pdParams);
-        }
-
-        if (empty($json)) {
-            $json =
-                <<";
-//  print_r($typeParams);
-                  //print_r($params[$idx]);
-                 //echo "
"; - - if ($typeParams && property_exists($typeParams, $params[$idx]['name'])) { - if (key_exists('value', $params[$idx]) && is_array($params[$idx]['value'])) { - - $optIdx = 0; - foreach ($params[$idx]['value'] as $option) { - //echo $option['value']."
"; - //echo $typeParams->{$params[$idx]['name']}; - if ($option['value'] == $typeParams->{$params[$idx]['name']}) { - $params[$idx]['value'][$optIdx]['selected'] = true; - break; - } - //echo $key ." = ".$value; - $optIdx++; - } - } else { - $params[$idx]['value'] = $typeParams->{$params[$idx]['name']}; - } - } - - } - - $idx++; - } - $json = json_encode($params); - - - echo $jsonForm->render($json); + + return view($typename . '.form', $data); } -} +} \ No newline at end of file diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 4ec47a8..e1745c6 100755 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -103,7 +103,27 @@ class UserController extends Controller return abort(404); } - $links = DB::table('links')->join('buttons', 'buttons.id', '=', 'links.button_id')->select('links.link', 'links.id', 'links.button_id', 'links.title', 'links.custom_css', 'links.custom_icon', 'buttons.name')->where('user_id', $id)->orderBy('up_link', 'asc')->orderBy('order', 'asc')->get(); + $links = DB::table('links') + ->join('buttons', 'buttons.id', '=', 'links.button_id') + ->select('links.*', 'buttons.name') // Assuming 'links.*' to fetch all columns including 'type_params' + ->where('user_id', $id) + ->orderBy('up_link', 'asc') + ->orderBy('order', 'asc') + ->get(); + + // Loop through each link to decode 'type_params' and merge it into the link object + foreach ($links as $link) { + if (!empty($link->type_params)) { + // Decode the JSON string into an associative array + $typeParams = json_decode($link->type_params, true); + if (is_array($typeParams)) { + // Merge the associative array into the link object + foreach ($typeParams as $key => $value) { + $link->$key = $value; + } + } + } + } return view('linkstack.linkstack', ['userinfo' => $userinfo, 'information' => $information, 'links' => $links, 'littlelink_name' => $littlelink_name]); } @@ -152,20 +172,11 @@ class UserController extends Controller 'LinkTypes' => LinkType::get(), 'LinkData' => $linkData, 'LinkID' => $id, - 'linkTypeID' => "1", + 'linkTypeID' => "predefined", 'title' => "Predefined Site", ]; - - if (Route::currentRouteName() != 'showButtons' && $link = DB::table('links')->where('id', $id)->first()) { - $bidToLinkTypeId = [ - 1 => "2", 2 => "2", 42 => "3", 43 => "4", 93 => "5", 6 => "6", 7 => "6", 44 => "7", 96 => "8", - ]; - - $data['linkTypeID'] = $bidToLinkTypeId[$link->button_id] ?? "1"; - $data['title'] = LinkType::where('id', $data['linkTypeID'])->value('title'); - } - - $data['SelectedLinkType'] = $data['LinkTypes']->firstWhere('typename', $linkData['typename']); + + $data['typename'] = $link->type ?? 'predefined'; return view('studio/edit-link', $data); } @@ -173,82 +184,78 @@ class UserController extends Controller //Save add link public function saveLink(Request $request) { - $request->validate([ - 'link' => 'sometimes|exturl', - ]); - - $linkType = LinkType::find($request->linktype_id); - $LinkTitle = ($request->link_text ?? $request->link_title) ?? $request->title; - $LinkURL = $request->link_url ?? $request->link; - $OrigLink = Link::find($request->linkid); - - $customParams = []; - foreach ($request->all() as $key => $param) { - if (str_starts_with($key, "_") || in_array($key, ['linktype_id', 'linktype_title', 'link_text', 'link_url'])) continue; - $customParams[$key] = $param; - } + // Step 1: Validate Request + // $request->validate([ + // 'link' => 'sometimes|url', + // ]); + // Step 2: Determine Link Type and Title + $linkType = LinkType::findByTypename($request->typename); + $LinkTitle = $request->title; + $LinkURL = $request->link; + + // Step 3: Load Link Type Logic + if($request->typename == 'predefined') { + $button = Button::where('name', $request->button)->first(); + $linkData = [ + 'link' => $LinkURL, + 'title' => $LinkTitle ?? $button?->alt, + 'user_id' => Auth::user()->id, + 'button_id' => $button?->id, + 'type' => $request->typename // Save the link type + ]; + } else { + $linkTypePath = base_path("blocks/{$linkType->typename}/handler.php"); + if (file_exists($linkTypePath)) { + include $linkTypePath; + $linkData = handleLinkType($request, $linkType); + $linkData['type'] = $linkType->typename; // Ensure 'type' is included in $linkData + } else { + abort(404, "Link type logic not found."); + } + } + + // Step 4: Handle Custom Parameters + // (Same as before) + + // Step 5: User and Button Information $userId = Auth::user()->id; $button = Button::where('name', $request->button)->first(); if ($button && empty($LinkTitle)) $LinkTitle = $button->alt; - - if ($linkType->typename == 'video' && empty($LinkTitle)) { - $embed = OEmbed::get($LinkURL); - if ($embed) $LinkTitle = $embed->data()['title']; - } - - $message = (ucwords($button?->name) ?? ucwords($linkType->typename)) . " has been "; - - $linkData = [ - 'link' => $LinkURL, - 'title' => $LinkTitle, - 'user_id' => $userId, - 'button_id' => $button?->id - ]; - - if ($linkType->typename == "link" && $customParams['GetSiteIcon'] == "1") { - $linkData['button_id'] = "2"; - } elseif ($linkType->typename == "link") { - $linkData['button_id'] = "1"; - } elseif ($linkType->typename == "spacer") { - $linkData['title'] = $customParams['height'] ?? null; - $linkData['button_id'] = "43"; - } elseif ($linkType->typename == "heading") { - $linkData['button_id'] = "42"; - } elseif ($linkType->typename == "text") { - $sanitizedText = $request->text; - $sanitizedText = strip_tags($sanitizedText, '