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 = - <<"; - - 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'].""; -// print_r($typeParams); - //print_r($params[$idx]); - //echo "
');
- $sanitizedText = preg_replace("/]*)>/i", "", $sanitizedText);
- $sanitizedText = strip_tags_except_allowed_protocols($sanitizedText);
- $linkData['title'] = $sanitizedText;
- $linkData['button_id'] = "93";
- } elseif (in_array($linkType->typename, ["email", "telephone"])) {
- $linkData['button_id'] = $button?->id;
- } elseif ($linkType->typename == "vcard") {
- $data = $request->only([
- 'prefix', 'first_name', 'middle_name', 'last_name', 'suffix', 'nickname',
- 'organization', 'vtitle', 'role', 'work_url', 'email', 'work_email',
- 'home_phone', 'work_phone', 'cell_phone', 'home_address_label', 'home_address_street',
- 'home_address_city', 'home_address_state', 'home_address_zip', 'home_address_country',
- 'work_address_label', 'work_address_street', 'work_address_city', 'work_address_state',
- 'work_address_zip', 'work_address_country'
- ]);
- $linkData['link'] = json_encode($data);
- $linkData['button_id'] = 96;
- }
-
+
+ // Step 6: Prepare Link Data
+ // (Handled by the included file)
+
+ // Step 7: Save or Update Link
+ $OrigLink = Link::find($request->linkid);
+ $linkColumns = Schema::getColumnListing('links'); // Get all column names of links table
+ $filteredLinkData = array_intersect_key($linkData, array_flip($linkColumns)); // Filter $linkData to only include keys that are columns in the links table
+
+ // Combine remaining variables into one array and convert to JSON for the type_params column
+ $customParams = array_diff_key($linkData, $filteredLinkData);
+
+ // Check if $linkType->custom_html is defined and not null
+ if (isset($linkType->custom_html)) {
+ // Add $linkType->custom_html to the $customParams array
+ $customParams['custom_html'] = $linkType->custom_html;
+ }
+
+ $filteredLinkData['type_params'] = json_encode($customParams);
+
if ($OrigLink) {
- $OrigLink->update($linkData);
- $message .= "updated";
+ $currentValues = $OrigLink->getAttributes();
+ $nonNullFilteredLinkData = array_filter($filteredLinkData, function($value) {return !is_null($value);});
+ $updatedValues = array_merge($currentValues, $nonNullFilteredLinkData);
+ $OrigLink->update($updatedValues);
+ $message = "Link updated";
} else {
- $links = new Link($linkData);
- $links->user_id = $userId;
- $links->save();
- $links->order = ($links->id - 1);
- $links->save();
- $message .= "added";
+ $link = new Link($filteredLinkData);
+ $link->user_id = $userId;
+ $link->save();
+ $message = "Link added";
}
-
+
+ // Step 8: Redirect
$redirectUrl = $request->input('param') == 'add_more' ? 'studio/add-link' : 'studio/links';
return Redirect($redirectUrl)->with('success', $message);
}
diff --git a/app/Models/Link.php b/app/Models/Link.php
index 6b39f5e..4f19727 100644
--- a/app/Models/Link.php
+++ b/app/Models/Link.php
@@ -9,7 +9,7 @@ class Link extends Model
{
use HasFactory;
- protected $fillable = ['link', 'title', 'button_id', 'type_params', 'typename'];
+ protected $fillable = ['link', 'title', 'button_id', 'type_params', 'type'];
protected static function boot()
{
diff --git a/app/Models/LinkType.php b/app/Models/LinkType.php
index a0bee96..10025bd 100644
--- a/app/Models/LinkType.php
+++ b/app/Models/LinkType.php
@@ -1,13 +1,97 @@
directories($blocksPath);
+ $linkTypes = collect();
+
+ // Prepend "predefined" entry to the $linkTypes list
+ $predefinedLinkType = new self([
+ 'id' => 1,
+ 'typename' => 'predefined',
+ 'title' => null,
+ 'description' => null,
+ 'icon' => 'bi bi-boxes',
+ 'custom_html' => false,
+ ]);
+
+ $linkTypes->prepend($predefinedLinkType);
+
+ foreach ($directories as $dir) {
+ $configPath = $dir . '/config.yml';
+ if (file_exists($configPath)) {
+ $configData = Yaml::parse(file_get_contents($configPath));
+
+ // Create a new instance of LinkType for each config file
+ $linkType = new self([
+ 'id' => $configData['id'] ?? 0,
+ 'typename' => $configData['typename'] ?? null,
+ 'title' => $configData['title'] ?? null,
+ 'description' => $configData['description'] ?? null,
+ 'icon' => $configData['icon'] ?? null,
+ 'custom_html' => $configData['custom_html'] ?? [],
+ ]);
+ $linkTypes->push($linkType);
+ }
+ }
+
+ $custom_order = [
+ 'predefined',
+ 'link',
+ 'vcard',
+ 'email',
+ 'telephone',
+ 'heading',
+ 'spacer',
+ 'text',
+ ];
+
+ $sorted = $linkTypes->sortBy(function ($item) use ($custom_order) {
+ $index = array_search($item->typename, $custom_order);
+ return $index !== false ? $index : count($custom_order);
+ });
+
+ return $sorted->values();
+ }
+
+ /**
+ * Check if a LinkType with the given typename exists.
+ *
+ * @param string $typename
+ * @return bool
+ */
+ public static function existsByTypename($typename)
+ {
+ return self::get()->contains('typename', $typename);
+ }
+
+ /**
+ * Find a LinkType by its typename.
+ *
+ * @param string $typename
+ * @return LinkType|null
+ */
+ public static function findByTypename($typename)
+ {
+ return self::get()->firstWhere('typename', $typename);
+ }
}
diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php
index 9dffc74..903dc93 100755
--- a/app/Providers/AppServiceProvider.php
+++ b/app/Providers/AppServiceProvider.php
@@ -6,6 +6,7 @@ use Illuminate\Support\Facades\Validator;
use Illuminate\Support\ServiceProvider;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Facades\View;
class AppServiceProvider extends ServiceProvider
{
@@ -41,5 +42,6 @@ class AppServiceProvider extends ServiceProvider
$allowed_schemes = ['http', 'https', 'mailto', 'tel'];
return in_array(parse_url($value, PHP_URL_SCHEME), $allowed_schemes, true);
});
+ View::addNamespace('blocks', base_path('blocks'));
}
}
diff --git a/blocks/email/config.yml b/blocks/email/config.yml
new file mode 100644
index 0000000..c1ba023
--- /dev/null
+++ b/blocks/email/config.yml
@@ -0,0 +1,5 @@
+id: 6
+typename: email
+title: "Custom Link"
+icon: "bi bi-envelope-fill"
+custom_html: false
\ No newline at end of file
diff --git a/resources/views/components/pageitems/email-form.blade.php b/blocks/email/form.blade.php
similarity index 100%
rename from resources/views/components/pageitems/email-form.blade.php
rename to blocks/email/form.blade.php
diff --git a/blocks/email/handler.php b/blocks/email/handler.php
new file mode 100644
index 0000000..e3c1fbc
--- /dev/null
+++ b/blocks/email/handler.php
@@ -0,0 +1,19 @@
+ $request->title,
+ 'button_id' => "6",
+ 'link' => $request->link,
+ ];
+
+ return $linkData;
+}
\ No newline at end of file
diff --git a/blocks/heading/config.yml b/blocks/heading/config.yml
new file mode 100644
index 0000000..439d9a6
--- /dev/null
+++ b/blocks/heading/config.yml
@@ -0,0 +1,5 @@
+id: 3
+typename: heading
+title: "Custom Link"
+icon: "bi bi-card-heading"
+custom_html: true
\ No newline at end of file
diff --git a/blocks/heading/display.blade.php b/blocks/heading/display.blade.php
new file mode 100644
index 0000000..1b6db7c
--- /dev/null
+++ b/blocks/heading/display.blade.php
@@ -0,0 +1 @@
+
{{ $link->title }}
');
+ $sanitizedText = preg_replace("/]*)>/i", "", $sanitizedText);
+
+ // Assuming strip_tags_except_allowed_protocols is a custom function defined elsewhere
+ // This function should sanitize the text further by removing all tags except those allowed
+ // and ensuring all protocols in href attributes are safe.
+ $sanitizedText = strip_tags_except_allowed_protocols($sanitizedText);
+
+ // Prepare the link data
+ $linkData = [
+ 'title' => $sanitizedText,
+ 'button_id' => "93", // Assuming '93' is a predefined ID for a "text" button
+ ];
+
+ return $linkData;
+}
\ No newline at end of file
diff --git a/blocks/vcard/config.yml b/blocks/vcard/config.yml
new file mode 100644
index 0000000..c18c2f1
--- /dev/null
+++ b/blocks/vcard/config.yml
@@ -0,0 +1,4 @@
+id: 8
+typename: vcard
+title: "Custom Link"
+icon: "bi bi-person-square"
\ No newline at end of file
diff --git a/resources/views/components/pageitems/vcard-form.blade.php b/blocks/vcard/form.blade.php
similarity index 100%
rename from resources/views/components/pageitems/vcard-form.blade.php
rename to blocks/vcard/form.blade.php
diff --git a/blocks/vcard/handler.php b/blocks/vcard/handler.php
new file mode 100644
index 0000000..8fe8ac0
--- /dev/null
+++ b/blocks/vcard/handler.php
@@ -0,0 +1,29 @@
+only([
+ 'prefix', 'first_name', 'middle_name', 'last_name', 'suffix', 'nickname',
+ 'organization', 'vtitle', 'role', 'work_url', 'email', 'work_email',
+ 'home_phone', 'work_phone', 'cell_phone', 'home_address_label', 'home_address_street',
+ 'home_address_city', 'home_address_state', 'home_address_zip', 'home_address_country',
+ 'work_address_label', 'work_address_street', 'work_address_city', 'work_address_state',
+ 'work_address_zip', 'work_address_country'
+ ]);
+
+ // Prepare the link data
+ $linkData = [
+ 'title' => $request->link_title,
+ 'link' => json_encode($data), // Encode the vCard data as JSON
+ 'button_id' => "96",
+ ];
+
+ return $linkData;
+}
\ No newline at end of file
diff --git a/config/view.php b/config/view.php
index 1a27ed6..f3248ce 100755
--- a/config/view.php
+++ b/config/view.php
@@ -16,6 +16,7 @@ return [
'paths' => [
resource_path('views'),
base_path('themes'),
+ base_path('blocks'),
],
/*
diff --git a/database/seeders/ButtonSeeder.php b/database/seeders/ButtonSeeder.php
index ba2fb0e..cfc11da 100755
--- a/database/seeders/ButtonSeeder.php
+++ b/database/seeders/ButtonSeeder.php
@@ -43,7 +43,7 @@ class ButtonSeeder extends Seeder
],
[
- "name" => "buy me a coffee",
+ "name" => "coffee",
"alt" => "Buy Me a Coffee",
"exclude" => false,
"group" => "default",
diff --git a/resources/views/components/pageitems/heading-display.blade.php b/resources/views/components/pageitems/heading-display.blade.php
deleted file mode 100644
index 840d7f0..0000000
--- a/resources/views/components/pageitems/heading-display.blade.php
+++ /dev/null
@@ -1,5 +0,0 @@
-
", $title < 10 ? $title : 10); } else { echo "
"; } @endphp
- @break
- @case('heading')
- {{ $link->title }}