Constancia.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. include_once '../../include/xTemplate.class.php';
  3. require_once '../../include/mpdf/autoload.php';
  4. require_once '../../include/util.php';
  5. class Constancia {
  6. private $config;
  7. private $stylesheet;
  8. private $header;
  9. private $footer;
  10. private $tipos;
  11. private $carpeta;
  12. private $usr;
  13. private $pdo;
  14. private $fecha;
  15. private $tiposConstancias = array('CONSTANCIA' => 1, 'RECONOCIMIENTO' => 2);
  16. function __construct($pdo, $fecha, $concurso, $idusuario){
  17. $defaultConfig = (new Mpdf\Config\ConfigVariables())->getDefaults();
  18. /********* FONTS **********/
  19. $fontDirs = $defaultConfig['fontDir'];
  20. $defaultFontConfig = (new Mpdf\Config\FontVariables())->getDefaults();
  21. $fontData = $defaultFontConfig['fontdata'] +
  22. [ 'tipo' => [
  23. 'R' => '../../fonts/indivisaFont/ttf/IndivisaDisplaySans-Thin.ttf',
  24. ]] +
  25. [ 'texto-a' => [
  26. 'R' => '../../fonts/indivisaFont/ttf/IndivisaTextSans-Light.ttf',
  27. ]] +
  28. ['indivisa-text' => [
  29. 'R' => '../../fonts/indivisaFont/ttf/IndivisaTextSans-Regular.ttf',
  30. 'B' => '../../fonts/indivisaFont/ttf/IndivisaTextSans-Bold.ttf',
  31. 'BI' => '../../fonts/indivisaFont/ttf/IndivisaTextSans-BoldItalic.ttf',
  32. ]];
  33. /********* CONFIGURACIÓN INICIAL **********/
  34. $this->config = [
  35. 'mode'=>'utf-8', 'format'=>'Letter',
  36. 'margin_header'=> 21, 'margin_top'=>52, 'margin_left'=> 35, 'margin_right'=> 30, 'margin_footer'=>10,
  37. 'fontDir' => array_merge($fontDirs, [__DIR__]), 'fontdata' => $fontData, 'default_font' => 'indivisa-text'
  38. ];
  39. /********* HOJA DE ESTILOS **********/
  40. $this->stylesheet = '<style>' . file_get_contents('../css/pdf/constancia.css') . '</style>';
  41. /********* HEADER Y FOOTER **********/
  42. $this->header = '<img id="logo" src="../img/pdf/lasalle_pdf.jpg">';
  43. /********* MARCA DE AGUA **********/
  44. $this->carpeta = 'const/export/';
  45. if (!file_exists($_SERVER['DOCUMENT_ROOT']. $this->carpeta . '/marca.png')) {
  46. //$marca = 'Facultad de Ingeniería - ';
  47. $marca = 'FI - ' . $concurso['nom'] . ' CIDIT ' . $concurso['anio'] . ' | '; //No acepta caracteres especiales
  48. $this->generaMarcaDeAgua($marca, $concurso['carpeta']);
  49. }
  50. $this->tipos = array('participante' => 'Constancia', 'ganador' =>'Reconocimiento', 'asesor' => 'Reconocimiento', 'evaluador' => 'Reconocimiento', 'organizador' => 'Constancia');
  51. $this->pdo = $pdo;
  52. $this->fecha = $fecha;
  53. $this->usr = $idusuario;
  54. }
  55. function getUser(){
  56. return $this->usr;
  57. }
  58. function fechaConstancia($fechaTxt){
  59. $fechaTxt = trim($fechaTxt);
  60. if(substr($fechaTxt,2,1) == "/" && substr($fechaTxt,5,1) == "/"){// dd/mm/aaaa
  61. $fechaArr = explode("/", $fechaTxt);
  62. return mesNombre($fechaArr[1])." ".intval($fechaArr[0])." de ".$fechaArr[2];
  63. }
  64. if(substr($fechaTxt,4,1) == "-" && substr($fechaTxt,7,1) == "-"){// aaaa-mm-dd
  65. $fechaArr = explode("-", $fechaTxt);
  66. return mesNombre($fechaArr[1])." ".intval($fechaArr[2])." de ".$fechaArr[0];
  67. }
  68. return "";
  69. }
  70. function generaFooter($concurso, $participante, $motivo, $tipo, $numFirmas){
  71. /********* ASIGNA FIRMAS **********/
  72. $xtpl = new XTemplate('../adds/tpl/constancia/footer.tpl.html');
  73. $fecha = $this->fechaConstancia($this->fecha);
  74. $xtpl->assign('FECHA_CARTA', $fecha);
  75. if($numFirmas >= 2)
  76. $numFirmas = 2; // 1 -> sólo una firma / 2 -> 2 o más firmas
  77. $firma = md5('12478');
  78. $xtpl->assign('NUM', $numFirmas);
  79. $xtpl->assign('MARCA', $this->carpeta . '/marca.png');
  80. $xtpl->assign('FIRMA', $firma . '.png');
  81. $xtpl->assign('NOMBRE', 'Mtro. Carlos Alfonso Herrera Anda');
  82. $xtpl->assign('PUESTO', 'Secretario Académico');
  83. $xtpl->parse('main.firma');
  84. $firma = md5('3557');
  85. $xtpl->assign('NUM', $numFirmas);
  86. $xtpl->assign('MARCA', $this->carpeta . '/marca.png');
  87. $xtpl->assign('FIRMA', $firma . '.png');
  88. $xtpl->assign('NOMBRE', 'Ing. Edmundo G. Barrera Monsiváis');
  89. $xtpl->assign('PUESTO', 'Director de la Facultad de Ingeniería');
  90. $xtpl->parse('main.firma');
  91. $xtpl->assign('NUM', $numFirmas);
  92. $folio = $this->creaFolio($concurso, $participante, $motivo, $tipo);
  93. if (!empty($folio)){
  94. $xtpl->assign('FOLIO', $folio);
  95. $xtpl->parse('main');
  96. $this->footer = $xtpl->text('main');
  97. return true;
  98. } else
  99. return false;
  100. }
  101. function generaParticipante($nombre, $proyecto, $clave, $concurso, $anio, $numFirmas){
  102. if ($this->generaFooter($concurso, $nombre, 'Participante', strtoupper($this->tipos['participante']), $numFirmas)) {
  103. $this->xtpl = new XTemplate('../adds/tpl/constancia/constancia.tpl.html');
  104. $this->xtpl->assign('TIPO', $this->tipos['participante']);
  105. $this->xtpl->assign('NOMBRE', $nombre);
  106. $this->xtpl->assign('PROYECTO', $proyecto);
  107. $this->xtpl->assign('CONCURSO', $concurso);
  108. $this->xtpl->assign('ANIO', $anio);
  109. $this->xtpl->parse('main.participante');
  110. $this->xtpl->parse('main');
  111. return $this->generaPDF($clave . ' - ' . $nombre,'temps/participantes',$this->tipos['participante']);
  112. } else
  113. return false;
  114. }
  115. function generaAsesor($nombre, $concurso, $anio){
  116. if ($this->generaFooter($concurso, $nombre, 'Asesor', strtoupper($this->tipos['asesor']))) {
  117. $this->xtpl = new XTemplate('../adds/tpl/constancia/constancia.tpl.html');
  118. $this->xtpl->assign('TIPO', $this->tipos['asesor']);
  119. $this->xtpl->assign('NOMBRE', $nombre);
  120. $this->xtpl->assign('CONCURSO', $concurso);
  121. $this->xtpl->assign('ANIO', $anio);
  122. $this->xtpl->parse('main.asesor');
  123. $this->xtpl->parse('main');
  124. return $this->generaPDF($nombre,'temps/asesores',$this->tipos['asesor']);
  125. } else
  126. return false;
  127. }
  128. function generaGanador($nombre, $lugar, $proyecto, $clave, $categoria, $concurso, $anio, $periodo){
  129. if ($this->generaFooter($concurso, $nombre, 'Ganador', strtoupper($this->tipos['ganador']))) {
  130. $this->xtpl = new XTemplate('../adds/tpl/constancia/constancia.tpl.html');
  131. $this->xtpl->assign('TIPO', $this->tipos['ganador']);
  132. $this->xtpl->assign('NOMBRE', $nombre);
  133. $this->xtpl->assign('LUGAR', $lugar);
  134. $this->xtpl->assign('PROYECTO', $proyecto);
  135. $this->xtpl->assign('CATEGORIA', $categoria);
  136. $this->xtpl->assign('CONCURSO', $concurso);
  137. $this->xtpl->assign('ANIO', $anio);
  138. $this->xtpl->assign('PERIODO', $periodo);
  139. $this->xtpl->parse('main.ganador');
  140. $this->xtpl->parse('main');
  141. return $this->generaPDF($clave . ' - ' . $nombre,'temps/ganadores',$this->tipos['ganador']);
  142. } else
  143. return false;
  144. }
  145. function generaEvaluador($nombre, $concurso, $anio){
  146. if ($this->generaFooter($concurso, $nombre, 'Comité Evaluador', strtoupper($this->tipos['evaluador']))) {
  147. $this->xtpl = new XTemplate('../adds/tpl/constancia/constancia.tpl.html');
  148. $this->xtpl->assign('TIPO', $this->tipos['evaluador']);
  149. $this->xtpl->assign('NOMBRE', $nombre);
  150. $this->xtpl->assign('CONCURSO', $concurso);
  151. $this->xtpl->assign('ANIO', $anio);
  152. $this->xtpl->parse('main.evaluador');
  153. $this->xtpl->parse('main');
  154. return $this->generaPDF($nombre,'temps/evaluadores',$this->tipos['evaluador']);
  155. } else
  156. return false;
  157. }
  158. function generaOrganizador($nombre, $puesto, $concurso, $anio){
  159. if ($this->generaFooter($concurso, $nombre, 'Comité Organizador', strtoupper($this->tipos['organizador']))) {
  160. $this->xtpl = new XTemplate('../adds/tpl/constancia/constancia.tpl.html');
  161. $this->xtpl->assign('TIPO', $this->tipos['organizador']);
  162. $this->xtpl->assign('NOMBRE', $nombre);
  163. $this->xtpl->assign('PUESTO', $puesto);
  164. $this->xtpl->assign('CONCURSO', $concurso);
  165. $this->xtpl->assign('ANIO', $anio);
  166. $this->xtpl->parse('main.organizador');
  167. $this->xtpl->parse('main');
  168. return $this->generaPDF($nombre,'temps/organizadores',$this->tipos['organizador']);
  169. } else
  170. return false;
  171. }
  172. private function generaPDF($nombre, $carpeta, $tipo){
  173. $error = false;
  174. try {
  175. $mpdf = new Mpdf\Mpdf($this->config);
  176. $mpdf->SetProtection(array('print','print-highres'),'',md5(time()));
  177. $mpdf->SHYlang = 'es';
  178. $mpdf->SetHTMLHeader($this->header);
  179. $mpdf -> SetTitle($tipo . '-' . $nombre);
  180. $mpdf -> SetAuthor('Facultad de Ingeniería. © Universidad La Salle A.C. 2021 Todos los derechos Reservados.');
  181. $mpdf -> WriteHTML($this->stylesheet);
  182. $mpdf -> WriteHTML($this->xtpl->text('main'));
  183. $mpdf->SetHTMLFooter($this->footer);
  184. $mpdf -> Output('../' . $carpeta . '/'. str_replace(' ', '', $nombre) . '.pdf','F');
  185. } catch (\Mpdf\MpdfException $e) {
  186. $error = true;
  187. }
  188. return $error;
  189. }
  190. private function generaMarcaDeAgua($marca, $carpeta) {
  191. $imagen = imagecreatetruecolor(344,143); // Crear la imagen de 591x379
  192. $textColor = imagecolorallocate($imagen, 224, 224, 224); //Colores del texto
  193. $transparente = imagecolorallocate ($imagen, 0, 0, 0); //Color que cambiará a transparene
  194. imagecolortransparent($imagen, $transparente); // Hacer el fondo transparente
  195. for ($cont = 0; $cont < 5; $cont++) { $marca .= $marca; }
  196. // Escribirlo
  197. for ($cont=1; $cont <= 50; $cont++){
  198. if ($cont%2 == 0)
  199. $val = -5;
  200. else
  201. $val = 5;
  202. imagestring($imagen,1, $val, 10*$cont, $marca, $textColor); //Escribe el texto
  203. }
  204. header('Content-Type: image/png');
  205. imagepng($imagen,'../concursos/' . $carpeta . '/constancias/marca.png'); //Genera la imagen
  206. imagedestroy($imagen);
  207. }
  208. private function creaFolio($concurso, $participante, $motivo, $tipo) {
  209. $evento = preg_replace('/[^0-9]+/', '', $concurso);
  210. $folio = $this->getFolio($evento, $participante, $motivo, $this->tiposConstancias[strtoupper($tipo)]);
  211. if (!empty($folio))
  212. $folio = 'FI - C' . $evento . ' - ' . sprintf("%04d", $folio);
  213. return $folio;
  214. }
  215. private function getFolio($evento,$participante,$motivo,$tipo){
  216. $folio = '';
  217. try {
  218. $stmt = $this->pdo->prepare('Select * from fi_constanciaLog(3,:evento,:participante,:motivo,:tipo,null,null,:usr)');
  219. $stmt->bindParam(':evento', $evento);
  220. $stmt->bindParam(':participante', $participante);
  221. $stmt->bindParam(':motivo', $motivo);
  222. $stmt->bindParam(':tipo', $tipo);
  223. $stmt->bindParam(':usr', $this->usr);
  224. $stmt->execute();
  225. $folio = $stmt->fetch();
  226. return $folio['ConstanciaLog_id'];
  227. } catch (\PDOException $ex){
  228. $folio = '';
  229. }
  230. $stmt->closeCursor();
  231. $stmt = null;
  232. return $folio;
  233. }
  234. }