- Resolves issues identified by @rafaehlers during testing
This commit is contained in:
@ -11,106 +11,110 @@ use MailPoet\Util\Helpers;
|
||||
use MailPoet\Util\XLSXWriter;
|
||||
|
||||
class Export {
|
||||
public $export_confirmed_option;
|
||||
public $export_format_option;
|
||||
public $group_by_segment_option;
|
||||
public $segments;
|
||||
public $subscribers_without_segment;
|
||||
public $subscriber_fields;
|
||||
public $export_file;
|
||||
public $export_file_URL;
|
||||
public $profiler_start;
|
||||
|
||||
public function __construct($data) {
|
||||
$this->exportConfirmedOption = $data['exportConfirmedOption'];
|
||||
$this->exportFormatOption = $data['exportFormatOption'];
|
||||
$this->groupBySegmentOption = $data['groupBySegmentOption'];
|
||||
$this->export_confirmed_option = $data['export_confirmed_option'];
|
||||
$this->export_format_option = $data['export_format_option'];
|
||||
$this->group_by_segment_option = $data['group_by_segment_option'];
|
||||
$this->segments = $data['segments'];
|
||||
$this->subscribersWithoutSegment = array_search(0, $this->segments);
|
||||
$this->subscriberFields = $data['subscriberFields'];
|
||||
$this->exportFile = $this->getExportFile($this->exportFormatOption);
|
||||
$this->exportFileURL = $this->getExportFileURL($this->exportFile);
|
||||
$this->profilerStart = microtime(true);
|
||||
$this->subscribers_without_segment = array_search(0, $this->segments);
|
||||
$this->subscriber_fields = $data['subscriber_fields'];
|
||||
$this->export_file = $this->getExportFile($this->export_format_option);
|
||||
$this->export_file_URL = $this->getExportFileURL($this->export_file);
|
||||
$this->profiler_start = microtime(true);
|
||||
}
|
||||
|
||||
function process() {
|
||||
$subscribers = $this->getSubscribers();
|
||||
$subscriberCustomFields = $this->getSubscriberCustomFields();
|
||||
$formattedSubscriberFields = $this->formatSubscriberFields(
|
||||
$this->subscriberFields,
|
||||
$subscriberCustomFields
|
||||
$subscriber_custom_fields = $this->getSubscriberCustomFields();
|
||||
$formatted_subscriber_fields = $this->formatSubscriberFields(
|
||||
$this->subscriber_fields,
|
||||
$subscriber_custom_fields
|
||||
);
|
||||
try {
|
||||
if($this->exportFormatOption === 'csv') {
|
||||
$CSVFile = fopen($this->exportFile, 'w');
|
||||
$formatCSV = function ($row) {
|
||||
if($this->export_format_option === 'csv') {
|
||||
$CSV_file = fopen($this->export_file, 'w');
|
||||
$format_CSV = function ($row) {
|
||||
return '"' . str_replace('"', '\"', $row) . '"';
|
||||
};
|
||||
// add UTF-8 BOM (3 bytes, hex EF BB BF) at the start of the file for
|
||||
// Excel to automatically recognize the encoding
|
||||
fwrite($CSVFile, chr(0xEF) . chr(0xBB) . chr(0xBF));
|
||||
if($this->groupBySegmentOption) {
|
||||
$formattedSubscriberFields[] = __('List');
|
||||
fwrite($CSV_file, chr(0xEF) . chr(0xBB) . chr(0xBF));
|
||||
if($this->group_by_segment_option) {
|
||||
$formatted_subscriber_fields[] = __('Segment');
|
||||
}
|
||||
fwrite(
|
||||
$CSVFile,
|
||||
$CSV_file,
|
||||
implode(
|
||||
',',
|
||||
array_map(
|
||||
$formatCSV,
|
||||
$formattedSubscriberFields
|
||||
$format_CSV,
|
||||
$formatted_subscriber_fields
|
||||
)
|
||||
) . "\n"
|
||||
);
|
||||
foreach ($subscribers as $subscriber) {
|
||||
$row = $this->formatSubscriberData(
|
||||
$subscriber,
|
||||
$formattedSubscriberFields
|
||||
);
|
||||
if($this->groupBySegmentOption) {
|
||||
foreach($subscribers as $subscriber) {
|
||||
$row = $this->formatSubscriberData($subscriber);
|
||||
if($this->group_by_segment_option) {
|
||||
$row[] = ucwords($subscriber['segment_name']);
|
||||
}
|
||||
fwrite($CSVFile, implode(',', array_map($formatCSV, $row)) . "\n");
|
||||
fwrite($CSV_file, implode(',', array_map($format_CSV, $row)) . "\n");
|
||||
}
|
||||
fclose($CSVFile);
|
||||
fclose($CSV_file);
|
||||
} else {
|
||||
$writer = new XLSXWriter();
|
||||
$writer->setAuthor('MailPoet (www.mailpoet.com)');
|
||||
$headerRow = array($formattedSubscriberFields);
|
||||
$lastSegment = false;
|
||||
$header_row = array($formatted_subscriber_fields);
|
||||
$last_segment = false;
|
||||
$rows = array();
|
||||
foreach ($subscribers as $subscriber) {
|
||||
if($lastSegment && $lastSegment !== $subscriber['segment_name'] &&
|
||||
$this->groupBySegmentOption
|
||||
foreach($subscribers as $subscriber) {
|
||||
if($last_segment && $last_segment !== $subscriber['segment_name'] &&
|
||||
$this->group_by_segment_option
|
||||
) {
|
||||
$writer->writeSheet(
|
||||
array_merge($headerRow, $rows), ucwords($lastSegment)
|
||||
array_merge($header_row, $rows), ucwords($last_segment)
|
||||
);
|
||||
$rows = array();
|
||||
}
|
||||
// detect RTL language and set Excel to properly display the sheet
|
||||
$RTLRegex = '/\p{Arabic}|\p{Hebrew}/u';
|
||||
$RTL_regex = '/\p{Arabic}|\p{Hebrew}/u';
|
||||
if(!$writer->rtl && (
|
||||
preg_grep($RTLRegex, $subscriber) ||
|
||||
preg_grep($RTLRegex, $formattedSubscriberFields))
|
||||
preg_grep($RTL_regex, $subscriber) ||
|
||||
preg_grep($RTL_regex, $formatted_subscriber_fields))
|
||||
) {
|
||||
$writer->rtl = true;
|
||||
}
|
||||
$rows[] = $this->formatSubscriberData(
|
||||
$subscriber,
|
||||
$formattedSubscriberFields
|
||||
);
|
||||
$lastSegment = $subscriber['segment_name'];
|
||||
$rows[] = $this->formatSubscriberData($subscriber);
|
||||
$last_segment = $subscriber['segment_name'];
|
||||
}
|
||||
$writer->writeSheet(
|
||||
array_merge($headerRow, $rows),
|
||||
($this->groupBySegmentOption) ?
|
||||
array_merge($header_row, $rows),
|
||||
($this->group_by_segment_option) ?
|
||||
ucwords($subscriber['segment_name']) :
|
||||
'MailPoet'
|
||||
__('All Segments')
|
||||
);
|
||||
$writer->writeToFile($this->exportFile);
|
||||
$writer->writeToFile($this->export_file);
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
} catch(Exception $e) {
|
||||
return array(
|
||||
'result' => false,
|
||||
'error' => $e->getMessage()
|
||||
'errors' => array($e->getMessage())
|
||||
);
|
||||
}
|
||||
return array(
|
||||
'result' => true,
|
||||
'data' => array(
|
||||
'totalExported' => count($subscribers),
|
||||
'exportFileURL' => $this->exportFileURL
|
||||
'exportFileURL' => $this->export_file_URL
|
||||
),
|
||||
'profiler' => $this->timeExecution()
|
||||
);
|
||||
@ -134,11 +138,11 @@ class Export {
|
||||
))
|
||||
->orderByAsc('segment_name')
|
||||
->filter('filterWithCustomFieldsForExport');
|
||||
if($this->subscribersWithoutSegment !== false) {
|
||||
if($this->subscribers_without_segment !== false) {
|
||||
$subscribers = $subscribers
|
||||
->selectExpr('CASE WHEN ' . Segment::$_table . '.name IS NOT NULL ' .
|
||||
'THEN ' . Segment::$_table . '.name ' .
|
||||
'ELSE "' . __('Not In List') . '" END as segment_name'
|
||||
'ELSE "' . __('Not In Segment') . '" END as segment_name'
|
||||
)
|
||||
->whereRaw(
|
||||
SubscriberSegment::$_table . '.segment_id IN (' .
|
||||
@ -151,11 +155,11 @@ class Export {
|
||||
->select(Segment::$_table . '.name', 'segment_name')
|
||||
->whereIn(SubscriberSegment::$_table . '.segment_id', $this->segments);
|
||||
}
|
||||
if(!$this->groupBySegmentOption) {
|
||||
if(!$this->group_by_segment_option) {
|
||||
$subscribers =
|
||||
$subscribers->groupBy(Subscriber::$_table . '.id');
|
||||
}
|
||||
if($this->exportConfirmedOption) {
|
||||
if($this->export_confirmed_option) {
|
||||
$subscribers =
|
||||
$subscribers->where(Subscriber::$_table . '.status', 'subscribed');
|
||||
}
|
||||
@ -166,7 +170,7 @@ class Export {
|
||||
|
||||
function getExportFileURL($file) {
|
||||
return sprintf(
|
||||
'%s/%s/%s',
|
||||
'%s%s/%s',
|
||||
Env::$plugin_url,
|
||||
Env::$temp_name,
|
||||
basename($file)
|
||||
@ -189,32 +193,28 @@ class Export {
|
||||
);
|
||||
}
|
||||
|
||||
function formatSubscriberFields($subscriberFields, $subscriberCustomFields) {
|
||||
$bootStrapMenu = new BootStrapMenu();
|
||||
$translatedFields = $bootStrapMenu->getSubscriberFields();
|
||||
function formatSubscriberFields($subscriber_fields, $subscriber_custom_fields) {
|
||||
$bootstrap_menu = new BootStrapMenu();
|
||||
$translated_fields = $bootstrap_menu->getSubscriberFields();
|
||||
return array_map(function ($field) use (
|
||||
$translatedFields, $subscriberCustomFields
|
||||
$translated_fields, $subscriber_custom_fields
|
||||
) {
|
||||
$field = (isset($translatedFields[$field])) ?
|
||||
ucfirst($translatedFields[$field]) :
|
||||
$field = (isset($translated_fields[$field])) ?
|
||||
ucfirst($translated_fields[$field]) :
|
||||
ucfirst($field);
|
||||
return (isset($subscriberCustomFields[$field])) ?
|
||||
$subscriberCustomFields[$field] : $field;
|
||||
}, $subscriberFields);
|
||||
return (isset($subscriber_custom_fields[$field])) ?
|
||||
ucfirst($subscriber_custom_fields[$field]) : $field;
|
||||
}, $subscriber_fields);
|
||||
}
|
||||
|
||||
function formatSubscriberData($subscriber, $subscriberCustomFields) {
|
||||
return array_map(function ($field) use (
|
||||
$subscriber, $subscriberCustomFields
|
||||
) {
|
||||
return (isset($subscriberCustomFields[$field])) ?
|
||||
$subscriberCustomFields[$field] :
|
||||
$subscriber[$field];
|
||||
}, $this->subscriberFields);
|
||||
function formatSubscriberData($subscriber) {
|
||||
return array_map(function ($field) use ($subscriber) {
|
||||
return $subscriber[$field];
|
||||
}, $this->subscriber_fields);
|
||||
}
|
||||
|
||||
function timeExecution() {
|
||||
$profilerEnd = microtime(true);
|
||||
return ($profilerEnd - $this->profilerStart) / 60;
|
||||
$profiler_end = microtime(true);
|
||||
return ($profiler_end - $this->profiler_start) / 60;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user