View source
<?php
class GdprTasksSarWorker {
public function processItem($data) {
$task = gdpr_task_load($data);
switch ($task->status) {
case 'requested':
$this
->initialise($task);
break;
case 'processed':
$this
->compile($task);
break;
}
}
protected function initialise(\GDPRTask $task) {
$field_info = field_info_field('gdpr_tasks_sar_export');
$directory = $field_info['settings']['uri_scheme'] . '://gdpr_sars';
file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
do {
$uuid = ctools_uuid_generate();
if (file_exists("{$directory}/{$uuid}.zip") || file_exists("{$directory}/{$uuid}/")) {
continue;
}
$file = file_save_data('', "{$directory}/{$uuid}.zip", FILE_EXISTS_ERROR);
} while (!$file);
$content_directory = "{$directory}/{$uuid}";
file_prepare_directory($content_directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
$task->gdpr_tasks_sar_export[LANGUAGE_NONE][0] = array(
'fid' => $file->fid,
'display' => TRUE,
);
$task->status = 'building';
$task
->save();
$this
->build($task);
}
protected function build(\GDPRTask $task) {
$wrapper = $task
->wrapper();
$field_info = field_info_field('gdpr_tasks_sar_export');
$directory = $field_info['settings']['uri_scheme'] . '://gdpr_sars/';
$directory .= basename($task
->wrapper()->gdpr_tasks_sar_export->file->url
->value(), '.zip');
$all_data = gdpr_tasks_collect_rta_data($task
->getOwner());
$csvs = array();
foreach ($all_data as $plugin_id => $data) {
if ($plugin_id == '_assets') {
$wrapper->gdpr_tasks_sar_export_assets = $data;
continue;
}
if (!isset($csvs[$data['file']]['_header'][$data['plugin_name']])) {
$csvs[$data['file']]['_plugin_id'][$data['plugin_name']] = $data['plugin_name'];
$csvs[$data['file']]['_header'][$data['plugin_name']] = $data['label'];
}
if (!isset($csvs[$data['file']][$data['row_id']])) {
$csvs[$data['file']][$data['row_id']] = array();
}
$csvs[$data['file']][$data['row_id']] += array_fill_keys(array_keys($csvs[$data['file']]['_header']), '');
$csvs[$data['file']][$data['row_id']][$data['plugin_name']] = $data['value'];
}
$files = array();
foreach ($wrapper->gdpr_tasks_sar_export_parts as $item) {
$filename = basename($item->file->url
->value(), '.csv');
$files[$filename] = $item->file
->value();
}
foreach ($csvs as $filename => $data) {
if (!isset($files[$filename])) {
$file = file_save_data('', "{$directory}/{$filename}.csv", FILE_EXISTS_REPLACE);
$task->gdpr_tasks_sar_export_parts[LANGUAGE_NONE][] = array(
'fid' => $file->fid,
'display' => TRUE,
);
}
else {
$file = $files[$filename];
}
$this
->writeCsv($file->uri, $data);
clearstatcache(TRUE, $file->uri);
file_save($file);
}
$task->status = 'reviewing';
$task
->save();
}
protected function compile(\GDPRTask $task) {
$wrapper = $task
->wrapper();
$file = $wrapper->gdpr_tasks_sar_export->file;
$file_path = drupal_realpath($file
->value()->uri);
$zip = new ZipArchive();
if (!$zip
->open($file_path, ZipArchive::CREATE | ZipArchive::OVERWRITE)) {
drupal_set_message(t('Error opening file.'), 'error');
return;
}
$part_files = array();
foreach ($wrapper->gdpr_tasks_sar_export_parts as $item) {
$part_file = $item->file
->value();
$part_files[] = $part_file;
$data = $this
->readCsv($part_file->uri);
array_shift($data);
$this
->writeCsv($part_file->uri, $data);
file_save($part_file);
$zip
->addFile(drupal_realpath($part_file->uri), $part_file->filename);
}
foreach ($wrapper->gdpr_tasks_sar_export_assets as $item) {
$asset_file = $item->file
->value();
$filename = "assets/{$asset_file->fid}." . pathinfo($asset_file->uri, PATHINFO_EXTENSION);
$zip
->addFile(drupal_realpath($asset_file->uri), $filename);
}
$task->gdpr_tasks_sar_export_parts = NULL;
$task->gdpr_tasks_sar_export_assets = NULL;
$zip
->close();
$file
->value()->uid = $task
->getOwner()->uid;
$file
->save();
foreach ($part_files as $part_file) {
file_delete($part_file);
}
$task->status = 'closed';
$task
->save();
gdpr_tasks_send_mail('task_processed', $task);
}
public static function readCsv($filename) {
$data = array();
$handle = fopen($filename, 'r');
while (!feof($handle)) {
$data[] = fgetcsv($handle);
}
fclose($handle);
return $data;
}
protected function writeCsv($filename, array $content) {
$handler = fopen($filename, 'w');
fprintf($handler, chr(0xef) . chr(0xbb) . chr(0xbf));
foreach ($content as $row) {
fputcsv($handler, $row);
}
fclose($handler);
}
}