/**
  * Completes the job by zipping up the generated export and creating an
  * export record for it.
  */
 protected function complete()
 {
     $siteTitle = SiteConfig::current_site_config()->Title;
     $filename = preg_replace('/[^a-zA-Z0-9-.+]/', '-', sprintf('%s-%s.zip', $siteTitle, date('c')));
     $dir = Folder::findOrMake(SiteExportExtension::EXPORTS_DIR);
     $dirname = ASSETS_PATH . '/' . SiteExportExtension::EXPORTS_DIR;
     $pathname = "{$dirname}/{$filename}";
     SiteExportUtils::zip_directory($this->tempDir, "{$dirname}/{$filename}");
     Filesystem::removeFolder($this->tempDir);
     $file = new File();
     $file->ParentID = $dir->ID;
     $file->Title = $siteTitle . ' ' . date('c');
     $file->Filename = $dir->Filename . $filename;
     $file->write();
     $export = new SiteExport();
     $export->ParentClass = $this->rootClass;
     $export->ParentID = $this->rootId;
     $export->Theme = $this->theme;
     $export->BaseUrlType = ucfirst($this->baseUrlType);
     $export->BaseUrl = $this->baseUrl;
     $export->ArchiveID = $file->ID;
     $export->write();
     if ($this->email) {
         $email = new Email();
         $email->setTo($this->email);
         $email->setTemplate('SiteExportCompleteEmail');
         $email->setSubject(sprintf('Site Export For "%s" Complete', $siteTitle));
         $email->populateTemplate(array('SiteTitle' => $siteTitle, 'Link' => $file->getAbsoluteURL()));
         $email->send();
     }
 }
 public function doExport($data, $form)
 {
     $data = $form->getData();
     $links = array();
     $siteTitle = SiteConfig::current_site_config()->Title;
     // If the queued jobs module is installed, then queue up an export
     // job rather than performing the export.
     if (class_exists('QueuedJobService')) {
         $job = new SiteExportJob($form->getRecord());
         $job->theme = $data['ExportSiteTheme'];
         $job->baseUrl = $data['ExportSiteBaseUrl'];
         $job->baseUrlType = $data['ExportSiteBaseUrlType'];
         $job->email = $data['ExportSiteCompleteEmail'];
         singleton('QueuedJobService')->queueJob($job);
         return new SS_HTTPResponse($form->dataFieldByName('SiteExports')->FieldHolder(), 200, 'The site export job has been queued.');
     }
     // First generate a temp directory to store the export content in.
     $temp = TEMP_FOLDER;
     $temp .= sprintf('/siteexport_%s', date('Y-m-d-His'));
     mkdir($temp);
     $exporter = new SiteExporter();
     $exporter->root = $form->getRecord();
     $exporter->theme = $data['ExportSiteTheme'];
     $exporter->baseUrl = $data['ExportSiteBaseUrl'];
     $exporter->makeRelative = $data['ExportSiteBaseUrlType'] == 'rewrite';
     $exporter->exportTo($temp);
     // Then place the exported content into an archive, stored in the assets
     // root, and create a site export for it.
     $filename = preg_replace('/[^a-zA-Z0-9-.+]/', '-', sprintf('%s-%s.zip', $siteTitle, date('c')));
     $dir = Folder::findOrMake(self::EXPORTS_DIR);
     $dirname = ASSETS_PATH . '/' . self::EXPORTS_DIR;
     $pathname = "{$dirname}/{$filename}";
     SiteExportUtils::zip_directory($temp, "{$dirname}/{$filename}");
     Filesystem::removeFolder($temp);
     $file = new File();
     $file->ParentID = $dir->ID;
     $file->Title = $siteTitle . ' ' . date('c');
     $file->Filename = $dir->Filename . $filename;
     $file->write();
     $export = new SiteExport();
     $export->ParentClass = $form->getRecord()->class;
     $export->ParentID = $form->getRecord()->ID;
     $export->Theme = SSViewer::current_theme();
     $export->BaseUrlType = ucfirst($data['ExportSiteBaseUrlType']);
     $export->BaseUrl = $data['ExportSiteBaseUrl'];
     $export->ArchiveID = $file->ID;
     $export->write();
     return new SS_HTTPResponse($form->dataFieldByName('SiteExports')->FieldHolder(), 200, 'The site export has been generated.');
 }