74 lines
2.4 KiB
PHP
74 lines
2.4 KiB
PHP
<?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§ion=search
|
|
$pattern = '/\#(\S+)/i';
|
|
$replacement = '<a target=\'_blank\' href="'.self::VK_LINK.'/feed?q=%23$1§ion=search">$0</a>';
|
|
$res = preg_replace($pattern, $replacement, $res);
|
|
|
|
return $res;
|
|
}
|
|
}
|