293 lines
11 KiB
PHP
293 lines
11 KiB
PHP
<?
|
|
require_once "{$_SERVER['DOCUMENT_ROOT']}/class/c_login.php";
|
|
header('Content-Type: application/json');
|
|
|
|
if (!Login::is_logged()) {
|
|
header('HTTP/1.1 401 Unauthorized');
|
|
echo json_encode(array('error' => 'No se ha iniciado sesión'));
|
|
exit();
|
|
}
|
|
$user = Login::get_user();
|
|
try {
|
|
|
|
switch ($_SERVER['REQUEST_METHOD']) {
|
|
case 'GET':
|
|
$facultad_id = $user->facultad['facultad_id'];
|
|
$avisos = $db->query(
|
|
"SELECT * FROM aviso
|
|
WHERE
|
|
(CURRENT_DATE BETWEEN aviso_fecha_inicial AND aviso_fecha_final) AND
|
|
(facultad_id = :facultad_id OR :facultad_id IS NULL) AND
|
|
aviso_estado
|
|
ORDER BY aviso_id DESC",
|
|
array('facultad_id' => $facultad_id)
|
|
);
|
|
|
|
/*
|
|
if (empty($avisos)) {
|
|
header('HTTP/1.1 404 Not Found');
|
|
echo json_encode(array('error' => 'No hay avisos disponibles'));
|
|
exit();
|
|
}
|
|
*/
|
|
|
|
$avisos = array_map(fn($aviso) => array(
|
|
...$aviso,
|
|
'carreras' => $db->query(
|
|
"SELECT carrera_id, carrera_nombre FROM aviso_carrera
|
|
JOIN carrera USING (carrera_id)
|
|
WHERE aviso_id = :aviso_id",
|
|
array('aviso_id' => $aviso['aviso_id'])
|
|
),
|
|
'profesores' => $db->query(
|
|
"SELECT profesor_id, profesor_clave, profesor_nombre FROM aviso_profesor
|
|
JOIN profesor USING (profesor_id)
|
|
WHERE aviso_id = :aviso_id",
|
|
array('aviso_id' => $aviso['aviso_id'])
|
|
),
|
|
), $avisos);
|
|
echo json_encode($avisos);
|
|
break;
|
|
case 'POST':
|
|
$raw_input = file_get_contents('php://input');
|
|
if (empty($raw_input)) {
|
|
header('HTTP/1.1 400 Bad Request');
|
|
echo json_encode(array('error' => 'No se recibieron parámetros'));
|
|
exit();
|
|
}
|
|
|
|
$input_data = json_decode($raw_input);
|
|
if (json_last_error() !== JSON_ERROR_NONE) {
|
|
header('HTTP/1.1 400 Bad Request');
|
|
echo json_encode(array('error' => 'Invalid JSON format'));
|
|
exit();
|
|
}
|
|
|
|
|
|
$schema = <<<JSON
|
|
{
|
|
"\$schema": "http://json-schema.org/draft-07/schema#",
|
|
"type": "object",
|
|
"required": ["aviso_fecha_inicial", "aviso_fecha_final", "aviso_titulo", "aviso_texto"],
|
|
"properties": {
|
|
"aviso_fecha_inicial": {
|
|
"type": "string",
|
|
"format": "date"
|
|
},
|
|
"aviso_fecha_final": {
|
|
"type": "string",
|
|
"format": "date"
|
|
},
|
|
"aviso_texto": {
|
|
"type": "string"
|
|
},
|
|
"aviso_titulo": {
|
|
"type": "string"
|
|
},
|
|
"carreras": {
|
|
"type": "array",
|
|
"items": {
|
|
"type": "integer",
|
|
"minimum": 1
|
|
},
|
|
"minItems": 0,
|
|
"uniqueItems": true
|
|
},
|
|
"profesores": {
|
|
"type": "array",
|
|
"items": {
|
|
"type": "integer",
|
|
"minimum": 1
|
|
},
|
|
"minItems": 0,
|
|
"uniqueItems": true
|
|
}
|
|
},
|
|
"anyOf": [
|
|
{"required": ["carreras"]},
|
|
{"required": ["profesores"]}
|
|
]
|
|
}
|
|
JSON;
|
|
// VALIDATE JSON SCHEMA
|
|
$validate = new JsonSchema\Validator();
|
|
$validate->validate($input_data, json_decode($schema));
|
|
|
|
if (!$validate->isValid()) {
|
|
header('HTTP/1.1 400 Bad Request');
|
|
echo json_encode(
|
|
array(
|
|
'error' => 'El formato de la solicitud es incorrecto',
|
|
'success' => false,
|
|
'errors' => $validate->getErrors()
|
|
)
|
|
);
|
|
exit();
|
|
}
|
|
|
|
$aviso_id = $db->insert(
|
|
'aviso',
|
|
array(
|
|
'aviso_fecha_inicial' => $input_data->aviso_fecha_inicial,
|
|
'aviso_fecha_final' => $input_data->aviso_fecha_final,
|
|
'aviso_texto' => $input_data->aviso_texto,
|
|
'facultad_id' => $user->facultad['facultad_id'],
|
|
),
|
|
'aviso_id'
|
|
);
|
|
|
|
if (isset($input_data->carreras)) {
|
|
array_walk($input_data->carreras, fn($carrera_id) => $db->insert('aviso_carrera', array('aviso_id' => $aviso_id, 'carrera_id' => $carrera_id)));
|
|
}
|
|
if (isset($input_data->profesores)) {
|
|
array_walk($input_data->profesores, fn($profesor_id) => $db->insert('aviso_profesor', array('aviso_id' => $aviso_id, 'profesor_id' => $profesor_id)));
|
|
}
|
|
|
|
echo json_encode(
|
|
array(
|
|
'aviso_id' => $aviso_id,
|
|
'msg' => 'Aviso creado exitosamente',
|
|
'success' => true
|
|
)
|
|
);
|
|
break;
|
|
case 'PUT':
|
|
$raw_input = file_get_contents('php://input');
|
|
if (empty($raw_input)) {
|
|
header('HTTP/1.1 400 Bad Request');
|
|
echo json_encode(array('error' => 'No se recibieron parámetros'));
|
|
exit();
|
|
}
|
|
|
|
$input_data = json_decode($raw_input);
|
|
if (json_last_error() !== JSON_ERROR_NONE) {
|
|
header('HTTP/1.1 400 Bad Request');
|
|
echo json_encode(array('error' => 'Invalid JSON format'));
|
|
exit();
|
|
}
|
|
|
|
$schema = <<<JSON
|
|
{
|
|
"\$schema": "http://json-schema.org/draft-07/schema#",
|
|
"type": "object",
|
|
"required": ["aviso_id", "aviso_fecha_final"],
|
|
"properties": {
|
|
"aviso_id": {
|
|
"type": "integer",
|
|
"minimum": 1
|
|
},
|
|
"aviso_fecha_final": {
|
|
"type": "string",
|
|
"format": "date"
|
|
}
|
|
}
|
|
}
|
|
JSON;
|
|
|
|
// VALIDATE JSON SCHEMA
|
|
$validate = new JsonSchema\Validator();
|
|
$validate->validate($input_data, json_decode($schema));
|
|
|
|
if (!$validate->isValid()) {
|
|
header('HTTP/1.1 400 Bad Request');
|
|
echo json_encode(
|
|
array(
|
|
'error' => 'El formato de la solicitud es incorrecto',
|
|
'errors' => $validate->getErrors(),
|
|
'success' => false,
|
|
)
|
|
);
|
|
exit();
|
|
}
|
|
|
|
$db->where('aviso_id', $input_data->aviso_id)
|
|
->update(
|
|
'aviso',
|
|
array(
|
|
'aviso_fecha_final' => $input_data->aviso_fecha_final,
|
|
),
|
|
);
|
|
|
|
if (isset($input_data->carreras)) {
|
|
$db->where('aviso_id', $input_data->aviso_id)->delete('aviso_carrera');
|
|
array_walk($input_data->carreras, fn($carrera_id) => $db->insert('aviso_carrera', array('aviso_id' => $input_data->aviso_id, 'carrera_id' => $carrera_id)));
|
|
}
|
|
|
|
if (isset($input_data->profesores)) {
|
|
$db->where('aviso_id', $input_data->aviso_id)->delete('aviso_profesor');
|
|
array_walk($input_data->profesores, fn($profesor_id) => $db->insert('aviso_profesor', array('aviso_id' => $input_data->aviso_id, 'profesor_id' => $profesor_id)));
|
|
}
|
|
|
|
echo json_encode(
|
|
array(
|
|
'msg' => 'Aviso actualizado exitosamente',
|
|
'success' => true
|
|
)
|
|
);
|
|
break;
|
|
|
|
case 'DELETE':
|
|
$raw_input = file_get_contents('php://input');
|
|
if (empty($raw_input)) {
|
|
header('HTTP/1.1 400 Bad Request');
|
|
echo json_encode(array('error' => 'No se recibieron parámetros'));
|
|
exit();
|
|
}
|
|
|
|
$input_data = json_decode($raw_input);
|
|
if (json_last_error() !== JSON_ERROR_NONE) {
|
|
header('HTTP/1.1 400 Bad Request');
|
|
echo json_encode(array('error' => 'Invalid JSON format'));
|
|
exit();
|
|
}
|
|
|
|
$schema = <<<JSON
|
|
{
|
|
"\$schema": "http://json-schema.org/draft-07/schema#",
|
|
"type": "object",
|
|
"required": ["aviso_id"],
|
|
"properties": {
|
|
"aviso_id": {
|
|
"type": "integer",
|
|
"minimum": 1
|
|
}
|
|
}
|
|
}
|
|
JSON;
|
|
|
|
// VALIDATE JSON SCHEMA
|
|
$validate = new JsonSchema\Validator();
|
|
$validate->validate($input_data, json_decode($schema));
|
|
|
|
if (!$validate->isValid()) {
|
|
header('HTTP/1.1 400 Bad Request');
|
|
echo json_encode(
|
|
array(
|
|
'error' => 'El formato de la solicitud es incorrecto',
|
|
'errors' => $validate->getErrors(),
|
|
'success' => false,
|
|
)
|
|
);
|
|
exit();
|
|
}
|
|
|
|
$result = $db->where('aviso_id', $input_data->aviso_id)->update('aviso', array('aviso_estado' => false));
|
|
echo json_encode(
|
|
array(
|
|
'msg' => 'Aviso eliminado exitosamente',
|
|
'success' => true,
|
|
'result' => $result
|
|
)
|
|
);
|
|
|
|
break;
|
|
}
|
|
} catch (PDOException $e) {
|
|
echo json_encode(
|
|
array(
|
|
'error' => $e->getMessage(),
|
|
'query' => $db->getLastQuery(),
|
|
'exception' => $e->getTraceAsString()
|
|
)
|
|
);
|
|
} |