public function saveExport(AmForms_ExportModel $export)
{
$isNewExport = !$export->id;
// Get the export record
if ($export->id) {
$exportRecord = AmForms_ExportRecord::model()->findById($export->id);
if (!$exportRecord) {
throw new Exception(Craft::t('No export exists with the ID “{id}”.', array('id' => $export->id)));
}
} else {
$exportRecord = new AmForms_ExportRecord();
}
// Get the form
$form = craft()->amForms_forms->getFormById($export->formId);
if (!$form) {
throw new Exception(Craft::t('No form exists with the ID “{id}”.', array('id' => $export->formId)));
}
// Export attributes
if ($isNewExport) {
// Do we need to get the total submissions to export?
if (!$export->submissions && !$export->startRightAway) {
// Set total records to export
$export->total = craft()->db->createCommand()->select('COUNT(*)')->from('amforms_submissions')->where('formId=:formId', array(':formId' => $export->formId))->queryScalar();
}
// We need to create an export file when we already have the submissions
// Or when we have no manually given submissions and don't export right way
if (!$export->startRightAway || $export->submissions) {
// Create a new export file
$export->file = $this->_createExportFile($export, $form);
}
}
$exportRecord->setAttributes($export->getAttributes(), false);
// Validate the attributes
$exportRecord->validate();
$export->addErrors($exportRecord->getErrors());
if (!$export->hasErrors()) {
if ($export->startRightAway) {
// Get max power
craft()->config->maxPowerCaptain();
// Run the export!
return $this->runExport($export);
} else {
// Save the export!
$result = $exportRecord->save(false);
// Skip validation now
// Start export task?
if ($result && $isNewExport) {
// Start task
$params = array('exportId' => $exportRecord->id, 'batchSize' => craft()->amForms_settings->getSettingsValueByHandleAndType('exportRowsPerSet', AmFormsModel::SettingExport, 100));
craft()->tasks->createTask('AmForms_Export', Craft::t('{form} export', array('form' => $form->name)), $params);
// Notify user
craft()->userSession->setNotice(Craft::t('Export started.'));
}
return $result;
}
}
return false;
}