funciones_helper.php 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. defined('BASEPATH') or exit('No direct script access allowed');
  3. /**
  4. *
  5. * Helpers Funciones_helper
  6. *
  7. * This Helpers for ...
  8. *
  9. * @package CodeIgniter
  10. * @category Helpers
  11. * @author Setiawan Jodi <jodisetiawan@fisip-untirta.ac.id>
  12. * @link https://github.com/setdjod/myci-extension/
  13. *
  14. */
  15. // ------------------------------------------------------------------------
  16. if (!function_exists('guardar_log')) {
  17. function guardar_log($nombre_archivo = "", $texto = "", $echo = false, $tipo = 1)
  18. {
  19. $textoTipoLogico = "";
  20. if ($tipo == 1) {
  21. $textoTipoLogico = "INFO";
  22. } else if ($tipo == 2) {
  23. $textoTipoLogico = "ERROR";
  24. }
  25. //CREAMOS EL REGISTRO PARA EL LOG
  26. $registro = date('d-m-Y H:i:s') . ' - ' . $textoTipoLogico . " - " . $texto;
  27. //FICHERO DE LOG
  28. $file_name = APPPATH . 'logs/' . $nombre_archivo . '.txt';
  29. //GUARDAMOS EL REGISTRO EN EL FICHERO DE LOG
  30. $f = fopen($file_name, "a");
  31. fwrite($f, $registro . PHP_EOL);
  32. fclose($f);
  33. //SI ES NECESARIO SE MUESTRA POR PANTALLA
  34. if ($echo == true) {
  35. //echo $registro . '<br>';
  36. }
  37. return $texto . '<br>';
  38. }
  39. }
  40. if (!function_exists("insertBucle")) {
  41. function insertBucle($fila_archivo, $nombreTabla, $nombreLog): int
  42. {
  43. $contador = 1;
  44. $conjunto = array();
  45. $CI = &get_instance();
  46. foreach ($fila_archivo as $row) {
  47. $conjunto[] = $row;
  48. $contador++;
  49. if ($contador % 5000 == 0) {
  50. guardar_log($nombreLog, count($conjunto));
  51. $CI->db->insert_batch($nombreTabla, $conjunto);
  52. $conjunto = array();
  53. }
  54. }
  55. if (count($conjunto) > 0) {
  56. guardar_log($nombreLog, count($conjunto));
  57. $CI->db->insert_batch($nombreTabla, $conjunto);
  58. }
  59. unset($conjunto);
  60. gc_collect_cycles();
  61. /*$query = $this->db->query($sql);
  62. $result = $query->result();
  63. $query->free_result();*/
  64. return $CI->db->insert_id();
  65. }
  66. }
  67. if (!function_exists("insertBucleDuplicateOnKey")) {
  68. function insertBucleDuplicateOnKey($fila_archivo, $nombreTabla, $nombreLog)
  69. {
  70. $contador = 1;
  71. $conjunto = array();
  72. $CI = &get_instance();
  73. foreach ($fila_archivo as $row) {
  74. if ($nombreTabla == "bruto_datos_iniciales" || $nombreTabla == "maestro_datos_iniciales_licitacion") {
  75. if (is_array($row)) {
  76. guardar_log($nombreLog, "ID_licitacion-" . $row['id_licitacion']);
  77. } else {
  78. guardar_log($nombreLog, "ID_licitacion-" . $row->id_licitacion);
  79. }
  80. }
  81. $conjunto[] = $row;
  82. $contador++;
  83. if ($contador % 5000 == 0) {
  84. guardar_log($nombreLog, "Insertados - " . count($conjunto));
  85. //$CI->db->insert_batch($nombreTabla, $conjunto);
  86. $CI->db->insert_on_duplicate_update_batch($nombreTabla, $conjunto, $nombreLog);
  87. $conjunto = array();
  88. }
  89. }
  90. if (count($conjunto) > 0) {
  91. guardar_log($nombreLog, "Insertados - " . count($conjunto));
  92. //$CI->db->insert_batch($nombreTabla, $conjunto);
  93. $CI->db->insert_on_duplicate_update_batch($nombreTabla, $conjunto, $nombreLog);
  94. }
  95. unset($conjunto);
  96. gc_collect_cycles();
  97. return $CI->db->insert_id();
  98. }
  99. }
  100. if (!function_exists(("check_permisos_template"))) {
  101. function check_permisos_template($view_file_name, $data_array = array(), $vista_permiso = "home")
  102. {
  103. $CI = &get_instance();
  104. if ($CI->session->zonas != "") {
  105. $CI->load->model("Home_model");
  106. $vistas = $CI->Home_model->getPermisosVistaUsuario($CI->session->tipo_usuario);
  107. if ($CI->session->tipo_usuario == 1) {
  108. $CI->template_dashboard->load_template($view_file_name, $data_array);
  109. } else {
  110. if (in_array($vista_permiso, $vistas)) {
  111. $CI->template_dashboard->load_template($view_file_name, $data_array);
  112. } else {
  113. redirect("home");
  114. }
  115. }
  116. } else {
  117. redirect("login", "refresh");
  118. }
  119. }
  120. }
  121. if (!function_exists("pretty_dump")) {
  122. function pretty_dump($data)
  123. {
  124. echo '<pre>' . var_export($data, true) . '</pre>';
  125. }
  126. }
  127. if (!function_exists("enviarEmail")) {
  128. function enviarEmail($asunto, $body, $email_envio, $adjuntos = array(), $print_result = true)
  129. {
  130. print_r($body);
  131. $CI = &get_instance();
  132. $CI->load->library('email');
  133. $config['protocol'] = 'smtp';
  134. $config['smtp_host'] = 'smtp.gmail.com';
  135. $config['smtp_user'] = 'chcalvoleon@mindden.com';
  136. $config['smtp_pass'] = '';
  137. $config['smtp_crypto'] = 'ssl';
  138. $config['smtp_port'] = 465;
  139. $config['mailtype'] = 'html';
  140. $config['wordwrap'] = 'TRUE';
  141. $config['newline'] = "\r\n";
  142. $config['priority'] = 5;
  143. $config['crlf'] = "\r\n";
  144. $CI->email->initialize($config);
  145. $CI->email->clear(true);
  146. $CI->email->from('chcalvoleon@mindden.com');
  147. $CI->email->to($email_envio);
  148. $CI->email->bcc("chcalvoleon@mindden.com");
  149. $CI->email->subject($asunto);
  150. $CI->email->message($body);
  151. foreach ($adjuntos as $row) {
  152. $CI->email->attach($row);
  153. }
  154. if ($print_result) {
  155. echo $CI->email->send(false);
  156. echo $CI->email->print_debugger();
  157. } else {
  158. $CI->email->send(false);
  159. }
  160. }
  161. }
  162. // ------------------------------------------------------------------------
  163. /* End of file Funciones_helper.php */
  164. /* Location: ./application/helpers/Funciones_helper.php */