action.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <?php
  2. Action::register('factory_search_part',function(&$response){
  3. global $myUser,$_;
  4. User::check_access('factory','read');
  5. require_once(__DIR__.SLASH.'Template.class.php');
  6. $i = 0;
  7. foreach(Template::all($_['template']) as $key=>$template):
  8. $response['rows'][] = array(
  9. 'active' => $i==0,
  10. 'langage' => $template['syntax'],
  11. 'label' => $key
  12. );
  13. $i++;
  14. endforeach;
  15. });
  16. Action::register('factory_search_filters',function(&$response){
  17. global $myUser,$_;
  18. User::check_access('factory','read');
  19. require_once(__DIR__.SLASH.'Template.class.php');
  20. $response['rows'] = Template::filters($_['template']);
  21. });
  22. Action::register('factory_autocomplete_plugin',function(&$response){
  23. global $myUser,$_;
  24. User::check_access('factory','read');
  25. $response['rows'] = array();
  26. foreach(glob(__ROOT__.PLUGIN_PATH.'*') as $pluginPath){
  27. if(!is_dir($pluginPath)) continue;
  28. $plugin = basename($pluginPath);
  29. if(preg_match('|'.$_['keyword'].'|i', $plugin)){
  30. $manifestPath = $pluginPath.SLASH.'app.json';
  31. $manifest = array(
  32. 'value' => $plugin,
  33. 'label' => $plugin,
  34. 'name' => $plugin
  35. );
  36. if(file_exists($manifestPath)){
  37. $manifest = json_decode(file_get_contents($pluginPath.SLASH.'app.json'),true);
  38. $manifest['value'] = $plugin;
  39. $manifest['label'] = $manifest['name'];
  40. $manifest['name'] = $manifest['value'];
  41. if(empty($manifest['icon'])) $manifest['icon'] = 'fas fa-question';
  42. if(empty($manifest['color'])) $manifest['color'] = '#273c75';
  43. }
  44. $response['rows'][] = $manifest;
  45. }
  46. }
  47. });
  48. Action::register('factory_autocomplete_entity',function(&$response){
  49. global $myUser,$_;
  50. $response['rows'] = array();
  51. User::check_access('factory','read');
  52. if(isset($_['plugin']) && $_['plugin'] == 'false'){
  53. //si plugin = false on prends toutes les classes du projet
  54. $classDirectory = array_merge(glob(__ROOT__.'class'.SLASH.'*.class.php'),glob(__ROOT__.PLUGIN_PATH.'*'.SLASH.'*.class.php'));
  55. }else if(empty($_['plugin']) || $_['plugin']=='core'){
  56. //si plugin = vide ou core on prends les classes core du projet
  57. $classDirectory = glob(__ROOT__.'class'.SLASH.'*.class.php');
  58. }else{
  59. //si plugin != vide et != false ou core on prends les classes du plugin ciblé
  60. $classDirectory = glob(__ROOT__.PLUGIN_PATH.$_['plugin'].SLASH.'*.class.php');
  61. }
  62. foreach($classDirectory as $path){
  63. if(is_dir($path)) continue;
  64. $parent = basename(dirname($path));
  65. $entity = str_replace('.class.php','',basename($path));
  66. if(preg_match('|'.$_['keyword'].'|i', $entity)) $response['rows'][] = array(
  67. 'name'=> $entity,
  68. 'parent' => $parent,
  69. 'path' => $path,
  70. 'value'=> $entity
  71. );
  72. }
  73. });
  74. Action::register('factory_autocomplete_entity_select',function(&$response){
  75. global $myUser,$_;
  76. User::check_access('factory','read');
  77. $response['rows'] = array();
  78. $path = __ROOT__.PLUGIN_PATH.$_['plugin'].SLASH.$_['entity'].'.class.php';
  79. if(empty($_['plugin']) || $_['plugin']=='core') $path = __ROOT__.'class'.SLASH.$_['entity'].'.class.php';
  80. if(!file_exists($path)) return;
  81. //new version via fields object
  82. require_once($path);
  83. $item = new $_['entity']();
  84. global $databases_credentials;
  85. $sgbd = $databases_credentials[$item->baseUid]['connector'];
  86. $types = FieldType::available();
  87. $typesKey = array_keys($types );
  88. $response['label'] = $_['entity']::entityLabel();
  89. foreach ($item->fields as $field => $type) {
  90. if(in_array($field, array('id', 'created', 'updated', 'creator', 'updater'))) continue;
  91. if(!is_array($type)) $type = array('type'=>$type,'label'=>$field );
  92. if(isset($type['link']) ){
  93. $type['type'] = 'entity-external-entity';
  94. }
  95. $type['key'] = $field;
  96. $response['rows'][] = $type;
  97. }
  98. });
  99. Action::register('factory_render',function(&$response){
  100. User::check_access('factory','read');
  101. global $myUser,$_;
  102. require_once(__DIR__.SLASH.'Template.class.php');
  103. $entity = factory_sanitize($_['entity']);
  104. $table = factory_sanitize($_['entity'], true);
  105. $plugin = factory_sanitize($_['plugin']);
  106. $entityLabel = !empty($_['entityLabel']) ? addslashes($_['entityLabel']) : $_['entity'] ;
  107. $description = isset($_['description']) ? $_['description'] : '';
  108. $fields = array();
  109. $links = array();
  110. $categories = Template::types();
  111. $types = array();
  112. $types = array_merge($categories['Types sql (deprecated)'],$categories['Types programmes']);
  113. if(isset($_['fields'])){
  114. foreach ($_['fields'] as $key => $value){
  115. if($key=='' || $key==1)continue;
  116. if(empty($value['label'])) $value['label'] = ucfirst($key);
  117. $value['key'] = lcFirst(factory_sanitize($key));
  118. $value['label_escaped'] = addslashes($value['label']);
  119. //gestion des entité liées
  120. if($value['type'] == 'entity-external-entity'){
  121. $type = $types['integer'];
  122. $link = array('linkedKey'=>$value['key']);
  123. if(!isset($value['entityPath'])) $value['entityPath'] = '';
  124. $value['entityRelativePath'] = str_replace(__ROOT__,'',$value['entityPath']);
  125. $class = str_replace('.class.php','',basename($value['entityPath']));
  126. $link['linkedEntity'] = $class;
  127. $link['linkedPath'] = $value['entityPath'];
  128. $link['linkedRelativePath'] = str_replace(__ROOT__,'',$link['linkedPath']);
  129. $link['linkedRequirePath'] = '__ROOT__.SLASH.\''.$link['linkedRelativePath'].'\'';
  130. $links[] = $link;
  131. }else{
  132. $type = $types[$value['type']];
  133. }
  134. //Pour pouvoir acceder au nom de l'entité dans une boucle de field
  135. $value['Entity'] = $entity;
  136. $value['entity'] = strtolower($entity);
  137. $value['type'] = $type->slug;
  138. foreach (array_keys($types) as $slug)
  139. $value['is'.ucfirst($slug)] = false;
  140. $value['is'.ucfirst($value['type'])] = true;
  141. switch($value['type']){
  142. case 'list':
  143. case 'choice':
  144. $value['values'] = array($value['key'].'-1'=>$value['label']);
  145. break;
  146. case 'dictionary':
  147. $value['attributes']['data-slug'] = '"'.strtolower($plugin).'_'.strtolower($entity).'_'.$value['key'].'"';
  148. break;
  149. case 'int':
  150. case 'integer':
  151. $value['value'] = '<?php echo $'.$value['entity'].'->'.$value['key'].'; ?>';
  152. break;
  153. case 'image':
  154. case 'file':
  155. $value['attributes']['data-action'] = '"'.strtolower($plugin).'_'.strtolower($entity).'_'.$value['key'].'"';
  156. $value['attributes']['data-id'] = '"'.$value['key'].'"';
  157. $value['attributes']['data-data'] = "'{\"id\":\"<?php echo \$".$value['entity']."->id; ?>\"}'";
  158. break;
  159. default:
  160. $value['value'] = '<?php echo $'.$value['entity'].'->'.$value['key'].'; ?>';
  161. break;
  162. }
  163. $value['id'] = ''.$value['key'].'';
  164. $value['input'] = FieldType::toHtml($value,$types);
  165. $value['input'] = $value['input']['input'];
  166. $value['attributes']['value'] = '';
  167. $value['input_novalue'] =FieldType::toHtml($value,$types);
  168. $value['input_novalue'] = $value['input_novalue']['input'];
  169. $value['type'] = $type->toArray();
  170. //mapping slug fieldtype / slug recherche avancée
  171. $value['type']['search-slug'] = $value['type']['slug'];
  172. $fields[lcfirst(factory_sanitize($key))] = $value;
  173. }
  174. }
  175. $templates = Template::all($_['template']);
  176. $template = $templates[$_['part']];
  177. $data = array(
  178. 'Entity' => $entity,
  179. 'entity' => strtolower($entity),
  180. 'ENTITY' => strtoupper($entity),
  181. 'EntityLabel' => $entityLabel,
  182. 'entityLabel' => strtolower($entityLabel),
  183. 'ENTITYLABEL' => strtoupper($entityLabel),
  184. 'table' => $table,
  185. 'user.fullname' => $myUser->fullName(),
  186. 'user.mail' => $myUser->mail,
  187. 'plugin' => strtolower($plugin),
  188. 'PLUGIN' => strtoupper($plugin),
  189. 'Plugin' => ucfirst(strtolower($plugin)),
  190. 'Plugin.label' => ucfirst(strtolower($_['pluginLabel'])),
  191. 'plugin.label' => $_['pluginLabel'],
  192. 'plugin.color' => $_['plugin-color'],
  193. 'plugin.icon' => $_['plugin-icon'],
  194. 'fields' => $fields,
  195. 'links' => $links,
  196. 'linksCount' => count($links)> 0 ? 1: 0,
  197. 'description' => $description,
  198. );
  199. $filters = Template::filters($_['template']);
  200. foreach ($filters as $filter)
  201. $data[$filter['slug']] = isset($_['filters']) && in_array($filter['slug'], $_['filters']);
  202. foreach(array('-','_','.') as $symbol){
  203. $readable = factory_sanitize(str_replace(' ',$symbol,trim($_['entity'])),false,'_.-');
  204. $readable = mb_strtolower($readable);
  205. if(strpos($readable,$plugin.$symbol) === 0)
  206. $readable = substr($readable, strlen($plugin.$symbol));
  207. $data['entity'.$symbol.'readable'] = $readable;
  208. }
  209. //genere un id plugin_entité en déduplicant si entité = plugin
  210. $data['plugin_entity_deduplicate'] = $data['plugin'] == $data['entity_readable'] ? $data['plugin'] : $data['plugin'].'_'.$data['entity_readable'];
  211. $data['plugin_table_deduplicate'] = $data['plugin'] == $data['table'] ? $data['plugin'] : $data['plugin'].'_'.$data['table'];
  212. $stream = file($template['file']);
  213. array_shift($stream);
  214. $stream = implode($stream);
  215. $stream = template($stream,$data,true);
  216. //remplacement des [-[-value-]-] par {{value}} (mustache escape)
  217. $stream = preg_replace('/\[-\[-(.*)-\]-\]/isU', '{{$1}}', $stream);
  218. if(isset($_['generate']) && $_['generate']==1){
  219. if(!isset($_['plugin']) || !isset($_['entity'])) return;
  220. $relativePath = str_replace(Template::dir($_['template']),'',$template['file']);
  221. $relativePath = template($relativePath,$data,true);
  222. $pluginPath = PLUGIN_PATH.strtolower($plugin);
  223. $filePath = $pluginPath.SLASH.$relativePath;
  224. $parentFolder = dirname($filePath);
  225. if(!file_exists($parentFolder)) mkdir($parentFolder,0755,true);
  226. file_put_contents($filePath,$stream);
  227. }
  228. echo htmlentities($stream);
  229. exit();
  230. });
  231. ?>