Files
SIGE-WEB-snapshot/wp-content/plugins/sige-events/includes/apoio.php

194 lines
6.6 KiB
PHP

<?php
if (!defined('ABSPATH')) {
exit;
}
function showPageApoio()
{
global $wpdb;
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
// Check if the user is registered as military
$militar = get_user_meta($user_id, 'militar', true);
if ($militar) {
$url = home_url('/apoio-militares/');
return "<a style='font-size: 25px' href=\"$url\">Solicite apoio de rancho e transporte clicando aqui</a>";
}
return '';
}
add_shortcode('show_page_apoio', 'showPageApoio');
function frontendApoio()
{
if (!is_user_logged_in()) {
return '
<div style="
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: #808080; /* Gray background */
color: #fff; /* White text color */
padding: 20px;
box-shadow: 0px 0px 15px rgba(0, 0, 0, 0.3);
z-index: 1000;
text-align: center;
border-radius: 10px;
">
<p>É necessário estar logado para acessar esta página.</p>
</div>
<meta http-equiv="refresh" content="2;url=https://sige.ita.br/log-in/" />';
}
global $wpdb;
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
$year = date('Y');
// Check if the user is registered as military
$militar = get_user_meta($user_id, 'militar', true);
if ($militar == 'Não') {
return '
<div style="
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: #808080; /* Gray background */
color: #fff; /* White text color */
padding: 20px;
box-shadow: 0px 0px 15px rgba(0, 0, 0, 0.3);
z-index: 1000;
text-align: center;
border-radius: 10px;
">
<p>Apenas militares podem solicitar o tipo de apoio fornecido nessa página.</p>
</div>
<meta http-equiv="refresh" content="2;url=https://sige.ita.br/" />';
$referer = wp_get_referer();
$safe_referer = wp_validate_redirect($referer, home_url());
wp_safe_redirect(home_url());
exit;
}
// Verificar se o militar já solicitou apoio anteriormente
$apoio = $wpdb->get_row($wpdb->prepare("
SELECT * FROM {$wpdb->prefix}sige_apoio WHERE user_id = %d AND ano = %d", $user_id, $year));
if ($apoio == null) {
$apoio = (object) array(
"id" => null,
"almoco_1" => 0,
"almoco_2" => 0,
"almoco_3" => 0,
"rodoviario" => "",
"aereo" => "",
);
}
// Process form submission before any HTML output
if (isset($_POST['submit'])) {
$almoco_1 = isset($_POST['almoco_dia_1']) ? 1 : 0;
$almoco_2 = isset($_POST['almoco_dia_2']) ? 1 : 0;
$almoco_3 = isset($_POST['almoco_dia_3']) ? 1 : 0;
$rodoviario = sanitize_text_field($_POST['apoio_rodoviario']);
$aereo = sanitize_text_field($_POST['apoio_aereo']);
// Store options in database
if ($apoio->id != null) {
$wpdb->update(
"{$wpdb->prefix}sige_apoio",
[
"user_id" => $user_id,
"ano" => $year,
"almoco_1" => $almoco_1,
"almoco_2" => $almoco_2,
"almoco_3" => $almoco_3,
"rodoviario" => $rodoviario,
"aereo" => $aereo
],
["id" => $apoio->id]
);
} else {
$wpdb->insert(
"{$wpdb->prefix}sige_apoio",
[
"user_id" => $user_id,
"ano" => $year,
"almoco_1" => $almoco_1,
"almoco_2" => $almoco_2,
"almoco_3" => $almoco_3,
"rodoviario" => $rodoviario,
"aereo" => $aereo
]
);
}
wp_safe_redirect(esc_url($_SERVER['REQUEST_URI']));
exit;
}
echo '<form method="post" action="">
<h1>Solicitação de Apoio para o SIGE</h1>
<hr style="border: 2px solid black; margin-top: 5px;">
<h2>Apoio de rancho</h2>
Almoço dia 29 de setembro: <input type="checkbox" name="almoco_dia_1"' . ($apoio->almoco_1 == 1 ? ' checked="checked"' : "") . '><br>
Almoço dia 30 de setembro: <input type="checkbox" name="almoco_dia_2"' . ($apoio->almoco_2 == 1 ? ' checked="checked"' : "") . '><br>
Almoço dia 1 de outubro: <input type="checkbox" name="almoco_dia_3"' . ($apoio->almoco_3 == 1 ? ' checked="checked"' : "") . '><br>
<br>
<hr style="border: 2px solid black; margin-top: 5px;">
<h2>Apoio rodoviário</h2>
Entre o nome do hotel ou endereço caso queira apoio de viatura<br>
<input type="text" name="apoio_rodoviario" value="' . esc_attr($apoio->rodoviario) . '"><br>
<br>
<hr style="border: 2px solid black; margin-top: 5px;">
<h2>Apoio Aéreo</h2>
Caso necessite de apoio aéreo, insira a cidade de origem:<br>
<input type="text" name="apoio_aereo"' . ($apoio->aereo != "" ? "value=" . $apoio->aereo : "") . '><br>
<br>
<button type="submit" name="submit">Enviar</button>
</form>';
}
add_shortcode('apoio', 'frontendApoio');
function getDataApoioRancho(): void
{
global $wpdb;
if (!is_user_logged_in()) {
// Add the style to the head section of the page
echo '<p>You need to be logged in to view this page.</p>';
}
$user = wp_get_current_user();
$roles = $user->roles;
$has_access = array('comissao_apoio', 'administrator');
$user_allowed_roles = array_intersect($roles, $has_access);
if (empty($user_allowed_roles)) {
// Add the style for the restricted message
echo '
<p>Sorry, you don\'t have permission to view this content.</p>
';
} else {
$current_year = date('Y');
foreach (range(1, 3) as $day) {
$almoco = <<<SQL
SELECT meta_value, COUNT(*) as num FROM {$wpdb->prefix}usermeta
WHERE meta_key = 'circulo_hierarquico' and user_id in
(SELECT user_id FROM {$wpdb->prefix}sige_apoio WHERE almoco_%d = 1 and ano = %d )
GROUP by meta_value ORDER by meta_value
SQL;
$result = $wpdb->get_results($wpdb->prepare($almoco, $day, $current_year), ARRAY_A);
echo '<h3>Solicitações de almoço para o ' . $day . 'º Dia</h3>';
foreach ($result as $data) {
echo $data['meta_value'] . ': ' . $data['num'] . '<br>';
}
echo '<hr style="border: 2px solid black; margin-top: 5px;">';
}
}
}
add_shortcode('DataApoio', 'getDataApoioRancho');