sergeybodin 2023-09-01 14:56:39 +03:00
commit 6d07d94229
19 changed files with 305 additions and 34 deletions

View File

@ -45,6 +45,14 @@ class PagesController extends Controller {
return fractal($model, new PageTransformer())->respond();
}
public function move(Request $request, $id): JsonResponse {
$model = $this->model->byUuid($id)->firstOrFail();
$parent = Page::byUuid($request->get('parent'))->first();
$model->move($request->get('ord'), $parent);
return fractal($model, new PageTransformer())->respond();
}
public function store(Request $request): void {
}

View File

@ -5,7 +5,7 @@ namespace App\Imports;
use App\Models\Asset;
use App\Models\Registries\Registry;
use App\Models\Registries\RegistryType;
use App\Services\Documents\DocumentDownloadService;
use App\Services\FileDownloadService;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
@ -48,7 +48,7 @@ class NtdRegistryImport extends Import implements ToCollection, WithHeadingRow {
}
public function download($url): ?Asset {
return (new DocumentDownloadService())->download($url, 'registries/ntd');
return (new FileDownloadService())->download($url, 'registries/ntd');
}
public function checkLink($link) {

View File

@ -47,6 +47,10 @@ class Page extends Model {
return $this->hasMany(Page::class, 'parent_id')->orderBy('ord');
}
public function siblings(): HasMany {
return $this->hasMany(Page::class, 'parent_id', 'parent_id');
}
public function sections(): MorphToMany {
return $this->objects()->wherePivot('group', '=', 'sections');
}
@ -124,6 +128,37 @@ class Page extends Model {
return null;
}
public function move($ord, ?Page $parent = null) {
$prevParent = $this->parent;
if (($parent->id ?? 0) === ($prevParent->id ?? 0)) {
($ord > $this->ord) ? $this->moveSet('backward', $this->ord, $ord, $parent) : $this->moveSet('forward', $ord, $this->ord, $parent);
} else $this->moveSet('forward', $ord, null, $parent);
$this->update(['parent_id' => $parent->id ?? 0, 'ord' => $ord]);
$this->trimIndexes([$prevParent->id ?? 0, $parent->id ?? 0]);
}
public function moveSet($dir = 'forward', $ordFrom = null, $ordTo = null, ?Page $parent = null) {
$query = Page::query()->where(['parent_id' => $parent->id ?? 0])->orderBy('ord');
if ($ordFrom !== null) $query->where('ord', '>=', $ordFrom);
if ($ordTo !== null) $query->where('ord', '<=', $ordTo);
$query->get()->each(function($page) use($dir) {
$page->update(['ord' => ($dir === 'forward') ? ($page->ord + 1) : ($page->ord - 1)]);
});
}
public function trimIndexes($parentIds) {
collect(is_array($parentIds) ? $parentIds : [$parentIds])->unique()->each(function($parentId) {
Page::query()->where(['parent_id' => $parentId])->orderBy('ord')->orderBy('id')->get()->each(function($page, $index) {
if ($page->ord !== $index) $page->update(['ord' => $index]);
});
});
}
public function getMaxOrd(): int {
$res = $this->siblings()->max('ord');
return ($res !== null) ? ($res + 1) : 0;
}
public static function root() {
return self::query()->where(['parent_id' => 0])->orderBy('ord')->get();
}

View File

@ -19,6 +19,7 @@ class RegistryType {
public const TITLES = [
self::SIMPLE => 'Простой реестр',
self::CATEGORIZED => 'Простой категоризированный реестр',
self::RULESET => 'Реестр сводов правил',
self::LABORATORIES => 'Реестр испытательных лабораторий',
self::CERTIFIERS => 'Реестр органов по сертификации',

View File

@ -95,6 +95,9 @@ class User extends Authenticatable {
public function getIsAdminAttribute(): bool {
return $this->hasRole('Administrator') || $this->isMainCompanyAdmin;
}
public function getIsSuperAdminAttribute(): bool {
return $this->hasRole('Administrator');
}
public function getIsModeratorAttribute(): bool {
return $this->membership()->where(['role' => CompanyMemberRole::MODERATOR])->mainCompany()->exists();
}
@ -112,6 +115,7 @@ class User extends Authenticatable {
public function getPrivilegesAttribute(): array {
return [
'super_admin' => $this->isSuperAdmin,
'admin' => $this->isAdmin,
'expert' => $this->isExpert,
'main_company_member' => $this->isMainCompanyMember

View File

@ -2,6 +2,7 @@
namespace App\Services\Documents;
use App\Services\FileDownloadService;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
@ -36,6 +37,6 @@ class DocumentGeneratorService {
}
public function makeAsset($path, $name) {
return (new DocumentDownloadService())->makeAsset($path, $name);
return (new FileDownloadService())->makeAsset($path, $name);
}
}

View File

@ -1,18 +1,23 @@
<?php
namespace App\Services\Documents;
namespace App\Services;
use App\Models\Asset;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
class DocumentDownloadService {
protected array $mimes = [
class FileDownloadService {
protected array $documentMimes = [
'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'pdf' => 'application/pdf',
'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
];
protected array $imageMimes = [
'jpg' => 'image/jpg',
'jpeg' => 'image/jpeg',
'png' => 'image/png'
];
public function __construct() {
@ -21,20 +26,25 @@ class DocumentDownloadService {
public function download($url, $dir = null, $filename = null): ?Asset {
$info = pathinfo($url);
if (!empty($this->mimes[$info['extension'] ?? null])) {
$path = "public/documents";
$filename = $filename ? "{$filename}.{$info['extension']}" : $info['basename'];
$ext = $info['extension'] ?? null;
if (!empty($this->documentMimes[$ext])) $path = 'public/documents';
elseif (!empty($this->imageMimes[$ext])) $path = 'public/images';
if (!empty($path)) {echo("$url is trying to download\n");
$filename = $filename ? "{$filename}.{$ext}" : $info['basename'];
$path = $dir ? "{$path}/{$dir}/{$filename}" : "{$path}/{$filename}";
$asset = Asset::query()->where(['path' => $path])->first();
if (!$asset && Storage::put($path, Http::get($url)->body())) $asset = $this->makeAsset($path);
elseif ($asset) var_dump($asset->path);
if (!$asset && Storage::put($path, Http::get($url)->body())) {
$asset = $this->makeAsset($path);
echo("Downloaded {$asset->path}\n");
} elseif ($asset) echo("{$asset->path} already exist\n");
}
return $asset ?? null;
}
public function makeAsset($path, $name = null) {
$info = pathinfo($path);
return Asset::create([
'type' => 'document',
'type' => !empty($this->documentMimes[$info['extension'] ?? null]) ? 'document' : 'image',
'path' => $path,
'mime' => $this->mimes[$info['extension']] ?? null,
'name' => $name ?? $info['basename'],

View File

@ -0,0 +1,97 @@
<?php
namespace App\Services\Forms\Pages;
use App\Models\Objects\FieldType;
use App\Models\Pages\Page;
use App\Models\Pages\PageSubType;
use App\Models\Pages\PageType;
use App\Models\Registries\RegistryType;
use App\Services\Forms\FormsService;
use App\Transformers\Pages\PageTransformer;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Str;
class PageForms extends FormsService {
public array $formTitles = ['create' => 'Создание страницы', 'update' => 'Редактирование страницы'];
public function form(?string $id = null, array $data = []): array {
$model = Page::byUuid($id)->first();
$groups = [
[
'name' => 'common', 'fields' => $this->commonGroupFields($model),
'dynamic' => [
['field' => 'type', 'hide' => ['registry_type', 'subtype']],
['field' => 'type', 'value' => PageType::REGISTRY, 'show' => ['registry_type']],
['field' => 'type', 'value' => PageType::PUBLICATIONS, 'show' => ['subtype']]
]
]
];
return ['title' => $this->formTitle($model), 'data' => $groups];
}
public function commonGroupFields(?Page $model): array {
$fields = [
[
'name' => 'name',
'title' => 'Название',
'type' => FieldType::STRING,
'required' => true,
'value' => $model->name ?? null
],
[
'name' => 'slug',
'title' => 'Адрес',
'type' => FieldType::STRING,
'hidden' => !$model,
'value' => $model->slug ?? null
],
[
'name' => 'type',
'title' => 'Вид',
'type' => FieldType::RELATION,
'required' => true,
'options' => $this->getRelationItems(PageType::TITLES),
'value' => $this->getRelationValue(PageType::TITLES, $model->type ?? null)
],
[
'name' => 'registry_type',
'title' => 'Вид реестра',
'type' => FieldType::RELATION,
'required' => true,
'options' => $this->getRelationItems(RegistryType::TITLES),
'value' => $this->getRelationValue(RegistryType::TITLES, $model->registry->type ?? null)
],
[
'name' => 'subtype',
'title' => 'Вид публикации',
'type' => FieldType::RELATION,
'required' => true,
'options' => $this->getRelationItems(PageSubType::TITLES),
'value' => $this->getRelationValue(PageSubType::TITLES, $model->sub_type ?? null)
]
];
return ['data' => $fields];
}
public function store(array $data): ?JsonResponse {
$parent = Page::byUuid($data['parent'] ?? null)->first();
$data['parent_id'] = $parent->id ?? 0;
$data['slug'] = $data['slug'] ?? Str::slug(Str::transliterate($data['name'] ?? null));
$model = Page::create($data);
$model->update(['ord' => $model->getMaxOrd()]);
if ($model->type === PageType::REGISTRY) $model->registry->update(['type' => $data['registry_type'] ?? RegistryType::SIMPLE]);
return fractal($model, new PageTransformer())->respond();
}
public function update(string $id, array $data): ?JsonResponse {
$model = Page::byUuid($id)->firstOrFail();
$data['slug'] = $data['slug'] ?? Str::slug(Str::transliterate($data['name'] ?? null));
$model->update($data);
if ($model->type === PageType::REGISTRY) $model->registry->update(['type' => $data['registry_type'] ?? RegistryType::SIMPLE]);
return fractal($model->fresh(), new PageTransformer())->respond();
}
}

View File

@ -4,5 +4,6 @@ namespace App\Services\Forms\Pages;
class PageFormsServices {
public static array $services = [
'page' => PageForms::class
];
}

View File

@ -42,7 +42,7 @@ class EntryForms extends FormsService {
],
[
'name' => 'number',
'title' => 'Номер записи',
'title' => 'Номер документа',
'type' => FieldType::STRING,
'value' => $model->number ?? null
],

View File

@ -0,0 +1,47 @@
<?php
namespace App\Services\Registries;
use App\Models\Pages\Page;
use App\Models\Publications\PublicationType;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Str;
use PHPHtmlParser\Dom;
class NewsImportService extends RegistryImportService {
public function test() {
}
public function import() {
$page = Page::byUrl('/press-tsentr/novosti');
$nodes = $this->dom->find('article.pressRoomNews_article')->toArray();
foreach ($nodes as $node) {
$pre = $node->find('pre', 0);
$img = $node->find('img', 0);
$asset = $this->download(Str::replace('http://faufcc.ru.opt-images.1c-bitrix-cdn.ru', 'https://faufcc.ru', $img->src), 'publications/news');
$link = $node->find('header a', 0);
$serialized = $pre->text;
$name = trim(explode('[', explode('[NAME] =>', $serialized)[1])[0]);
$published_at = trim(explode('[', explode('[ACTIVE_FROM] =>', $serialized)[1] ?? null)[0] ?? null);
$excerpt = trim(explode('[', explode('[PREVIEW_TEXT] =>', $serialized)[1] ?? null)[0] ?? null);
$content = $this->parseContent("https://faufcc.ru{$link->href}");
$model = $page->publications()->firstOrCreate(['name' => $name]);
$model->update(['type' => PublicationType::NEWS, 'published_at' => $published_at ? Date::create($published_at) : null,
'excerpt' => $excerpt, 'slug' => Str::slug($name), 'poster_id' => $asset->id ?? null, 'is_published' => true]);
$section = $model->getObject('page-section-html', 'sections');
$section->setValue('html-required', $content);
}
}
public function parseContent($url) {
$dom = new Dom;
$dom->loadFromUrl($url);
$node = $dom->find('.user-container', 0);
if (($v = $node->find('h1')) && $v->count()) $v->delete();
if (($v = $node->find('img')) && $v->count()) $v->delete();
return trim($node->innerHTML);
}
}

View File

@ -4,7 +4,8 @@ namespace App\Services\Registries;
use App\Models\Asset;
use App\Models\Registries\Registry;
use App\Services\Documents\DocumentDownloadService;
use App\Services\FileDownloadService;
use Illuminate\Support\Str;
use PHPHtmlParser\Dom;
class RegistryImportService {
@ -15,7 +16,8 @@ class RegistryImportService {
protected array $mimes = [
'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'pdf' => 'application/pdf',
'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'jpg' => 'image/jpg'
];
@ -28,7 +30,11 @@ class RegistryImportService {
public function download($url, $dir = null, $filename = null): ?Asset {
return (new DocumentDownloadService())->download($url, $dir, $filename);
$info = parse_url($url);
if (empty($info['host'])) {
$url = 'https://' . Str::replace('//', '/', "www.faufcc.ru/{$url}");
}
return (new FileDownloadService())->download($url, $dir, $filename);
}
}

View File

@ -0,0 +1,40 @@
<?php
namespace App\Services\Registries;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Str;
class TechnicalCertificatesImportService extends RegistryImportService {
public function test() {
var_dump($this->registry);
}
public function import() {
$nodes = $this->dom->find('table tbody tr')->toArray();
foreach ($nodes as $node) {
list($product_name, $product_purpose, $producer, $links, $active_since, $active_till) = $node->find('td')->toArray();
$number = null;
$asset = null;
$conclusionAsset = null;
$active_since = $active_since->text ? Date::create($active_since->text) : null;
$active_till = $active_till->text ? Date::create($active_till->text) : null;
foreach ($links->find('a')->toArray() as $link) {
if (trim($link->text) === 'Техническое заключение') {
$conclusionAsset = $this->download($link->href, 'registries/ts/conclusions');
} else {
$number = $link->text;
$asset = $this->download($link->href, 'registries/ts/certificates');
}
}
if (!$number) $number = trim($links->text);
$entry = $this->registry->entries()->firstOrCreate(['name' => trim($product_name->text), 'category_id' => 0]);
$entry->update(['number' => $number, 'asset_id' => $asset->id ?? null, 'active_since' => $active_since, 'active_till' => $active_till]);
$data = ['developer-name' => Str::limit(Str::replace('&quot;', '"', trim($producer->text)), 495), 'product-purpose' => trim($product_purpose->text), 'technical-conclusion' => $conclusionAsset];
$entry->properties->setValues($data);
}
}
}

View File

@ -17,7 +17,7 @@ class CreateFieldHtmlValuesTable extends Migration
$table->id();
$table->integer('object_id')->index()->nullable();
$table->integer('field_id')->index()->nullable();
$table->text('value')->nullable();
$table->mediumText('value')->nullable();
$table->integer('ord')->index()->default(0);
$table->timestamps();
});

View File

@ -17,7 +17,7 @@ class CreateRegistryEntriesTable extends Migration
$table->id();
$table->char('uuid', 36)->index()->unique();
$table->integer('registry_id')->index()->nullable();
$table->integer('category_id')->index()->nullable();
$table->integer('category_id')->index()->default(0);
$table->integer('asset_id')->index()->nullable();
$table->string('number')->index()->nullable();
$table->string('name', 750)->index()->nullable();

View File

@ -9,8 +9,7 @@ use App\Models\Registries\RegistryType;
use Illuminate\Database\Seeder;
use Illuminate\Support\Str;
class PagesTableSeeder extends Seeder
{
class PagesTableSeeder extends Seeder {
public array $pages = [
'О центре' => [
'children' => [
@ -143,17 +142,17 @@ class PagesTableSeeder extends Seeder
]
];
public function run()
{
$ord = 0;
collect($this->pages)->each(function ($data, $name) use (&$ord) {
$data['ord'] = $ord++;
$this->importPage($name, $data);
});
public function run() {
if (!Page::query()->count()) {
$ord = 0;
collect($this->pages)->each(function ($data, $name) use (&$ord) {
$data['ord'] = $ord++;
$this->importPage($name, $data);
});
}
}
public function importPage($name, $data, ?Page $parent = null)
{
public function importPage($name, $data, ?Page $parent = null) {
$slug = Str::slug(Str::transliterate($name));
$data += ['type' => $data['type'] ?? PageType::CONTENT, 'name' => $name];
$page = Page::firstOrCreate(['parent_id' => $parent->id ?? 0, 'slug' => $slug]);

View File

@ -11,10 +11,7 @@ class UsersTableSeeder extends Seeder {
];
public array $admins = [
['email' => 'shyctpuk@mail.ru', 'name' => 'Константин Митрофанов'],
['email' => 'n.astashkevich@gmail.com', 'name' => 'Николай Асташкевич'],
['email' => 'sergey@bodin.ru', 'name' => 'Сергей Бодин'],
['email' => 'test@test.ru', 'name' => 'Иван Иванов']
['email' => 'admin@test.ru', 'name' => 'Админ Админович Админов', 'password' => 'DybgEs']
];
@ -34,6 +31,7 @@ class UsersTableSeeder extends Seeder {
if ($user = User::where(['email' => $email])->first()) $user->update($data);
else $user = User::factory()->create($data);
$user->assignRole($role);
if ($password = $data['password'] ?? null) $user->setPassword($password);
}
});
}

View File

@ -23,6 +23,10 @@ Route::group(['prefix' => 'pages'], function() {
Route::get('/root', 'Api\Pages\PagesController@root');
Route::get('/find', 'Api\Pages\PagesController@find');
Route::get('/{id}', 'Api\Pages\PagesController@show');
Route::group(['middleware' => ['auth:api']], function() {
Route::put('/{id}', 'Api\Pages\PagesController@move');
Route::delete('/{id}', 'Api\Pages\PagesController@destroy');
});
});
Route::group(['prefix' => 'publications'], function() {

View File

@ -1,6 +1,5 @@
<?php
use App\Imports\CompaniesImport;
use App\Models\Registries\Registry;
use App\Models\Registries\RegistryType;
use App\Models\User;
@ -54,4 +53,25 @@ Artisan::command('dev:import-ntd', function() {
Excel::import(new \App\Imports\NtdRegistryImport(), Storage::path('import/registries/ntd.xlsx'));
});
Artisan::command('htmlparser:import-ts', function() {
$registry = Registry::query()->where(['type' => RegistryType::TECHNICAL_CERTIFICATES])->first();
$url = 'https://www.faufcc.ru/_deyatelnost/_otsenka-prigodnosti/_reestr-tekhnicheskikh-svidetelstv/?PAGEN_1=';
for ($i = 1; $i <= 44; $i++) {
echo "Parsing page {$i}\n";
$service = new \App\Services\Registries\TechnicalCertificatesImportService($registry, "{$url}{$i}");
$service->import();
}
});
Artisan::command('htmlparser:import-news', function() {
$url = 'https://www.faufcc.ru/_press-tsentr/novosti/?PAGEN_1=';
for ($i = 1; $i <= 88; $i++) {
echo "Parsing page {$i}\n";
$service = new \App\Services\Registries\NewsImportService(Registry::find(1), "{$url}{$i}");
$service->import();
}
});