action.php 9.7 KB

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