Skip to content

Commit

Permalink
Merge pull request #489 from RedeMapas/feature/modelos-all-entidades
Browse files Browse the repository at this point in the history
Feature/modelos all entidades
  • Loading branch information
lpirola authored Sep 10, 2024
2 parents 27a78bf + 68999f4 commit a33a609
Show file tree
Hide file tree
Showing 9 changed files with 270 additions and 393 deletions.
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 @@ protected function appendMetadata(array &$entities) {
$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];
}

$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();

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

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

if($this->isAjax()){
$this->json($this->opportunity);
$this->json($this->entityOpportunity);
}else{
$app->redirect($app->request->getReferer());
}
Expand All @@ -35,27 +34,34 @@ function ALL_generateopportunity(){
$app = App::i();

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

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

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

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

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

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

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

$opportunities = $app->em->createQuery("
SELECT
op.id
FROM
MapasCulturais\Entities\Opportunity op
");

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

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

private function generateModel() : ProjectOpportunity
private function generateModel()
{
$app = App::i();

Expand All @@ -85,47 +91,47 @@ private function generateModel() : ProjectOpportunity
$name = $postData['name'];
$description = $postData['description'];

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

$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);
$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);

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


}

private function generateOpportunity() : ProjectOpportunity
private function generateOpportunity()
{
$app = App::i();

$postData = $this->postData;

$name = $postData['name'];

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

$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);
$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);

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

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

// duplica o método de avaliação para a oportunidade primária
$evaluationMethodConfigurations = $app->repo('EvaluationMethodConfiguration')->findBy([
'opportunity' => $this->opportunity
'opportunity' => $this->entityOpportunity
]);
foreach ($evaluationMethodConfigurations as $evaluationMethodConfiguration) {
$newMethodConfiguration = clone $evaluationMethodConfiguration;
$newMethodConfiguration->setOpportunity($this->opportunityModel);
$newMethodConfiguration->setOpportunity($this->entityOpportunityModel);
$newMethodConfiguration->save(true);

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

$phases = $app->repo('Opportunity')->findBy([
'parent' => $this->opportunity
'parent' => $this->entityOpportunity
]);
foreach ($phases as $phase) {
if (!$phase->getMetadata('isLastPhase')) {
$newPhase = clone $phase;
$newPhase->setParent($this->opportunityModel);
$newPhase->setParent($this->entityOpportunityModel);

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

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

foreach ($phases as $phase) {
Expand All @@ -209,29 +215,29 @@ private function generatePhases() : void

private function generateMetadata($isModel = 1, $isModelPublic = 0) : void
{
foreach ($this->opportunity->getMetadata() as $metadataKey => $metadataValue) {
foreach ($this->entityOpportunity->getMetadata() as $metadataKey => $metadataValue) {
if (!is_null($metadataValue) && $metadataValue != '') {
$this->opportunityModel->setMetadata($metadataKey, $metadataValue);
$this->entityOpportunityModel->setMetadata($metadataKey, $metadataValue);
}
}

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

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

private function generateRegistrationFieldsAndFiles() : void
{
foreach ($this->opportunity->getRegistrationFieldConfigurations() as $registrationFieldConfiguration) {
foreach ($this->entityOpportunity->getRegistrationFieldConfigurations() as $registrationFieldConfiguration) {
$fieldConfiguration = clone $registrationFieldConfiguration;
$fieldConfiguration->setOwnerId($this->opportunityModel->getId());
$fieldConfiguration->setOwnerId($this->entityOpportunityModel->getId());
$fieldConfiguration->save(true);
}

foreach ($this->opportunity->getRegistrationFileConfigurations() as $registrationFileConfiguration) {
foreach ($this->entityOpportunity->getRegistrationFileConfigurations() as $registrationFileConfiguration) {
$fileConfiguration = clone $registrationFileConfiguration;
$fileConfiguration->setOwnerId($this->opportunityModel->getId());
$fileConfiguration->setOwnerId($this->entityOpportunityModel->getId());
$fileConfiguration->save(true);
}

Expand Down
Loading

0 comments on commit a33a609

Please sign in to comment.