public static function executeExportRecords(Application $app, Request $request, $contentTypeAccessHash, $token, Module $module = null)
 {
     /** @var Repository $repository */
     $repository = $app['repos']->getRepositoryByContentTypeAccessHash($contentTypeAccessHash);
     if ($repository) {
         $contentTypeDefinition = $repository->getContentTypeDefinition();
         $app['context']->setCurrentRepository($repository);
         $app['context']->setCurrentContentType($contentTypeDefinition);
         $workspace = $request->get('workspace');
         if (!$contentTypeDefinition->hasWorkspace($workspace)) {
             $workspace = 'default';
         }
         $language = $request->get('language');
         if (!$contentTypeDefinition->hasLanguage($language)) {
             $language = 'default';
         }
         $format = $request->get('format');
         $exporter = new Exporter();
         if ($format == 'j') {
             $data = $exporter->exportJSON($repository, $repository->getContentTypeDefinition()->getName(), $workspace, $language);
             $filename = strtolower(date('Ymd') . '_export_' . $contentTypeDefinition->getName() . '_' . $workspace . '_' . $language . '.json');
         } else {
             $data = $exporter->exportXLSX($repository, $repository->getContentTypeDefinition()->getName(), $workspace, $language);
             $filename = strtolower(date('Ymd') . '_export_' . $contentTypeDefinition->getName() . '_' . $workspace . '_' . $language . '.xlsx');
         }
         if ($data) {
             // Redirect output to a client’s web browser
             header('Content-Type: application/vnd.ms-excel');
             header('Content-Disposition: attachment;filename="' . $filename . '"');
             header('Cache-Control: max-age=0');
             // If you're serving to IE 9, then the following may be needed
             header('Cache-Control: max-age=1');
             // If you're serving to IE over SSL, then the following may be needed
             header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
             // Date in the past
             header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
             // always modified
             header('Cache-Control: cache, must-revalidate');
             // HTTP/1.1
             header('Pragma: public');
             // HTTP/1.0
             $response = new Response($data);
             $cookie = new Cookie("anycontent-download", $token, 0, '/', null, false, false);
             //Not http only!
             $response->headers->setCookie($cookie);
             $app['context']->addSuccessMessage('Records exported to ' . $filename);
             return $response;
         }
     }
     $app['context']->addErrorMessage('Could not export records.');
     return new RedirectResponse($app['url_generator']->generate('listRecords', array('contentTypeAccessHash' => $contentTypeAccessHash, 'page' => $app['context']->getCurrentListingPage(), 'workspace' => $app['context']->getCurrentWorkspace(), 'language' => $app['context']->getCurrentLanguage())));
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $app = $this->getSilexApplication();
     $verbose = !$input->getOption('print');
     /** @var RepositoryManager $repositoryManager */
     $repositoryManager = $app['repos'];
     $contentTypeName = $input->getArgument('contentType');
     $repositoryName = $input->getArgument('repository');
     $workspace = 'default';
     if ($input->hasArgument('workspace')) {
         $workspace = $input->getArgument('workspace');
     }
     $language = 'default';
     if ($input->hasArgument('language')) {
         $language = $input->getArgument('language');
     }
     if ($verbose) {
         $output->writeln('');
         $output->writeln('Starting export for content type ' . $contentTypeName);
         $output->writeln('');
     }
     $exporter = new Exporter();
     if ($verbose) {
         $exporter->setOutput($output);
     }
     $repository = $repositoryManager->getRepositoryById($repositoryName);
     if (!$repository) {
         $output->writeln(self::escapeError . 'Repository ' . $repositoryName . ' unknown. Use the list command to show available repositories.' . self::escapeReset);
         return;
     }
     if (!$repository->hasContentType($contentTypeName)) {
         $output->writeln(self::escapeError . 'Repository ' . $repositoryName . ' does not have a content type named ' . $contentTypeName . '. Use the list command to show available content types.' . self::escapeReset);
     }
     if (!$repository->getContentTypeDefinition($contentTypeName)->hasWorkspace($workspace)) {
         $output->writeln(self::escapeError . 'Content type ' . $contentTypeName . ' does not have a workspace named ' . $workspace . self::escapeReset);
     }
     if (!$repository->getContentTypeDefinition($contentTypeName)->hasLanguage($language)) {
         $output->writeln(self::escapeError . 'Content type ' . $contentTypeName . ' does not have a language named ' . $language . self::escapeReset);
     }
     if ($input->getOption('xlsx') == true) {
         $filename = $contentTypeName . '.' . $workspace . '.' . $language . '.xlsx';
         $data = $exporter->exportXLSX($repository, $contentTypeName, $workspace, $language);
         if (!$data) {
             $output->writeln(self::escapeError . 'Could not access repository ' . $repositoryName . '.' . self::escapeReset);
             return;
         }
     } else {
         $filename = $contentTypeName . '.' . $workspace . '.' . $language . '.json';
         $data = $exporter->exportJSON($repository, $contentTypeName, $workspace, $language);
         if (!$data) {
             $output->writeln(self::escapeError . 'Could not access repository ' . $repositoryName . '.' . self::escapeReset);
             return;
         }
     }
     $filesystem = new Filesystem();
     if ($verbose) {
         $output->writeln('');
         $output->writeln('Dumping data to ' . $filename);
         $filesystem->dumpFile($filename, $data);
         $output->writeln('');
         $output->writeln('Done');
         $output->writeln('');
     } else {
         $output->write($data);
     }
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $app = $this->getSilexApplication();
     /** @var RepositoryManager $repositoryManager */
     $repositoryManager = $app['repos'];
     $repositoryName = $input->getArgument('repository');
     $repository = $repositoryManager->getRepositoryById($repositoryName);
     if (!$repository) {
         $output->writeln(self::escapeError . 'Repository ' . $repositoryName . ' unknown. Use the list command to show available repositories.' . self::escapeReset);
         return;
     }
     if ($repository->getTitle() == '') {
         $repository->setTitle($repository->getName());
     }
     $filesystem = new Filesystem();
     $path = rtrim($input->getArgument('path'), '/');
     if (!$filesystem->exists($path)) {
         $output->writeln(self::escapeError . 'Could not access folder ' . $path . self::escapeReset);
         return;
     }
     $output->writeln('');
     $output->writeln('Starting export for repository ' . $repository->getTitle() . ' (' . $repository->getName() . ')');
     $output->writeln('');
     $exporter = new Exporter();
     $exporter->setOutput($output);
     $path .= '/' . $repositoryName;
     if (!$filesystem->exists($path)) {
         $filesystem->mkdir($path);
     }
     foreach ($repository->getContentTypeDefinitions() as $definition) {
         $contentTypeName = $definition->getName();
         $output->writeln('');
         $output->writeln(self::escapeCyan . 'Starting export for content type ' . $contentTypeName . self::escapeReset);
         $output->writeln('');
         foreach ($definition->getWorkspaces() as $workspace => $workspaceTitle) {
             foreach ($definition->getLanguages() as $language => $languageTitle) {
                 $output->writeln('');
                 $output->writeln(self::escapeYellow . 'Selecting workspace ' . $workspaceTitle . ' language ' . $languageTitle . self::escapeReset);
                 $output->writeln('');
                 $folder = $path . '/data/content/' . $contentTypeName . '/' . $workspace . '/' . $language;
                 if (file_exists($folder) && $input->getOption('merge') == false) {
                     $filesystem->remove($folder);
                 }
                 if (!file_exists($folder)) {
                     $filesystem->mkdir($folder);
                 }
                 if ($input->getOption('xlsx') == true) {
                     $data = $exporter->exportXLSX($repository, $contentTypeName, $workspace, $language);
                     if (!$data) {
                         $output->writeln(self::escapeError . 'Could not access repository ' . $repositoryName . '.' . self::escapeReset);
                         return;
                     }
                 } else {
                     $data = $exporter->exportJSON($repository, $contentTypeName, $workspace, $language);
                     if (!$data) {
                         $output->writeln(self::escapeError . 'Could not access repository ' . $repositoryName . '.' . self::escapeReset);
                         return;
                     }
                     $data = json_decode($data, true);
                     $output->writeln('');
                     $output->writeln(self::escapeCyan . 'Splitting ' . count($data['records']) . ' records into single json files.' . self::escapeReset);
                     $output->writeln('');
                     foreach ($data['records'] as $id => $record) {
                         $filename = $folder . '/' . $id . '.json';
                         $filesystem->dumpFile($filename, json_encode($record, JSON_PRETTY_PRINT));
                     }
                 }
             }
         }
     }
     //
     //
     //        $filesystem = new Filesystem();
     //
     //        if ($verbose) {
     //            $output->writeln('');
     //            $output->writeln('Dumping data to ' . $filename);
     //            $filesystem->dumpFile($filename, $data);
     //            $output->writeln('');
     //            $output->writeln('Done');
     //            $output->writeln('');
     //        } else {
     //            $output->write($data);
     //        }
 }