n^4

mail@pastecode.io avatar
unknown
php
a month ago
15 kB
8
Indexable
Never
<?php

namespace App\Console\Commands;

use App\Blog;
use App\Branch;
use App\Career;
use App\Doctor;
use App\FaqCategory;
use App\PatientStory;
use App\Services;
use App\VisitingDoctor;
use Spatie\Sitemap\Tags\Url;
use Illuminate\Console\Command;
use Spatie\Sitemap\Sitemap;

class GenerateSitemap extends Command
{
    protected $signature = 'sitemap:generate';
    protected $description = 'Generate the sitemap for the website';

    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        set_time_limit(0);
        ini_set('memory_limit', '512M');
        $countries = ['sa', 'eg', 'ae', 'qa', 'ye'];
        $specializations = ['eye', 'dental', 'ent'];
        $languages = ['en_US', 'ar'];

        $sitemap = Sitemap::create();

        $pagesCounter = 0;
        // Generate URLs based on the parameters
        foreach ($countries as $country) {
            foreach ($specializations as $specialization) {
                foreach ($languages as $language) {
                    $url = url("/$country/$specialization/$language");
                    $sitemap->add(Url::create($url));
                    $pagesCounter++;
                }
            }
        }

        // Generate URLs for the pages
        $peges = [
            'services',
            'branches',
            'our-doctors',
            'visit-doctor',
            'patient-stories',
            'blogs',
            'news',
            'contact',
            'faq',
            'job-applications',
            'about-us',
            'about-us/missions',
            'about-us/milestones',
            'about-us/board-of-directors',
            'about-us/ceo-message',
            'about-us/executive-team',
            'about-us/awards',
            'tenders-vendors',
            'satisfaction',
            'outpatient-survey',
            'inpatient-survey',
            'privacy-policy',
            'all-rights-reserved'
        ];

        foreach ($peges as $page) {
            foreach ($countries as $country) {
                foreach ($specializations as $specialization) {
                    foreach ($languages as $language) {
                        $url = url("/$country/$specialization/$language/$page");
                        $sitemap->add(Url::create($url)
                            ->setChangeFrequency('monthly')
                            ->setPriority(1.0));
                        $pagesCounter++;
                    }
                }
            }
        }

        //blogs
        $blogsCounter = 0;
        Blog::blogs()->withoutGlobalScopes()->with(['countries', 'specializations'])->chunk(50, function ($blogs) use ($sitemap, $languages, $countries, $specializations, &$blogsCounter) {
            foreach ($blogs as $blog) {
                $blogCountries = $blog->countries->isEmpty() ? $countries : $blog->countries;
                $blogSpecializations = $blog->specializations->isEmpty() ? $specializations : $blog->specializations;
                foreach ($blogCountries as $country) {
                    foreach ($blogSpecializations as $specialization) {
                        foreach ($languages as $language) {
                            if ($blog->lang == $language) {
                                $countryCode = is_string($country) ? $country : $country->code;
                                $specializationCode = is_string($specialization) ? $specialization : $specialization->code;

                                $slug = urldecode($blog->slug);
                                $url = url("/$countryCode/$specializationCode/$language/blogs/{$slug}");
                                $sitemap->add(Url::create($url)->setLastModificationDate($blog->updated_at)
                                        ->setChangeFrequency('monthly')
                                        ->setPriority(0.5));

                                $blogsCounter++;
                            }
                        }
                    }
                }
            }
        });

