public function runExport(AmForms_ExportModel $export, $limit = null, $offset = null)
{
// Validate export file (if send by task)
if (!IOHelper::fileExists($export->file) && !$export->startRightAway) {
return false;
}
// Get submissions
$params = array('formId' => $export->formId, 'limit' => $limit, 'offset' => $offset);
// Are there manually given submissions?
if ($export->submissions) {
$params['id'] = $export->submissions;
}
$criteria = craft()->amForms_submissions->getCriteria($params);
$this->_addExportCriteria($export, $criteria);
$submissions = $criteria->find();
// Add submissions to export file
if ($submissions && count($submissions) > 0) {
// Get form
$form = craft()->amForms_forms->getFormById($export->formId);
if (!$form) {
return false;
}
// Get field types
$fields = $this->getExportFields($form);
// Export submission to a zip file?
if ($export->submissions) {
// Add all fields
$this->_exportFields[$export->id] = $fields;
// Export submission
foreach ($submissions as $submission) {
$this->_exportSubmissionToZip($export, $submission);
}
} else {
// Get the export file
if ($export->startRightAway) {
// Open output buffer
ob_start();
// Write to output stream
$this->_exportFiles['manual'] = fopen('php://output', 'w');
// Create columns
fputcsv($this->_exportFiles['manual'], $this->_getExportColumns($export, $form), $this->_delimiter);
} else {
$this->_exportFiles[$export->id] = fopen($export->file, 'a');
}
// Get field handles and columns that should be included
$columnCounter = 0;
$this->_exportFields[$export->id] = array();
foreach ($export->map['fields'] as $fieldHandle => $columnName) {
if ($export->map['included'][$fieldHandle] && isset($fields[$fieldHandle])) {
// Add field to export fields
$field = $fields[$fieldHandle];
if (is_array($field)) {
$field = (object) $field;
// Fix standard fields
}
$this->_exportFields[$export->id][$fieldHandle] = $field;
// Remember how much space this field is taking
$spaceCounter = 0;
// Add column so we know where to place the data later
switch ($field->type) {
case 'Matrix':
$blockTypes = $field->getFieldType()->getSettings()->getBlockTypes();
foreach ($blockTypes as $blockType) {
$blockTypeFields = $blockType->getFields();
$this->_exportColumns[$export->id][$field->handle . ':' . $blockType->handle] = $columnCounter;
$columnCounter += count($blockTypeFields);
$spaceCounter += count($blockTypeFields);
}
break;
default:
$this->_exportColumns[$export->id][$field->handle] = $columnCounter;
$spaceCounter++;
break;
}
$columnCounter++;
$this->_exportSpaceCounter[$export->id][$field->handle] = $spaceCounter;
}
}
// Export submission model
foreach ($submissions as $submission) {
$this->_exportSubmission($export, $submission);
}
// Close export file
fclose($this->_exportFiles[$export->startRightAway ? 'manual' : $export->id]);
if ($export->startRightAway) {
// Close buffer and return data
$data = ob_get_clean();
// Use windows friendly newlines
$data = str_replace("\n", "\r\n", $data);
return $data;
}
}
}
return true;
}