Skip to content

Commit

Permalink
Merge pull request #85 from TheDMSGroup/ENG-187b
Browse files Browse the repository at this point in the history
[ENG-187] export buttons / date sortingon datatables
  • Loading branch information
heathdutton authored Jun 21, 2018
2 parents c2c3789 + e226c47 commit a9fa4f0
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 11 deletions.
78 changes: 71 additions & 7 deletions Assets/build/contactclient_events_datatable.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,81 @@ 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: '<<lBf>rtip>',
buttons: [
'excelHtml5',
'csvHtml5'
],
columnDefs: [
{
"targets": [ 0 ],
"visible": false,
"searchable": false
{
"targets": [ 0 ],
"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('<tfoot></tfoot>');
var tr = mQuery('<tr class=\'pageTotal\' style=\'font-weight: 600; background: #fafafa;\'></tr>');
var tr2 = mQuery('<tr class=\'grandTotal\' style=\'font-weight: 600; background: #fafafa;\'></tr>');
tr.append(mQuery('<td colspan=\'1\'>Page totals</td>'));
tr2.append(mQuery('<td colspan=\'1\'>Grand totals</td>'));
for (var i = 2; i < columns; i++) {
tr.append(mQuery('<td class=\'td-right\'></td>'));
tr2.append(mQuery('<td class=\'td-right\'></td>'));
}
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
});//getScriptsCachedOnce - datatables css
}); //getScriptsCachedOnce - datatables js
Expand Down
38 changes: 35 additions & 3 deletions Controller/ContactClientController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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;
Expand Down Expand Up @@ -253,7 +255,7 @@ protected function getUpdateSelectParams(
return $options;
}

protected function convertChartStatsToDatatable($stats)
protected function convertChartStatsToDatatable($stats, $unit)
{
$tableData = [
'labels' => [],
Expand All @@ -262,12 +264,42 @@ 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);
$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': // 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;
}
$row[$key][0] = $key;
$row[$key][1] = $dateStr;
$row[$key][$column + 2] = $data;
Expand Down
2 changes: 1 addition & 1 deletion Model/ContactClientModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit a9fa4f0

Please sign in to comment.