fixes and updates by list from 29.08.2023

master
Константин 2023-08-30 15:57:32 +03:00
parent c8be83be63
commit 1c45642789
14 changed files with 222 additions and 18 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

@ -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

@ -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

@ -5,6 +5,7 @@ namespace App\Services\Registries;
use App\Models\Asset;
use App\Models\Registries\Registry;
use App\Services\Documents\DocumentDownloadService;
use Illuminate\Support\Str;
use PHPHtmlParser\Dom;
class RegistryImportService {
@ -28,6 +29,10 @@ class RegistryImportService {
public function download($url, $dir = null, $filename = null): ?Asset {
$info = parse_url($url);
if (empty($info['host'])) {
$url = 'https://' . Str::replace('//', '/', "www.faufcc.ru/{$url}");
}
return (new DocumentDownloadService())->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 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,17 @@ 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();
}
});