action.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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'] = lcFirst(factory_sanitize($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['typeLabel'] = $type['label'];
  117. $value['sql-type'] = $type['sql-type'];
  118. $value['input'] = factory_render($type['input'],$value);
  119. $fields[lcfirst(factory_sanitize($key))] = $value;
  120. }
  121. }
  122. $filters = array();
  123. if(isset($_['filters'])){
  124. foreach ($_['filters'] as $key => $value){
  125. if($value == '')continue;
  126. $filters[] = $value;
  127. }
  128. }
  129. $templates = Template::all($_['template']);
  130. $template = $templates[$_['part']];
  131. $data = array(
  132. 'Entity' => $entity,
  133. 'entity' => strtolower($entity),
  134. 'ENTITY' => strtoupper($entity),
  135. 'table' => $table,
  136. 'user.fullname' => $myUser->fullName(),
  137. 'user.mail' => $myUser->mail,
  138. 'plugin' => strtolower($plugin),
  139. 'PLUGIN' => strtoupper($plugin),
  140. 'Plugin' => ucfirst(strtolower($plugin)),
  141. 'fields' => $fields,
  142. 'links' => $links,
  143. 'linksCount' => count($links)> 0 ? 1: 0,
  144. 'filters' => $filters,
  145. 'description' => $description,
  146. );
  147. foreach(array('-','_','.') as $symbol){
  148. $readable = factory_sanitize(str_replace(' ',$symbol,trim($_['entity'])),false,'_.-');
  149. $readable = mb_strtolower($readable);
  150. if(strpos($readable,$plugin.$symbol) === 0)
  151. $readable = substr($readable, strlen($plugin.$symbol));
  152. $data['entity'.$symbol.'readable'] = $readable;
  153. }
  154. $stream = file($template['file']);
  155. array_shift($stream);
  156. $stream = implode($stream);
  157. $stream = factory_render($stream,$data);
  158. if(isset($_['generate']) && $_['generate']==1){
  159. if(!isset($_['plugin']) || !isset($_['entity'])) return;
  160. $relativePath = str_replace(Template::dir($_['template']),'',$template['file']);
  161. $relativePath = factory_render($relativePath,$data);
  162. $pluginPath = PLUGIN_PATH.strtolower($plugin);
  163. $filePath = $pluginPath.SLASH.$relativePath;
  164. $parentFolder = dirname($filePath);
  165. if(!file_exists($parentFolder)) mkdir($parentFolder,0755,true);
  166. file_put_contents($filePath,$stream);
  167. }
  168. echo htmlentities($stream);
  169. break;
  170. }
  171. ?>