Added News page

This commit is contained in:
amikhaylov
2026-05-15 13:56:57 +03:00
parent 18a843b710
commit 4886612f7f
9 changed files with 172 additions and 4 deletions
+1 -2
View File
@@ -9,13 +9,12 @@ class GigsController extends Controller
public function __construct(
private GigsLoader $loader,
) {
$this->addCssFile('gigs.css');
}
public function index()
{
$events = $this->loader->getEvents();
$this->addCssFile('gigs.css');
return $this->render('gigs')
->with(compact('events'));
+26
View File
@@ -0,0 +1,26 @@
<?php
namespace App\Http\Controllers;
use App\Models\Loaders\NewsLoader;
use App\Views\NewsView;
class NewsController extends Controller
{
public function __construct(
private NewsLoader $newsLoader,
private NewsView $view,
) {
$this->addCssFile('news.css');
}
public function index()
{
$news = $this->newsLoader->getNews();
$this->view->prepareData($news);
return $this->render('news')
->with(compact('news'));
}
}
+18
View File
@@ -0,0 +1,18 @@
<?php
namespace App\Models\Loaders;
use App\Models\ORM\News;
class NewsLoader
{
public function getNews(int $offset = 0, int $limit = 10): array
{
$data = News::select("*")
->limit($limit)
->offset($offset)
->get();
return $data ? $data->toArray() : [];
}
}
+11
View File
@@ -0,0 +1,11 @@
<?php
namespace App\Models\ORM;
use Illuminate\Database\Eloquent\Model;
class News extends Model
{
protected $table = 'allNewsView';
protected $primaryKey = 'id';
}
+73
View File
@@ -0,0 +1,73 @@
<?php
namespace App\Views;
class NewsView
{
const VK_LINK = 'vk.com';
public function shortBody(string $text): string
{
$tmp = explode("<br>",$text);
$short = array_slice($tmp,0,1);
return implode("<br>",$short);
}
public function longBody(string $text): string
{
$tmp = explode("<br>",$text);
$long = array_slice($tmp,1);
return implode("<br>",$long);
}
public function prepareData(array &$data): void
{
foreach($data as $index => $row) {
if($row['author_type'] === 0) {
$keyword = "club";
} else {
$keyword = "id";
}
if($row['post_type'] === "copy") {
$data[$index]['author_link'] = sprintf("%s/%s%d", self::VK_LINK, $keyword, $row['author']);
$data[$index]['article_link'] = sprintf("%s/%s%d?w=wall%d_%d",
self::VK_LINK, $keyword, $row['author'], $row['author'], $row['copy_post_id']
);
} else {
$data[$index]['author_link'] = sprintf("%s/%s%d", self::VK_LINK, $keyword, $row['author']);
$data[$index]['article_link'] = sprintf("%s/%s%d?w=wall%d_%d",
self::VK_LINK, $keyword, $row['author'], $row['author'], $row['id']
);
}
$data[$index]['body1'] = $this->convertMediaLinks($this->shortBody($row['article']));
$data[$index]['body2'] = $this->convertMediaLinks($this->longBody($row['article']));
}
}
private function cleanBreaks(string $text): string
{
return str_replace("<br><br>", "<br>", $text);
}
public function convertMediaLinks(string $link): string
{
$pattern = '/(http|https):\/\/(\S+)/i';
$replacement = '<a href="$0" target="_blank">$0</a>';
$res = preg_replace($pattern,$replacement,$link);
$pattern = '/\[([^\[\]\|]+)\|([^\[\]\|]+)\]/i';
$replacement = sprintf('<a target=\'_blank\' href=\'%s/$1\'>$2</a>', self::VK_LINK);
$res = preg_replace($pattern,$replacement,$res);
#https://vk.com/feed?q=%23ledstarband&section=search
$pattern = '/\#(\S+)/i';
$replacement = '<a target=\'_blank\' href="'.self::VK_LINK.'/feed?q=%23$1&section=search">$0</a>';
$res = preg_replace($pattern, $replacement, $res);
return $res;
}
}