import.plugin.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. //Déclaration d'un item de menu dans le menu principal
  3. function import_menu(&$menuItems){
  4. global $myUser;
  5. if(!$myUser->can('import','read')) return;
  6. $menuItems[] = array(
  7. 'sort'=>3,
  8. 'url'=>'index.php?module=import',
  9. 'label'=>'Import',
  10. 'icon'=> 'fas fa-file-import',
  11. 'color'=> '#2ecc71'
  12. );
  13. }
  14. //Cette fonction va generer une page quand on clique sur Import dans menu
  15. function import_page(){
  16. global $_;
  17. if(!isset($_['module']) || $_['module'] !='import') return;
  18. $page = !isset($_['page']) ? 'list.import' : $_['page'];
  19. $page = str_replace('..','',$page);
  20. $file = __DIR__.SLASH.'page.'.$page.'.php';
  21. if(!file_exists($file)) throw new Exception("Page ".$page." inexistante");
  22. require_once($file);
  23. }
  24. //Fonction executée lors de l'activation du plugin
  25. function import_install($id){
  26. if($id != 'fr.core.import') return;
  27. Entity::install(__DIR__);
  28. }
  29. //Fonction executée lors de la désactivation du plugin
  30. function import_uninstall($id){
  31. if($id != 'fr.core.import') return;
  32. Entity::uninstall(__DIR__);
  33. }
  34. //Déclaration des sections de droits du plugin
  35. Right::register('import',array('label'=>'Gestion des droits sur le plugin Import'));
  36. //cette fonction comprends toutes les actions du plugin qui ne nécessitent pas de vue html
  37. function import_action(){
  38. require_once(__DIR__.SLASH.'action.php');
  39. }
  40. //Déclaration du menu de réglages
  41. function import_menu_setting(&$settingMenu){
  42. global $myUser;
  43. if(!$myUser->can('import','configure')) return;
  44. $settingMenu[]= array(
  45. 'sort' =>1,
  46. 'url' => 'setting.php?section=global.import',
  47. 'icon' => 'fas fa-angle-right',
  48. 'label' => 'Import'
  49. );
  50. }
  51. //Déclaration des pages de réglages
  52. function import_content_setting(){
  53. global $_;
  54. if(file_exists(__DIR__.SLASH.'setting.'.$_['section'].'.php'))
  55. require_once(__DIR__.SLASH.'setting.'.$_['section'].'.php');
  56. }
  57. //Récupere la lettre excel (ou le groupe de lettres) en fonction du numéro de colonne
  58. function import_column_letter($columnNumber){
  59. $alphabet = str_split('ABCDEFGHIJKLMNOPQRSTUVWXYZ');
  60. $multiplicator = floor($columnNumber / 26);
  61. $columnNumber = $columnNumber - ($multiplicator*26);
  62. $parentLetter = ($multiplicator!=0) ? import_column_letter($multiplicator-1) :'';
  63. return $parentLetter.$alphabet[$columnNumber];
  64. }
  65. function import_report_line(&$response,$context,$type,$typeLabel,$message){
  66. if(!isset($response['lines'][$context['lineNumber']])) $response['lines'][$context['lineNumber']] = array('state'=>ImportMapping::UNIQUE_SKIP,'columns'=>array());
  67. foreach($context['columns'] as $i=>$columnLetter){
  68. if(!isset($response['lines'][$context['lineNumber']])) $response['lines'][$context['lineNumber']] = array('columns'=>array());
  69. if(!isset($context['line'][$i])) $context['line'][$i] = array('value'=>'');
  70. $response['lines'][$context['lineNumber']]['columns'][$i] = array('value'=>$context['line'][$i]['value']);
  71. }
  72. $response['lines'][$context['lineNumber']]['columns'][$context['columnNumber']]['state'] = $type ;
  73. $response['lines'][$context['lineNumber']]['columns'][$context['columnNumber']]['stateLabel'] = $typeLabel ;
  74. $response['lines'][$context['lineNumber']]['columns'][$context['columnNumber']]['message'] = $message;
  75. }
  76. function import_import_macros(&$macros){
  77. $macros = array(
  78. 'dictionary' => array(
  79. 'slug'=>'dictionary',
  80. 'label'=>'Convertir en liste configurable',
  81. 'description'=>'Convertis les valeurs en dictionnaire (liste configurable)',
  82. 'mutator'=> function($value,$context = array()){
  83. if(!isset($context['dictionnaries'])) $context['dictionnaries'] = array();
  84. $parentSlug = slugify($context['plugin']).'_'.slugify($context['entity']).'_'.$context['entityField']['column'];
  85. //récuperation du dico racine / construition si inexistant
  86. if(!isset($context['dictionnaries'][$parentSlug])){
  87. $parent = Dictionary::bySlug($parentSlug);
  88. if(!$parent){
  89. $parent = new Dictionary();
  90. $parent->slug = $parentSlug;
  91. $parent->label = $context['entityLabel'].' : '.$context['entityField']['label'];
  92. $parent->parent = 0;
  93. $parent->state = Dictionary::ACTIVE;
  94. $parent->save();
  95. }
  96. $context['dictionnaries'][$parentSlug] = array('id'=>$parent->id,'childs'=>array());
  97. }
  98. $valueSlug = $parentSlug.'_'.slugify($value);
  99. if(!isset($context['dictionnaries'][$parentSlug]['childs'][$valueSlug])){
  100. $item = Dictionary::bySlug($valueSlug);
  101. if(!$item){
  102. $item = new Dictionary();
  103. $item->slug = $valueSlug;
  104. $item->label = $value;
  105. $item->parent = $context['dictionnaries'][$parentSlug]['id'];
  106. $item->state = Dictionary::ACTIVE;
  107. $item->save();
  108. }
  109. $context['dictionnaries'][$parentSlug]['childs'][$valueSlug] = $item->id;
  110. }
  111. return $context['dictionnaries'][$parentSlug]['childs'][$valueSlug];
  112. }),
  113. 'firm' => array(
  114. 'slug'=>'firm',
  115. 'label'=>'Convertir en établissement ERP',
  116. 'description'=>'Convertis les textes en établissement ERP (si existants)',
  117. 'mutator'=> function($value,&$context = array()){
  118. if(!isset($context['firms'])){
  119. $context['firms'] = array();
  120. foreach(Firm::loadAll() as $firm){
  121. $context['firms'][slugify($firm->label)] = $firm;
  122. }
  123. }
  124. //var_dump($value,slugify($value),$context['firms']);
  125. if(!isset($context['firms'][slugify($value)])) return '';
  126. return $context['firms'][slugify($value)]->id;
  127. }),
  128. 'price' => array(
  129. 'slug'=>'price',
  130. 'label'=>'Convertir en prix € ERP',
  131. 'description'=>'Convertis les textes en prix € ERP',
  132. 'mutator'=> function($value,&$context = array()){
  133. $value = str_replace(',','.',$value);
  134. $value = preg_replace('/[^0-9\.]/i','',$value);
  135. return $value;
  136. }),
  137. 'breakline' => array(
  138. 'slug'=>'breakline',
  139. 'label'=>'Convertir les sauts de lignes',
  140. 'description'=>'Convertir les sauts de lignes HTML en saut de lignes classiques',
  141. 'mutator'=> function($value,&$context = array()){
  142. $value = str_replace('<br>',"\n",$value);
  143. return $value;
  144. }),
  145. 'boolean' => array(
  146. 'slug'=>'boolean',
  147. 'label'=>'Convertir en vrai/faux ERP',
  148. 'description'=>'Convertis les oui/non O/N en vrai/faux ERP',
  149. 'mutator'=> function($value,&$context = array()){
  150. $value = str_replace(array(' ',"\r","\n","\t"),'',mb_strtolower($value));
  151. switch($value){
  152. case 'o':
  153. case 'oui':
  154. case 'x':
  155. case 'yes':
  156. case 'ok':
  157. case 'vrai':
  158. case '1':
  159. $value = 1;
  160. break;
  161. case 'n':
  162. case 'non':
  163. case 'no':
  164. case 'ko':
  165. case 'faux':
  166. case '0':
  167. $value = 0;
  168. break;
  169. default:
  170. $value = '';
  171. break;
  172. }
  173. return $value;
  174. }),
  175. 'date' => array(
  176. 'slug' => 'date',
  177. 'label'=>'Convertir en date ERP',
  178. 'description'=>'Convertis la date excel en date ERP (timestamp)',
  179. 'mutator'=> function($value,&$context = array()){
  180. $value = trim($value);
  181. if(empty($value)) return '';
  182. $date = explode('/',str_replace(array('-',' '), '/', $value));
  183. if(count($date)<3) return 0;
  184. if(strlen($date[2]) == 4){
  185. $year = $date[2];
  186. $month = $date[1];
  187. $day = $date[0];
  188. }else if(strlen($date[0]) == 4){
  189. $year = $date[0];
  190. $month = $date[1];
  191. $day = $date[2];
  192. }else{
  193. return 0;
  194. }
  195. if(!is_numeric($month)){
  196. $numericMonth = false;
  197. $longNames = array('janvier','fevrier','mars','avril','mai','juin','juillet','aout','septembre','octobre','novembre','decembre');
  198. $shortNames = array('jan','fev','mar','avr','mai','juin','juil','aout','sept','oct','nov','dec');
  199. if(isset( $longNames[slugify($month)] ))$numericMonth = $longNames[slugify($month)];
  200. if(isset( $shortNames[slugify($month)] ))$numericMonth = $shortNames[slugify($month)];
  201. $month = $numericMonth;
  202. if(!$month) return 0;
  203. }
  204. $m = 0;
  205. $h = 0;
  206. if(isset($date[3])){
  207. list($h,$m) = explode(':',$date[3]);
  208. }
  209. if(!is_numeric($day) || !is_numeric($month) || !is_numeric($year)) return 0;
  210. return mktime($h,$m,0,$month,$day,$year);
  211. }
  212. ),
  213. 'empty-breakline' => array(
  214. 'slug'=>'empty-breakline',
  215. 'label'=>'Supprimer les sauts de lignes',
  216. 'description'=>'Supprimer les sauts de lignes (texte et html)',
  217. 'mutator'=> function($value,&$context = array()){
  218. $value = str_replace(array('<br>',"\n","\r"),"",$value);
  219. return $value;
  220. })
  221. );
  222. }
  223. require_once(__DIR__.SLASH.'action.php');
  224. //Déclaration des settings de base
  225. //Types possibles : voir FieldType.class.php. Un simple string définit une catégorie.
  226. Configuration::setting('import',array(
  227. "Général",
  228. //'import_enable' => array("label"=>"Activer","type"=>"boolean"),
  229. ));
  230. /*
  231. */
  232. //Déclation des assets
  233. Plugin::addCss("/css/main.css");
  234. Plugin::addJs("/js/main.js");
  235. Plugin::addCss("/css/component.css");
  236. Plugin::addJs("/js/component.js");
  237. //Mapping hook / fonctions
  238. Plugin::addHook("install", "import_install");
  239. Plugin::addHook("uninstall", "import_uninstall");
  240. Plugin::addHook("menu_main", "import_menu");
  241. Plugin::addHook("page", "import_page");
  242. Plugin::addHook("menu_setting", "import_menu_setting");
  243. Plugin::addHook("content_setting", "import_content_setting");
  244. Plugin::addHook("import_macros","import_import_macros");
  245. ?>