diff --git a/app/Filament/Resources/AcademicJournalResource/Pages/CreateAcademicJournal.php b/app/Filament/Resources/AcademicJournalResource/Pages/CreateAcademicJournal.php index 90305fc..3231e5d 100644 --- a/app/Filament/Resources/AcademicJournalResource/Pages/CreateAcademicJournal.php +++ b/app/Filament/Resources/AcademicJournalResource/Pages/CreateAcademicJournal.php @@ -5,8 +5,120 @@ namespace App\Filament\Resources\AcademicJournalResource\Pages; use App\Filament\Resources\AcademicJournalResource; use Filament\Actions; use Filament\Resources\Pages\CreateRecord; +use Illuminate\Support\Str; class CreateAcademicJournal extends CreateRecord { protected static string $resource = AcademicJournalResource::class; + + protected array $seoData; + + + protected function mutateFormDataBeforeCreate(array $data): array + { + $this->seoData = $this->generateSeo($data); + $data['search_data'] = $this->generateSearchData($data['about_program']); + return $data; + } + + protected function afterCreate(): void + { + $this->record->seo()->create($this->seoData); + } + + private function generateSeo(array $data) : array + { + $title = $data['title']; + $rowData = $this->getBlockBySeoActiveState('paragraph', $data['about_program']); + if ($rowData === null) { + $rowData = $this->getFirstBlockByName('paragraph', $data['about_program']); + } + if ($rowData !== null) { + $description = strip_tags($rowData['data']['content']); + } else { + $description = null; + } $image = ($data['preview'] !== null) ? $data['preview'] : null; + + return [ + 'title' => $title, + 'description' => Str::limit(htmlspecialchars($description, ENT_QUOTES, 'UTF-8'), 160), + 'image' => $image, + ]; + } + + 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 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; + } } diff --git a/app/Filament/Resources/AcademicJournalResource/Pages/EditAcademicJournal.php b/app/Filament/Resources/AcademicJournalResource/Pages/EditAcademicJournal.php index a83429f..93f600e 100644 --- a/app/Filament/Resources/AcademicJournalResource/Pages/EditAcademicJournal.php +++ b/app/Filament/Resources/AcademicJournalResource/Pages/EditAcademicJournal.php @@ -5,11 +5,127 @@ namespace App\Filament\Resources\AcademicJournalResource\Pages; use App\Filament\Resources\AcademicJournalResource; use Filament\Actions; use Filament\Resources\Pages\EditRecord; +use Illuminate\Support\Str; class EditAcademicJournal extends EditRecord { protected static string $resource = AcademicJournalResource::class; + protected array $seoData; + + + protected function mutateFormDataBeforeSave(array $data): array + { + $this->seoData = $this->generateSeo($data); + $data['search_data'] = $this->generateSearchData($data['about_program']); + + return $data; + } + + protected function afterSave(): void + { + $this->record->seo()->update($this->seoData); + } + + + 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 generateSeo(array $data) : array + { + $title = $data['title']; + $rowData = $this->getBlockBySeoActiveState('paragraph', $data['about_program']); + if ($rowData === null) { + $rowData = $this->getFirstBlockByName('paragraph', $data['about_program']); + } + if ($rowData !== null) { + $description = strip_tags($rowData['data']['content']); + } else { + $description = null; + } + $image = ($this->record->preview !== null) ? $this->record->preview : null; + + return [ + 'title' => $title, + 'description' => Str::limit(htmlspecialchars($description, ENT_QUOTES, 'UTF-8'), 160), + 'image' => $image, + ]; + } + + 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; + } + + + 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; + } + protected function getHeaderActions(): array { return [ diff --git a/app/Filament/Resources/DepartmentResource/Pages/CreateDepartment.php b/app/Filament/Resources/DepartmentResource/Pages/CreateDepartment.php index 7bdcb1b..a943b2d 100644 --- a/app/Filament/Resources/DepartmentResource/Pages/CreateDepartment.php +++ b/app/Filament/Resources/DepartmentResource/Pages/CreateDepartment.php @@ -5,8 +5,120 @@ namespace App\Filament\Resources\DepartmentResource\Pages; use App\Filament\Resources\DepartmentResource; use Filament\Actions; use Filament\Resources\Pages\CreateRecord; +use Illuminate\Support\Str; class CreateDepartment extends CreateRecord { protected static string $resource = DepartmentResource::class; + + protected array $seoData; + + + protected function mutateFormDataBeforeCreate(array $data): array + { + $this->seoData = $this->generateSeo($data); + $data['search_data'] = $this->generateSearchData($data['content']); + return $data; + } + + protected function afterCreate(): void + { + $this->record->seo()->create($this->seoData); + } + + private function generateSeo(array $data) : array + { + $title = $data['title']; + $rowData = $this->getBlockBySeoActiveState('paragraph', $data['content']); + if ($rowData === null) { + $rowData = $this->getFirstBlockByName('paragraph', $data['content']); + } + if ($rowData !== null) { + $description = strip_tags($rowData['data']['content']); + } else { + $description = null; + } $image = ($data['preview'] !== null) ? $data['preview'] : null; + + return [ + 'title' => $title, + 'description' => Str::limit(htmlspecialchars($description, ENT_QUOTES, 'UTF-8'), 160), + 'image' => $image, + ]; + } + + 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 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; + } } diff --git a/app/Filament/Resources/DepartmentResource/Pages/EditDepartment.php b/app/Filament/Resources/DepartmentResource/Pages/EditDepartment.php index d9686be..e044bc0 100644 --- a/app/Filament/Resources/DepartmentResource/Pages/EditDepartment.php +++ b/app/Filament/Resources/DepartmentResource/Pages/EditDepartment.php @@ -5,11 +5,127 @@ namespace App\Filament\Resources\DepartmentResource\Pages; use App\Filament\Resources\DepartmentResource; use Filament\Actions; use Filament\Resources\Pages\EditRecord; +use Illuminate\Support\Str; class EditDepartment extends EditRecord { protected static string $resource = DepartmentResource::class; + protected array $seoData; + + + protected function mutateFormDataBeforeSave(array $data): array + { + $this->seoData = $this->generateSeo($data); + $data['search_data'] = $this->generateSearchData($data['content']); + + return $data; + } + + protected function afterSave(): void + { + $this->record->seo()->update($this->seoData); + } + + + 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 generateSeo(array $data) : array + { + $title = $data['title']; + $rowData = $this->getBlockBySeoActiveState('paragraph', $data['content']); + if ($rowData === null) { + $rowData = $this->getFirstBlockByName('paragraph', $data['content']); + } + if ($rowData !== null) { + $description = strip_tags($rowData['data']['content']); + } else { + $description = null; + } + $image = ($this->record->preview !== null) ? $this->record->preview : null; + + return [ + 'title' => $title, + 'description' => Str::limit(htmlspecialchars($description, ENT_QUOTES, 'UTF-8'), 160), + 'image' => $image, + ]; + } + + 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; + } + + + 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; + } + protected function getHeaderActions(): array { return [ diff --git a/app/Filament/Resources/DivisionResource/Pages/CreateDivision.php b/app/Filament/Resources/DivisionResource/Pages/CreateDivision.php index 4be3562..4502ff8 100644 --- a/app/Filament/Resources/DivisionResource/Pages/CreateDivision.php +++ b/app/Filament/Resources/DivisionResource/Pages/CreateDivision.php @@ -5,8 +5,120 @@ namespace App\Filament\Resources\DivisionResource\Pages; use App\Filament\Resources\DivisionResource; use Filament\Actions; use Filament\Resources\Pages\CreateRecord; +use Illuminate\Support\Str; class CreateDivision extends CreateRecord { protected static string $resource = DivisionResource::class; + + protected array $seoData; + + + protected function mutateFormDataBeforeCreate(array $data): array + { + $this->seoData = $this->generateSeo($data); + $data['search_data'] = $this->generateSearchData($data['description']); + return $data; + } + + protected function afterCreate(): void + { + $this->record->seo()->create($this->seoData); + } + + private function generateSeo(array $data) : array + { + $title = $data['title']; + $rowData = $this->getBlockBySeoActiveState('paragraph', $data['description']); + if ($rowData === null) { + $rowData = $this->getFirstBlockByName('paragraph', $data['description']); + } + if ($rowData !== null) { + $description = strip_tags($rowData['data']['content']); + } else { + $description = null; + } $image = ($data['preview'] !== null) ? $data['preview'] : null; + + return [ + 'title' => $title, + 'description' => Str::limit(htmlspecialchars($description, ENT_QUOTES, 'UTF-8'), 160), + 'image' => $image, + ]; + } + + 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 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; + } } diff --git a/app/Filament/Resources/DivisionResource/Pages/EditDivision.php b/app/Filament/Resources/DivisionResource/Pages/EditDivision.php index 302dd58..d8b2700 100644 --- a/app/Filament/Resources/DivisionResource/Pages/EditDivision.php +++ b/app/Filament/Resources/DivisionResource/Pages/EditDivision.php @@ -5,11 +5,127 @@ namespace App\Filament\Resources\DivisionResource\Pages; use App\Filament\Resources\DivisionResource; use Filament\Actions; use Filament\Resources\Pages\EditRecord; +use Illuminate\Support\Str; class EditDivision extends EditRecord { protected static string $resource = DivisionResource::class; + protected array $seoData; + + + protected function mutateFormDataBeforeSave(array $data): array + { + $this->seoData = $this->generateSeo($data); + $data['search_data'] = $this->generateSearchData($data['description']); + + return $data; + } + + protected function afterSave(): void + { + $this->record->seo()->update($this->seoData); + } + + + 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 generateSeo(array $data) : array + { + $title = $data['title']; + $rowData = $this->getBlockBySeoActiveState('paragraph', $data['description']); + if ($rowData === null) { + $rowData = $this->getFirstBlockByName('paragraph', $data['description']); + } + if ($rowData !== null) { + $description = strip_tags($rowData['data']['content']); + } else { + $description = null; + } + $image = ($this->record->preview !== null) ? $this->record->preview : null; + + return [ + 'title' => $title, + 'description' => Str::limit(htmlspecialchars($description, ENT_QUOTES, 'UTF-8'), 160), + 'image' => $image, + ]; + } + + 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; + } + + + 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; + } + protected function getHeaderActions(): array { return [ diff --git a/app/Filament/Resources/FacultyResource/Pages/CreateFaculty.php b/app/Filament/Resources/FacultyResource/Pages/CreateFaculty.php index d04ba8a..825725b 100644 --- a/app/Filament/Resources/FacultyResource/Pages/CreateFaculty.php +++ b/app/Filament/Resources/FacultyResource/Pages/CreateFaculty.php @@ -5,8 +5,120 @@ namespace App\Filament\Resources\FacultyResource\Pages; use App\Filament\Resources\FacultyResource; use Filament\Actions; use Filament\Resources\Pages\CreateRecord; +use Illuminate\Support\Str; class CreateFaculty extends CreateRecord { protected static string $resource = FacultyResource::class; + + protected array $seoData; + + + protected function mutateFormDataBeforeCreate(array $data): array + { + $this->seoData = $this->generateSeo($data); + $data['search_data'] = $this->generateSearchData($data['content']); + return $data; + } + + protected function afterCreate(): void + { + $this->record->seo()->create($this->seoData); + } + + private function generateSeo(array $data) : array + { + $title = $data['title']; + $rowData = $this->getBlockBySeoActiveState('paragraph', $data['content']); + if ($rowData === null) { + $rowData = $this->getFirstBlockByName('paragraph', $data['content']); + } + if ($rowData !== null) { + $description = strip_tags($rowData['data']['content']); + } else { + $description = null; + } $image = ($data['preview'] !== null) ? $data['preview'] : null; + + return [ + 'title' => $title, + 'description' => Str::limit(htmlspecialchars($description, ENT_QUOTES, 'UTF-8'), 160), + 'image' => $image, + ]; + } + + 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 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; + } } diff --git a/app/Filament/Resources/FacultyResource/Pages/EditFaculty.php b/app/Filament/Resources/FacultyResource/Pages/EditFaculty.php index b08b12c..f2560ce 100644 --- a/app/Filament/Resources/FacultyResource/Pages/EditFaculty.php +++ b/app/Filament/Resources/FacultyResource/Pages/EditFaculty.php @@ -5,11 +5,127 @@ namespace App\Filament\Resources\FacultyResource\Pages; use App\Filament\Resources\FacultyResource; use Filament\Actions; use Filament\Resources\Pages\EditRecord; +use Illuminate\Support\Str; class EditFaculty extends EditRecord { protected static string $resource = FacultyResource::class; + protected array $seoData; + + + protected function mutateFormDataBeforeSave(array $data): array + { + $this->seoData = $this->generateSeo($data); + $data['search_data'] = $this->generateSearchData($data['content']); + + return $data; + } + + protected function afterSave(): void + { + $this->record->seo()->update($this->seoData); + } + + + 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 generateSeo(array $data) : array + { + $title = $data['title']; + $rowData = $this->getBlockBySeoActiveState('paragraph', $data['content']); + if ($rowData === null) { + $rowData = $this->getFirstBlockByName('paragraph', $data['content']); + } + if ($rowData !== null) { + $description = strip_tags($rowData['data']['content']); + } else { + $description = null; + } + $image = ($this->record->preview !== null) ? $this->record->preview : null; + + return [ + 'title' => $title, + 'description' => Str::limit(htmlspecialchars($description, ENT_QUOTES, 'UTF-8'), 160), + 'image' => $image, + ]; + } + + 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; + } + + + 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; + } + protected function getHeaderActions(): array { return [ diff --git a/app/Filament/Resources/LibraryNewsResource/Pages/CreateLibraryNews.php b/app/Filament/Resources/LibraryNewsResource/Pages/CreateLibraryNews.php index fa56ad2..d3e35cb 100644 --- a/app/Filament/Resources/LibraryNewsResource/Pages/CreateLibraryNews.php +++ b/app/Filament/Resources/LibraryNewsResource/Pages/CreateLibraryNews.php @@ -5,8 +5,120 @@ namespace App\Filament\Resources\LibraryNewsResource\Pages; use App\Filament\Resources\LibraryNewsResource; use Filament\Actions; use Filament\Resources\Pages\CreateRecord; +use Illuminate\Support\Str; class CreateLibraryNews extends CreateRecord { protected static string $resource = LibraryNewsResource::class; + + protected array $seoData; + + + protected function mutateFormDataBeforeCreate(array $data): array + { + $this->seoData = $this->generateSeo($data); + $data['search_data'] = $this->generateSearchData($data['content']); + return $data; + } + + protected function afterCreate(): void + { + $this->record->seo()->create($this->seoData); + } + + private function generateSeo(array $data) : array + { + $title = $data['title']; + $rowData = $this->getBlockBySeoActiveState('paragraph', $data['content']); + if ($rowData === null) { + $rowData = $this->getFirstBlockByName('paragraph', $data['content']); + } + if ($rowData !== null) { + $description = strip_tags($rowData['data']['content']); + } else { + $description = null; + } $image = ($data['preview'] !== null) ? $data['preview'] : null; + + return [ + 'title' => $title, + 'description' => Str::limit(htmlspecialchars($description, ENT_QUOTES, 'UTF-8'), 160), + 'image' => $image, + ]; + } + + 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 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; + } } diff --git a/app/Filament/Resources/LibraryNewsResource/Pages/EditLibraryNews.php b/app/Filament/Resources/LibraryNewsResource/Pages/EditLibraryNews.php index bfe4736..233675c 100644 --- a/app/Filament/Resources/LibraryNewsResource/Pages/EditLibraryNews.php +++ b/app/Filament/Resources/LibraryNewsResource/Pages/EditLibraryNews.php @@ -5,11 +5,127 @@ namespace App\Filament\Resources\LibraryNewsResource\Pages; use App\Filament\Resources\LibraryNewsResource; use Filament\Actions; use Filament\Resources\Pages\EditRecord; +use Illuminate\Support\Str; class EditLibraryNews extends EditRecord { protected static string $resource = LibraryNewsResource::class; + protected array $seoData; + + + protected function mutateFormDataBeforeSave(array $data): array + { + $this->seoData = $this->generateSeo($data); + $data['search_data'] = $this->generateSearchData($data['content']); + + return $data; + } + + protected function afterSave(): void + { + $this->record->seo()->update($this->seoData); + } + + + 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 generateSeo(array $data) : array + { + $title = $data['title']; + $rowData = $this->getBlockBySeoActiveState('paragraph', $data['content']); + if ($rowData === null) { + $rowData = $this->getFirstBlockByName('paragraph', $data['content']); + } + if ($rowData !== null) { + $description = strip_tags($rowData['data']['content']); + } else { + $description = null; + } + $image = ($this->record->preview !== null) ? $this->record->preview : null; + + return [ + 'title' => $title, + 'description' => Str::limit(htmlspecialchars($description, ENT_QUOTES, 'UTF-8'), 160), + 'image' => $image, + ]; + } + + 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; + } + + + 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; + } + protected function getHeaderActions(): array { return [ diff --git a/app/Filament/Resources/VirtualExhibitionResource/Pages/CreateVirtualExhibition.php b/app/Filament/Resources/VirtualExhibitionResource/Pages/CreateVirtualExhibition.php index dfc9782..3d0f105 100644 --- a/app/Filament/Resources/VirtualExhibitionResource/Pages/CreateVirtualExhibition.php +++ b/app/Filament/Resources/VirtualExhibitionResource/Pages/CreateVirtualExhibition.php @@ -5,8 +5,120 @@ namespace App\Filament\Resources\VirtualExhibitionResource\Pages; use App\Filament\Resources\VirtualExhibitionResource; use Filament\Actions; use Filament\Resources\Pages\CreateRecord; +use Illuminate\Support\Str; class CreateVirtualExhibition extends CreateRecord { protected static string $resource = VirtualExhibitionResource::class; + + protected array $seoData; + + + protected function mutateFormDataBeforeCreate(array $data): array + { + $this->seoData = $this->generateSeo($data); + $data['search_data'] = $this->generateSearchData($data['content']); + return $data; + } + + protected function afterCreate(): void + { + $this->record->seo()->create($this->seoData); + } + + private function generateSeo(array $data) : array + { + $title = $data['title']; + $rowData = $this->getBlockBySeoActiveState('paragraph', $data['content']); + if ($rowData === null) { + $rowData = $this->getFirstBlockByName('paragraph', $data['content']); + } + if ($rowData !== null) { + $description = strip_tags($rowData['data']['content']); + } else { + $description = null; + } $image = ($data['preview'] !== null) ? $data['preview'] : null; + + return [ + 'title' => $title, + 'description' => Str::limit(htmlspecialchars($description, ENT_QUOTES, 'UTF-8'), 160), + 'image' => $image, + ]; + } + + 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 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; + } } diff --git a/app/Filament/Resources/VirtualExhibitionResource/Pages/EditVirtualExhibition.php b/app/Filament/Resources/VirtualExhibitionResource/Pages/EditVirtualExhibition.php index a500bf3..9de0606 100644 --- a/app/Filament/Resources/VirtualExhibitionResource/Pages/EditVirtualExhibition.php +++ b/app/Filament/Resources/VirtualExhibitionResource/Pages/EditVirtualExhibition.php @@ -5,11 +5,127 @@ namespace App\Filament\Resources\VirtualExhibitionResource\Pages; use App\Filament\Resources\VirtualExhibitionResource; use Filament\Actions; use Filament\Resources\Pages\EditRecord; +use Illuminate\Support\Str; class EditVirtualExhibition extends EditRecord { protected static string $resource = VirtualExhibitionResource::class; + protected array $seoData; + + + protected function mutateFormDataBeforeSave(array $data): array + { + $this->seoData = $this->generateSeo($data); + $data['search_data'] = $this->generateSearchData($data['content']); + + return $data; + } + + protected function afterSave(): void + { + $this->record->seo()->update($this->seoData); + } + + + 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 generateSeo(array $data) : array + { + $title = $data['title']; + $rowData = $this->getBlockBySeoActiveState('paragraph', $data['content']); + if ($rowData === null) { + $rowData = $this->getFirstBlockByName('paragraph', $data['content']); + } + if ($rowData !== null) { + $description = strip_tags($rowData['data']['content']); + } else { + $description = null; + } + $image = ($this->record->preview !== null) ? $this->record->preview : null; + + return [ + 'title' => $title, + 'description' => Str::limit(htmlspecialchars($description, ENT_QUOTES, 'UTF-8'), 160), + 'image' => $image, + ]; + } + + 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; + } + + + 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; + } + protected function getHeaderActions(): array { return [ diff --git a/database/migrations/2024_11_03_225111_add_search_data_to_academic_journals.php b/database/migrations/2024_11_03_225111_add_search_data_to_academic_journals.php new file mode 100644 index 0000000..d9f51f3 --- /dev/null +++ b/database/migrations/2024_11_03_225111_add_search_data_to_academic_journals.php @@ -0,0 +1,28 @@ +text('search_data')->nullable()->after('for_authors'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('academic_journals', function (Blueprint $table) { + $table->dropColumn('search_data'); + }); + } +}; diff --git a/database/migrations/2024_11_03_225233_add_is_active_to_academic_journals.php b/database/migrations/2024_11_03_225233_add_is_active_to_academic_journals.php new file mode 100644 index 0000000..6630935 --- /dev/null +++ b/database/migrations/2024_11_03_225233_add_is_active_to_academic_journals.php @@ -0,0 +1,28 @@ +boolean('is_active')->default(true)->after('for_authors'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('academic_journals', function (Blueprint $table) { + $table->dropColumn('is_active'); + }); + } +};