TextExport.class.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. class TextExport
  3. {
  4. public static $mime = 'text/plain';
  5. public static $extension = 'txt';
  6. public static $description = 'Fichier de texte brut';
  7. public static function sample($dataset){
  8. $stream = '';
  9. foreach($dataset as $macro => $infos)
  10. $stream .= ($infos['type']=='list') ? '{{#'.$macro.'}}{{/'.$macro.'}} : '.$infos['desc'].PHP_EOL : '{{'.$macro.'}} : '.$infos['desc'].PHP_EOL;
  11. return $stream;
  12. }
  13. public static function from_template($source, $data, $return, $isHtml=false){
  14. $fileParts = explode('.', $source);
  15. $ext = count($fileParts)>1 ? '.'.end($fileParts) : '';
  16. if(count(explode(SLASH, $ext)) > 1) $ext = '';
  17. $destination = File::dir().'tmp'.SLASH.'template.'.time().'-'.rand(0,100).$ext;
  18. $source = File::dir().$source;
  19. copy($source,$destination);
  20. $stream = file_get_contents($destination);
  21. if(mb_detect_encoding($stream, 'UTF-8', true) == false) $stream = utf8_encode($stream);
  22. foreach($data as $tag=>$value){
  23. if(is_array($value)){
  24. $stream = preg_replace_callback(
  25. '#\{\{\#'.$tag.'\}\}(.*)\{{\/'.$tag.'\}\}#si',
  26. function ($match) use($value) {
  27. $html = '';
  28. foreach($value as $line){
  29. $bloc = $match[1];
  30. foreach($line as $k=>$v){
  31. $bloc = str_replace('{{'.$k.'}}',$v,$bloc);
  32. $bloc = self::if_condition($bloc, $line);
  33. $bloc = self::else_condition($bloc, $line);
  34. }
  35. $html .=$bloc;
  36. }
  37. return $html;
  38. },
  39. $stream
  40. );
  41. continue;
  42. }
  43. //Pas d'images possibles dans un fichier texte
  44. if(substr($value,0,2)=='::' && !$isHtml) continue;
  45. //Remplacement d'image pour les fichiers HTML/PDF
  46. if(substr($value,0,2)=='::') $value = substr($value,2);
  47. $stream = str_replace('{{'.$tag.'}}', $value, $stream);
  48. }
  49. $stream = self::if_condition($stream, $data);
  50. $stream = self::else_condition($stream, $data);
  51. file_put_contents($destination, $stream);
  52. if($return!='stream') return $destination;
  53. $stream = file_get_contents($destination);
  54. unlink($destination);
  55. return ($isHtml ? $stream : utf8_decode($stream));
  56. }
  57. public static function if_condition($stream, $data){
  58. // $ifRegex = '/\{\{\#([^\/\:\#}]*)\}\}(.*?)\{\{\/\1\}\}/is';
  59. // $stream = recursive_preg_replace($ifRegex, $stream, $data);
  60. //conditions
  61. $ifRegex = '/\{\{\#([^\/\:\#}]*)\}\}(.*?)\{\{\/\1\}\}/is';
  62. return preg_replace_callback($ifRegex,function($matches) use ($data) {
  63. $key = $matches[1];
  64. $stream = $matches[2];
  65. return !isset($data[$key]) || (!is_array($data[$key]) && empty($data[$key])) || (is_array($data[$key]) && count($data[$key])==0) ?'':$stream;
  66. },$stream);
  67. }
  68. public static function else_condition($stream, $data){
  69. // $elseRegex = '/\{\{\^([^\/\:\#}]*)\}\}(.*?)\{\{\/\1\}\}/is';
  70. // $stream = recursive_preg_replace($elseRegex, $stream, $data);
  71. //conditions
  72. $elseRegex = '/\{\{\^([^\/\:\#}]*)\}\}(.*?)\{\{\/\1\}\}/is';
  73. return preg_replace_callback($elseRegex,function($matches) use ($data) {
  74. $key = $matches[1];
  75. $stream = $matches[2];
  76. return (isset($data[$key]) && !is_array($data[$key]) && !empty($data[$key])) || (is_array($data[$key]) && count($data[$key])>0) ?'':$stream;
  77. },$stream);
  78. }
  79. }