main.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. var editor = null;
  2. function init_plugin_statistic(){
  3. if($.urlParam('page')=='report'){
  4. stats_search_widget();
  5. $('.grid-stack').gridstack({
  6. resizable: {
  7. handles: 'e, se, s, sw, w'
  8. },
  9. animate: true,
  10. });
  11. }
  12. if($.urlParam('page')=='edit_report')
  13. stats_report_search_filter(function(){});
  14. $('.module-statistic.section-report').click(function(){
  15. $('.grid-stack-item-content').removeClass('active');
  16. });
  17. }
  18. function stats_select_widget(event,element){
  19. $('.grid-stack-item-content').removeClass('active');
  20. $(element).addClass('active');
  21. event.stopPropagation();
  22. }
  23. function stats_select_properties_tab(){
  24. $("#widgetTab .nav-item").addClass('hidden');
  25. $("#widgetTab .nav-link").removeClass('active show');
  26. $("#widgetTabContent .tab-pane").removeClass('active show');
  27. $("#widgetPropertiesTab").removeClass('hidden');
  28. $("#properties-tab").addClass('active show');
  29. $("#properties-panel").addClass('active show');
  30. }
  31. function stats_print_report(){
  32. $('.widgetBlock').addClass('widgetBlock-print');
  33. $('.widgetBlock').each(function(i,element){
  34. var widget = $(element);
  35. var canvas = $('canvas',widget).get(0);
  36. if(!canvas) return;
  37. var url = canvas.toDataURL('image/png');
  38. $('.grid-stack-item-content',widget).append('<img src="'+url+'">');
  39. });
  40. $('.widgetBlock canvas').addClass('hidden');
  41. $('#gridtack').removeClass('grid-stack');
  42. $('.widgetBlock').removeClass('grid-stack-item');
  43. setTimeout(function(){
  44. window.print();
  45. $('#gridtack').addClass('grid-stack');
  46. $('.widgetBlock').addClass('grid-stack-item');
  47. $('.widgetBlock').each(function(i,element){
  48. var widget = $(element);
  49. $('.widgetBlock canvas').removeClass('hidden');
  50. $('.grid-stack-item-content img',widget).remove();
  51. $('.widgetBlock').removeClass('widgetBlock-print');
  52. });
  53. },500);
  54. }
  55. function stats_export_view_table(element){
  56. var widget = $(element).closest('.widgetBlock');
  57. var id = widget.attr('data-gs-id');
  58. //si appellé depuis la modale d'édition du widget
  59. if(!id) id = $('#widgetModal').attr('data-widget');
  60. var filters = stats_get_filters();
  61. var filterString = '';
  62. for(var key in filters){
  63. filterString +="&filters%5B"+key+"%5D="+filters[key];
  64. }
  65. window.location = 'action.php?action=stats_export_view_table'+filterString+'&id='+id;
  66. }
  67. function stats_save_widget_element(){
  68. var data = {};
  69. data.action = 'stats_save_widget_element';
  70. data.widget = $('#widget').data('id');
  71. data.label = $('#label').val();
  72. data.type = $('#type').val();
  73. data.id = $('#elements thead tr').attr('data-id');
  74. $.action(data,function(r){
  75. $('#label').val('');
  76. $('#elements thead tr').removeAttr('data-id');
  77. stats_search_widget_element();
  78. });
  79. }
  80. function stats_search_widget_element(callback){
  81. $('#elements').fill({
  82. action:'stats_search_widget_element',
  83. id: $('#widget').data('id'),
  84. },function(){
  85. if(callback != null)
  86. $('#elements tbody').find('td:visible:first').click();
  87. $('#elements tbody').sortable({
  88. axis: "y",
  89. helper: function (e, ui) {
  90. ui.children().each(function() {
  91. $(this).width($(this).width());
  92. });
  93. return ui;
  94. },
  95. handle: ".btn-element-sort",
  96. //
  97. update: function( event, ui ){
  98. var sort = [];
  99. $('#elements tbody tr:visible').each(function(){
  100. sort.push({
  101. id : $(this).attr('data-id'),
  102. type : $(this).attr('data-type'),
  103. });
  104. });
  105. $.action({
  106. action:'stats_widget_element_sort',
  107. sort: sort
  108. });
  109. }
  110. });
  111. });
  112. }
  113. function stats_select_widget_line(){
  114. var line = $('#elements tbody tr').eq(1);
  115. line.addClass('active');
  116. stats_edit_widget_element(line);
  117. }
  118. function stats_save_widget(){
  119. var data = {};
  120. data.action = 'stats_update_widget';
  121. data.id = $('#widget').data('id');
  122. data.label = $('#widgetLabel').val();
  123. $.action(data,function(r){
  124. $('#widgetPropertiesTitle').html('Widget : ');
  125. $('#widgetProperties').html(r.label);
  126. });
  127. }
  128. //filter
  129. function stats_get_filters(){
  130. var data = {};
  131. $('#filter-form input,select:visible,textarea').each(function(i,input){
  132. var slug = $(input).attr('data-slug');
  133. if($(input).hasClass('data-type-user') ) return ;
  134. if (slug=='' || !slug) return;
  135. data[slug] = $(input).val();
  136. });
  137. return data;
  138. }
  139. function stats_report_import(){
  140. $.action({
  141. action : 'stats_report_import',
  142. file : $('#import').get(0).files
  143. },function(r){
  144. document.location.reload(true);
  145. });
  146. }
  147. function stats_report_search_filter(){
  148. $('#filterTable').fill({
  149. action:'stats_report_search_filter',
  150. report:$('#report').attr('data-id')
  151. },function(){
  152. $('#filterTable tbody tr:visible').each(function(i,element){
  153. $(element).find('[data-tpl-type="right"]').attr('data-type',"right");
  154. });
  155. init_components();
  156. $('#filterTable tbody').sortable({
  157. /*items: 'tr',*/
  158. handle : '.btn-move',
  159. placeholder: "navigation-sortable-placeholder",
  160. axis: "y",
  161. cursor: "move",
  162. stop : function(e, ui){
  163. var sort = [];
  164. $('#filterTable tr:visible').each(function(i,element){
  165. if(!$(this).attr('data-id') || $(this).attr('data-id')=='') return;
  166. sort.push($(this).attr('data-id'));
  167. });
  168. $.action({
  169. action : 'stats_report_move_filter',
  170. sort : sort
  171. });
  172. },
  173. //handle: ".navigation-template-item"
  174. });
  175. });
  176. }
  177. function stats_add_filter(){
  178. var data = $.getForm('#filter-form');
  179. data.id = $('#filter-form').attr('data-id');
  180. data.action = 'stats_save_filter';
  181. data.report = $('#report').attr('data-id');
  182. data.label = data.filterLabel;
  183. data.datasource = data.filterDataSource;
  184. data.type = data.filterType;
  185. data.default = data.filterDefault;
  186. //data.id = data.filterDefault;
  187. if(data.report==''){
  188. alert('Veuillez enregistrer le rapport avant d\'ajouter des filtres');
  189. return;
  190. }
  191. $.action(data,function(r){
  192. $('#filter-form').removeAttr('data-id').find('input').val('');
  193. $('#filter-form select').prop('selectedIndex',0);
  194. stats_report_search_filter();
  195. });
  196. }
  197. function stats_delete_filter(element, event){
  198. if(!confirm('Êtes-vous sûr de vouloir supprimer ce filtre ?')) return;
  199. event.stopPropagation();
  200. var data = {};
  201. var line = $(element).closest('tr');
  202. data.action = 'stats_delete_filter';
  203. data.id = line.attr('data-id');
  204. $.action(data,function(r){
  205. line.remove();
  206. });
  207. }
  208. function stats_change_filter_type(){
  209. $('.filter-source-container').addClass('hidden');
  210. if($('#filterType option:selected').attr('data-source'))
  211. $('.filter-source-container').removeClass('hidden');
  212. $('#filterDefault').attr('placeholder',$('#filterType option:selected').attr('data-placeholder'));
  213. }
  214. function stats_edit_filter(element){
  215. var data = {};
  216. var line = $(element).closest('tr');
  217. data.action = 'stats_edit_filter';
  218. data.id = line.attr('data-id');
  219. $.action(data,function(r){
  220. $.setForm('#filter-form',{ filterLabel : r.label ,filterType : r.type ,filterDefault : r.default,filterDataSource : r.datasource });
  221. $('#filter-form').attr('data-id',r.id);
  222. stats_change_filter_type();
  223. });
  224. }
  225. //report
  226. function stats_save_report(){
  227. $.action({
  228. action : 'stats_save_report',
  229. label : $('#label').val(),
  230. report : $('#report').attr('data-id'),
  231. row : $('#row').val(),
  232. column : $('#column').val(),
  233. icon : $('#icon').val(),
  234. color : $('#color').val()
  235. },function(r){
  236. $.urlParam('id',r.id);
  237. $('.btn-open').attr('href',"index.php?module=statistic&page=report&id="+r.id);
  238. $('#report').attr('data-id',r.id);
  239. });
  240. }
  241. function stats_search_widget(){
  242. var report = $('#report').attr('data-id');
  243. $.action({
  244. action : 'stats_search_widget',
  245. report : report
  246. },function(r){
  247. var grid = $('.grid-stack').data('gridstack');
  248. grid.removeAll();
  249. if(r.widgets.length==0){
  250. $('#filter-form').addClass('hidden');
  251. $('.no-data').removeClass('hidden');
  252. }else{
  253. $('.no-data').addClass('hidden');
  254. }
  255. for(var key in r.widgets){
  256. var widget = r.widgets[key];
  257. var grid = $('.grid-stack').data('gridstack');
  258. var tpl = $($('#widgetTemplate').html());
  259. if(widget.content) $('.grid-stack-item-content',tpl).html(widget.content);
  260. if(widget.background) $('.grid-stack-item-content',tpl).css('background',widget.background);
  261. if(!widget.edit){
  262. $('.btn-widget-edit',tpl).addClass('hidden');
  263. $('.grid-stack-item-content',tpl).removeAttr('ondblclick').removeAttr('onclick');
  264. }
  265. if(!widget.delete) $('.btn-widget-delete',tpl).addClass('hidden');
  266. if(!widget.edit) $(tpl).attr('data-gs-no-move','yes').attr('data-gs-no-resize','yes');
  267. var element = grid.addWidget(tpl, widget.x, widget.y, widget.width, widget.height, false,3,12,3,12,widget.id);
  268. }
  269. stats_refresh_widget_content();
  270. $('.grid-stack').on('change', function(event, items) {
  271. var data = {
  272. action : 'stats_save_widget',
  273. report : $('#report').attr('data-id'),
  274. widgets : []
  275. };
  276. for(var i in items){
  277. data.widgets.push({
  278. id : $(items[i].el[0]).attr('data-gs-id'),
  279. x : items[i].x,
  280. y : items[i].y,
  281. width : items[i].width,
  282. height : items[i].height
  283. });
  284. }
  285. $.action(data,function(response){
  286. for(var i in items){
  287. if(!items[i].id)
  288. $(items[i].el[0]).attr('data-gs-id',response.lastId);
  289. }
  290. });
  291. });
  292. $(document).keyup(function(e){
  293. if($('body').hasClass('modal-open')) return;
  294. if(e.keyCode==46 && $('.module-statistic .grid-stack-item-content.active').length)
  295. stats_delete_widget();
  296. });
  297. $('#add-widget').click(function() {
  298. $('.no-data').addClass('hidden');
  299. var grid = $('.grid-stack').data('gridstack');
  300. var widget = $($('#widgetTemplate').html());
  301. grid.addWidget(widget, 0, 0, 3, 3, true,3,12,3,12);
  302. });
  303. });
  304. }
  305. function stats_refresh_widget_content(){
  306. var report = $('#report').attr('data-id');
  307. $('.btn-process').addClass('btn-preloader');
  308. $.action({
  309. action : 'stats_refresh_widget_content',
  310. report : report,
  311. filters : stats_get_filters(),
  312. defaultSuccess : false
  313. },function(r){
  314. for(var key in r.widgets){
  315. var widget = r.widgets[key];
  316. //var container = $('.widgetBlock[data-gs-x="'+widget.x+'"][data-gs-y="'+widget.y+'"]');
  317. var container = $('.widgetBlock[data-gs-id="'+widget.id+'"]');
  318. container.find('.grid-stack-item-content').css('background-color','#ffffff').html(widget.html);
  319. }
  320. $('.dataTables_info').addClass('hidden');
  321. init_components();
  322. });
  323. }
  324. function stats_select_widget_view(element){
  325. if(typeof(element) !== 'undefined'){
  326. $('#view li').removeClass('selected');
  327. $(element).addClass('selected');
  328. }
  329. $.action({
  330. action : 'stats_update_widget',
  331. id : $('#widgetModal').attr('data-widget'),
  332. view : $('#view li.selected').attr('data-uid')
  333. },function(r){
  334. $.action({
  335. action : 'stats_preview_view',
  336. filters : stats_get_filters(),
  337. widget : $('#widgetModal').attr('data-widget'),
  338. view : $('#view li.selected').attr('data-uid'),
  339. defaultSuccess : false
  340. },function(r){
  341. $("#widgetTab .nav-link").removeClass('active show');
  342. $("#widgetTabContent .tab-pane:not('.workspace .tab-pane')").removeClass('active show');
  343. $("#widgetTab .nav-item").not("#widgetPropertiesTab").removeClass('hidden');
  344. $("#preview-tab").addClass('active show');
  345. $("#preview-panel").addClass('active show');
  346. $('#preview-panel').html(r.html);
  347. $('.dataTables_info').addClass('hidden');
  348. init_components();
  349. },function(r){
  350. $("#widgetTab .nav-link").removeClass('active show');
  351. $("#widgetTabContent .tab-pane:not('.workspace .tab-pane')").removeClass('active show');
  352. $("#widgetTab .nav-item").not("#widgetPropertiesTab").removeClass('hidden');
  353. $("#preview-tab").addClass('active show');
  354. $("#preview-panel").addClass('active show');
  355. $('#preview-panel').html('<code class="error-code">'+r.error+'</code><hr><pre class="error-trace">'+r.trace+'</pre>');
  356. });
  357. });
  358. }
  359. function stats_properties_load(){
  360. $.action({
  361. action : 'stats_properties_load',
  362. id : $('#widget').attr('data-id')
  363. },function(r){
  364. $('.properties-block').html(r.html);
  365. init_components('.properties-block');
  366. });
  367. }
  368. function stats_properties_save(){
  369. var data = $('#properties-form').toJson();
  370. data.action = 'stats_properties_save';
  371. data.id = $('#widget').attr('data-id');
  372. $.action(data,function(r){
  373. $('.properties-block').html(r.html);
  374. $.message('success','Enregistré');
  375. });
  376. }
  377. function stats_rename_widget_element(element,event){
  378. event.stopPropagation();
  379. var line = $(element).closest('tr');
  380. $.action({
  381. action : 'stats_rename_widget_element',
  382. type : line.attr('data-type'),
  383. id : line.attr('data-id')
  384. },function(r){
  385. $.setForm('#elements thead tr',r);
  386. $('#elements thead tr').attr('data-id',r.id);
  387. });
  388. }
  389. function stats_delete_widget_element(element,event){
  390. event.stopPropagation();
  391. if(!confirm('Êtes vous sûr de vouloir supprimer cet item ?')) return;
  392. var line = $(element).closest('tr');
  393. $.action({
  394. action : 'stats_delete_widget_element',
  395. type : line.attr('data-type'),
  396. id : line.attr('data-id')
  397. },function(r){
  398. $('#elements tbody').find('td:visible:first').click();
  399. line.remove();
  400. });
  401. }
  402. function stats_edit_widget_element(element){
  403. var line = $(element);
  404. $('#elements tr').removeClass('active');
  405. line.addClass('active');
  406. var id = line.attr('data-id');
  407. var type = line.attr('data-type');
  408. var editorOutput = $('#editorOutput');
  409. $.action({
  410. action : 'stats_edit_widget_element',
  411. type : type,
  412. defaultSuccess : false,
  413. id : id,
  414. filters : stats_get_filters()
  415. },function(r){
  416. var editPanel = $("#edit-panel");
  417. var widgetTabContainer = $("#widgetTab");
  418. $(".nav-link", widgetTabContainer).removeClass('active show');
  419. $("#widgetTabContent .tab-pane").removeClass('active show');
  420. $(".nav-item", widgetTabContainer).not("#widgetPropertiesTab").removeClass('hidden');
  421. $("#edit-tab").addClass('active show');
  422. editPanel.addClass('active show');
  423. editPanel.html(r.edit);
  424. $('#workspace').attr('data-type',type).attr('data-id',id);
  425. if(r.script){
  426. eval('var script = '+r.script);
  427. script();
  428. }
  429. editorOutput.removeAttr('style').html('<code class="success-code">Requête exécutée avec succès</code><hr><pre>'+r.data['data']+'</pre>');
  430. if(r.meta){
  431. for(var k in r.meta){
  432. editorOutput.append('<h5 class="text-warning">'+k+'</h5><pre>'+r.meta[k]+'</pre>');
  433. }
  434. }
  435. $('#output-panel').removeAttr('style').html('<code class="success-code">Requête exécutée avec succès</code><hr><div><label><i class="far fa-eye"></i> Données en sortie</label></div><pre>'+r.data+'</pre>');
  436. },function(r){
  437. var editPanel = $("#edit-panel");
  438. var widgetTabContainer = $("#widgetTab");
  439. $(".nav-link", widgetTabContainer).removeClass('active show');
  440. $("#widgetTabContent .tab-pane").removeClass('active show');
  441. $(".nav-item", widgetTabContainer).not("#widgetPropertiesTab").removeClass('hidden');
  442. $("#edit-tab").addClass('active show');
  443. editPanel.addClass('active show');
  444. editPanel.html(r.edit);
  445. $('#workspace').attr('data-type',type).attr('data-id',id);
  446. if(window[r.script]!=null) window[r.script]();
  447. $('#editorOutput').html('<code class="error-code">'+r.error+'</code><hr><pre>'+r.trace+'</pre>');
  448. $('#output-panel').html('<code class="error-code">'+r.error+'</code><hr><pre>'+r.trace+'</pre>');
  449. });
  450. }
  451. function stats_edit_widget_content(element){
  452. var cell = $(element).closest('.widgetBlock');
  453. var report = $('#report').attr('data-id');
  454. var modal = $('#widgetModal');
  455. var id = cell.attr('data-gs-id');
  456. $.action({
  457. action : 'stats_edit_widget_content',
  458. id : cell.attr('data-gs-id')
  459. },function(r){
  460. modal.modal('show');
  461. modal.attr('data-widget',cell.attr('data-gs-id'));
  462. $('.modal-body', modal).html(r.content);
  463. modal.on('shown.bs.modal', function () {
  464. $('.stats-element-panel').panelResize({
  465. minWidth : 300
  466. });
  467. });
  468. stats_search_widget_element(true);
  469. $('#preview-tab').on('click', function(){
  470. stats_select_widget_view();
  471. });
  472. });
  473. }
  474. function synonyms(cm, option) {
  475. return new Promise(function(accept) {
  476. setTimeout(function() {
  477. var cursor = cm.getCursor(), line = cm.getLine(cursor.line);
  478. var start = cursor.ch, end = cursor.ch;
  479. while (start && /\w/.test(line.charAt(start - 1))) --start;
  480. while (end < line.length && /\w/.test(line.charAt(end))) ++end;
  481. var word = line.slice(start, end).toLowerCase();
  482. for (var i = 0; i < comp.length; i++) if (comp[i].indexOf(word) != -1)
  483. return accept({
  484. list: comp[i],
  485. from: CodeMirror.Pos(cursor.line, start),
  486. to: CodeMirror.Pos(cursor.line, end)
  487. });
  488. return accept(null);
  489. }, 100)
  490. });
  491. }
  492. function stats_element_preview(element,callback){
  493. var editorOutput = $('#editorOutput');
  494. var output = $('#output-panel');
  495. editorOutput.css('text-align', 'center').html('<i class="fas fa-sync-alt fa-spin"></i>');
  496. output.css('text-align', 'center').html('<i class="fas fa-sync-alt fa-spin"></i>');
  497. if(element) $(element).addClass('btn-preloader');
  498. $.action({
  499. action : 'stats_element_preview',
  500. defaultSuccess : false,
  501. type : $('#workspace').attr('data-type'),
  502. filters : stats_get_filters(),
  503. id : $('#workspace').attr('data-id'),
  504. },function(r){
  505. $('#tab-tables').removeClass('active show');
  506. $('#tab-results').addClass('active show');
  507. $('.tools-area').find('a').removeClass('active');
  508. $('.tools-area').find('a:first').addClass('active');
  509. editorOutput.removeAttr('style').html('<code class="success-code">Requête exécutée avec succès</code><hr><pre>'+r.data+'</pre>');
  510. if(r.meta){
  511. for(var k in r.meta){
  512. editorOutput.append('<h5 class="text-warning">'+k+'</h5><pre>'+r.meta[k]+'</pre>');
  513. }
  514. }
  515. output.removeAttr('style').html('<code class="success-code">Requête exécutée avec succès</code><hr><div><label><i class="far fa-eye"></i> Données en sortie</label></div><pre>'+r.data+'</pre>');
  516. if(callback) callback();
  517. },function(r){
  518. $('#tab-tables').removeClass('active show');
  519. $('#tab-results').addClass('active show');
  520. $('.tools-area').find('a').removeClass('active');
  521. $('.tools-area').find('a:first').addClass('active');
  522. editorOutput.removeAttr('style').html('<code class="error-code">'+r.error+'</code><hr><pre class="mt-3">'+r.trace+'</pre>');
  523. output.removeAttr('style').html('<code class="error-code">'+r.error+'</code><hr><pre class="mt-3">'+r.trace+'</pre>');
  524. });
  525. }
  526. function stats_element_query_table_filter(input){
  527. var keyword = $(input).val().toLowerCase();
  528. clearInterval(window.queryTableFilter);
  529. window.queryTableFilter = setTimeout(function(){
  530. $('#stats-query-tables .stats-widget-element > li:not(:first)').each(function(){
  531. var line = $(this);
  532. if( line.attr('data-table').toLowerCase().indexOf(keyword) === -1 ){
  533. line.addClass('hidden');
  534. }else{
  535. line.removeClass('hidden');
  536. }
  537. });
  538. },500);
  539. }
  540. function stats_element_init(type){
  541. $(function(){
  542. var object = type=='query' ? '#query' : '#source'
  543. window.editor = CodeMirror.fromTextArea($(object).get(0), {
  544. mode: type=='query' ? 'text/x-mysql' : 'text/x-php',
  545. theme: 'monokai',
  546. lineWrapping: true,
  547. gutter: true,
  548. fixedGutter : true,
  549. lineNumbers: true,
  550. matchBrackets : true
  551. });
  552. editor.on("blur", function(){
  553. stats_element_content_save();
  554. });
  555. if(type=='query') stats_save_widget_connection();
  556. });
  557. }
  558. function stats_element_content_save(){
  559. var workspace = $('#workspace');
  560. var status = $('#server-status');
  561. if(window.editor.getValue()=='') return;
  562. $('#editor div.CodeMirror:visible').prepend($('<div id="save-overlay"><i class="fas fa-sync fa-spin fa-3x"></i></div>'));
  563. var overlay = $('#save-overlay');
  564. $.action({
  565. action :'stats_save_widget_element',
  566. type : workspace.attr('data-type'),
  567. id : workspace.attr('data-id'),
  568. sql : editor.getValue(),
  569. source : editor.getValue(),
  570. connection : $('#connection').val(),
  571. },function(r){
  572. overlay.remove();
  573. status.html('<code class="success-code"><i class="fas fa-check"></i> Modifications enregistrées</code>');
  574. setTimeout(function() { $('#server-status>*').addClass('hidden'); }, 3000);
  575. },function(r){
  576. overlay.remove();
  577. status.html('<code class="error-code"><i class="fas fa-times"></i> Modifications non enregistrées</code>');
  578. });
  579. }
  580. function stats_save_widget_connection(){
  581. $('.stats-table-tab > a').click();
  582. $.action({
  583. action: 'stats_save_widget_connection',
  584. id: $('#elements').find('tr.active[data-type="Query"]').attr('data-id'),
  585. connection: $('#connection').val()
  586. },function(){
  587. $('#stats-query-tables > ul').fill({
  588. action:'stats_search_query_tables',
  589. connection: $('#connection').val(),
  590. },function(){
  591. });
  592. });
  593. }
  594. function stats_search_query_columns(element,event){
  595. event.stopPropagation();
  596. var line = $(element).closest('li');
  597. var fieldBloc = $('ul',line);
  598. fieldBloc.toggleClass('hidden');
  599. if(!fieldBloc.hasClass('hidden')){
  600. $.action({
  601. action:'stats_search_query_columns',
  602. table:line.attr('data-table'),
  603. connection: $('#connection').val(),
  604. },function(r){
  605. fieldBloc.removeClass('hidden');
  606. var tpl = '<li data-column="{{column}}" onclick="stats_query_add_column(this,event)">{{column}} <small class="text-muted">{{type}}</small></li>';
  607. var html = '';
  608. for(var k in r.rows){
  609. var item = Mustache.render(tpl,r.rows[k]);
  610. html+=item;
  611. }
  612. fieldBloc.html(html);
  613. });
  614. }
  615. }
  616. function stats_query_add_table(element){
  617. var table = $('span',element).text();
  618. window.editor.setValue(window.editor.getValue()+table);
  619. stats_element_content_save();
  620. }
  621. function stats_query_add_column(element,event){
  622. event.stopPropagation();
  623. var column = $(element).attr('data-column');
  624. window.editor.setValue(window.editor.getValue()+column);
  625. stats_element_content_save();
  626. }
  627. function stats_delete_widget(element){
  628. if(!confirm('Êtes-vous sûr de vouloir supprimer ce widget ?')) return;
  629. if(!element) element = $('.grid-stack-item-content.active');
  630. var cell = $(element).closest('.widgetBlock');
  631. $.action({
  632. action : 'stats_delete_widget',
  633. id : cell.attr('data-gs-id'),
  634. },function(){
  635. var grid = $('.grid-stack').data('gridstack');
  636. grid.removeWidget(cell);
  637. if($('.widgetBlock:visible').length==0) $('.no-data').removeClass('hidden');
  638. });
  639. }
  640. function stats_export_report(){
  641. window.location = 'action.php?action=stats_export_report&id='+$('#report').attr('data-id');
  642. }
  643. function stats_delete_report(element){
  644. if(!confirm('Êtes-vous sûr de vouloir supprimer ce rapport ?')) return;
  645. window.location = 'action.php?action=stats_delete_report&id='+$(element).closest('.report-bloc').attr('data-id');
  646. }
  647. /** CONNECTION **/
  648. function init_setting_statistic(){
  649. search_connection();
  650. }
  651. // SEARCH
  652. function search_connection(callback){
  653. $('#connections').fill({
  654. action:'stats_search_connection'
  655. },function(){
  656. if(callback!=null) callback();
  657. });
  658. }
  659. // SAVE
  660. function stats_save_connection(account){
  661. var data = $('#connectionForm').toJson();
  662. data.account = account == true;
  663. data.id = $('#connectionForm').attr('data-id');
  664. data.meta = $('.stats-connection-info').toJson();
  665. $.action(data,function(r){
  666. $.message('success','Enregistré');
  667. if(account) return;
  668. $('#connectionForm input').val('');
  669. $('#connectionForm').attr('data-id','');
  670. search_connection();
  671. });
  672. }
  673. //change handler
  674. function stats_connection_handler_change(callback){
  675. $.action({action:'stats_connection_handler_search',
  676. handler:$('#handler').val()},function(r){
  677. $('.stats-connection-info').html(r.html);
  678. if(callback) callback();
  679. });
  680. }
  681. function stats_connection_test(element){
  682. var line = $(element).closest('tr');
  683. $('.btn-test',line).addClass('btn-preloader');
  684. $.action({action:'stats_connection_test',id:line.attr('data-id')},function(r){
  685. $.message('success',r.message);
  686. });
  687. }
  688. // EDIT
  689. function stats_edit_connection(element){
  690. var line = $(element).closest('tr');
  691. $.action({action:'stats_edit_connection',id:line.attr('data-id')},function(r){
  692. $.setForm('#connectionForm',r);
  693. stats_connection_handler_change(function(){
  694. $.setForm('#connectionForm',r.meta);
  695. });
  696. $('#connectionForm').attr('data-id',r.id);
  697. });
  698. }
  699. // DELETE
  700. function stats_delete_connection(element){
  701. if(!confirm('Êtes vous sûr de vouloir supprimer cet item?')) return;
  702. var line = $(element).closest('tr');
  703. $.action({
  704. action : 'stats_delete_connection',
  705. id : line.attr('data-id')
  706. },function(r){
  707. line.remove();
  708. });
  709. }