        //news
        $newsCounter = 0;
        Blog::news()->withoutGlobalScopes()->with(['countries', 'specializations'])->chunk(50, function ($blogs) use ($sitemap, $languages, $countries, $specializations, &$newsCounter) {
            foreach ($blogs as $blog) {
                $blogCountries = $blog->countries->isEmpty() ? $countries : $blog->countries;
                $blogSpecializations = $blog->specializations->isEmpty() ? $specializations : $blog->specializations;
                foreach ($blogCountries as $country) {
                    foreach ($blogSpecializations as $specialization) {
                        foreach ($languages as $language) {
                            if ($blog->lang == $language) {
                                $countryCode = is_string($country) ? $country : $country->code;
                                $specializationCode = is_string($specialization) ? $specialization : $specialization->code;

                                $slug = urldecode($blog->slug);
                                $url = url("/$countryCode/$specializationCode/$language/news/{$slug}");
                                $sitemap->add(Url::create($url)
                                        ->setLastModificationDate($blog->updated_at)
                                        ->setChangeFrequency('monthly')
                                        ->setPriority(0.5));
                                $newsCounter++;
                            }
                        }
                    }
                }
            }
        });


        //services
        $servicesCounter = 0;
        Services::withoutGlobalScopes()->with(['countries', 'specializations'])->chunk(50, function ($services) use ($sitemap, $languages, $countries, $specializations, &$servicesCounter) {
            foreach ($services as $service) {
                $serviceCountries = $service->countries->isEmpty() ? $countries : $service->countries;
                $serviceSpecializations = $service->specializations->isEmpty() ? $specializations : $service->specializations;
                foreach ($serviceCountries as $country) {
                    foreach ($serviceSpecializations as $specialization) {
                        foreach ($languages as $language) {
                            if ($service->lang == $language) {
                                $countryCode = is_string($country) ? $country : $country->code;
                                $specializationCode = is_string($specialization) ? $specialization : $specialization->code;

                                $slug = urldecode($service->slug);
                                $url = url("/$countryCode/$specializationCode/$language/services/{$slug}");
                                $sitemap->add(Url::create($url)
                                        ->setLastModificationDate($service->updated_at)
                                        ->setChangeFrequency('monthly')
                                        ->setPriority(0.8));
                                $servicesCounter++;
                            }
                        }
                    }
                }
            }
        });


        // Process doctors
        $doctorsCounter = 0;
        Doctor::withoutGlobalScopes()->with(['country', 'specialization'])->chunk(50, function ($doctors) use ($sitemap, $languages, &$doctorsCounter) {
            foreach ($doctors as $doctor) {
                foreach ($languages as $language) {
                    $countryCode = $doctor->country?->code;
                    $specializationCode = $doctor->specialization?->code;

                    $slug = $doctor->slug;
                    $url = url("/$countryCode/$specializationCode/$language/our-doctors/{$slug}");
                    $sitemap->add(Url::create($url)
                        ->setLastModificationDate($doctor->updated_at)
                        ->setChangeFrequency('weekly')
                        ->setPriority(0.8));
                    $doctorsCounter++;
                }
            }
        });

        // Process branches
        $branchesCounter = 0;
        Branch::withoutGlobalScopes()->with(['country', 'specializations'])->chunk(50, function ($branches) use ($sitemap,$specializations, $languages, &$branchesCounter) {
            foreach ($branches as $branch) {
                foreach ($languages as $language) {
                    $countryCode = $branch->country?->code;
                    $branchSpecializations = $branch->specializations->isEmpty() ? $specializations : $branch->specializations;
                    foreach ($branchSpecializations as $specialization) {
                        $specializationCode = is_string($specialization) ? $specialization : $specialization->code;
                        $slug = urlencode($branch->slug);
                        $url = url("/$countryCode/$specializationCode/$language/branches/{$slug}");
                        $sitemap->add(Url::create($url)
                            ->setLastModificationDate($branch->updated_at)
                            ->setChangeFrequency('weekly')
                            ->setPriority(0.8));
                        $branchesCounter++;
                    }
                }
            }
        });

