Removes option to group by lists during export

This commit is contained in:
Vlad
2018-02-16 10:36:12 -05:00
committed by pavel-mailpoet
parent e0052bbb93
commit 0d69b05ac0
5 changed files with 17 additions and 58 deletions

View File

@@ -17,7 +17,6 @@ define(
jQuery(document).ready(function () { // eslint-disable-line func-names jQuery(document).ready(function () { // eslint-disable-line func-names
var segmentsContainerElement; var segmentsContainerElement;
var subscriberFieldsContainerElement; var subscriberFieldsContainerElement;
var groupBySegmentOptionElement;
var nextStepButton; var nextStepButton;
var renderSegmentsAndFields; var renderSegmentsAndFields;
var subscribersExportTemplate; var subscribersExportTemplate;
@@ -43,7 +42,6 @@ define(
// define reusable variables // define reusable variables
segmentsContainerElement = jQuery('#export_lists'); segmentsContainerElement = jQuery('#export_lists');
subscriberFieldsContainerElement = jQuery('#export_columns'); subscriberFieldsContainerElement = jQuery('#export_columns');
groupBySegmentOptionElement = jQuery(':checkbox[name="option_group_by_list"]');
nextStepButton = jQuery('a.mailpoet_export_process'); nextStepButton = jQuery('a.mailpoet_export_process');
renderSegmentsAndFields = function (container, data) { // eslint-disable-line func-names renderSegmentsAndFields = function (container, data) { // eslint-disable-line func-names
if (container.data('select2')) { if (container.data('select2')) {
@@ -100,13 +98,6 @@ define(
else { else {
toggleNextStepButton('off'); toggleNextStepButton('off');
} }
if (segmentsContainerElement.select2('data').length > 1 && window.exportData.groupBySegmentOption) {
jQuery('.mailpoet_group_by_list').show();
}
else if (window.exportData.groupBySegmentOption) {
jQuery('.mailpoet_group_by_list').hide();
}
}); });
}; };
@@ -133,7 +124,6 @@ define(
action: 'processExport', action: 'processExport',
data: JSON.stringify({ data: JSON.stringify({
export_format_option: exportFormat, export_format_option: exportFormat,
group_by_segment_option: (groupBySegmentOptionElement.is(':visible')) ? groupBySegmentOptionElement.prop('checked') : false,
segments: (window.exportData.segments) ? segmentsContainerElement.val() : false, segments: (window.exportData.segments) ? segmentsContainerElement.val() : false,
subscriber_fields: subscriberFieldsContainerElement.val() subscriber_fields: subscriberFieldsContainerElement.val()
}) })

View File

@@ -14,7 +14,6 @@ use MailPoet\Util\XLSXWriter;
class Export { class Export {
public $export_format_option; public $export_format_option;
public $group_by_segment_option;
public $segments; public $segments;
public $subscribers_without_segment; public $subscribers_without_segment;
public $subscriber_fields; public $subscriber_fields;
@@ -30,7 +29,6 @@ class Export {
set_time_limit(0); set_time_limit(0);
} }
$this->export_format_option = $data['export_format_option']; $this->export_format_option = $data['export_format_option'];
$this->group_by_segment_option = $data['group_by_segment_option'];
$this->segments = $data['segments']; $this->segments = $data['segments'];
$this->subscribers_without_segment = array_search(0, $this->segments); $this->subscribers_without_segment = array_search(0, $this->segments);
$this->subscriber_fields = $data['subscriber_fields']; $this->subscriber_fields = $data['subscriber_fields'];
@@ -76,12 +74,10 @@ class Export {
$format_CSV = function($row) { $format_CSV = function($row) {
return '"' . str_replace('"', '\"', $row) . '"'; return '"' . str_replace('"', '\"', $row) . '"';
}; };
$formatted_subscriber_fields[] = __('List', 'mailpoet');
// add UTF-8 BOM (3 bytes, hex EF BB BF) at the start of the file for // add UTF-8 BOM (3 bytes, hex EF BB BF) at the start of the file for
// Excel to automatically recognize the encoding // Excel to automatically recognize the encoding
fwrite($CSV_file, chr(0xEF) . chr(0xBB) . chr(0xBF)); fwrite($CSV_file, chr(0xEF) . chr(0xBB) . chr(0xBF));
if($this->group_by_segment_option) {
$formatted_subscriber_fields[] = __('List', 'mailpoet');
}
fwrite( fwrite(
$CSV_file, $CSV_file,
implode( implode(
@@ -97,9 +93,7 @@ class Export {
$processed_subscribers += count($subscribers); $processed_subscribers += count($subscribers);
foreach($subscribers as $subscriber) { foreach($subscribers as $subscriber) {
$row = $this->formatSubscriberData($subscriber); $row = $this->formatSubscriberData($subscriber);
if($this->group_by_segment_option) {
$row[] = ucwords($subscriber['segment_name']); $row[] = ucwords($subscriber['segment_name']);
}
fwrite($CSV_file, implode(',', array_map($format_CSV, $row)) . "\n"); fwrite($CSV_file, implode(',', array_map($format_CSV, $row)) . "\n");
} }
$offset += $this->subscriber_batch_size; $offset += $this->subscriber_batch_size;
@@ -122,19 +116,14 @@ class Export {
$current_segment = ucwords($subscriber['segment_name']); $current_segment = ucwords($subscriber['segment_name']);
// Sheet header (1st row) will be written only if: // Sheet header (1st row) will be written only if:
// * This is the first time we're processing a segment // * This is the first time we're processing a segment
// * "Group by subscriber option" is turned AND the previous subscriber's // * The previous subscriber's segment is different from the current subscriber's segment
// segment is different from the current subscriber's segment
// Header will NOT be written if: // Header will NOT be written if:
// * We have already processed the segment. Because SQL results are not // * We have already processed the segment. Because SQL results are not
// sorted by segment name (due to slow queries when using ORDER BY and LIMIT), // sorted by segment name (due to slow queries when using ORDER BY and LIMIT),
// we need to keep track of processed segments so that we do not create header // we need to keep track of processed segments so that we do not create header
// multiple times when switching from one segment to another and back. // multiple times when switching from one segment to another and back.
if((!count($processed_segments) || if((!count($processed_segments) || $last_segment !== $current_segment) &&
($last_segment !== $current_segment && $this->group_by_segment_option) (!in_array($last_segment, $processed_segments) || !in_array($current_segment, $processed_segments))
) &&
(!in_array($last_segment, $processed_segments) ||
!in_array($current_segment, $processed_segments)
)
) { ) {
$this->writeXLSX( $this->writeXLSX(
$XLSX_writer, $XLSX_writer,
@@ -165,12 +154,7 @@ class Export {
} }
function writeXLSX($XLSX_writer, $segment, $data) { function writeXLSX($XLSX_writer, $segment, $data) {
return $XLSX_writer->writeSheetRow( return $XLSX_writer->writeSheetRow(ucwords($segment), $data);
($this->group_by_segment_option) ?
ucwords($segment) :
__('All Lists', 'mailpoet'),
$data
);
} }
function getSubscribers($offset, $limit) { function getSubscribers($offset, $limit) {
@@ -207,7 +191,8 @@ class Export {
) )
) )
->filter('filterWithCustomFieldsForExport') ->filter('filterWithCustomFieldsForExport')
->groupBy(Subscriber::$_table . '.id'); ->groupBy(Subscriber::$_table . '.id')
->groupBy(Segment::$_table . '.id');
if($this->subscribers_without_segment !== false) { if($this->subscribers_without_segment !== false) {
// if there are subscribers who do not belong to any segment, use // if there are subscribers who do not belong to any segment, use
@@ -230,9 +215,6 @@ class Export {
->selectExpr('MAX(' . Segment::$_table . '.name) as segment_name') ->selectExpr('MAX(' . Segment::$_table . '.name) as segment_name')
->whereIn(SubscriberSegment::$_table . '.segment_id', $this->segments); ->whereIn(SubscriberSegment::$_table . '.segment_id', $this->segments);
} }
if($this->group_by_segment_option) {
$subscribers = $subscribers->groupBy(Segment::$_table . '.id');
}
$subscribers = $subscribers $subscribers = $subscribers
->whereNull(Subscriber::$_table . '.deleted_at') ->whereNull(Subscriber::$_table . '.deleted_at')
->offset($offset) ->offset($offset)

View File

@@ -103,8 +103,6 @@ class ExportTest extends \MailPoetTest {
function testItCanConstruct() { function testItCanConstruct() {
expect($this->export->export_format_option) expect($this->export->export_format_option)
->equals('csv'); ->equals('csv');
expect($this->export->group_by_segment_option)
->equals(false);
expect($this->export->segments) expect($this->export->segments)
->equals( ->equals(
array( array(
@@ -177,41 +175,32 @@ class ExportTest extends \MailPoetTest {
function testItCanGetSubscribers() { function testItCanGetSubscribers() {
$this->export->segments = array(1); $this->export->segments = array(1);
$subscribers = $this->export->getSubscribers(0, 10); $subscribers = $this->export->getSubscribers(0, 10);
expect(count($subscribers))->equals(2); expect($subscribers)->count(2);
$this->export->segments = array(2); $this->export->segments = array(2);
$subscribers = $this->export->getSubscribers(0, 10); $subscribers = $this->export->getSubscribers(0, 10);
expect(count($subscribers))->equals(2); expect($subscribers)->count(2);
$this->export->segments = array( $this->export->segments = array(
1, 1,
2 2
); );
$subscribers = $this->export->getSubscribers(0, 10); $subscribers = $this->export->getSubscribers(0, 10);
expect(count($subscribers))->equals(3); expect($subscribers)->count(4);
} }
function testItCanGroupSubscribersBySegments() { function testItAlwaysGroupsSubscribersBySegments() {
$this->export->group_by_segment_option = true;
$this->export->subscribers_without_segment = true; $this->export->subscribers_without_segment = true;
$subscribers = $this->export->getSubscribers(0, 10); $subscribers = $this->export->getSubscribers(0, 10);
expect(count($subscribers))->equals(5); expect($subscribers)->count(5);
} }
function testItCanGetSubscribersOnlyWithoutSegments() { function testItCanGetSubscribersOnlyWithoutSegments() {
$this->export->segments = array(0); $this->export->segments = array(0);
$this->export->subscribers_without_segment = true; $this->export->subscribers_without_segment = true;
$subscribers = $this->export->getSubscribers(0, 10); $subscribers = $this->export->getSubscribers(0, 10);
expect(count($subscribers))->equals(1); expect($subscribers)->count(1);
expect($subscribers[0]['segment_name'])->equals('Not In Segment'); expect($subscribers[0]['segment_name'])->equals('Not In Segment');
} }
function testItCanGetSubscribersOnlyInSegments() {
SubscriberSegment::where('subscriber_id', 3)
->findOne()
->delete();
$subscribers = $this->export->getSubscribers(0, 10);
expect(count($subscribers))->equals(2);
}
function testItRequiresWritableExportFile() { function testItRequiresWritableExportFile() {
try { try {
$this->export->export_path = '/fake_folder'; $this->export->export_path = '/fake_folder';
@@ -231,7 +220,7 @@ class ExportTest extends \MailPoetTest {
} catch(\Exception $e) { } catch(\Exception $e) {
$this->fail('Export to .csv process threw an exception'); $this->fail('Export to .csv process threw an exception');
} }
expect($result['totalExported'])->equals(3); expect($result['totalExported'])->equals(4);
expect($result['exportFileURL'])->notEmpty(); expect($result['exportFileURL'])->notEmpty();
try { try {
@@ -241,7 +230,7 @@ class ExportTest extends \MailPoetTest {
} catch(\Exception $e) { } catch(\Exception $e) {
$this->fail('Export to .xlsx process threw an exception'); $this->fail('Export to .xlsx process threw an exception');
} }
expect($result['totalExported'])->equals(3); expect($result['totalExported'])->equals(4);
expect($result['exportFileURL'])->notEmpty(); expect($result['exportFileURL'])->notEmpty();
} }

View File

@@ -1,6 +1,5 @@
{ {
"export_format_option": "csv", "export_format_option": "csv",
"group_by_segment_option": false,
"segments": [ "segments": [
"1", "1",
"2" "2"

View File

@@ -89,8 +89,7 @@
subscriberFieldsSelect2 = subscriberFieldsSelect2 =
<%= subscriberFieldsSelect2|raw %>, <%= subscriberFieldsSelect2|raw %>,
exportData = { exportData = {
segments: segments.length || null, segments: segments.length || null
groupBySegmentOption: segments.length > 1 === true
}; };
</script> </script>
<% endblock %> <% endblock %>