150 lines
3.9 KiB
PHP
150 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Blog;
|
|
use App\Models\Category;
|
|
use Illuminate\Http\Request;
|
|
|
|
class SitemapController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
ob_clean();
|
|
$blogs = Blog::query()
|
|
->where('status', 1)
|
|
->whereNull('notBlog')
|
|
->orderBy('published_at', 'desc')
|
|
->paginate(500, ['*'], 'page', $page ?? 1)
|
|
->withQueryString();
|
|
$news = Blog::query()
|
|
->where('status', 1)
|
|
->where('notBlog', 1)
|
|
->orderBy('published_at', 'desc')
|
|
->paginate(500, ['*'], 'page', $page ?? 1)
|
|
->withQueryString();
|
|
$totalPagespost = $blogs->lastPage();
|
|
$totalPagenews = $news->lastPage();
|
|
|
|
return response()->view('sitemap.index' , ['news' => $totalPagenews , 'countNews' => count($news) , 'blogs' => $totalPagespost , 'countBlogs' => count($blogs) ])->header('Content-Type', 'text/xml');
|
|
}
|
|
|
|
|
|
public function posts($page = null)
|
|
{
|
|
if ($page == 1) {
|
|
return redirect()->route('post.sitemap');
|
|
}
|
|
|
|
ob_clean();
|
|
$blogs = Blog::query()
|
|
->where('status', 1)
|
|
->whereNull('notBlog')
|
|
->orderBy('published_at', 'desc')
|
|
->paginate(500, ['*'], 'page', $page ?? 1)
|
|
->withQueryString();
|
|
|
|
|
|
|
|
$blogImages = [];
|
|
|
|
foreach ($blogs as $blog) {
|
|
$content = $blog->content;
|
|
|
|
if (empty($content)) {
|
|
continue;
|
|
}
|
|
|
|
$pattern = '/<img[^>]+src="([^">]+)"/i';
|
|
preg_match_all($pattern, $content, $matches);
|
|
|
|
$extractedImages = $matches[1] ?? [];
|
|
|
|
$blogImages[$blog->id] = $extractedImages;
|
|
}
|
|
|
|
|
|
return response()->view('sitemap.posts', compact('blogs', 'blogImages'))->header('Content-Type', 'text/xml');
|
|
}
|
|
|
|
public function news($page = null)
|
|
{
|
|
if ($page == 1) {
|
|
return redirect()->route('news.sitemap');
|
|
}
|
|
|
|
ob_clean();
|
|
$news = Blog::query()
|
|
->where('status', 1)
|
|
->where('notBlog', 1)
|
|
->orderBy('published_at', 'desc')
|
|
->paginate(500, ['*'], 'page', $page ?? 1)
|
|
->withQueryString();
|
|
|
|
$blogImages = [];
|
|
|
|
foreach ($news as $blog) {
|
|
$content = $blog->content;
|
|
|
|
if (empty($content)) {
|
|
continue;
|
|
}
|
|
|
|
$pattern = '/<img[^>]+src="([^">]+)"/i';
|
|
preg_match_all($pattern, $content, $matches);
|
|
|
|
$extractedImages = $matches[1] ?? [];
|
|
|
|
$blogImages[$blog->id] = $extractedImages;
|
|
}
|
|
|
|
|
|
return response()->view('sitemap.news', compact('news', 'blogImages'))->header('Content-Type', 'text/xml');
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function page()
|
|
{
|
|
ob_clean();
|
|
|
|
$blogs = Blog::all();
|
|
|
|
$latestTimestamp = null;
|
|
|
|
foreach ($blogs as $blog) {
|
|
$createdTimestamp = strtotime($blog->published_at);
|
|
$updatedTimestamp = strtotime($blog->updated_at);
|
|
|
|
if ($latestTimestamp === null || $createdTimestamp > $latestTimestamp || $updatedTimestamp > $latestTimestamp) {
|
|
$latestTimestamp = max($createdTimestamp, $updatedTimestamp);
|
|
}
|
|
}
|
|
|
|
$latestCarbon = \Carbon\Carbon::createFromTimestamp($latestTimestamp, 'Asia/Tehran');
|
|
|
|
return response()->view('sitemap.page', ['latestTimestamp' => $latestCarbon])->header('Content-Type', 'text/xml');
|
|
}
|
|
|
|
public function postcategory()
|
|
{
|
|
ob_clean();
|
|
|
|
$categories = Category::where('parent' , null )->get();
|
|
|
|
return response()->view('sitemap.postcategory', ['categories' => $categories])->header('Content-Type', 'text/xml');
|
|
}
|
|
|
|
public function newscategory()
|
|
{
|
|
ob_clean();
|
|
|
|
$categories = Category::all();
|
|
|
|
return response()->view('sitemap.newscategory', ['categories' => $categories])->header('Content-Type', 'text/xml');
|
|
}
|
|
|
|
}
|