factory.plugin.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. //Déclaration d'un item de menu dans le menu principal
  3. function factory_menu(&$menuItems){
  4. global $_,$myUser;
  5. if(!$myUser->can('factory','read')) return;
  6. $menuItems[] = array(
  7. 'sort'=>100,
  8. 'url'=>'index.php?module=factory',
  9. 'label'=>'Factory',
  10. 'icon'=> 'fas fa-cube',
  11. 'color'=> '#ef1e4e'
  12. );
  13. }
  14. //Cette fonction va generer une page quand on clique sur Modele dans menu
  15. function factory_page(){
  16. global $_,$myUser;
  17. if(!isset($_['module']) || $_['module'] !='factory') return;
  18. $page = !isset($_['page']) ? 'factory' : $_['page'];
  19. $file = __DIR__.SLASH.'page.'.$page.'.php';
  20. if(!file_exists($file)) throw new Exception("Page ".$page." inexistante");
  21. require_once($file);
  22. }
  23. //Fonction executée lors de l'activation du plugin
  24. function factory_install($id){
  25. if($id != 'fr.idleman.factory') return;
  26. Entity::install(__DIR__);
  27. }
  28. //Fonction executée lors de la désactivation du plugin
  29. function factory_uninstall($id){
  30. if($id != 'fr.idleman.factory') return;
  31. Entity::uninstall(__DIR__);
  32. }
  33. //cette fonction comprends toutes les actions du plugin qui ne nécessitent pas de vue html
  34. function factory_action(){
  35. require_once(__DIR__.SLASH.'action.php');
  36. }
  37. function factory_sanitize($text, $tableName=false,$allowChars='_'){
  38. $returned = '';
  39. foreach (explode(' ',trim($text)) as $word) {
  40. $returned .= $tableName ? mb_strtolower($word).'_' : ucfirst(mb_strtolower($word));
  41. }
  42. if($tableName) $returned = rtrim($returned, '_');
  43. $unwanted = array( 'Š'=>'S', 'š'=>'s', 'Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
  44. 'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U',
  45. 'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss', 'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c',
  46. 'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o',
  47. 'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y' );
  48. $returned = strtr($returned, $unwanted);
  49. return preg_replace('|[^a-z0-9'.preg_quote($allowChars).']|i','',$returned);
  50. }
  51. function factory_render($sql,$data=array()){
  52. //filters
  53. $sql = preg_replace_callback('/{{~(!)?([^}]*)}}(.*?){{\/~}}/is',function($matches) use ($data) {
  54. $key = trim($matches[2]);
  55. $sql = $matches[3];
  56. $not = $matches[1];
  57. if($not=='!'){
  58. return isset($data['filters']) && in_array($key, $data['filters']) ?'':$sql;
  59. }else{
  60. return !isset($data['filters']) || !in_array($key, $data['filters']) ?'':$sql;
  61. }
  62. },$sql);
  63. //loop
  64. $sql = preg_replace_callback('/{{\:([^\/\:\?}]*)}}(.*?){{\/\:[^\/\:\?}]*}}/s',function($matches) use ($data) {
  65. $key = $matches[1];
  66. $sqlTpl = $matches[2];
  67. //print_r('LOOP ON '.$key.PHP_EOL);
  68. $sql = '';
  69. if(isset($data[$key])){
  70. $i = 0;
  71. $values = $data[$key];
  72. foreach($values as $key=>$value){
  73. $i++;
  74. $last = $i == count($values);
  75. $occurence = str_replace(array('{{key}}'),array($key),$sqlTpl);
  76. if(is_array($value)){
  77. foreach ($value as $key2 => $value2)
  78. $occurence = str_replace(array('{{value.'.$key2.'}}'),array($value2),$occurence);
  79. }else{
  80. $occurence = str_replace(array('{{value}}'),array($value),$occurence);
  81. }
  82. $occurence = preg_replace_callback('/{{\;}}(.*?){{\/\;}}/',function($matches) use ($last){
  83. return $last? '': $matches[1];
  84. },$occurence);
  85. $sql.= $occurence;
  86. }
  87. return $sql;
  88. }
  89. return '';
  90. },$sql);
  91. //conditions
  92. $sql = preg_replace_callback('/{{\?([^\/\:\?}]*)}}(.*?){{\/\?[^\/\:\?}]*}}/s',function($matches) use ($data) {
  93. $key = $matches[1];
  94. $sql = $matches[2];
  95. return !isset($data[$key]) || (is_array($data[$key]) && count($data[$key])==0) ?'':$sql;
  96. },$sql);
  97. //simple vars
  98. $sql = preg_replace_callback('/{{([^\/\:\;\?}]*)}}/',function($matches) use ($data) {
  99. $key = $matches[1];
  100. return isset($data[$key])?$data[$key]:$matches[0];
  101. },$sql);
  102. return $sql;
  103. }
  104. //Déclaration des sections de droits du plugin
  105. function factory_section(&$sections){
  106. $sections['factory'] = "Gestion des droits sur le plugin Factory";
  107. }
  108. //Déclation des assets
  109. Plugin::addCss("/css/main.css");
  110. Plugin::addJs("/js/main.js");
  111. //Mapping hook / fonctions
  112. Plugin::addHook("install", "factory_install");
  113. Plugin::addHook("uninstall", "factory_uninstall");
  114. Plugin::addHook("section", "factory_section");
  115. Plugin::addHook("menu_main", "factory_menu");
  116. Plugin::addHook("page", "factory_page");
  117. Plugin::addHook("action", "factory_action");
  118. ?>