-
Notifications
You must be signed in to change notification settings - Fork 0
/
Poema.php
149 lines (121 loc) · 4.87 KB
/
Poema.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php class Poema{
function esAleatorio($poema){
if($poema == "RANDOM"){
return true;
}
return false;
}
function getPoemas(){
$data = file_get_contents("JSON/Poemas.json");
return json_decode($data, true);
}
function foundPoema($Poema,$objetivo){
if($Poema["Info"]["Titulo"] == $objetivo){
return true;
}else{
return false;
}
}
function getSup($index,$len){
if($index == $len -1){
return 0;
}
return $index + 1;
}
function existsPoema($Poemas,$objetivo){
foreach($Poemas as $Poema){
if($this->foundPoema($Poema,$objetivo)){
return true;
}
}
return false;
}
function getInf($index,$len){
if($index == 0){
return $len-1;
}
return $index - 1;
}
function getTitulo($index,$Poemas){
return $Poemas[$index]["Info"]["Titulo"];
}
function contarPoema($poema,$i){
include "conex.php";
date_default_timezone_set("America/Mexico_City");
$ip = $_SERVER['REMOTE_ADDR'];
$sqlConsultar = $con->query("SELECT * FROM Contador WHERE Id = '$i' AND Poema = '$poema' ORDER BY id desc");
$contarConsultar = $sqlConsultar->num_rows;
if($contarConsultar == 0){
$sqlInsertar = $con->query("INSERT INTO `Contador`(`Ip`, `Fecha`, `Poema`) VALUES ('$ip',now(),'$poema');");
}else{
$row = $sqlConsultar->fetch_array();
$fechaRegistro = $row['Fecha'];
$fechaActual = date("Y-m-d H:i:s");
$nuevaFecha = strtoTime($fechaRegistro.+"3 hour");
$nuevaFecha = date("Y-m-d H:i:s",$nuevaFecha);
if($fechaActual >= $nuevaFecha){
$sqlInsertar = $con->query("INSERT INTO `Contador`(`Ip`, `Fecha`, `Poema`) VALUES ('$ip',now(),'$poema');");
}
}
$sqlActualizar = $con->query("UPDATE Poemas SET Visitas = Visitas + 1 WHERE Id = $i");
}
function show(){
echo '<div class="container-fluid Main-Container text-center">';
if (isset($_GET['spr'])) {
$this->mostrarPoema("RANDOM");
}else if (isset($_GET['poema'])) {
$this->mostrarPoema($_GET['poema']);
}
echo '</div>';
}
function canShow(){
if(isset($_GET['poema'])){
return true;
}else{
return false;
}
}
function mostrarPoema($poema) {
$Poemas = $this->getPoemas();
$i = 0;
if($this->esAleatorio($poema)){
$j = rand(0,count($Poemas)-1);
$poema = $this->getTitulo($j,$Poemas);
}
foreach ($Poemas as $Poema) {
if($this->foundPoema($Poema,$poema)){
echo '<div class="Titulo">'.$poema.'</div>';
foreach($Poema["Poema"] as $Estrofa){
echo '<div class="Estrofa">';
foreach($Estrofa as $Verso){
echo '<div class="Verso">'.$Verso.'</div>';
}
echo '</div>';
}
echo '<div>';
echo '<div class="Button-Nav align-content-center">';
$indexSup = $this->getSup($i, count($Poemas));
$indexInf = $this->getInf($i, count($Poemas));
echo '<a href='."'Poema.php?poema=".$this->getTitulo($indexInf,$Poemas)."'".'type="button" class="Nav-Button"><i class="fas fa-chevron-circle-left fa-2x"></i></a>';
echo '<a href="Poemas.php" type="button" class="Nav-Button"><i class="fas fa-home fa-2x"></i></a>';
echo '<a href='."'Poema.php?poema=".$this->getTitulo($indexSup,$Poemas)."'".'type="button" class="Nav-Button"><i class="fas fa-chevron-circle-right fa-2x"></i></a>';
echo '</div>';
echo '</div>';
break;
}
$i = $i + 1;
}
$this->contarPoema($poema,$i);
}
}
?>
<?php
$P = new Poema();
if($P->canShow()){
if($P->existsPoema($P->getPoemas(),$_GET['poema'])){
include 'header.php';
$P->show();
include 'footer.php';
}
}
?>