Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions Web/Models/Entities/GeodbCity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php declare(strict_types=1);
namespace openvk\Web\Models\Entities;
use openvk\Web\Models\Repositories\GeodbCountries;
use openvk\Web\Models\Repositories\GeodbLogs;
use openvk\Web\Models\Repositories\Users;
use openvk\Web\Models\RowModel;

class GeodbCity extends RowModel
{
protected $tableName = "geodb_cities";

function getId(): int
{
return (int) $this->getRecord()->id;
}

function getCountry(): GeodbCountry
{
return (new GeodbCountries)->get($this->getRecord()->country);
}

function getName(): string
{
return $this->getRecord()->name;
}

function getNativeName(): ?string
{
return $this->getRecord()->native_name;
}

function getCanonicalName(): string
{
return $this->getNativeName() ?? $this->getName();
}

function getSimplified(): array
{
return [
"id" => $this->getId(),
"name" => $this->getName(),
"native_name" => $this->getNativeName()
];
}

function getRequestSender(): ?User
{
return (new Users)->get((int) $this->getRecord()->is_request);
}

function save(User $user, $table): void
{
if(is_null($this->record)) {
$this->record = $table->insert($this->changes);
(new GeodbLogs)->create($user, $this->tableName, 0, $this->getRecord()->toArray(), $this->changes);
} else if($this->deleted) {
(new GeodbLogs)->create($user, $this->tableName, 2, $this, $this->changes);
$this->record = $table->insert((array) $this->record);
} else {
(new GeodbLogs)->create($user, $this->tableName, 1, $this, $this->changes);
$table->get($this->record->id)->update($this->changes);
$this->record = $table->get($this->record->id);
}

$this->changes = [];
}
}
114 changes: 114 additions & 0 deletions Web/Models/Entities/GeodbCountry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php declare(strict_types=1);
namespace openvk\Web\Models\Entities;
use openvk\Web\Models\Repositories\GeodbCities;
use openvk\Web\Models\Repositories\GeodbEducation;
use openvk\Web\Models\Repositories\GeodbLogs;
use openvk\Web\Models\Repositories\GeodbRights;
use openvk\Web\Models\RowModel;

class GeodbCountry extends RowModel
{
protected $tableName = "geodb_countries";

function getId(): int
{
return (int) $this->getRecord()->id;
}

function getCode(): string
{
return $this->getRecord()->code;
}

function getFlagURL(): string
{
return "/assets/packages/static/openvk/img/flags/" . $this->getRecord()->flag . ".gif";
}

function getName(): string
{
return $this->getRecord()->name;
}

function getNativeName(): ?string
{
return $this->getRecord()->native_name;
}

function getCanonicalName(): string
{
return $this->getNativeName() ?? $this->getName();
}

function getEditors(): \Traversable
{
return (new GeodbRights)->getList(NULL, $this->getId());
}

function getUserRights(int $user_id): ?GeodbEditor
{
return (new GeodbRights)->getCountryPermission($user_id, $this->getId());
}

function isUserCanEditEducation(int $user_id): bool
{
$rights = $this->getUserRights($user_id);
if (!$rights) return false;

return $rights->canEditEducation();
}

function isUserCanEditCities(int $user_id): bool
{
$rights = $this->getUserRights($user_id);
if (!$rights) return false;

return $rights->canEditCities();
}

function getSimplified(): array
{
return [
"id" => $this->getId(),
"name" => $this->getName(),
"native_name" => $this->getNativeName()
];
}

function getCitiesCount(): int
{
return count(iterator_to_array((new GeodbCities)->getList($this->getId())));
}

function getSchoolsCount(): int
{
return count(iterator_to_array((new GeodbEducation)->getSchools($this->getId())));
}

function getUniversitiesCount(): int
{
return count(iterator_to_array((new GeodbEducation)->getUniversities($this->getId())));
}

function getFlagCode(): string
{
return $this->getRecord()->flag;
}

function save(User $user, $table, ?bool $new = false): void
{
if(is_null($this->record)) {
$this->record = $table->insert($this->changes);
(new GeodbLogs)->create($user, $this->tableName, 0, $this->getRecord()->toArray(), $this->changes);
} else if($this->deleted) {
(new GeodbLogs)->create($user, $this->tableName, 2, $this, $this->changes, $new);
$this->record = $table->insert((array) $this->record);
} else {
(new GeodbLogs)->create($user, $this->tableName, 1, $this, $this->changes, $new);
$table->get($this->record->id)->update($this->changes);
$this->record = $table->get($this->record->id);
}

$this->changes = [];
}
}
58 changes: 58 additions & 0 deletions Web/Models/Entities/GeodbEditor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php declare(strict_types=1);
namespace openvk\Web\Models\Entities;
use openvk\Web\Models\Repositories\GeodbCountries;
use openvk\Web\Models\Repositories\GeodbLogs;
use openvk\Web\Models\Repositories\Users;
use openvk\Web\Models\RowModel;

