Add (v1)
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Post;
|
||||
use App\Models\PostCategory;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class PostController extends Controller
|
||||
@@ -27,10 +28,113 @@ public function edit(Request $request, $post_id)
|
||||
{
|
||||
$post = Post::find($post_id);
|
||||
|
||||
if (!is_null($post))
|
||||
{
|
||||
return view('admin.posts.upsert', compact('post'));
|
||||
if (! is_null($post)) {
|
||||
return view('admin.posts.upsert', compact('post'));
|
||||
}
|
||||
|
||||
return redirect()->back()->with('error', 'Post does not exist.');
|
||||
}
|
||||
|
||||
public function postUpsert(Request $request)
|
||||
{
|
||||
|
||||
$post_data = [
|
||||
'id' => $request->input('id', null),
|
||||
'publish_date' => $request->input('publish_date', null),
|
||||
'title' => $request->input('title'),
|
||||
'slug' => $request->input('slug'),
|
||||
'excerpt' => $request->input('excerpt'),
|
||||
'author_id' => intval($request->input('author_id', 1)),
|
||||
'featured' => filter_var($request->input('featured'), FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE),
|
||||
'featured_image' => $request->input('featured_image'),
|
||||
'editor' => 'editorjs',
|
||||
'body' => json_decode($request->input('body')),
|
||||
'post_format' => 'standard',
|
||||
'comment_count' => 0,
|
||||
'likes_count' => 0,
|
||||
'status' => $request->input('status'),
|
||||
];
|
||||
|
||||
$post_categories = $this->normalizeCategories($request->input('categories'));
|
||||
|
||||
if (! empty($post_data['id'])) {
|
||||
// It's an update - find the existing post by its ID
|
||||
$existingPost = Post::find($post_data['id']);
|
||||
|
||||
// Check if the post with the given ID exists
|
||||
if ($existingPost) {
|
||||
// Update the existing post with the new data
|
||||
$existingPost->update($post_data);
|
||||
|
||||
// Handle PostCategory records for the existing post
|
||||
$existingPostCategoryIds = $existingPost?->post_categories->pluck('id')->toArray();
|
||||
|
||||
// Find the IDs of PostCategory records that should be removed
|
||||
$postCategoriesToRemove = array_diff($existingPostCategoryIds, $post_categories);
|
||||
|
||||
// Remove the unwanted PostCategory records
|
||||
if (! empty($postCategoriesToRemove)) {
|
||||
PostCategory::whereIn('id', $postCategoriesToRemove)->delete();
|
||||
}
|
||||
|
||||
// Find the new PostCategory records that should be added
|
||||
$postCategoriesToAdd = array_diff($post_categories, $existingPostCategoryIds);
|
||||
|
||||
// Create the new PostCategory records
|
||||
foreach ($postCategoriesToAdd as $categoryId) {
|
||||
PostCategory::create([
|
||||
'post_id' => $existingPost->id,
|
||||
'category_id' => $categoryId,
|
||||
]);
|
||||
}
|
||||
|
||||
// Return a response indicating a successful update
|
||||
return response()->json(['message' => 'Post updated successfully', 'action' => 'redirect_back']);
|
||||
} else {
|
||||
// If the post with the given ID doesn't exist, you can handle the error as per your requirement
|
||||
return response()->json(['error' => 'Post not found'], 404);
|
||||
}
|
||||
} else {
|
||||
// It's a new post - create a new record using Post::create
|
||||
$newPost = Post::create($post_data);
|
||||
|
||||
// Create the new PostCategory records for the new post
|
||||
foreach ($post_categories as $categoryId) {
|
||||
PostCategory::create([
|
||||
'post_id' => $newPost->id,
|
||||
'category_id' => $categoryId,
|
||||
]);
|
||||
}
|
||||
|
||||
// Return a response indicating a successful creation
|
||||
return response()->json(['message' => 'Post created successfully', 'action' => 'redirect_back']);
|
||||
}
|
||||
}
|
||||
|
||||
private function normalizeCategories($categories)
|
||||
{
|
||||
if (empty($categories) || is_null($categories)) {
|
||||
// If the input is empty or null, return an empty array
|
||||
return [];
|
||||
} elseif (is_numeric($categories)) {
|
||||
// If the input is a numeric value (integer or string), return an array with the integer value
|
||||
return [(int) $categories];
|
||||
} else {
|
||||
// If the input is a string with separated commas or a JSON string that becomes an array of integers, return an array of integers
|
||||
if (is_string($categories)) {
|
||||
// Attempt to convert the string to an array of integers
|
||||
$categoryIds = json_decode($categories, true);
|
||||
|
||||
// Check if the decoding was successful and the result is an array of integers
|
||||
if (is_array($categoryIds) && ! empty($categoryIds)) {
|
||||
$categoryIds = array_map('intval', $categoryIds);
|
||||
|
||||
return $categoryIds;
|
||||
}
|
||||
}
|
||||
|
||||
// If the input format doesn't match any of the above cases, return an empty array
|
||||
return [];
|
||||
}
|
||||
return redirect()->back()->with('error','Post does not exist.');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,10 +8,25 @@
|
||||
use App\Models\Post;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
use Artesaos\SEOTools\Facades\SEOTools;
|
||||
use Artesaos\SEOTools\Facades\SEOMeta;
|
||||
use Artesaos\SEOTools\Facades\OpenGraph;
|
||||
use Artesaos\SEOTools\Facades\JsonLd;
|
||||
use Artesaos\SEOTools\Facades\JsonLdMulti;
|
||||
|
||||
|
||||
class HomeController extends Controller
|
||||
{
|
||||
public function index(Request $request)
|
||||
{
|
||||
|
||||
SEOTools::metatags();
|
||||
SEOTools::twitter();
|
||||
SEOTools::opengraph();
|
||||
SEOTools::jsonLd();
|
||||
SEOTools::setTitle("Top Product Reviews, Deals & New Launches");
|
||||
SEOTools::setDescription("Explore ProductAlert for in-depth product reviews and incredible deals. We cover Beauty, Tech, Home Appliances, Health & Fitness, Parenting, and more.");
|
||||
|
||||
$country = strtolower($request->session()->get('country'));
|
||||
|
||||
return redirect()->route('home.country', ['country' => $country]);
|
||||
@@ -19,6 +34,8 @@ public function index(Request $request)
|
||||
|
||||
public function country(Request $request, $country)
|
||||
{
|
||||
|
||||
|
||||
$country_locale = CountryLocale::where('slug', $country)->first();
|
||||
|
||||
if (! is_null($country_locale)) {
|
||||
@@ -50,6 +67,17 @@ public function country(Request $request, $country)
|
||||
->take(10)
|
||||
->get();
|
||||
|
||||
|
||||
SEOTools::metatags();
|
||||
SEOTools::twitter();
|
||||
SEOTools::opengraph();
|
||||
SEOTools::jsonLd();
|
||||
|
||||
$country_name = get_country_name_by_iso($country_locale->country_iso);
|
||||
|
||||
SEOTools::setTitle("Your {$country_name} Guide to Product Reviews & Top Deals");
|
||||
SEOTools::setDescription("Discover trusted product reviews and unbeatable deals at ProductAlert {$country_name}, your local guide to smart shopping.");
|
||||
|
||||
return view('front.country', compact('country_locale', 'featured_posts', 'latest_posts')
|
||||
);
|
||||
}
|
||||
@@ -83,6 +111,20 @@ public function countryCategory(Request $request, $country, $category)
|
||||
->distinct()
|
||||
->paginate(15);
|
||||
|
||||
SEOTools::metatags();
|
||||
SEOTools::twitter();
|
||||
SEOTools::opengraph();
|
||||
SEOTools::jsonLd();
|
||||
|
||||
$country_name = get_country_name_by_iso($country_locale->country_iso);
|
||||
|
||||
SEOTools::setTitle("Top {$category->name} Reviews in {$country_name}");
|
||||
|
||||
$category_name = strtolower($category->name);
|
||||
|
||||
SEOTools::setDescription("Stay updated with the latest {$category_name} product launches in {$country_name}. Find in-depth reviews and exciting deals with ProductAlert, your guide to {$category_name} shopping.");
|
||||
|
||||
|
||||
return view('front.country_category', compact('country_locale', 'category', 'latest_posts'));
|
||||
}
|
||||
|
||||
@@ -101,6 +143,17 @@ public function all(Request $request, $country)
|
||||
->distinct()
|
||||
->paginate(15);
|
||||
|
||||
SEOTools::metatags();
|
||||
SEOTools::twitter();
|
||||
SEOTools::opengraph();
|
||||
SEOTools::jsonLd();
|
||||
|
||||
$country_name = get_country_name_by_iso($country_locale->country_iso);
|
||||
|
||||
SEOTools::setTitle("Find Product Reviews and Best Deals for {$country_name}");
|
||||
|
||||
SEOTools::setDescription("Discover the latest product reviews and unbeatable deals at ProductAlert, your guide to shopping in {$country_name}. Stay on top of fresh product updates.");
|
||||
|
||||
return view('front.country_all', compact('country_locale', 'latest_posts'));
|
||||
}
|
||||
|
||||
@@ -110,6 +163,32 @@ public function post(Request $request, $country, $post_slug)
|
||||
|
||||
if (! is_null($post)) {
|
||||
|
||||
|
||||
SEOMeta::setTitle($post->title);
|
||||
SEOMeta::setDescription($post->excerpt);
|
||||
SEOMeta::addMeta('article:published_time', $post->publish_date, 'property');
|
||||
SEOMeta::addMeta('article:section', $post->post_category->category->name, 'property');
|
||||
|
||||
OpenGraph::setDescription($post->excerpt);
|
||||
OpenGraph::setTitle($post->title);
|
||||
OpenGraph::setUrl(url()->current());
|
||||
OpenGraph::addProperty('type', 'article');
|
||||
OpenGraph::addProperty('locale', $post->post_category->category->country_locale->i18n);
|
||||
OpenGraph::addImage($post->featured_image);
|
||||
|
||||
$jsonld_multi = JsonLdMulti::newJsonLd();
|
||||
$jsonld_multi->setTitle($post->title)
|
||||
->setDescription($post->excerpt)
|
||||
->setType('Article')
|
||||
->addImage($post->featured_image)
|
||||
->addValue('author', $post->author->name)
|
||||
->addValue('datePublished', $post->publish_at)
|
||||
->addValue('dateCreated', $post->publish_at)
|
||||
->addValue('dateModified', $post->updated_at->format('Y-m-d'))
|
||||
->addValue('description', $post->excerpt)
|
||||
->addValue('articleBody', trim(preg_replace('/\s\s+/', ' ', strip_tags($post->html_body))))
|
||||
;
|
||||
|
||||
return view('front.post', compact('post'));
|
||||
}
|
||||
abort(404);
|
||||
|
||||
@@ -4,13 +4,9 @@
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Intervention\Image\Facades\Image;
|
||||
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
use Intervention\Image\Facades\Image;
|
||||
|
||||
class ImageUploadController extends Controller
|
||||
{
|
||||
@@ -26,8 +22,8 @@ public function index(Request $request)
|
||||
|
||||
// Generate a unique filename for the uploaded file and LQIP version
|
||||
$uuid = Str::uuid()->toString();
|
||||
$fileName = time() . '_' . $uuid . '.jpg';
|
||||
$lqipFileName = time() . '_' . $uuid . '_lqip.jpg';
|
||||
$fileName = time().'_'.$uuid.'.jpg';
|
||||
$lqipFileName = time().'_'.$uuid.'_lqip.jpg';
|
||||
|
||||
// Convert the file to JPEG format using Intervention Image library
|
||||
$image = Image::make($file->getRealPath())->encode('jpg', 100);
|
||||
@@ -46,8 +42,8 @@ public function index(Request $request)
|
||||
$image->encode('jpg', 50);
|
||||
|
||||
// Save the processed image to the 'r2' storage driver under the 'uploads' directory
|
||||
$filePath = 'uploads/' . $fileName;
|
||||
$lqipFilePath = 'uploads/' . $lqipFileName;
|
||||
$filePath = 'uploads/'.$fileName;
|
||||
$lqipFilePath = 'uploads/'.$lqipFileName;
|
||||
Storage::disk('r2')->put($filePath, $image->stream()->detach());
|
||||
|
||||
// Save the original image to a temporary file and open it again
|
||||
@@ -56,7 +52,7 @@ public function index(Request $request)
|
||||
$clonedImage = Image::make($tempImagePath);
|
||||
|
||||
// Create the LQIP version of the image using a small size while maintaining the aspect ratio
|
||||
$lqipImage = $clonedImage->fit(10, 10, function ($constraint) use ($originalWidth, $originalHeight) {
|
||||
$lqipImage = $clonedImage->fit(10, 10, function ($constraint) {
|
||||
$constraint->aspectRatio();
|
||||
});
|
||||
$lqipImage->encode('jpg', 5);
|
||||
|
||||
Reference in New Issue
Block a user