action.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. global $_,$conf;
  3. switch($_['action']){
  4. case 'factory_search_part':
  5. Action::write(function(&$response){
  6. global $myUser,$_;
  7. require_once(__DIR__.SLASH.'Template.class.php');
  8. $i = 0;
  9. foreach(Template::all($_['template']) as $key=>$template):
  10. $response['rows'][] = array(
  11. 'active' => $i==0,
  12. 'langage' => $template['syntax'],
  13. 'label' => $key
  14. );
  15. $i++;
  16. endforeach;
  17. });
  18. break;
  19. case 'factory_search_filters':
  20. Action::write(function(&$response){
  21. global $myUser,$_;
  22. require_once(__DIR__.SLASH.'Template.class.php');
  23. $response['rows'] = array();
  24. $sections = array();
  25. foreach (Template::all($_['template']) as $template) {
  26. $stream = file_get_contents($template['file']);
  27. preg_match_all('/{{~!?([^}]*)}}/i', $stream, $matches,PREG_SET_ORDER);
  28. foreach($matches as $section)
  29. $sections[] = array('section'=>trim($section[1]));
  30. }
  31. $sections = array_map("unserialize", array_unique(array_map("serialize", $sections)));
  32. sort($sections);
  33. $response['rows'] = $sections;
  34. });
  35. break;
  36. case 'factory_autocomplete_plugin':
  37. Action::write(function(&$response){
  38. global $myUser,$_;
  39. $response['rows'] = array();
  40. foreach(glob(__ROOT__.PLUGIN_PATH.'*') as $plugin){
  41. if(!is_dir($plugin)) continue;
  42. $plugin = basename($plugin);
  43. if(preg_match('|'.$_['keyword'].'|i', $plugin)) $response['rows'][] = array('name'=>$plugin,'value'=>$plugin);
  44. }
  45. });
  46. break;
  47. case 'factory_entity_search':
  48. Action::write(function(&$response){
  49. global $myUser,$_;
  50. $response['rows'] = array();
  51. foreach(glob(__ROOT__.PLUGIN_PATH.$_['plugin'].SLASH.'*.class.php') as $entity){
  52. if(is_dir($entity)) continue;
  53. $entity = str_replace('.class.php','',basename($entity));
  54. $response['rows'][] = $entity;
  55. }
  56. });
  57. break;
  58. case 'factory_autocomplete_entity':
  59. Action::write(function(&$response){
  60. global $myUser,$_;
  61. $response['rows'] = array();
  62. foreach(glob(__ROOT__.PLUGIN_PATH.$_['plugin'].SLASH.'*.class.php') as $entity){
  63. if(is_dir($entity)) continue;
  64. $entity = str_replace('.class.php','',basename($entity));
  65. if(preg_match('|'.$_['keyword'].'|i', $entity)) $response['rows'][] = array('name'=>$entity,'value'=>$entity);
  66. }
  67. });
  68. break;
  69. case 'factory_autocomplete_entity_select':
  70. Action::write(function(&$response){
  71. global $myUser,$_;
  72. $response['rows'] = array();
  73. $path = __ROOT__.PLUGIN_PATH.$_['plugin'].SLASH.$_['entity'].'.class.php';
  74. if(!file_exists($path)) return;
  75. //new version via fields object
  76. require_once($path);
  77. $item = new $_['entity']();
  78. $sgbd = BASE_SGBD;
  79. $types = array_keys($sgbd::types());
  80. foreach ($item->fields as $field => $type) {
  81. if(in_array($field, array('id', 'created', 'updated', 'creator', 'updater'))) continue;
  82. if(!in_array($type, $types)) continue;
  83. $response['rows'][] = array('label'=>$field, 'type'=>$type);
  84. }
  85. //Old version via preg_match
  86. // $stream = file_get_contents($path);
  87. // preg_match_all('|\'([^\']*)\' \=\> \'([^\']*)\',?|ism', $stream , $matches,PREG_SET_ORDER);
  88. // foreach ($matches as $match) {
  89. // if($match[1]=='id') continue;
  90. // if(count($match)!=3) continue;
  91. // $response['rows'][] = array('label' => $match[1],'type' => $match[2]);
  92. // }
  93. });
  94. break;
  95. case 'factory_render':
  96. global $myUser;
  97. require_once(__DIR__.SLASH.'Template.class.php');
  98. $entity = factory_sanitize($_['entity']);
  99. $table = factory_sanitize($_['entity'], true);
  100. $plugin = factory_sanitize($_['plugin']);
  101. $description = isset($_['description']) ? $_['description'] : '';
  102. $fields = array();
  103. $links = array();
  104. $types = Template::types();
  105. if(isset($_['fields'])){
  106. foreach ($_['fields'] as $key => $value){
  107. if($key=='' || $key==1)continue;
  108. if($value['label']=='') $value['label'] = ucfirst($key);
  109. $value['key'] = $key;
  110. if(strpos($value['type'], 'entity-')!==false){
  111. $type = $types['int'];
  112. $links[] = array('key'=>$value['key'],'entity'=>str_replace('entity-','',$value['type']));
  113. }else{
  114. $type = $types[$value['type']];
  115. }
  116. $value['sql-type'] = $type['sql-type'];
  117. $value['input'] = factory_render($type['input'],$value);
  118. $fields[lcfirst(factory_sanitize($key))] = $value;
  119. }
  120. }
  121. $filters = array();
  122. if(isset($_['filters'])){
  123. foreach ($_['filters'] as $key => $value){
  124. if($value == '')continue;
  125. $filters[] = $value;
  126. }
  127. }
  128. $templates = Template::all($_['template']);
  129. $template = $templates[$_['part']];
  130. $data = array(
  131. 'Entity' => $entity,
  132. 'entity' => strtolower($entity),
  133. 'ENTITY' => strtoupper($entity),
  134. 'table' => $table,
  135. 'user.fullname' => $myUser->fullName(),
  136. 'user.mail' => $myUser->mail,
  137. 'plugin' => strtolower($plugin),
  138. 'PLUGIN' => strtoupper($plugin),
  139. 'Plugin' => ucfirst(strtolower($plugin)),
  140. 'fields' => $fields,
  141. 'links' => $links,
  142. 'linksCount' => count($links)> 0 ? 1: 0,
  143. 'filters' => $filters,
  144. 'description' => $description,
  145. );
  146. $stream = file($template['file']);
  147. array_shift($stream);
  148. $stream = implode($stream);
  149. $stream = factory_render($stream,$data);
  150. if(isset($_['generate']) && $_['generate']==1){
  151. if(!isset($_['plugin']) || !isset($_['entity'])) return;
  152. $relativePath = str_replace(Template::dir($_['template']),'',$template['file']);
  153. $relativePath = factory_render($relativePath,$data);
  154. $pluginPath = PLUGIN_PATH.strtolower($plugin);
  155. $filePath = $pluginPath.SLASH.$relativePath;
  156. $parentFolder = dirname($filePath);
  157. if(!file_exists($parentFolder)) mkdir($parentFolder,0755,true);
  158. file_put_contents($filePath,$stream);
  159. }
  160. echo htmlentities($stream);
  161. break;
  162. }
  163. ?>