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/limit registration #112

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
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: 49 additions & 18 deletions Theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use MapasCulturais\Themes\BaseV1;
use MapasCulturais\Entities\Agent;
use MapasCulturais\Entities\Opportunity;
use MapasCulturais\Entities\ProjectOpportunity;

// Constante para definir itens por página
define("ITEMS_PER_PAGE", 100);
Expand Down Expand Up @@ -310,23 +309,6 @@ protected function _init()
$this->part('singles/agents/type', ['entity' => $entity]);
});

/*$app->hook('template(opportunity.<<create|edit|single>>.registration-list-header):end', function () use ($app) {
if ($app->user->is('admin')) {
echo '<th class="registration-status-col">Administrador</th>';
}
});

$app->hook('template(opportunity.<<create|edit|single>>.registration-list-item):end', function () use ($app) {
if ($app->user->is('admin')) {
echo '<td><button data-id="{{reg.id}}" onclick=\'if (confirm("Tem certeza que você deseja apagar a inscrição n. on-" + this.dataset.id + " ?")) {$.ajax({url: MapasCulturais.baseURL + "/registration/remove/registration_id:"+ this.dataset.id , success: function(result){ if(result.success) {MapasCulturais.Messages.success("Inscrição excluida com sucesso!");} else{ MapasCulturais.Messages.error(result.error);} }});}\'> Apagar </button> </td>';
}
});

$app->hook('template.opportunity.single.header.registration-item', function ($registrationId) use ($app) {
if ($app->user->is('admin')) {
echo '<button class="btn btn-danger" data-id=' . $registrationId . ' onclick=\'if (confirm("Tem certeza que você deseja apagar a inscrição n. on-" + this.dataset.id + " ?")) {$.ajax({url: MapasCulturais.baseURL + "/registration/remove/registration_id:"+ this.dataset.id , success: function(result){ if(result.success) {MapasCulturais.Messages.success("Inscrição excluida com sucesso!");} else{ MapasCulturais.Messages.error(result.error);} }});}\'> Apagar </button>';
}
});*/

/* Adicionando novos campos na entidade entity revision agent */
$app->hook('template(entityrevision.history.tab-about-service):end', function () {
Expand Down Expand Up @@ -689,6 +671,12 @@ protected function _init()
$opportunity = $entity instanceof Opportunity ? $entity : $entity->opportunity;
$html = Utils::getTermsByOpportunity($html, $opportunity);
});

$app->hook("entity(Registration).send:before", function () use ($app, $theme) {
$reg = $this;
$locationTheme = true;
$theme->hasExceededRegistrationLimit($app, $reg, $locationTheme);
});
}

/**
Expand Down Expand Up @@ -1223,4 +1211,47 @@ public function fixAgentPermission($user)
}
}
}

/**
* Verifica se o limite de inscrições foi atingido e retorna um json para uso no javascript
* ou boolean para uso na view
*/
public function hasExceededRegistrationLimit($app, $reg, $locationTheme)
{
$countReg = $app->repo('Registration')->findBy(['opportunity' => $reg->opportunity->id]);
$limit = false;
// verifica se tem o campo e valor diferente de null
foreach ($reg->opportunity->getMetadata() as $key => $valueMeta) {
if($key == 'registrationLimit' && !is_null($valueMeta) && $valueMeta == "0" )
{
$limit = false;
}elseif(// Verifica o limite
$key == 'registrationLimit' &&
!is_null($valueMeta) &&
count($countReg) > (int) $reg->opportunity->getMetadata('registrationLimit')
){
$limit = true;
}
}

if($limit && $locationTheme)
{
$this->jsonError(200, 'exceeded');
}elseif( count($countReg) > (int) $reg->opportunity->getMetadata('registrationLimit') ){
return $limit;
}
}

/**
* Funcionalidade para uso quando necessitar enviar mensagem de erro ao javascript
* int $status
* object|array $data
* string void
*/
public function jsonError($status, $data): string
{
$app = App::i();
$app->contentType('application/json');
return $app->halt($status, json_encode(['error' => true, 'data' => $data]));
}
}
3 changes: 3 additions & 0 deletions assets/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -9981,3 +9981,6 @@ button.mfp-arrow-left {
border-radius: 5px;padding: 0 0.23333rem; height: 30px;font-size: 10px;
}

.text-danger {
color: #a94442;
}