first commit
This commit is contained in:
@@ -0,0 +1,169 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\PostResource\Pages;
|
||||
|
||||
use App\Filament\Resources\PostResource;
|
||||
use Closure;
|
||||
use Filament\Actions;
|
||||
use Filament\Notifications\Actions\Action;
|
||||
use Filament\Notifications\Notification;
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Notifications\Messages\BroadcastMessage;
|
||||
use PhpParser\Node\Expr\AssignOp\Mod;
|
||||
|
||||
class CreatePost extends CreateRecord
|
||||
{
|
||||
protected static string $resource = PostResource::class;
|
||||
|
||||
protected function mutateFormDataBeforeCreate(array $data): array
|
||||
{
|
||||
$result = "";
|
||||
foreach ($data['content'] as $block) {
|
||||
$result .= $this->getDataFromBlocks($block);
|
||||
}
|
||||
|
||||
// Удаляем лишние пробелы и переносы строк
|
||||
$result = preg_replace('/\s+/', ' ', $result);
|
||||
$result = trim($result);
|
||||
|
||||
|
||||
// Приводим текст к нижнему регистру
|
||||
$data['search_data'] = strtolower($result);
|
||||
|
||||
return $data;
|
||||
}
|
||||
protected function afterCreate(): void
|
||||
{
|
||||
$recipient = auth()->user();
|
||||
|
||||
Notification::make()
|
||||
->title('Новость на проверку')
|
||||
->body('Новая запись была создана!')
|
||||
->actions([
|
||||
Action::make('view')
|
||||
->label('Проверить')
|
||||
->button()
|
||||
->markAsRead()
|
||||
->url(PostResource::getUrl('edit', ['record' => $this->record])),
|
||||
|
||||
])
|
||||
|
||||
->sendToDatabase($recipient);
|
||||
}
|
||||
|
||||
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 .= "<h" . $block['data']['level'] . ">" . $block['data']['text'] . "</h" . $block['data']['level'] . ">";
|
||||
break;
|
||||
case "embded":
|
||||
$convertedHtml .= "<div><iframe width='560' height='315' src='" . $block['data']['embed'] . "' frameborder='0' allow='autoplay; encrypted-media' allowfullscreen></iframe></div>";
|
||||
break;
|
||||
case "paragraph":
|
||||
$convertedHtml .= "<p>" . $block['data']['text'] . "</p>";
|
||||
break;
|
||||
case "delimiter":
|
||||
$convertedHtml .= "<hr />";
|
||||
break;
|
||||
case "image":
|
||||
$convertedHtml .= "<img class='img-fluid' src='" . $block['data']['file']['url'] . "' title='" . $block['data']['caption'] . "' /><br /><em>" . $block['data']['caption'] . "</em>";
|
||||
break;
|
||||
case "list":
|
||||
$convertedHtml .= "<ul>";
|
||||
foreach ($block['data']['items'] as $li) {
|
||||
$convertedHtml .= "<li>" . $li . "</li>";
|
||||
}
|
||||
$convertedHtml .= "</ul>";
|
||||
break;
|
||||
case "table":
|
||||
$convertedHtml .= "<table>";
|
||||
if ($block['data']['withHeadings']) {
|
||||
$convertedHtml .= "<thead><tr>";
|
||||
foreach ($block['data']['content'][0] as $th) {
|
||||
$convertedHtml .= "<th>" . $th . "</th>";
|
||||
}
|
||||
$convertedHtml .= "</tr></thead>";
|
||||
}
|
||||
$convertedHtml .= "<tbody>";
|
||||
foreach ($block['data']['content'] as $row) {
|
||||
$convertedHtml .= "<tr>";
|
||||
foreach ($row as $td) {
|
||||
$convertedHtml .= "<td>" . $td . "</td>";
|
||||
}
|
||||
$convertedHtml .= "</tr>";
|
||||
}
|
||||
$convertedHtml .= "</tbody></table>";
|
||||
break;
|
||||
default:
|
||||
echo "Unknown block type " . $block['type'];
|
||||
break;
|
||||
}
|
||||
}
|
||||
return $convertedHtml;
|
||||
}
|
||||
|
||||
private function getDataFromBlocks($block) : string
|
||||
{
|
||||
$data = "";
|
||||
switch ($block['type']) {
|
||||
case 'paragraph':
|
||||
$data .= strip_tags($block['data']['content']) . " ";
|
||||
break;
|
||||
case 'heading':
|
||||
$data .= strip_tags($block['data']['content']) . " ";
|
||||
break;
|
||||
case 'files':
|
||||
foreach ($block['data']['file'] as $file) {
|
||||
$data .= $file['title'] . " ";
|
||||
}
|
||||
break;
|
||||
case 'person':
|
||||
$data .= $block['data']['name'] . " ";
|
||||
break;
|
||||
case 'stepper':
|
||||
$data .= $block['data']['step_name'] . " ";
|
||||
foreach ($block['data']['steps'] as $step) {
|
||||
$data .= $step['title'] . " ";
|
||||
$data .= strip_tags($step['content']) . " ";
|
||||
}
|
||||
break;
|
||||
case 'tabs':
|
||||
foreach ($block['data']['tab'] as $item) {
|
||||
foreach ($item['content'] as $block) {
|
||||
$data .= $this->getDataFromBlocks($block);
|
||||
};
|
||||
};
|
||||
break;
|
||||
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\PostResource\Pages;
|
||||
|
||||
use App\Filament\Resources\PostResource;
|
||||
use Filament\Actions;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
|
||||
class EditPost extends EditRecord
|
||||
{
|
||||
protected static string $resource = PostResource::class;
|
||||
|
||||
protected function mutateFormDataBeforeSave(array $data): array
|
||||
{
|
||||
$result = "";
|
||||
foreach ($data['content'] as $block) {
|
||||
$result .= $this->getDataFromBlocks($block);
|
||||
}
|
||||
|
||||
// Удаляем лишние пробелы и переносы строк
|
||||
$result = preg_replace('/\s+/', ' ', $result);
|
||||
$result = trim($result);
|
||||
|
||||
|
||||
// Приводим текст к нижнему регистру
|
||||
$data['search_data'] = strtolower($result);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
private function getDataFromBlocks($block) : string
|
||||
{
|
||||
$data = "";
|
||||
switch ($block['type']) {
|
||||
case 'paragraph':
|
||||
$data .= strip_tags($block['data']['content']) . " ";
|
||||
break;
|
||||
case 'heading':
|
||||
$data .= strip_tags($block['data']['content']) . " ";
|
||||
break;
|
||||
case 'files':
|
||||
foreach ($block['data']['file'] as $file) {
|
||||
$data .= $file['title'] . " ";
|
||||
}
|
||||
break;
|
||||
case 'person':
|
||||
$data .= $block['data']['name'] . " ";
|
||||
break;
|
||||
case 'stepper':
|
||||
$data .= $block['data']['step_name'] . " ";
|
||||
foreach ($block['data']['steps'] as $step) {
|
||||
$data .= $step['title'] . " ";
|
||||
$data .= strip_tags($step['content']) . " ";
|
||||
}
|
||||
break;
|
||||
case 'tabs':
|
||||
foreach ($block['data']['tab'] as $item) {
|
||||
foreach ($item['content'] as $block) {
|
||||
$data .= $this->getDataFromBlocks($block);
|
||||
};
|
||||
};
|
||||
break;
|
||||
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
Actions\DeleteAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\PostResource\Pages;
|
||||
|
||||
use App\Enums\PostStatus;
|
||||
use App\Filament\Resources\PostResource;
|
||||
use App\Models\Post;
|
||||
use Filament\Actions;
|
||||
use Filament\Resources\Components\Tab;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class ListPosts extends ListRecords
|
||||
{
|
||||
public \Illuminate\Support\Collection $postsByStatuses;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->postsByStatuses = Post::select('status', DB::raw('count(*) as post_count'))
|
||||
->groupBy('status')
|
||||
->pluck('post_count', 'status');
|
||||
}
|
||||
|
||||
protected static string $resource = PostResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
Actions\CreateAction::make(),
|
||||
];
|
||||
}
|
||||
|
||||
public function getTabs(): array
|
||||
{
|
||||
return [
|
||||
'status' => Tab::make('Новости на рассмотрении')->modifyQueryUsing(function (Builder $query) {
|
||||
$query->where('status', '=', PostStatus::VERIFICATION->value);
|
||||
})->badge($this->postsByStatuses[PostStatus::VERIFICATION->value] ?? 0),
|
||||
'All' => Tab::make('Все новости'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\PostResource\Pages;
|
||||
|
||||
use App\Filament\Resources\PostResource;
|
||||
use Filament\Actions;
|
||||
use Filament\Resources\Pages\ViewRecord;
|
||||
|
||||
class ViewPost extends ViewRecord
|
||||
{
|
||||
protected static string $resource = PostResource::class;
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user