Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/modelos all entidades #489

Merged
merged 5 commits into from
Sep 10, 2024
Merged
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
4 changes: 3 additions & 1 deletion src/core/ApiQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -1367,7 +1367,9 @@
$entity_id = $entity[$this->pk];

if (isset($metadata[$entity_id])) {
$can_view = $permissions[$entity_id];
if (isset($permissions[$entity_id])) {
$can_view = $permissions[$entity_id];

Check warning on line 1371 in src/core/ApiQuery.php

View check run for this annotation

Codecov / codecov/patch

src/core/ApiQuery.php#L1370-L1371

Added lines #L1370 - L1371 were not covered by tests
}

$meta = $metadata[$entity_id];
foreach($meta as $k => $v){
Expand Down
104 changes: 55 additions & 49 deletions src/core/Traits/EntityManagerModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,29 @@
namespace MapasCulturais\Traits;

use MapasCulturais\App;
use MapasCulturais\Entities\ProjectOpportunity;
use MapasCulturais\Entity;

trait EntityManagerModel {

private ProjectOpportunity $opportunity;
private ProjectOpportunity $opportunityModel;
private $entityOpportunity;
private $entityOpportunityModel;

function ALL_generatemodel(){
$app = App::i();

$this->requireAuthentication();
$this->opportunity = $this->requestedEntity;
$this->opportunityModel = $this->generateModel();
$this->entityOpportunity = $this->requestedEntity;
$this->entityOpportunityModel = $this->generateModel();

Check warning on line 17 in src/core/Traits/EntityManagerModel.php

View check run for this annotation

Codecov / codecov/patch

src/core/Traits/EntityManagerModel.php#L16-L17

Added lines #L16 - L17 were not covered by tests

$this->generateEvaluationMethods();
$this->generatePhases();
$this->generateMetadata();
$this->generateRegistrationFieldsAndFiles();

$this->opportunityModel->save(true);
$this->entityOpportunityModel->save(true);

Check warning on line 24 in src/core/Traits/EntityManagerModel.php

View check run for this annotation

Codecov / codecov/patch

src/core/Traits/EntityManagerModel.php#L24

Added line #L24 was not covered by tests

if($this->isAjax()){
$this->json($this->opportunity);
$this->json($this->entityOpportunity);

Check warning on line 27 in src/core/Traits/EntityManagerModel.php

View check run for this annotation

Codecov / codecov/patch

src/core/Traits/EntityManagerModel.php#L27

Added line #L27 was not covered by tests
}else{
$app->redirect($app->request->getReferer());
}
Expand All @@ -35,27 +34,34 @@
$app = App::i();

$this->requireAuthentication();
$this->opportunity = $this->requestedEntity;
$this->entityOpportunity = $this->requestedEntity;

Check warning on line 37 in src/core/Traits/EntityManagerModel.php

View check run for this annotation

Codecov / codecov/patch

src/core/Traits/EntityManagerModel.php#L37

Added line #L37 was not covered by tests

$this->opportunityModel = $this->generateOpportunity();
$this->entityOpportunityModel = $this->generateOpportunity();

Check warning on line 39 in src/core/Traits/EntityManagerModel.php

View check run for this annotation

Codecov / codecov/patch

src/core/Traits/EntityManagerModel.php#L39

Added line #L39 was not covered by tests

$this->generateEvaluationMethods();
$this->generatePhases();
$this->generateMetadata(0, 0);
$this->generateRegistrationFieldsAndFiles();

$this->opportunityModel->save(true);
$this->entityOpportunityModel->save(true);

Check warning on line 46 in src/core/Traits/EntityManagerModel.php

View check run for this annotation

Codecov / codecov/patch

src/core/Traits/EntityManagerModel.php#L46

Added line #L46 was not covered by tests

$this->json($this->opportunityModel);
$this->json($this->entityOpportunityModel);

Check warning on line 48 in src/core/Traits/EntityManagerModel.php

View check run for this annotation

Codecov / codecov/patch

src/core/Traits/EntityManagerModel.php#L48

Added line #L48 was not covered by tests
}

function GET_findOpportunitiesModels()
{
$app = App::i();
$dataModels = [];

$opportunities = $app->repo('Opportunity')->findAll();
foreach ($opportunities as $opp) {

$opportunities = $app->em->createQuery("

Check warning on line 56 in src/core/Traits/EntityManagerModel.php

View check run for this annotation

Codecov / codecov/patch

src/core/Traits/EntityManagerModel.php#L56

Added line #L56 was not covered by tests
SELECT
op.id
FROM
MapasCulturais\Entities\Opportunity op
");

Check warning on line 61 in src/core/Traits/EntityManagerModel.php

View check run for this annotation

Codecov / codecov/patch

src/core/Traits/EntityManagerModel.php#L61

Added line #L61 was not covered by tests

foreach ($opportunities->getResult() as $opportunity) {
$opp = $app->repo('Opportunity')->find($opportunity['id']);

Check warning on line 64 in src/core/Traits/EntityManagerModel.php

View check run for this annotation

Codecov / codecov/patch

src/core/Traits/EntityManagerModel.php#L63-L64

Added lines #L63 - L64 were not covered by tests
if ($opp->isModel) {
$phases = $opp->phases;

Expand All @@ -76,7 +82,7 @@
echo json_encode($dataModels);
}

private function generateModel() : ProjectOpportunity
private function generateModel()

Check warning on line 85 in src/core/Traits/EntityManagerModel.php

View check run for this annotation

Codecov / codecov/patch

src/core/Traits/EntityManagerModel.php#L85

Added line #L85 was not covered by tests
{
$app = App::i();

Expand All @@ -85,47 +91,47 @@
$name = $postData['name'];
$description = $postData['description'];

$this->opportunityModel = clone $this->opportunity;
$this->entityOpportunityModel = clone $this->entityOpportunity;

Check warning on line 94 in src/core/Traits/EntityManagerModel.php

View check run for this annotation

Codecov / codecov/patch

src/core/Traits/EntityManagerModel.php#L94

Added line #L94 was not covered by tests

$this->opportunityModel->name = $name;
$this->opportunityModel->status = -1;
$this->opportunityModel->shortDescription = $description;
$app->em->persist($this->opportunityModel);
$this->entityOpportunityModel->name = $name;
$this->entityOpportunityModel->status = -1;
$this->entityOpportunityModel->shortDescription = $description;
$app->em->persist($this->entityOpportunityModel);

Check warning on line 99 in src/core/Traits/EntityManagerModel.php

View check run for this annotation

Codecov / codecov/patch

src/core/Traits/EntityManagerModel.php#L96-L99

Added lines #L96 - L99 were not covered by tests
$app->em->flush();

// necessário adicionar as categorias, proponetes e ranges após salvar devido a trigger public.fn_propagate_opportunity_insert
$this->opportunityModel->registrationCategories = $this->opportunity->registrationCategories;
$this->opportunityModel->registrationProponentTypes = $this->opportunity->registrationProponentTypes;
$this->opportunityModel->registrationRanges = $this->opportunity->registrationRanges;
$this->opportunityModel->save(true);
$this->entityOpportunityModel->registrationCategories = $this->entityOpportunity->registrationCategories;
$this->entityOpportunityModel->registrationProponentTypes = $this->entityOpportunity->registrationProponentTypes;
$this->entityOpportunityModel->registrationRanges = $this->entityOpportunity->registrationRanges;
$this->entityOpportunityModel->save(true);

Check warning on line 106 in src/core/Traits/EntityManagerModel.php

View check run for this annotation

Codecov / codecov/patch

src/core/Traits/EntityManagerModel.php#L103-L106

Added lines #L103 - L106 were not covered by tests

return $this->opportunityModel;
return $this->entityOpportunityModel;

Check warning on line 108 in src/core/Traits/EntityManagerModel.php

View check run for this annotation

Codecov / codecov/patch

src/core/Traits/EntityManagerModel.php#L108

Added line #L108 was not covered by tests


}

private function generateOpportunity() : ProjectOpportunity
private function generateOpportunity()

Check warning on line 113 in src/core/Traits/EntityManagerModel.php

View check run for this annotation

Codecov / codecov/patch

src/core/Traits/EntityManagerModel.php#L113

Added line #L113 was not covered by tests
{
$app = App::i();

$postData = $this->postData;

$name = $postData['name'];

$this->opportunityModel = clone $this->opportunity;
$this->entityOpportunityModel = clone $this->entityOpportunity;

Check warning on line 121 in src/core/Traits/EntityManagerModel.php

View check run for this annotation

Codecov / codecov/patch

src/core/Traits/EntityManagerModel.php#L121

Added line #L121 was not covered by tests

$this->opportunityModel->name = $name;
$this->opportunityModel->status = Entity::STATUS_DRAFT;
$app->em->persist($this->opportunityModel);
$this->entityOpportunityModel->name = $name;
$this->entityOpportunityModel->status = Entity::STATUS_DRAFT;
$app->em->persist($this->entityOpportunityModel);

Check warning on line 125 in src/core/Traits/EntityManagerModel.php

View check run for this annotation

Codecov / codecov/patch

src/core/Traits/EntityManagerModel.php#L123-L125

Added lines #L123 - L125 were not covered by tests
$app->em->flush();

// necessário adicionar as categorias, proponetes e ranges após salvar devido a trigger public.fn_propagate_opportunity_insert
$this->opportunityModel->registrationCategories = $this->opportunity->registrationCategories;
$this->opportunityModel->registrationProponentTypes = $this->opportunity->registrationProponentTypes;
$this->opportunityModel->registrationRanges = $this->opportunity->registrationRanges;
$this->opportunityModel->save(true);
$this->entityOpportunityModel->registrationCategories = $this->entityOpportunity->registrationCategories;
$this->entityOpportunityModel->registrationProponentTypes = $this->entityOpportunity->registrationProponentTypes;
$this->entityOpportunityModel->registrationRanges = $this->entityOpportunity->registrationRanges;
$this->entityOpportunityModel->save(true);

Check warning on line 132 in src/core/Traits/EntityManagerModel.php

View check run for this annotation

Codecov / codecov/patch

src/core/Traits/EntityManagerModel.php#L129-L132

Added lines #L129 - L132 were not covered by tests

return $this->opportunityModel;
return $this->entityOpportunityModel;

Check warning on line 134 in src/core/Traits/EntityManagerModel.php

View check run for this annotation

Codecov / codecov/patch

src/core/Traits/EntityManagerModel.php#L134

Added line #L134 was not covered by tests
}

private function generateEvaluationMethods() : void
Expand All @@ -134,11 +140,11 @@

// duplica o método de avaliação para a oportunidade primária
$evaluationMethodConfigurations = $app->repo('EvaluationMethodConfiguration')->findBy([
'opportunity' => $this->opportunity
'opportunity' => $this->entityOpportunity

Check warning on line 143 in src/core/Traits/EntityManagerModel.php

View check run for this annotation

Codecov / codecov/patch

src/core/Traits/EntityManagerModel.php#L143

Added line #L143 was not covered by tests
]);
foreach ($evaluationMethodConfigurations as $evaluationMethodConfiguration) {
$newMethodConfiguration = clone $evaluationMethodConfiguration;
$newMethodConfiguration->setOpportunity($this->opportunityModel);
$newMethodConfiguration->setOpportunity($this->entityOpportunityModel);

Check warning on line 147 in src/core/Traits/EntityManagerModel.php

View check run for this annotation

Codecov / codecov/patch

src/core/Traits/EntityManagerModel.php#L147

Added line #L147 was not covered by tests
$newMethodConfiguration->save(true);

// duplica os metadados das configurações do modelo de avaliação
Expand All @@ -154,12 +160,12 @@
$app = App::i();

$phases = $app->repo('Opportunity')->findBy([
'parent' => $this->opportunity
'parent' => $this->entityOpportunity

Check warning on line 163 in src/core/Traits/EntityManagerModel.php

View check run for this annotation

Codecov / codecov/patch

src/core/Traits/EntityManagerModel.php#L163

Added line #L163 was not covered by tests
]);
foreach ($phases as $phase) {
if (!$phase->getMetadata('isLastPhase')) {
$newPhase = clone $phase;
$newPhase->setParent($this->opportunityModel);
$newPhase->setParent($this->entityOpportunityModel);

Check warning on line 168 in src/core/Traits/EntityManagerModel.php

View check run for this annotation

Codecov / codecov/patch

src/core/Traits/EntityManagerModel.php#L168

Added line #L168 was not covered by tests

foreach ($phase->getMetadata() as $metadataKey => $metadataValue) {
if (!is_null($metadataValue) && $metadataValue != '') {
Expand Down Expand Up @@ -194,7 +200,7 @@

if (isset($publishDate)) {
$phases = $app->repo('Opportunity')->findBy([
'parent' => $this->opportunityModel
'parent' => $this->entityOpportunityModel

Check warning on line 203 in src/core/Traits/EntityManagerModel.php

View check run for this annotation

Codecov / codecov/patch

src/core/Traits/EntityManagerModel.php#L203

Added line #L203 was not covered by tests
]);

foreach ($phases as $phase) {
Expand All @@ -209,29 +215,29 @@

private function generateMetadata($isModel = 1, $isModelPublic = 0) : void
{
foreach ($this->opportunity->getMetadata() as $metadataKey => $metadataValue) {
foreach ($this->entityOpportunity->getMetadata() as $metadataKey => $metadataValue) {

Check warning on line 218 in src/core/Traits/EntityManagerModel.php

View check run for this annotation

Codecov / codecov/patch

src/core/Traits/EntityManagerModel.php#L218

Added line #L218 was not covered by tests
if (!is_null($metadataValue) && $metadataValue != '') {
$this->opportunityModel->setMetadata($metadataKey, $metadataValue);
$this->entityOpportunityModel->setMetadata($metadataKey, $metadataValue);

Check warning on line 220 in src/core/Traits/EntityManagerModel.php

View check run for this annotation

Codecov / codecov/patch

src/core/Traits/EntityManagerModel.php#L220

Added line #L220 was not covered by tests
}
}

$this->opportunityModel->setMetadata('isModel', $isModel);
$this->opportunityModel->setMetadata('isModelPublic', $isModelPublic);
$this->entityOpportunityModel->setMetadata('isModel', $isModel);
$this->entityOpportunityModel->setMetadata('isModelPublic', $isModelPublic);

Check warning on line 225 in src/core/Traits/EntityManagerModel.php

View check run for this annotation

Codecov / codecov/patch

src/core/Traits/EntityManagerModel.php#L224-L225

Added lines #L224 - L225 were not covered by tests

$this->opportunityModel->saveTerms();
$this->entityOpportunityModel->saveTerms();

Check warning on line 227 in src/core/Traits/EntityManagerModel.php

View check run for this annotation

Codecov / codecov/patch

src/core/Traits/EntityManagerModel.php#L227

Added line #L227 was not covered by tests
}

private function generateRegistrationFieldsAndFiles() : void
{
foreach ($this->opportunity->getRegistrationFieldConfigurations() as $registrationFieldConfiguration) {
foreach ($this->entityOpportunity->getRegistrationFieldConfigurations() as $registrationFieldConfiguration) {

Check warning on line 232 in src/core/Traits/EntityManagerModel.php

View check run for this annotation

Codecov / codecov/patch

src/core/Traits/EntityManagerModel.php#L232

Added line #L232 was not covered by tests
$fieldConfiguration = clone $registrationFieldConfiguration;
$fieldConfiguration->setOwnerId($this->opportunityModel->getId());
$fieldConfiguration->setOwnerId($this->entityOpportunityModel->getId());

Check warning on line 234 in src/core/Traits/EntityManagerModel.php

View check run for this annotation

Codecov / codecov/patch

src/core/Traits/EntityManagerModel.php#L234

Added line #L234 was not covered by tests
$fieldConfiguration->save(true);
}

foreach ($this->opportunity->getRegistrationFileConfigurations() as $registrationFileConfiguration) {
foreach ($this->entityOpportunity->getRegistrationFileConfigurations() as $registrationFileConfiguration) {

Check warning on line 238 in src/core/Traits/EntityManagerModel.php

View check run for this annotation

Codecov / codecov/patch

src/core/Traits/EntityManagerModel.php#L238

Added line #L238 was not covered by tests
$fileConfiguration = clone $registrationFileConfiguration;
$fileConfiguration->setOwnerId($this->opportunityModel->getId());
$fileConfiguration->setOwnerId($this->entityOpportunityModel->getId());

Check warning on line 240 in src/core/Traits/EntityManagerModel.php

View check run for this annotation

Codecov / codecov/patch

src/core/Traits/EntityManagerModel.php#L240

Added line #L240 was not covered by tests
$fileConfiguration->save(true);
}

Expand Down
Loading
Loading