        // Process visiting doctors
        $visitingDoctorsCounter = 0;
        VisitingDoctor::withoutGlobalScopes()->with(['country', 'specialization'])->chunk(50, function ($visitingDoctors) use ($sitemap, $languages, &$visitingDoctorsCounter) {
            foreach ($visitingDoctors as $visitingDoctor) {
                foreach ($languages as $language) {
                    $countryCode = is_string($visitingDoctor->country) ? $visitingDoctor->country : $visitingDoctor->country?->code;
                    $specializationCode = $visitingDoctor->specialization?->code;

                    $slug = $visitingDoctor->slug;
                    $url = url("/$countryCode/$specializationCode/$language/visiting-doctor/{$slug}");
                    $sitemap->add(Url::create($url)
                        ->setLastModificationDate($visitingDoctor->updated_at)
                        ->setChangeFrequency('weekly')
                        ->setPriority(0.9));
                    $visitingDoctorsCounter++;
                }
            }
        });

        // Process patent stories
        $patentStoriesCounter = 0;
        PatientStory::withoutGlobalScopes()->with(['specialization'])->chunk(50, function ($patientStories) use ($sitemap, &$patentStoriesCounter) {
            foreach ($patientStories as $patientStory) {
                $specializationCode = $patientStory->specialization?->code;
                if ($specializationCode && $patientStory->lang) {
                    $slug = $patientStory->slug;
                    $url = url("/$specializationCode/{$patientStory->lang}/patent-stories?story={$slug}");
                    $sitemap->add(Url::create($url)
                        ->setLastModificationDate($patientStory->updated_at)
                        ->setChangeFrequency('weekly')
                        ->setPriority(0.6));
                    $patentStoriesCounter++;
                }
            }
        });


        // Process FAQ categories
        $faqCategoriesCounter = 0;
        FaqCategory::withoutGlobalScopes()->with(['specializations'])->chunk(50, function ($faqCategories) use ($sitemap, $countries, $languages, &$faqCategoriesCounter) {
            foreach ($faqCategories as $faqCategory) {
                foreach ($faqCategory->specializations as $specialization) {
                    foreach ($countries as $country) {
                        foreach ($languages as $language) {
                            $slug = $faqCategory->slug;
                            $url = url("/$country/$specialization?->code/$language/faq/{$slug}");
                            $sitemap->add(Url::create($url)
                                ->setLastModificationDate($faqCategory->updated_at)
                                ->setChangeFrequency('weekly')
                                ->setPriority(0.5));
                            $faqCategoriesCounter++;
                        }
                    }
                }
            }
        });

        // Process careers
        $careersCounter = 0;
        Career::withoutGlobalScopes()->chunk(50, function ($careers) use ($sitemap, $countries,$specializations, $languages, &$careersCounter) {
            foreach ($careers as $career) {
                foreach ($specializations as $specialization) {
                    foreach ($countries as $country) {
                        foreach ($languages as $language) {
                            $url = url("/$country/$specialization/$language/job-application/{$career->id}");
                            $sitemap->add(Url::create($url)
                                ->setLastModificationDate($career->updated_at)
                                ->setChangeFrequency('weekly')
                                ->setPriority(0.6));
                            $careersCounter++;
                        }
                    }
                }
            }
        });





        // Save the sitemap to a file
        $sitemap->writeToFile(public_path('sitemap.xml'));
        $this->info('Sitemap generated successfully.');
        $this->info("Total blogs: $blogsCounter");
        $this->info("Total news: $newsCounter");
        $this->info("Total services: $servicesCounter");
        $this->info("Total doctors: $doctorsCounter");
        $this->info("Total branches: $branchesCounter");
        $this->info("Total visiting doctors: $visitingDoctorsCounter");
        $this->info("Total patent stories: $patentStoriesCounter");
        $this->info("Total FAQ categories: $faqCategoriesCounter");
        $this->info("Total careers: $careersCounter");
        $this->info("Total pages: $pagesCounter");
        //total processed
        $total = $blogsCounter + $newsCounter + $servicesCounter + $doctorsCounter + $branchesCounter + $visitingDoctorsCounter + $patentStoriesCounter + $faqCategoriesCounter + $careersCounter + $pagesCounter;
        $this->info("Total processed: $total");
    }
}
Leave a Comment