Skip to content

Instantly share code, notes, and snippets.

@lipeRomani
Created August 9, 2018 12:27
Show Gist options
  • Save lipeRomani/a338b47b28204d88c374bed9969ab45b to your computer and use it in GitHub Desktop.
Save lipeRomani/a338b47b28204d88c374bed9969ab45b to your computer and use it in GitHub Desktop.
public function checkEmptyFields($data, ExecutionContextInterface $context)
{
$confirmationEmailValue = $data['confirmation_email']->getValue();
$blockPerTimeOrAccessValidity = $this->groupService->checkModuleIsActive('blockPerTimeOrAccessValidity');
$accessCodeModule = $this->moduleService->checkModuleIsActive('access_code');
$businessHoursModule = $this->moduleService->checkModuleIsActive('business_hours');
$emailField = $this->customFieldsService->getFieldByNameType('email');
$phoneField = ($this->customFieldsService->getFieldByNameType('phone') ?: $this->customFieldsService->getFieldByNameType('mobile'));
if ($accessCodeModule && $confirmationEmailValue == 1) {
$context
->buildViolation(
'Não é possível ativar a opção de [Confirmação de cadastro por e-mail] com o módulo Código de
Acesso ativo.'
)
->atPath('setup_enable_confirmation')
->addViolation();
}
if ($businessHoursModule && $confirmationEmailValue == 1) {
$context
->buildViolation(
'Não é possível ativar a opção de [Confirmação de cadastro por e-mail] com
o módulo Horário de Funcionamento ativo.'
)
->atPath('setup_enable_confirmation')
->addViolation();
}
if ($blockPerTimeOrAccessValidity && $confirmationEmailValue == 1) {
$context
->buildViolation(
'Não é possível ativar a opção de [Confirmação de cadastro por e-mail] com
o módulo Bloqueio por tempo / Validade de acesso (Grupo de visitantes) ativo.'
)
->atPath('setup_enable_confirmation')
->addViolation();
}
if (
$this->moduleService->checkModuleIsActive('access_code') !== true) {
if (isset($data['confirmation_email'])) {
if ($confirmationEmailValue == 1) {
if (!$emailField) {
$context
->buildViolation(
"Ativação de 'Confirmação de cadastro via E-mail' não disponível, pois você não possui
o campo E-MAIL no formulário de cadastro."
)
->atPath('enable_welcome_sms')
->addViolation();
}
if ($emailField) {
foreach ($emailField->getValidations() as $validations) {
$type = $validations['type'];
$value = $validations['value'];
if ($type == 'required' && $value == false) {
$context
->buildViolation(
"Ativação de 'Confirmação de cadastro via E-mail' não disponível, pois o campo
E-MAIL não é obrigatório no formulário de cadastro."
)
->atPath('confirmation_email')
->addViolation();
}
}
}
if ($data['confirmation_email_limit_time']->getValue() === null) {
$context
->buildViolation(
'O preenchimento do campo [Tempo limite para confirmação] é obrigatório.'
)
->atPath('confirmation_email')
->addViolation();
}
if ($data['confirmation_email_block_time']->getValue() === null) {
$context
->buildViolation(
'O preenchimento do campo [Tempo de bloqueio] é obrigatório.'
)
->atPath('confirmation_email')
->addViolation();
}
}
if ($confirmationEmailValue === true &&
$data['confirmation_email_limit_time']->getValue()
) {
$value = $data['confirmation_email_limit_time']->getValue();
$value = strtoupper($value);
$value = str_replace(['D', 'H', 'M', 'S'], ['DAY', 'HOUR', 'MINUTE', 'SECOND'], $value);
try {
new Period(new \DateTime(), $value);
} catch (\Exception $ex) {
return $context
->buildViolation(
'Informe um valor válido no campo [Tempo limite para confirmação], ex: 4d 12h 5m'
)
->atPath('confirmation_email_limit_time')
->addViolation();
}
}
if ($confirmationEmailValue === true &&
$data['confirmation_email_block_time']->getValue()
) {
$value = $data['confirmation_email_block_time']->getValue();
$value = strtoupper($value);
$value = str_replace(['D', 'H', 'M', 'S'], ['DAY', 'HOUR', 'MINUTE', 'SECOND'], $value);
try {
new Period(new \DateTime(), $value);
} catch (\Exception $ex) {
return $context
->buildViolation(
'Informe um valor válido no campo [Tempo de bloqueio], ex: 4d 12h 5m'
)
->atPath('confirmation_email_block_time')
->addViolation();
}
}
}
}
if (isset($data['confirmation_sms'])) {
if ($data['confirmation_sms']->getValue() == 1 && !$phoneField) {
if (!$phoneField) {
$context
->buildViolation(
"Ativação de 'Confirmação de cadastro via SMS' não disponível, pois você não possui o campo
TELEFONE/CELULAR no formulário de cadastro."
)
->atPath('confirmation_sms')
->addViolation();
}
if ($phoneField) {
foreach ($phoneField->getValidations() as $validations) {
$type = $validations['type'];
$value = $validations['value'];
if ($type == 'required' && $value == false) {
$context
->buildViolation(
"Ativação de 'Confirmação de cadastro via SMS' não disponível, pois o campo TELEFONE/CELULAR
não é obrigatório no formulário de cadastro."
)
->atPath('confirmation_sms')
->addViolation();
}
}
}
}
if ($data['confirmation_sms']->getValue() == 1 && $confirmationEmailValue == 1) {
if ($data['facebook_page_id']->getValue() === null) {
$context
->buildViolation(
'Apenas uma opção de confirmação de cadastro deverá ser selecionada. Via SMS ou via E-mail.'
)
->atPath('confirmation_sms')
->addViolation();
}
}
if (strpos($data['content_confirmation_sms_pt']->getValue(), '{ codigo }') == false ||
strpos($data['content_confirmation_sms_en']->getValue(), '{ codigo }') == false ||
strpos($data['content_confirmation_sms_es']->getValue(), '{ codigo }') == false) {
$context
->buildViolation(
'A variável { codigo } é obrigatória no conteúdo da mensagem enviada via SMS.'
)
->atPath('content_confirmation_sms_pt')
->addViolation();
}
}
if ($data['enable_welcome_sms']->getValue() == 1) {
if (!$phoneField) {
$context
->buildViolation(
"Ativação de 'Credenciais via SMS' não disponível, pois você não possui o campo TELEFONE no
formulário de cadastro."
)
->atPath('enable_welcome_sms')
->addViolation();
}
if ($phoneField) {
foreach ($phoneField->getValidations() as $validations) {
$type = $validations['type'];
$value = $validations['value'];
if ($type == 'required' && $value == false) {
$context
->buildViolation(
"Ativação de 'Credenciais via SMS' não disponível, pois o campo TELEFONE/CELULAR não é obrigatório
no formulário de cadastro."
)
->atPath('enable_welcome_sms')
->addViolation();
}
}
}
}
if ($data['authorize_email']->getValue() == 1) {
if (!$emailField) {
$context
->buildViolation(
"Ativação de 'Solicitar opt-in dos visitantes' não disponível, pois você não possui o campo E-MAIL
no formulário de cadastro."
)
->atPath('authorize_email')
->addViolation();
}
if ($emailField) {
foreach ($emailField->getValidations() as $validations) {
$type = $validations['type'];
$value = $validations['value'];
if ($type == 'required' && $value == false) {
$context
->buildViolation(
"Ativação de 'Solicitar opt-in dos visitantes' não disponível, pois o campo E-MAIL não é
obrigatório no formulário de cadastro."
)
->atPath('authorize_email')
->addViolation();
}
}
}
}
if (isset($data['facebook_checkin'])) {
if ($data['facebook_checkin']->getValue() === true) {
if ($data['facebook_page_id']->getValue()) {
$pageId = $data['facebook_page_id']->getValue();
$isPage = true;
try {
$request = $this->fb->get(
'/'.$pageId . '?fields=can_checkin',
$this->facebookAppId . '|' . $this->facebookAppSecret
);
$response = $request->getDecodedBody();
if (!array_key_exists('can_checkin', $response)) {
$isPage = false;
}
} catch (\Exception $ex) {
if (strpos($ex->getMessage(), 'Tried accessing nonexisting field (can_checkin)') !== false) {
$isPage = false;
} else {
return $context
->buildViolation(
'Erro na comunicação com o Facebook. Verifique se o ID informado é válido ou tente novamente.'
)
->atPath('facebook_page_id')
->addViolation();
}
}
if (!$isPage) {
return $context
->buildViolation(
'O ID informado não pertence a uma Página. O Check-in apenas é feito em PÁGINAS do
Facebook.'
)
->atPath('facebook_page_id')
->addViolation();
}
}
if ($data['facebook_page_id']->getValue() === null) {
$context
->buildViolation(
'Para ativar o checkin do Facebook, ao menos o campo ID da Página deve ser informado.'
)
->atPath('facebook_checkin')
->addViolation();
}
if (is_numeric($data['facebook_page_id']->getValue()) === false) {
return $context
->buildViolation(
'Informe um valor numérico válido no campo [URL ou ID da Página], ex:
239697379518401'
)
->atPath('facebook_page_id')
->addViolation();
}
}
if ($data['facebook_like']->getValue() === true) {
if ($data['facebook_page_id']->getValue()) {
$pageId = $data['facebook_page_id']->getValue();
$isPage = true;
try {
$request = $this->fb->get(
'/'.$pageId . '?fields=can_checkin',
$this->facebookAppId . '|' . $this->facebookAppSecret
);
$response = $request->getDecodedBody();
if (!array_key_exists('can_checkin', $response)) {
$isPage = false;
}
} catch (\Exception $ex) {
if (strpos($ex->getMessage(), 'Tried accessing nonexisting field (can_checkin)') !== false) {
$isPage = false;
} else {
return $context
->buildViolation(
'Erro na comunicação com o Facebook. Verifique se o ID informado é válido ou tente novamente.'
)
->atPath('facebook_page_id')
->addViolation();
}
}
if (!$isPage) {
return $context
->buildViolation(
'O ID informado não pertence a uma Página. O Curtir apenas é feito em PÁGINAS do
Facebook.'
)
->atPath('facebook_page_id')
->addViolation();
}
}
if ($data['facebook_page_id']->getValue() === null) {
$context
->buildViolation(
'Para ativar o Curtir do Facebook, o campo ID da Página deve ser informado.'
)
->atPath('facebook_like')
->addViolation();
}
if (is_numeric($data['facebook_page_id']->getValue()) === false) {
return $context
->buildViolation(
'Informe um valor numérico válido no campo [URL ou ID da Página], ex:
239697379518401'
)
->atPath('facebook_page_id')
->addViolation();
}
}
}
$redirectUrl = $data['redirect_url']->getValue();
if (substr($redirectUrl, 0, 5) != 'http:') {
if (substr($redirectUrl, 0, 5) != 'https') {
$context
->buildViolation(
'Informe uma URL de redirecionamento no formato válido, ex: http://www.google.com'
)
->atPath('setup_redirect_url')
->addViolation();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment