Template.class.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. class Template{
  3. public static function all($template){
  4. $templates = array();
  5. foreach (glob(self::dir($template).'*') as $path) {
  6. $relativePath = str_replace(self::dir($template),'',$path);
  7. if(is_file($path)){
  8. $infos = self::getManifest($path);
  9. $templates[$infos['label']] = array('file'=>$path,'syntax'=>$infos['syntax'],'infos' => $infos);
  10. }else{
  11. $templates = array_merge($templates,self::all($template.SLASH.$relativePath));
  12. }
  13. }
  14. uasort($templates,function($a,$b){
  15. if(!isset($a['infos']['sort']) && !isset($b['infos']['sort'])) return $a['infos']['label'] > $b['infos']['label'] ? 1 : -1 ;
  16. if( !isset($a['infos']['sort']) && isset($b['infos']['sort']) ) return 1;
  17. if( isset($a['infos']['sort']) && !isset($b['infos']['sort']) ) return -1;
  18. return $a['infos']['sort'] > $b['infos']['sort'] ? 1 : -1 ;
  19. });
  20. return $templates;
  21. }
  22. public static function dir($tpl = null){
  23. return __DIR__.SLASH.'template'.SLASH.(isset($tpl)? $tpl.SLASH:'');
  24. }
  25. public static function getManifest($file){
  26. $f = fopen($file, 'r');
  27. $line = fgets($f);
  28. fclose($f);
  29. return json_decode($line,true);
  30. }
  31. public static function filters($templatePath){
  32. $filters = array();
  33. foreach (Template::all($templatePath) as $template) {
  34. if(empty($template['infos']['options'])) continue;
  35. foreach ($template['infos']['options'] as $key => $value){
  36. if(!is_array($value))$value = array('label'=>$value);
  37. $value['slug'] = $key;
  38. $filters[$key] = $value;
  39. }
  40. }
  41. ksort($filters);
  42. return $filters;
  43. }
  44. public static function types(){
  45. $types = FieldType::available();
  46. $oldTypes = array();
  47. $instance= new User();
  48. //old pure sql types
  49. global $databases_credentials;
  50. $sgbd = $databases_credentials[$instance->baseUid]['connector'];
  51. foreach(array('dropzone') as $forbidden)
  52. if(isset($types[$forbidden])) unset($types[$forbidden]);
  53. foreach ($sgbd::types() as $key => $value) {
  54. $oldType = new FieldType();
  55. $oldType->slug = $key;
  56. $oldType->label = $key;
  57. $oldType->sqlType = $key;
  58. $oldType->icon = 'fas fa-font';
  59. $oldType->description = 'Type sql '.$key;
  60. $oldType->default = '';
  61. $oldType->onInput = $types['text']->onInput;
  62. $oldType->default_attributes = array(
  63. 'class'=>'"form-control"',
  64. 'type'=>'"text"'
  65. );
  66. $oldTypes[$key] = $oldType;
  67. }
  68. $entityField = new FieldType();
  69. $entityField->label = 'Entité';
  70. $entityField->sqlType = 'entity-external-entity';
  71. $entityField->icon = 'fas fa-font';
  72. $entityField->description = 'Entité ';
  73. $entityField->default = '';
  74. $entityField->onInput = $types['text']->onInput;
  75. $entityField->default_attributes = array(
  76. 'class'=>'"form-control"',
  77. 'type'=>'"text"'
  78. );
  79. return array(
  80. 'Types programmes'=>$types,
  81. 'Entités'=>array('entity-external-entity'=>$entityField),
  82. 'Types sql (deprecated)'=>$oldTypes);
  83. }
  84. }
  85. ?>