From d9f97063505ad4f2f7cc2d867d79e8cce238ec27 Mon Sep 17 00:00:00 2001 From: scottshipman Date: Thu, 21 Jun 2018 10:39:31 -0400 Subject: [PATCH 1/6] export buttons on datatables --- Assets/build/contactclient_events_datatable.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/Assets/build/contactclient_events_datatable.js b/Assets/build/contactclient_events_datatable.js index b82f5c07..014cc919 100644 --- a/Assets/build/contactclient_events_datatable.js +++ b/Assets/build/contactclient_events_datatable.js @@ -16,14 +16,20 @@ Mautic.loadContactClientEventsDatatable = function (tableData) { bSort : false, order: [0, 'asc'], bLengthChange: true, + dom: '<frtip>', + buttons: [ + 'excelHtml5', + 'csvHtml5' + ], columnDefs: [ - { - "targets": [ 0 ], - "visible": false, - "searchable": false - } - ] + { + "targets": [ 0 ], + "visible": false, + "searchable": false + } + ] }); + mQuery('#contactClientEventsTable_wrapper .dt-buttons').css({float: "right", marginLeft: "10px"}); }); //getScriptsCachedOnce - fonteawesome css });//getScriptsCachedOnce - datatables css }); //getScriptsCachedOnce - datatables js From c32dfec1d63818eb735151df008f9f38bcab4da9 Mon Sep 17 00:00:00 2001 From: scottshipman Date: Thu, 21 Jun 2018 11:04:15 -0400 Subject: [PATCH 2/6] add table summary --- .../build/contactclient_events_datatable.js | 62 ++++++++++++++++++- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/Assets/build/contactclient_events_datatable.js b/Assets/build/contactclient_events_datatable.js index 014cc919..8e9e5465 100644 --- a/Assets/build/contactclient_events_datatable.js +++ b/Assets/build/contactclient_events_datatable.js @@ -16,7 +16,7 @@ Mautic.loadContactClientEventsDatatable = function (tableData) { bSort : false, order: [0, 'asc'], bLengthChange: true, - dom: '<frtip>', + dom: '<rtip>', buttons: [ 'excelHtml5', 'csvHtml5' @@ -27,7 +27,65 @@ Mautic.loadContactClientEventsDatatable = function (tableData) { "visible": false, "searchable": false } - ] + ], + footerCallback: function (row, data, start, end, display) { + if (data && data.length === 0 || typeof data[0] === 'undefined') { + mQuery('#contactClientEventsTable').hide(); + return; + } + try { + // Add table footer if it doesnt exist + var container = mQuery('#contactClientEventsTable'); + var columns = data[0].length; + if (mQuery('tr.pageTotal').length === 0) { + var footer = mQuery(''); + var tr = mQuery(''); + var tr2 = mQuery(''); + tr.append(mQuery('Page totals')); + tr2.append(mQuery('Grand totals')); + for (var i = 2; i < columns; i++) { + tr.append(mQuery('')); + tr2.append(mQuery('')); + } + footer.append(tr); + footer.append(tr2); + container.append(footer); + } + + var api = this.api(); + + // Remove the formatting to get + // integer data for summation + var intVal = function (i) { + return typeof i === 'string' ? i.replace(/[\$,]/g, '') * 1 : typeof i === 'number' ? i : 0; + }; + + var total = mQuery('#' + container[0].id + ' thead th').length; + var footer1 = mQuery(container).find('tfoot tr:nth-child(1)'); + var footer2 = mQuery(container).find('tfoot tr:nth-child(2)'); + for (var i = 2; i < total +1; i++) { + var pageSum = api + .column(i, {page: 'current'}) + .data() + .reduce(function (a, b) { + return intVal(a) + intVal(b); + }, 0); + var sum = api + .column(i) + .data() + .reduce(function (a, b) { + return intVal(a) + intVal(b); + }, 0); + footer1.find('td:nth-child(' + (i) + ')').html(pageSum); + footer2.find('td:nth-child(' + (i) + ')').html(sum); + } + mQuery('#global-builder-overlay').hide(); + + } + catch (e) { + console.log(e); + } + } // FooterCallback }); mQuery('#contactClientEventsTable_wrapper .dt-buttons').css({float: "right", marginLeft: "10px"}); }); //getScriptsCachedOnce - fonteawesome css From 03a1200f4fd44d96e80a27256bb6f22ddb282f9b Mon Sep 17 00:00:00 2001 From: scottshipman Date: Thu, 21 Jun 2018 12:16:28 -0400 Subject: [PATCH 3/6] reformat dates and turn sroting back on --- .../build/contactclient_events_datatable.js | 4 +-- Controller/ContactClientController.php | 27 ++++++++++++++++--- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/Assets/build/contactclient_events_datatable.js b/Assets/build/contactclient_events_datatable.js index 8e9e5465..4fa45283 100644 --- a/Assets/build/contactclient_events_datatable.js +++ b/Assets/build/contactclient_events_datatable.js @@ -13,8 +13,8 @@ Mautic.loadContactClientEventsDatatable = function (tableData) { data: tableData.data, autoFill: true, columns: tableData.labels, - bSort : false, - order: [0, 'asc'], + //bSort : false, + order: [1, 'asc'], bLengthChange: true, dom: '<rtip>', buttons: [ diff --git a/Controller/ContactClientController.php b/Controller/ContactClientController.php index 321bbbf5..a73eaa6e 100644 --- a/Controller/ContactClientController.php +++ b/Controller/ContactClientController.php @@ -148,6 +148,8 @@ public function customizeViewArguments($args, $view) /** @var \MauticPlugin\MauticContactClientBundle\Model\ContactClientModel $model */ $model = $this->getModel('contactclient'); + $unit = $model->getTimeUnitFromDateRange(new \DateTime($chartFilterForm->get('date_from')->getData()), + new \DateTime($chartFilterForm->get('date_to')->getData())); if (in_array($chartFilterForm->get('type')->getData(), ['All Events', null])) { $stats = $model->getStats( @@ -171,7 +173,7 @@ public function customizeViewArguments($args, $view) $args['viewParameters']['stats'] = $stats; $args['viewParameters']['events'] = $model->getEngagements($item); $args['viewParameters']['chartFilterForm'] = $chartFilterForm->createView(); - $args['viewParameters']['tableData'] = $this->convertChartStatsToDatatable($stats); + $args['viewParameters']['tableData'] = $this->convertChartStatsToDatatable($stats, $unit); } return $args; @@ -253,7 +255,7 @@ protected function getUpdateSelectParams( return $options; } - protected function convertChartStatsToDatatable($stats) + protected function convertChartStatsToDatatable($stats, $unit) { $tableData = [ 'labels' => [], @@ -262,12 +264,31 @@ protected function convertChartStatsToDatatable($stats) if (!empty($stats)) { $tableData['labels'][] = ['title' => 'RowNum']; - $tableData['labels'][] = ['title' => 'Date']; + $tableData['labels'][] = ['title' => 'Date (Y-m-d)']; $row =[]; foreach ($stats['datasets'] as $column => $dataset) { $tableData['labels'][] = ['title' => $dataset['label']]; foreach ($dataset['data'] as $key => $data) { $dateStr = $stats['labels'][$key]; + $date = null; + switch ($unit) + { + case 'd': // M j, y + $date = date_create_from_format('M j, y', $dateStr); + break; + case 'H': // M j ga + $date = date_create_from_format('M j ga', $dateStr); + break; + case 'm': // M j ga + $date = date_create_from_format('M Y', $dateStr); + break; + case 'Y': // M j ga + $date = date_create_from_format('Y', $dateStr); + break; + default: + break; + } + $dateStr = !empty($date) ? $date->format('Y-m-d') : date('Y-m-d', strtotime($dateStr)); $row[$key][0] = $key; $row[$key][1] = $dateStr; $row[$key][$column + 2] = $data; From 9e51e2246a3aeb7107fecab8701382359bdff9bf Mon Sep 17 00:00:00 2001 From: scottshipman Date: Thu, 21 Jun 2018 12:17:32 -0400 Subject: [PATCH 4/6] php-cs-fixer --- Controller/ContactClientController.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Controller/ContactClientController.php b/Controller/ContactClientController.php index a73eaa6e..c399581d 100644 --- a/Controller/ContactClientController.php +++ b/Controller/ContactClientController.php @@ -148,7 +148,7 @@ public function customizeViewArguments($args, $view) /** @var \MauticPlugin\MauticContactClientBundle\Model\ContactClientModel $model */ $model = $this->getModel('contactclient'); - $unit = $model->getTimeUnitFromDateRange(new \DateTime($chartFilterForm->get('date_from')->getData()), + $unit = $model->getTimeUnitFromDateRange(new \DateTime($chartFilterForm->get('date_from')->getData()), new \DateTime($chartFilterForm->get('date_to')->getData())); if (in_array($chartFilterForm->get('type')->getData(), ['All Events', null])) { @@ -270,9 +270,8 @@ protected function convertChartStatsToDatatable($stats, $unit) $tableData['labels'][] = ['title' => $dataset['label']]; foreach ($dataset['data'] as $key => $data) { $dateStr = $stats['labels'][$key]; - $date = null; - switch ($unit) - { + $date = null; + switch ($unit) { case 'd': // M j, y $date = date_create_from_format('M j, y', $dateStr); break; @@ -288,7 +287,7 @@ protected function convertChartStatsToDatatable($stats, $unit) default: break; } - $dateStr = !empty($date) ? $date->format('Y-m-d') : date('Y-m-d', strtotime($dateStr)); + $dateStr = !empty($date) ? $date->format('Y-m-d') : date('Y-m-d', strtotime($dateStr)); $row[$key][0] = $key; $row[$key][1] = $dateStr; $row[$key][$column + 2] = $data; From 6c86b551de7b9070b466b8e469ef0619927d0d4f Mon Sep 17 00:00:00 2001 From: scottshipman Date: Thu, 21 Jun 2018 14:09:16 -0400 Subject: [PATCH 5/6] custom date formats fr sorting --- Controller/ContactClientController.php | 16 ++++++++++++++-- Model/ContactClientModel.php | 2 +- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/Controller/ContactClientController.php b/Controller/ContactClientController.php index c399581d..4e140b10 100644 --- a/Controller/ContactClientController.php +++ b/Controller/ContactClientController.php @@ -274,20 +274,32 @@ protected function convertChartStatsToDatatable($stats, $unit) switch ($unit) { case 'd': // M j, y $date = date_create_from_format('M j, y', $dateStr); + $dateStr = $date->format('Y-m-d'); break; case 'H': // M j ga $date = date_create_from_format('M j ga', $dateStr); + $dateStr = $date->format('Y-m-d - H:00'); + $tableData['labels'][1] = ['title' => 'Date/Time']; break; case 'm': // M j ga $date = date_create_from_format('M Y', $dateStr); + $dateStr = $date->format('Y-m'); + $tableData['labels'][1] = ['title' => 'Date (Y-m)']; break; - case 'Y': // M j ga + case 'Y': // Y $date = date_create_from_format('Y', $dateStr); + $dateStr = $date->format('Y'); + $tableData['labels'][1] = ['title' => 'Year']; + break; + case 'W': // W + $date = new \DateTime(); + $date->setISODate(date("Y"), str_replace("Week ", "", $dateStr)); + $dateStr = "Week " .$date->format('W'); + $tableData['labels'][1] = ['title' => 'Week #']; break; default: break; } - $dateStr = !empty($date) ? $date->format('Y-m-d') : date('Y-m-d', strtotime($dateStr)); $row[$key][0] = $key; $row[$key][1] = $dateStr; $row[$key][$column + 2] = $data; diff --git a/Model/ContactClientModel.php b/Model/ContactClientModel.php index 31198a47..3b1bd7c4 100644 --- a/Model/ContactClientModel.php +++ b/Model/ContactClientModel.php @@ -392,7 +392,7 @@ public function getTimeUnitFromDateRange($dateFrom, $dateTo) if ($dayDiff > 31) { $unit = 'W'; } - if ($dayDiff > 100) { + if ($dayDiff > 63) { $unit = 'm'; } if ($dayDiff > 1000) { From e226c47382ff63445148d3a2e164c76d261881d2 Mon Sep 17 00:00:00 2001 From: scottshipman Date: Thu, 21 Jun 2018 15:32:45 -0400 Subject: [PATCH 6/6] php cs fixer --- Controller/ContactClientController.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Controller/ContactClientController.php b/Controller/ContactClientController.php index 4e140b10..a45a1234 100644 --- a/Controller/ContactClientController.php +++ b/Controller/ContactClientController.php @@ -273,28 +273,28 @@ protected function convertChartStatsToDatatable($stats, $unit) $date = null; switch ($unit) { case 'd': // M j, y - $date = date_create_from_format('M j, y', $dateStr); + $date = date_create_from_format('M j, y', $dateStr); $dateStr = $date->format('Y-m-d'); break; case 'H': // M j ga - $date = date_create_from_format('M j ga', $dateStr); - $dateStr = $date->format('Y-m-d - H:00'); + $date = date_create_from_format('M j ga', $dateStr); + $dateStr = $date->format('Y-m-d - H:00'); $tableData['labels'][1] = ['title' => 'Date/Time']; break; case 'm': // M j ga - $date = date_create_from_format('M Y', $dateStr); - $dateStr = $date->format('Y-m'); + $date = date_create_from_format('M Y', $dateStr); + $dateStr = $date->format('Y-m'); $tableData['labels'][1] = ['title' => 'Date (Y-m)']; break; case 'Y': // Y - $date = date_create_from_format('Y', $dateStr); - $dateStr = $date->format('Y'); + $date = date_create_from_format('Y', $dateStr); + $dateStr = $date->format('Y'); $tableData['labels'][1] = ['title' => 'Year']; break; case 'W': // W $date = new \DateTime(); - $date->setISODate(date("Y"), str_replace("Week ", "", $dateStr)); - $dateStr = "Week " .$date->format('W'); + $date->setISODate(date('Y'), str_replace('Week ', '', $dateStr)); + $dateStr = 'Week '.$date->format('W'); $tableData['labels'][1] = ['title' => 'Week #']; break; default: