GOOD SHELL MAS BOY
Server: Apache/2.4.52 (Ubuntu)
System: Linux vmi1836763.contaboserver.net 5.15.0-130-generic #140-Ubuntu SMP Wed Dec 18 17:59:53 UTC 2024 x86_64
User: www-data (33)
PHP: 8.4.10
Disabled: NONE
Upload Files
File: /var/www/html/app/Livewire/Shop/Show.php
<?php

namespace App\Livewire\Shop;

use Livewire\Component;
use Illuminate\Support\Str;
use Livewire\WithFileUploads;
use App\Services\ApiEndpoints;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Session;


class Show extends Component
{
    use WithFileUploads;

    public $shop;
    public $user_id;
    public $selectedCategory;
    public $description;
    public $name;
    public $address;
    public $selectedState;
    public $states = [];
    public $cities = [];
    public $categories = [];
    public $selectedCity;
    public $profile_photo_url;
    public $profile_photo_public_id;
    public $workshop_photo_urls = [];
    public $workshop_photo_public_ids;

    public function mount()
    {
        $this->shop = Session::get("shop");
        Session::forget('success');
        Session::forget('error');

        if ($this->shop === null) {
            $this->getSellerShop();
        }

        // Set the component properties with the shop details
        $this->selectedCategory = $this->shop['category_id'];
        $this->description = $this->shop['description'];
        $this->name = $this->shop['name'];
        $this->address = $this->shop['address'];
        $this->selectedState = $this->shop['state_id'];
        $this->selectedCity = $this->shop['city_id'];
        $this->profile_photo_url = $this->shop['profile_photo_url'];
        $this->profile_photo_public_id = $this->shop['profile_photo_public_id'];
        $this->workshop_photo_urls = $this->shop['workshop_photo_urls'];
        $this->workshop_photo_public_ids = $this->shop['workshop_photo_public_ids'];

        $this->getCategories();
        $this->getStates();
        $this->getCities();
    }

    private function getCategories()
    {
        $apiEndpoints = new ApiEndpoints();
        $headers = $apiEndpoints->header();
        $response = Http::withHeaders($headers)
            ->get(ApiEndpoints::getCategory());
        $this->categories = $response->json()['data'];
    }

    private function getStates()
    {
        $apiEndpoints = new ApiEndpoints();
        $headers = $apiEndpoints->header();
        $response = Http::withHeaders($headers)
            ->get(ApiEndpoints::getCountryStates());
        $this->states = $response->json()['data'];
    }

    private function getCities()
    {
        $body = [
            'state_id' => $this->selectedState,
        ];
        $apiEndpoints = new ApiEndpoints();
        $headers = $apiEndpoints->header();
        $response = Http::withHeaders($headers)
            ->withBody(json_encode($body), 'application/json')
            ->get(ApiEndpoints::getStateCities());
        $this->cities = $response->json()['data'];
    }

    private function getSellerShop()
    {
        $this->user_id = Session::get("user")['id'];

        $body = [
            'user_id' => $this->user_id,
        ];

        $apiEndpoints = new ApiEndpoints();
        $headers = $apiEndpoints->header();
        $response = Http::withHeaders($headers)
            ->withBody(json_encode($body), 'application/json')
            ->get(ApiEndpoints::shopInfo());
        if ($response->successful()) {
            $shop = $response->json()['data'];
            Session::put('shop', $shop);
            $shop = Session::get('shop');
            Session::flash('success', 'Shop Info updated successfully.');
            return redirect()->to('/dashboard-my-shop');
        } else {
            Session::flash('error', 'Failed to update shop. Please try again.');
        }
    }


    public $new_profile_photo_url;
    public $new_profile_photo_public_id;
    public $new_workshop_photo_urls;
    public $new_workshop_photo_public_ids; 

