DOIPubIdExportPlugin::executeExportAction PHP Method

executeExportAction() public method

public executeExportAction ( $request, $objects, $filter, $tab, $objectsFileNamePart )
    function executeExportAction($request, $objects, $filter, $tab, $objectsFileNamePart)
    {
        $context = $request->getContext();
        $path = array('plugin', $this->getName());
        if ($request->getUserVar(EXPORT_ACTION_DEPOSIT)) {
            assert($filter != null);
            // Get the XML
            $exportXml = $this->exportXML($objects, $filter, $context);
            // Write the XML to a file.
            // export file name example: crossref-20160723-160036-articles-1.xml
            import('lib.pkp.classes.file.FileManager');
            $fileManager = new FileManager();
            $exportFileName = $this->getExportFileName($this->getExportPath(), $objectsFileNamePart, $context, '.xml');
            $fileManager->writeFile($exportFileName, $exportXml);
            // Deposit the XML file.
            $result = $this->depositXML($objects, $context, $exportFileName);
            // send notifications
            if ($result === true) {
                $this->_sendNotification($request->getUser(), $this->getDepositSuccessNotificationMessageKey(), NOTIFICATION_TYPE_SUCCESS);
            } else {
                if (is_array($result)) {
                    foreach ($result as $error) {
                        assert(is_array($error) && count($error) >= 1);
                        $this->_sendNotification($request->getUser(), $error[0], NOTIFICATION_TYPE_ERROR, isset($error[1]) ? $error[1] : null);
                    }
                }
            }
            // Remove all temporary files.
            $fileManager->deleteFile($exportFileName);
            // redirect back to the right tab
            $request->redirect(null, null, null, $path, null, $tab);
        }
        parent::executeExportAction($request, $objects, $filter, $tab, $objectsFileNamePart);
    }

Usage Example

Example #1
0
 /**
  * @copydoc ImportExportPlugin::executeExportAction()
  */
 function executeExportAction($request, $objects, $filter, $tab, $objectsFileNamePart)
 {
     $context = $request->getContext();
     $path = array('plugin', $this->getName());
     // Export
     if ($request->getUserVar(DOI_EXPORT_ACTION_EXPORT)) {
         $result = $this->_checkForTar();
         if ($result === true) {
             $exportedFiles = array();
             foreach ($objects as $object) {
                 // Get the XML
                 $exportXml = $this->exportXML($object, $filter, $context);
                 // Write the XML to a file.
                 // export file name example: datacite/20160723-160036-articles-1-1.xml
                 $objectFileNamePart = $objectsFileNamePart . '-' . $object->getId();
                 $exportFileName = $this->getExportFileName($objectFileNamePart, $context);
                 file_put_contents($exportFileName, $exportXml);
                 $exportedFiles[] = $exportFileName;
             }
             // If we have more than one export file we package the files
             // up as a single tar before going on.
             assert(count($exportedFiles) >= 1);
             if (count($exportedFiles) > 1) {
                 // tar file name: e.g. datacite/20160723-160036-articles-1.tar.gz
                 $finalExportFileName = $this->getExportPath() . date('Ymd-His') . '-' . $objectsFileNamePart . '-' . $context->getId() . '.tar.gz';
                 $finalExportFileType = DATACITE_EXPORT_FILE_TAR;
                 $this->_tarFiles($this->getExportPath(), $finalExportFileName, $exportedFiles);
                 // remove files
                 foreach ($exportedFiles as $exportedFile) {
                     $this->cleanTmpfile($exportedFile);
                 }
             } else {
                 $finalExportFileName = array_shift($exportedFiles);
                 $finalExportFileType = DATACITE_EXPORT_FILE_XML;
             }
             header('Content-Type: application/' . ($finalExportFileType == DATACITE_EXPORT_FILE_TAR ? 'x-gtar' : 'xml'));
             header('Cache-Control: private');
             header('Content-Disposition: attachment; filename="' . basename($finalExportFileName) . '"');
             readfile($finalExportFileName);
         } else {
             if (is_array($result)) {
                 foreach ($result as $error) {
                     assert(is_array($error) && count($error) >= 1);
                     $this->_sendNotification($request->getUser(), $error[0], NOTIFICATION_TYPE_ERROR, isset($error[1]) ? $error[1] : null);
                 }
             }
             // redirect back to the right tab
             $request->redirect(null, null, null, $path, null, $tab);
         }
     } elseif ($request->getUserVar(DOI_EXPORT_ACTION_DEPOSIT)) {
         $resultErrors = array();
         foreach ($objects as $object) {
             // Get the XML
             $exportXml = $this->exportXML($object, $filter, $context);
             // Write the XML to a file.
             // export file name example: datacite/20160723-160036-articles-1-1.xml
             $objectFileNamePart = $objectsFileNamePart . '-' . $object->getId();
             $exportFileName = $this->getExportFileName($objectFileNamePart, $context);
             file_put_contents($exportFileName, $exportXml);
             // Deposit the XML file.
             $result = $this->depositXML($object, $context, $exportFileName);
             if (is_array($result)) {
                 $resultErrors[] = $result;
             }
             // Remove all temporary files.
             $this->cleanTmpfile($exportFileName);
         }
         // send notifications
         if (empty($resultErrors)) {
             $this->_sendNotification($request->getUser(), $this->getDepositSuccessNotificationMessageKey(), NOTIFICATION_TYPE_SUCCESS);
         } else {
             foreach ($resultErrors as $error) {
                 assert(is_array($error) && count($error) >= 1);
                 $this->_sendNotification($request->getUser(), $error[0], NOTIFICATION_TYPE_ERROR, isset($error[1]) ? $error[1] : null);
             }
         }
         // redirect back to the right tab
         $request->redirect(null, null, null, $path, null, $tab);
     } else {
         return parent::executeExportAction($request, $objects, $filter, $tab, $objectsFileNamePart);
     }
 }
All Usage Examples Of DOIPubIdExportPlugin::executeExportAction