functions.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /*
  3. * Copyright (C) 2021 Echedey López Romero <elr@disroot.org>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. // Declare functions
  19. // Create option for a select based on a name, a value and selected value
  20. // with optional additional text
  21. function CreateOption($Selected, $Value, $Name, $AddText) {
  22. $HTML = '';
  23. if ($Name == $Selected) {
  24. if ($Value == '') {
  25. $HTML .= '<option class="font-weight-bold text-center" '
  26. . "value=\"$Value\" selected=\"\">$AddText" .
  27. "$Name</option>" . PHP_EOL;
  28. } else {
  29. $HTML .= '<option class="text-center" '
  30. . "value=\"$Value\" selected=\"\">$AddText"
  31. . "$Name</option>" . PHP_EOL;
  32. }
  33. } else {
  34. if ($Value == '') {
  35. $HTML .= '<option class="font-weight-bold text-center" '
  36. . "value=\"$Value\">$AddText" . "$Name</option>" . PHP_EOL;
  37. } else {
  38. $HTML .= '<option class="text-center" '
  39. . "value=\"$Value\">$AddText" . "$Name</option>" . PHP_EOL;
  40. }
  41. }
  42. return $HTML;
  43. }
  44. // Create a select based on a dataset, a name and a selected value
  45. function CreateSelect($DataSet, $Name, $Selected) {
  46. $HTML = '<select class="custom-select custom-select-lg text-center" '
  47. . "name=\"$Name\" id=\"$Name\">" . PHP_EOL;
  48. $AddText = 'Select a ';
  49. $HTML .= CreateOption($Selected, '', $Name, $AddText);
  50. foreach ($DataSet as $Key => $Value) {
  51. if (is_string($Key)) {
  52. $HTML .= CreateOption($Selected, $Key, $Key, '');
  53. } else {
  54. $HTML .= CreateOption($Selected, $Value, $Value, '');
  55. }
  56. }
  57. $HTML .= '</select>' . PHP_EOL;
  58. return $HTML;
  59. }
  60. // Return a reply based on a sign, a symbol, a message and the horoscope
  61. function ResultOut($Sign, $Symbol, $Message, &$Horoscope) {
  62. $RangeMonths = array_keys($Horoscope[$Sign]['range']);
  63. $StartDate = $RangeMonths[0] . ' '
  64. . $Horoscope[$Sign]['range'][$RangeMonths[0]];
  65. $EndDate = $RangeMonths[1] . ' '
  66. . $Horoscope[$Sign]['range'][$RangeMonths[1]];
  67. $HTML = '<div class="row my-1 border border-secondary rounded p-2">'
  68. . PHP_EOL;
  69. $HTML .= '<div class="container">' . PHP_EOL;
  70. $HTML .= '<div class="row mb-2">' . PHP_EOL;
  71. $HTML .= '<h2 class="m-auto text-center">' . $Sign . "</h2>" . PHP_EOL;
  72. $HTML .= '</div>' . PHP_EOL;
  73. $HTML .= '<div class="row mb-2">' . PHP_EOL;
  74. $HTML .= '<span class="d-block m-auto text-center t-icon">'
  75. . $Symbol . "</span>" . PHP_EOL;
  76. $HTML .= '</div>' . PHP_EOL;
  77. $HTML .= '<div class="row mb-2">' . PHP_EOL;
  78. $HTML .= '<span class="d-block m-auto text-center font-weight-bold">'
  79. . $StartDate . ' - ' . $EndDate . "</span>" . PHP_EOL;
  80. $HTML .= '</div>' . PHP_EOL;
  81. $HTML .= '<div class="row">' . PHP_EOL;
  82. $HTML .= '<p class="m-auto text-center">' . $Message . '</p>' . PHP_EOL;
  83. $HTML .= '</div>' . PHP_EOL;
  84. $HTML .= '</div>' . PHP_EOL;
  85. $HTML .= '</div>' . PHP_EOL;
  86. return $HTML;
  87. }
  88. // Return a reply with all the signs
  89. function ConsultAll(&$Horoscope) {
  90. $HTML = '';
  91. foreach ($Horoscope as $Sign => $Represent) {
  92. $Symbol = '';
  93. $Message = '';
  94. foreach ($Represent as $Name => $Content) {
  95. if ($Name == array_keys($Represent)[0]) {
  96. $Symbol = $Content;
  97. } else if($Name == array_keys($Represent)[1]) {
  98. $Message = $Content;
  99. }
  100. }
  101. $HTML .= ResultOut($Sign, $Symbol, $Message, $Horoscope);
  102. }
  103. return $HTML;
  104. }
  105. // Consult horoscope based on month and day
  106. function Consult(&$Months, $Month, $Day, &$Horoscope) {
  107. $HoroscopeNames = array_keys($Horoscope);
  108. $Sign = '';
  109. $Pos = 0;
  110. $Exit = false;
  111. while ($Pos < count($HoroscopeNames) && $Exit == false) {
  112. $HoroscopeName = $HoroscopeNames[$Pos];
  113. $MonthRange = $Horoscope[$HoroscopeNames[$Pos]]['range'];
  114. if (isset($MonthRange[$Month])) {
  115. if ((array_keys($MonthRange)[0] == $Month && $Day >= $MonthRange[$Month]
  116. || (array_keys($MonthRange)[1] == $Month && $Day <= $MonthRange[$Month]))) {
  117. $Sign = $HoroscopeName;
  118. $Exit = true;
  119. }
  120. }
  121. $Pos++;
  122. }
  123. $Symbol = $Horoscope[$Sign]['symbol'];
  124. $Message = $Horoscope[$Sign]['message'];
  125. return ResultOut($Sign, $Symbol, $Message, $Horoscope);
  126. }