    private function newWorkShopPhoto()
    {
        $new_workshop_photo_urls = [];
        $new_workshop_photo_public_ids = [];

        if ($this->new_workshop_photo_urls) {
            foreach ($this->new_workshop_photo_urls as $photo) {
                $uploadedPhoto = $photo->getRealPath();
                $uploadResult = cloudinary()->upload($uploadedPhoto, ['folder' => 'workshopPhotos']);

                // Add the uploaded photo's details to the new arrays
                $new_workshop_photo_urls[] = $uploadResult->getSecurePath();
                $new_workshop_photo_public_ids[] = $uploadResult->getPublicId();
            }

            // Get existing images (ensure they are arrays)
            $old_workshop_photo_urls = $this->workshop_photo_urls ?? [];
            $old_workshop_photo_public_ids = $this->workshop_photo_public_ids ?? [];

            // Combine old and new images
            $combined_workshop_photo_urls = array_merge($old_workshop_photo_urls, $new_workshop_photo_urls);
            $combined_workshop_photo_public_ids = array_merge($old_workshop_photo_public_ids, $new_workshop_photo_public_ids);

            // Optionally, limit the total to a maximum number of images
            $combined_workshop_photo_urls = array_slice($combined_workshop_photo_urls, 0, 4); // Limit to 4 images
            $combined_workshop_photo_public_ids = array_slice($combined_workshop_photo_public_ids, 0, 4);

            // Update the properties with the combined arrays
            $this->workshop_photo_urls = $combined_workshop_photo_urls;
            $this->workshop_photo_public_ids = $combined_workshop_photo_public_ids;
        } else {
            // If no new images are uploaded, use the existing images
            $this->workshop_photo_urls = $this->workshop_photo_urls ?? [];
            $this->workshop_photo_public_ids = $this->workshop_photo_public_ids ?? [];
        }
    }




    public function deleteWorkShopPhoto($publicId)
    {
        $body = [
            'public_id' => $publicId,
        ];
        $apiEndpoints = new ApiEndpoints();
        $headers = $apiEndpoints->header();
        $response = Http::withHeaders($headers)
            ->withBody(json_encode($body), 'application/json')
            ->delete(ApiEndpoints::deleteWorkShopPhoto());

        if ($response->successful()) {
            $shop = $response->json()['data'];
            Session::put("shop", $shop);
            $this->mount();
            $info = $response->json()['message'];
            $this->addError('workshop_photo_urls', $info);
        } else {
            Log::error('Failed to add a new service ' . $response->json()['message']);
            $this->addError('workshop_photo_urls', $response->json('message'));
        }
    }

    private function updateShopPhoto()
    {
        if ($this->new_profile_photo_url) {
            // Delete the existing profile photo in Cloudinary
            if ($this->profile_photo_public_id) {
                cloudinary()->destroy($this->profile_photo_public_id);
            }

            // Upload the new profile photo
            $uploadedFile = $this->new_profile_photo_url->getRealPath();
            $uploadResult = cloudinary()->upload($uploadedFile, ['folder' => 'sellerProfilePhoto']);

            // Set the new profile photo URL and public ID
            $this->profile_photo_url = $uploadResult->getSecurePath();
            $this->profile_photo_public_id = $uploadResult->getPublicId();
        }
    }

    public $category_id;

    public function updateShop()
    {
        try {

            $this->updateShopPhoto();

            $this->newWorkShopPhoto();

            $body = [
                'category_id' => $this->selectedCategory,
                'name' => $this->name,
                'description' => $this->description,
                'address' => $this->address,
                'city_id' => $this->selectedCity,
                'state_id' => $this->selectedState,
                'profile_photo_url' => $this->profile_photo_url,
                'profile_photo_public_id' => $this->profile_photo_public_id,
                'workshop_photo_urls' => $this->workshop_photo_urls,
                'workshop_photo_public_ids' => $this->workshop_photo_public_ids,
            ];
            $apiEndpoints = new ApiEndpoints();
            $headers = $apiEndpoints->header();
            $response = Http::withHeaders($headers)
                ->withBody(json_encode($body), 'application/json')
                ->put(ApiEndpoints::updateShop());
            if ($response->successful()) {
                $shop = $response->json()['data'];
                Session::put("shop", $shop);
                $this->mount();
                $this->addError('respond', $response->json()['message']);
            } else {
                $this->addError('workshop_photo_urls', $response->json()['message']);
            }
        } catch (\Throwable $th) {
            $this->addError('workshop_photo_urls', $th->getMessage());
        }
    }

    public function render()
    {
        return view('livewire.shop.show');
    }
}