func_excel.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. require_once "../vendor/autoload.php";
  3. require_once "../include/bd_pdo.php";
  4. $columnas = array(
  5. "SALÓN",
  6. "GRUPO",
  7. "CLAVE",
  8. "DOCENTE",
  9. "MATERIA",
  10. "LUNES",
  11. "MARTES",
  12. "MIÉRCOLES",
  13. "JUEVES",
  14. "VIERNES",
  15. "SÁBADO",
  16. "DURACIÓN",
  17. );
  18. $cl = ['salon', 'grupo', 'clave', 'maestro', 'materia',];
  19. define('HORARIO', 5);
  20. define('COLUMNA_MAXIMA', chr(count($columnas) + ord('A') - 1));
  21. function columna_nombre(string $columna): array
  22. {
  23. $nombre = "";
  24. $datos_nombre = explode(" ", str_replace(array("\n", chr(10), chr(13)), " ", trim(preg_replace('/_x([0-9a-fA-F]{4})_/', ' ', $columna))));
  25. $temp_nombre = explode(".", $datos_nombre[0]);
  26. if (count($temp_nombre) > 1) {
  27. $nombre .= $temp_nombre[1] . " ";
  28. $grado = $temp_nombre[0] . ".";
  29. array_shift($datos_nombre);
  30. } else
  31. $grado = strpos($datos_nombre[0], ".") !== false ? array_shift($datos_nombre) : null;
  32. $email = strpos($columna, "@") !== false ? array_pop($datos_nombre) : null;
  33. $nombre .= implode(" ", $datos_nombre);
  34. return array(
  35. "grado" => $grado,
  36. "nombre" => trim($nombre),
  37. "correo" => $email,
  38. );
  39. }
  40. ###
  41. function validar_columnas(object $sheet): void
  42. {
  43. global $columnas;
  44. $diff = array_diff(array_map(fn ($col) => trim($col), get_columns($sheet)), $columnas);
  45. #array clean
  46. if (!empty($diff)) {
  47. $diff = array_filter($diff, fn ($col) => !empty($col));
  48. throw new Exception("Error en el formato del archivo: " . (empty($diff) ? "Columnas vacías" : "Columnas incorrectas: [" . implode(", ", $diff) . "]"));
  49. }
  50. }
  51. ###
  52. function get_columns(object $sheet)
  53. {
  54. global $columnas;
  55. $columns = array();
  56. foreach ($sheet->getRowIterator(1, 1) as $row)
  57. foreach ($row->getCellIterator() as $index => $cell) {
  58. $columns[$index] = mb_strtoupper($cell->getValue() ?? "");
  59. if ($index == COLUMNA_MAXIMA) break;
  60. }
  61. return $columns;
  62. }
  63. ###
  64. function foreach_sheet(object $workbook, callable $callback = null): void
  65. {
  66. $sheets = $workbook->getSheetNames();
  67. // validate columns
  68. foreach ($sheets as $sheet) {
  69. $worksheet = $workbook->getSheetByName($sheet);
  70. validar_columnas($worksheet);
  71. foreach_register($worksheet, $callback, $sheet);
  72. }
  73. }
  74. ###
  75. function foreach_register(object $worksheet, callable $callback = null, string $sheet): void
  76. {
  77. foreach ($worksheet->getRowIterator(2) as $key => $row) {
  78. $row_data = array();
  79. foreach ($row->getCellIterator() as $index => $cell) {
  80. $row_data[] = str_replace(' ', '', $cell->getValue() ?? "");
  81. if ($index == COLUMNA_MAXIMA) break;
  82. }
  83. if ($callback !== null) {
  84. $callback($row_data, $key, $sheet);
  85. }
  86. }
  87. }
  88. function horario(array &$row, int $fila, string $sheet): string
  89. {
  90. global $cl, $db;
  91. date_default_timezone_set('UTC');
  92. $horario_tostring = "";
  93. for ($i = HORARIO; $i < count($row) - 1; $i++) {
  94. // echo $row[$i] . " $i\n";
  95. if (!empty(trim($row[$i] ?? ""))) {
  96. $row[$i] = str_replace(array(" -", "- ", " - "), "-", $row[$i]);
  97. $separar_por_espacios = EXPLODE(" ", trim(preg_replace('!\s+!', ' ', $row[$i])));
  98. foreach ($separar_por_espacios as $horario) {
  99. $hora = // if string includes : then is string else is excel date
  100. (strpos($horario, ":") === false)
  101. ? \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($horario)->format('H:i')
  102. : preg_replace('/[^0-9:]/', '', str_replace('.', ':', trim((strpos($horario, "-") !== false) ? explode("-", $horario)[0] : $horario)));
  103. if (
  104. $hora > "22:00" || $hora < "07:00" || explode(":", $hora)[1] % 15 !== 0
  105. ) {
  106. throw new Exception("Error en el formato del archivo: Hora incorrecta [$hora] en la fila $fila, hoja $sheet.");
  107. }
  108. $duración = end($row);
  109. if ($duración <= 180 and $duración >= 30 and $duración % 15 == 0)
  110. $bloques = $duración / 15;
  111. else if ($duración <= 3 and $duración >= 0.5)
  112. $bloques = $duración * 60 / 15;
  113. else
  114. throw new Exception("Error en el formato del archivo: Duración [$duración] incorrecta en la fila $fila, hoja $sheet.");
  115. $duraciónID = $db->where("duracion_bloques", $bloques)->get("duracion", 1, "duracion_id")[0]["duracion_id"];
  116. $horario_tostring .= ($i - 4) . ",$hora,$duraciónID;";
  117. }
  118. }
  119. }
  120. $row = array_combine($cl, array_intersect_key($row, array_keys($cl)));
  121. $horario_tostring = substr($horario_tostring, 0, -1);
  122. if (empty($horario_tostring))
  123. throw new Exception("Error en el formato del archivo: No se encontró horario en la fila $fila, hoja $sheet.");
  124. return $horario_tostring;
  125. }
  126. function validar_registro(array $row, int $fila): void
  127. {
  128. $tiene_horario = false;
  129. for ($i = 0; $i < HORARIO - 1; $i++)
  130. if (empty(trim($row[$i])))
  131. throw new Exception("Error faltan datos en la fila $fila de la hoja");
  132. for ($i = HORARIO; $i < COLUMNA_MAXIMA; $i++)
  133. if ($tiene_horario = !empty($row[$i]))
  134. break;
  135. if (!$tiene_horario)
  136. throw new Exception("Error en el formato del archivo: No se encontró horario en la fila $fila.");
  137. }
  138. function renglón_vacío(array $row)
  139. {
  140. foreach ($row as $columna)
  141. if (!empty($columna))
  142. return false;
  143. return true;
  144. }