seoData = $this->generateSeo($data);
$data['preview_text'] = $this->setPreviewText($data);
$data['publish_at'] = $this->setPublishDateTime($data['status']);
$data['search_data'] = $this->generateSearchData($data['content']);
$data['reading_time'] = $this->calculateReadingTime($data['search_data']);
return $data;
}
protected function afterCreate(): void
{
$this->record->seo()->create($this->seoData);
$this->sendNotify($this->record, auth()->user());
}
private function generateSeo(array $data) : array
{
$title = $data['title'];
$rowData = $this->getBlockBySeoActiveState('paragraph', $data['content']);
if ($rowData === null) {
$rowData = $this->getFirstBlockByName('paragraph', $data['content']);
}
$description = strip_tags($rowData['data']['content']);
$image = ($data['preview'] !== null) ? $data['preview'] : null;
return [
'title' => $title,
'description' => Str::limit($description, 160),
'image' => $image,
];
}
private function setPreviewText(array $data) : string
{
$rowData = $this->getBlockBySeoActiveState('paragraph', $data['content']);
if ($rowData === null) {
$rowData = $this->getFirstBlockByName('paragraph', $data['content']);
}
$preview_text = strip_tags($rowData['data']['content']);
return Str::limit($preview_text, 160);
}
private function generateSearchData(array $data) : string
{
$result = "";
foreach ($data as $block) {
$result .= $this->getDataFromBlocks($block);
}
// Удаляем лишние пробелы и переносы строк
$result = preg_replace('/\s+/', ' ', $result);
$result = trim($result);
return strtolower($result);
}
private function getFirstBlockByName(string $name, array $content) : array|null
{
$data = null;
foreach ($content as $block) {
$data = ($block['type'] === $name) ? $block : null;
break;
}
return $data;
}
private function getBlockBySeoActiveState(string $name, array $content) : array|null
{
$data = [];
foreach ($content as $block) {
if ($block['type'] === $name) {
$data[] = $block;
}
}
$block = null;
foreach ($data as $item) {
if ($item['data']['seo_active'] === true) {
$block = $item;
}
}
return $block;
}
private function sendNotify($post, $recipient) : void
{
Notification::make()
->title('Новость на проверку')
->body('Новая запись была создана!')
->actions([
Action::make('view')
->label('Проверить')
->button()
->markAsRead()
->url(PostResource::getUrl('edit', ['record' => $post])),
])->sendToDatabase($recipient);
}
private function setPublishDateTime(PostStatus $status) : Carbon|null
{
if ($status !== PostStatus::PUBLISHED) {
return null;
}
return Carbon::now();
}
private function calculateReadingTime(string $text): int
{
// Calculate the number of words in the text
$wordCount = str_word_count($text,0,"АаБбВвГгДдЕеЁёЖжЗзИиЙйКкЛлМмНнОоПпРрСсТтУуФфХхЦцЧчШшЩщЪъЫыЬьЭэЮюЯя");
// Calculate the average reading speed in words per minute
$wordsPerMinute = 120; // You can adjust this value based on your desired reading speed
// Calculate the reading time in minutes
$readingTime = $wordCount / $wordsPerMinute;
// Round the reading time to the nearest integer
$readingTime = round($readingTime);
return $readingTime;
}
protected function convertDataToHtml($blocks) {
$convertedHtml = "";
foreach ($blocks as $block) {
switch ($block['type']) {
case "header":
$convertedHtml .= "
" . $block['data']['text'] . "
"; break; case "delimiter": $convertedHtml .= "| " . $th . " | "; } $convertedHtml .= "
|---|
| " . $td . " | "; } $convertedHtml .= "