main.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. //CHARGEMENT DE LA PAGE
  2. function init_plugin_import(){
  3. switch($.urlParam('page')){
  4. case 'sheet.import':
  5. $('#file').change(function(){
  6. import_save(function(){
  7. if($('.plugin-import').attr('data-mode') != 'configure') return;
  8. import_template_setting();
  9. },true);
  10. });
  11. if($('.plugin-import').attr('data-mode') == 'configure'){
  12. import_template_setting(function(){
  13. import_mapping_search();
  14. });
  15. }
  16. break;
  17. default:
  18. break;
  19. }
  20. $('#imports').sortable_table({
  21. onSort : import_search
  22. });
  23. }
  24. function import_template_setting(callback){
  25. var id = $.urlParam('id');
  26. if(!id) return;
  27. $.action({
  28. action : 'import_template_setting',
  29. import : id,
  30. },function(response){
  31. /*var columns ='';
  32. for(var k in response.headers){
  33. var column = response.headers[k];
  34. columns+='<option value="'+column.value+'">'+column.value+'</option>';
  35. }
  36. columns+='<option value="fixed">Valeur fixe</option>';
  37. $('select[data-id="excelColumn"]').html(columns);*/
  38. $('#template-settings').html(response.form);
  39. if(callback) callback();
  40. });
  41. }
  42. //Enregistrement des configurations
  43. function import_setting_save(){
  44. $.action({
  45. action: 'import_setting_save',
  46. fields: $('#import-setting-form').toJson()
  47. },function(){
  48. $.message('success','Enregistré');
  49. });
  50. }
  51. /** IMPORT **/
  52. //Récuperation d'une liste import dans le tableau #imports
  53. function import_search(callback,exportMode){
  54. var box = new FilterBox('#import-filters');
  55. if(exportMode) $('.btn-export').addClass('btn-preloader');
  56. $('#imports').fill({
  57. action:'import_search',
  58. filters: box.filters(),
  59. sort: $('#imports').sortable_table('get'),
  60. export: !exportMode ? false : exportMode
  61. },function(response){
  62. if(!exportMode) $('.results-count > span').text(response.pagination.total);
  63. if(callback!=null) callback();
  64. });
  65. }
  66. //Ajout ou modification import
  67. function import_save(callback){
  68. var data = $('#import-form').toJson();
  69. data.meta = $('#template-settings').toJson();
  70. $.action(data,function(r){
  71. $('#import-form').attr('data-id',r.id);
  72. $.urlParam('id',r.id);
  73. init_components('.file-container');
  74. if(callback) callback();
  75. });
  76. }
  77. //Suppression import
  78. function import_delete(element){
  79. if(!confirm('Êtes vous sûr de vouloir supprimer cet item ?')) return;
  80. var line = $(element).closest('.item-line');
  81. $.action({
  82. action: 'import_delete',
  83. id: line.attr('data-id')
  84. },function(r){
  85. line.remove();
  86. $.message('info','Item supprimé');
  87. });
  88. }
  89. /** LINE D\'IMPORT MAPPéE **/
  90. function import_mapping_deduction(){
  91. var id = $.urlParam('id');
  92. if(!id || !entity) return $.message('warning','Entitée ou excel non renseigné');
  93. $('.btn-import-deduction').addClass('btn-preloader');
  94. $.action({
  95. action:'import_mapping_deduction',
  96. id: id
  97. },function(response){
  98. import_mapping_search();
  99. $.message('success',response.excelCount+' Champs excel trouvés, '+response.entityCount+' associations erp déduites');
  100. });
  101. }
  102. function import_excel_infos(callback){
  103. var id = $.urlParam('id');
  104. $.action({
  105. action:'import_excel_infos',
  106. id: id
  107. },function(response){
  108. if(callback) callback();
  109. });
  110. }
  111. function import_mapping_fixed_change(element){
  112. var fixedValue = $(element).closest('.import-label-column').find('.excelColumnFixedValue');
  113. fixedValue.toggleClass('hidden',!$(element).prop('checked'));
  114. if(!$(element).prop('checked')) fixedValue.val('');
  115. import_mapping_save(element);
  116. }
  117. //Récuperation d'une liste line d\'import mappée dans le tableau #importmappings
  118. function import_mapping_search(callback){
  119. var id = $.urlParam('id');
  120. var entity = $('#entity').val();
  121. if(!id || !entity) return;
  122. $('.entityField').html('');
  123. $.action({
  124. action : 'core_entity_attribute_search',
  125. path : entity
  126. },function(response){
  127. var html = '<option value="">-</option>';
  128. var subhtml = '';
  129. for(var k in response.fields){
  130. var field = response.fields[k];
  131. html += '<option '+(field.childs?'data-link':'')+' value="'+field.column+'">'+field.label+'</option>';
  132. //si l'attribut et lié a une autre entité, on stock les attributs de cette autre entitée en tableau global pour réutilisation dans import_check_foreign_attribute
  133. if(field.childs && field.entity !='Dictionary'){
  134. if(!window.importLinks ) window.importLinks = {};
  135. window.importLinks[field.column] = field.childs;
  136. }
  137. }
  138. html+= '<option value="otherEntity">Autre donnée</option>';
  139. $('.entityField').html(html);
  140. $.action({
  141. action:'import_mapping_search',
  142. id: id
  143. },function(response){
  144. $('#import-mappings tbody tr:not(:eq(0))').remove();
  145. var tpl = $('#import-mappings tbody tr:eq(0)').get(0).outerHTML.replace(/data-tpl-type/g,'data-type');
  146. for(var k in response.rows){
  147. var line = $(Mustache.render(tpl,response.rows[k]));
  148. line.removeClass('hidden');
  149. $('#import-mappings').append(line);
  150. line.fromJson(response.rows[k]);
  151. import_check_foreign_attribute(line.find('.entityField'));
  152. line.find('.entitySubField').val(response.rows[k].entitySubField);
  153. if(response.rows[k].entityRelatedField){
  154. line.find('.entityRelatedField,.entityRelated').removeClass('hidden');
  155. import_entity_related_change(line.find('.entityRelated'));
  156. }
  157. if(response.rows[k].excelColumnFixedValue){
  158. line.find('.fixedValueCheckbox').prop('checked',true);
  159. line.find('[data-id="excelColumnFixedValue"]').removeClass('hidden');
  160. }
  161. init_components(line);
  162. }
  163. if(callback!=null) callback();
  164. });
  165. });
  166. }
  167. function import_entity_field_change(element){
  168. var select = $(element);
  169. var line = $(select).closest('tr');
  170. if(select.val() == 'otherEntity'){
  171. var related = line.find('.entityRelated');
  172. related.attr('data-type',related.attr('data-tpl-type')).removeClass('hidden');
  173. init_components(line);
  174. related.change(function(){
  175. import_entity_related_change(related);
  176. });
  177. }else{
  178. import_check_foreign_attribute(element);
  179. }
  180. import_mapping_save(element);
  181. }
  182. function import_entity_related_change(related){
  183. var line = related.closest('tr');
  184. $.action({
  185. action : 'core_entity_attribute_search',
  186. path : related.val()
  187. },function(response){
  188. var relatedField = line.find('.entityRelatedField');
  189. relatedField.attr('data-type',relatedField.attr('data-tpl-type')).removeClass('hidden');
  190. var values = {};
  191. for (var k in response.fields) {
  192. var field = response.fields[k];
  193. values[field.column] = {id :k, label : field.label};
  194. if(field.childs){
  195. values[field.column].childs = {};
  196. for (var u in field.childs) {
  197. var child = field.childs[u];
  198. child.id = k+'.'+u;
  199. values[field.column].childs[child.column] = child;
  200. }
  201. }
  202. }
  203. relatedField.attr('data-values',JSON.stringify(values))
  204. init_components(line);
  205. });
  206. }
  207. //Lors de la selection d'un attirbut d'entité, vérifie qu'il n'existe pas
  208. // d'entité liée, si c'est le cas, ça affiche ses attributs en sous liste
  209. function import_check_foreign_attribute(element){
  210. var select = $(element);
  211. var line = $(select).closest('tr');
  212. if(!select.find('option:selected').hasAttr('data-link')){
  213. line.find('.entitySubField').addClass('hidden');
  214. return;
  215. }
  216. var html = '';
  217. if(!window.importLinks || !window.importLinks[select.val()]) return;
  218. var childs = window.importLinks[select.val()];
  219. for(var k in childs){
  220. var field = childs[k];
  221. html += '<option value="'+field.column+'">'+field.label+'</option>';
  222. }
  223. line.find('.entitySubField').removeClass('hidden').html(html);
  224. }
  225. //Ajout ou modification line d\'import mappée
  226. function import_mapping_save(element){
  227. var data = $(element).closest('tr').toJson();
  228. data.action = 'import_mapping_save';
  229. data.import = $.urlParam('id');
  230. $.action(data,function(r){
  231. $.message('success','Enregistré');
  232. });
  233. }
  234. function import_mapping_add(){
  235. var data = {action:'import_mapping_save'};
  236. data.import = $.urlParam('id');
  237. $.action(data,function(r){
  238. import_mapping_search();
  239. });
  240. }
  241. //Récuperation ou edition line d\'import mappée
  242. function import_mapping_edit(element){
  243. var line = $(element).closest('.item-line');
  244. $.action({
  245. action: 'import_mapping_edit',
  246. id: line.attr('data-id')
  247. },function(r){
  248. $('#import-mapping-form').fromJson(r);
  249. init_components('#import-mapping-form');
  250. $('#import-mapping-form').attr('data-id',r.id);
  251. });
  252. }
  253. //Suppression line d\'import mappée
  254. function import_mapping_delete(element){
  255. if(!confirm('Êtes vous sûr de vouloir supprimer cet/ces item(s) ?')) return;
  256. if(element!='all'){
  257. var line = $(element).closest('.item-line');
  258. var id = line.attr('data-id');
  259. }else{
  260. id = element;
  261. }
  262. $.action({
  263. action: 'import_mapping_delete',
  264. id: id,
  265. import: $.urlParam('id'),
  266. },function(r){
  267. if(line){
  268. line.remove();
  269. }else{
  270. $('#import-mappings .item-line:not(:eq(0))').remove();
  271. }
  272. $.message('info','Item(s) supprimé(s)');
  273. });
  274. }
  275. function import_execute(){
  276. $('.btn-import').addClass('btn-preloader');
  277. $.action({
  278. action : 'import_execute',
  279. id : $.urlParam('id')
  280. },function(r){
  281. $.message('info','Import terminé');
  282. var statsList = $('.import-stats-list');
  283. statsList.find('>li').remove();;
  284. var statsListTemplate = $('template',statsList).html();
  285. for(var slug in r.stats){
  286. var line = $(Mustache.render(statsListTemplate,r.stats[slug]));
  287. statsList.append(line);
  288. }
  289. $('.import-excel-table-container').removeClass('hidden');
  290. var table = $('.import-excel-table');
  291. if(r.stats.imported == r.stats.total){
  292. table.addClass('hidden');
  293. }else{
  294. table.removeClass('hidden');
  295. }
  296. table.find('.title-head tr th').html(r.fileName);
  297. var columnTemplate = $('thead template',table).html();
  298. var lineTemplate = $('tbody template:eq(0)',table).html();
  299. var columnsTemplate = $('tbody template:eq(1)',table).html();
  300. $('thead:eq(0) tr th',table).attr('colspan',r.columns.length+1);
  301. for(var i in r.columns){
  302. $('thead:eq(1) tr',table).append($(Mustache.render(columnTemplate,r.columns[i])));
  303. }
  304. table.find('.title-head tr th').attr("colspan",$('thead:eq(1) tr th',table).length);
  305. for(var i in r.lines){
  306. var line = r.lines[i];
  307. line.lineNumber = i;
  308. var tr = $(Mustache.render(lineTemplate,line));
  309. for(var u in line.columns){
  310. var td = $(Mustache.render(columnsTemplate,line.columns[u]));
  311. tr.append(td);
  312. }
  313. $('tbody',table).append(tr);
  314. }
  315. init_components($('#file').parent());
  316. });
  317. }