class GeodbEditor extends RowModel
{
protected $tableName = "geodb_editors";

function getId(): int
{
return (int) $this->getRecord()->id;
}

function getUser(): User
{
return (new Users)->get((int) $this->getRecord()->uid);
}

function getCountry(): ?GeodbCountry
{
return (new GeodbCountries)->get((int) $this->getRecord()->country);
}

function canEditEducation(): bool
{
return (bool) $this->getRecord()->edu;
}

function canEditCities(): bool
{
return (bool) $this->getRecord()->cities;
}

function save(User $user, $table): void
{
if(is_null($this->record)) {
$this->record = $table->insert($this->changes);
(new GeodbLogs)->create($user, $this->tableName, 0, $this->getRecord()->toArray(), $this->changes);
} else if($this->deleted) {
(new GeodbLogs)->create($user, $this->tableName, 2, $this, $this->changes);
$this->record = $table->insert((array) $this->record);
} else {
(new GeodbLogs)->create($user, $this->tableName, 1, $this, $this->changes);
$table->get($this->record->id)->update($this->changes);
$this->record = $table->get($this->record->id);
}

$this->changes = [];
}

function getName(): string
{
return $this->getUser()->getCanonicalName() . " (" . ($this->getCountry()->getCanonicalName()) . ")";
}
}
74 changes: 74 additions & 0 deletions Web/Models/Entities/GeodbFaculty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php declare(strict_types=1);
namespace openvk\Web\Models\Entities;
use openvk\Web\Models\Repositories\GeodbCities;
use openvk\Web\Models\Repositories\GeodbCountries;
use openvk\Web\Models\Repositories\GeodbEducation;
use openvk\Web\Models\Repositories\GeodbLogs;
use openvk\Web\Models\Repositories\GeodbSpecializations;
use openvk\Web\Models\Repositories\Users;
use openvk\Web\Models\RowModel;

class GeodbFaculty extends RowModel
{
protected $tableName = "geodb_faculties";

function getId(): int
{
return (int) $this->getRecord()->id;
}

function getUniversity(): GeodbUniversity
{
return (new GeodbEducation)->getUniversity((int) $this->getRecord()->university);
}

function getName(): string
{
return $this->getRecord()->name;
}

function getSimplified(): array
{
return [
"id" => $this->getId(),
"name" => $this->getName()
];
}

function getSpecializations(?bool $needDeleted = false, ?bool $simplified = false): \Traversable
{
return (new GeodbSpecializations)->getList($this->getId(), $needDeleted, $simplified);
}

function getRequestSender(): ?User
{
return (new Users)->get((int) $this->getRecord()->is_request);
}

function getCountry(): ?GeodbCountry
{
return $this->getUniversity()->getCountry();
}

function getCity(): ?GeodbCity
{
return $this->getUniversity()->getCity();
}

function save(User $user, $table): void
{
if(is_null($this->record)) {
$this->record = $table->insert($this->changes);
(new GeodbLogs)->create($user, $this->tableName, 0, $this->getRecord()->toArray(), $this->changes);
} else if($this->deleted) {
(new GeodbLogs)->create($user, $this->tableName, 2, $this, $this->changes);
$this->record = $table->insert((array) $this->record);
} else {
(new GeodbLogs)->create($user, $this->tableName, 1, $this, $this->changes);
$table->get($this->record->id)->update($this->changes);
$this->record = $table->get($this->record->id);
}

$this->changes = [];
}
}
81 changes: 81 additions & 0 deletions Web/Models/Entities/GeodbLog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php declare(strict_types=1);
namespace openvk\Web\Models\Entities;
use Chandler\Database\DatabaseConnection;
use openvk\Web\Models\Repositories\GeodbCities;
use openvk\Web\Models\Repositories\GeodbCountries;
use openvk\Web\Models\Repositories\GeodbEducation;
use openvk\Web\Models\Repositories\GeodbSpecializations;
use openvk\Web\Models\Repositories\Users;
use openvk\Web\Models\RowModel;

class GeodbLog extends RowModel
{
protected $tableName = "geodb_logs";

function getId(): int
{
return (int) $this->getRecord()->id;
}

function getUser(): ?User
{
return (new Users)->get((int) $this->getRecord()->user);
}

function getObjectTable(): string
{
return $this->getRecord()->object_table;
}

function getObjectId(): int
{
return $this->getRecord()->object_id;
}

function getObject()
{
$model = $this->getRecord()->object_model;
return new $model(DatabaseConnection::i()->getContext()->table($this->getObjectTable())->get($this->getObjectId()));
}

function getType(): string
{
return ["добавил", "отредактировал", "удалил", "восстановил"][$this->getRecord()->type];
}

function getObjectType(): string
{
return [
"geodb_countries" => "страну",
"geodb_cities" => "город",
"geodb_schools" => "школу",
"geodb_universities" => "университет",
"geodb_faculties" => "факультет",
"geodb_specializations" => "специальность",
"geodb_editors" => "редактора базы",
][$this->getRecord()->object_table];
}

function getObjectName(): string
{
return in_array($this->getObjectTable(), ["geodb_cities", "geodb_countries"]) ? $this->getObject()->getNativeName() : $this->getObject()->getName();
}

function getLogsText(): string
{
return $this->getRecord()->logs_text;
}

function getObjectURL(): string
{
switch ($this->getObjectTable()) {
case "geodb_countries": return "/editdb?act=country&id=" . $this->getObjectId();
case "geodb_cities": return "/editdb?act=city&id=" . $this->getObjectId();
case "geodb_schools": return "/editdb?act=school&id=" . $this->getObjectId();
case "geodb_universities": return "/editdb?act=university&id=" . $this->getObjectId();
case "geodb_editors": return $this->getObject()->getUser()->getURL();
case "geodb_faculties" || "geodb_specializations": return "/editdb?act=university&id=" . $this->getObject()->getUniversity()->getId();
default: return "/err404.php?id=" . $this->getObjectId();
}
}
}
Loading