Skip to content

Commit

Permalink
pkp#3462 Add import/export filter return function
Browse files Browse the repository at this point in the history
  • Loading branch information
defstat committed May 30, 2018
1 parent c7a6087 commit 3affbd2
Showing 1 changed file with 59 additions and 22 deletions.
81 changes: 59 additions & 22 deletions plugins/importexport/users/PKPUserImportExportPlugin.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,23 +117,28 @@ function display($args, $request) {
}
$temporaryFilePath = $temporaryFile->getFilePath();
libxml_use_internal_errors(true);
$importFilter = $this->importUsers(file_get_contents($temporaryFilePath), $context, $user);

$filter = $this->getUserImportExportFilter($context, $user);
$users = $this->importUsers(file_get_contents($temporaryFilePath), $context, $user, $filter);
$validationErrors = array_filter(libxml_get_errors(), function($a) {
return $a->level == LIBXML_ERR_ERROR || $a->level == LIBXML_ERR_FATAL;
});
$templateMgr->assign('validationErrors', $validationErrors);
libxml_clear_errors();
if ($importFilter->hasErrors()) {
$templateMgr->assign('filterErrors', $importFilter->getErrors());
if ($filter->hasErrors()) {
$templateMgr->assign('filterErrors', $filter->getErrors());
}
$templateMgr->assign('users', $importFilter->getLastOutput());
$templateMgr->assign('users', $users);
$json = new JSONMessage(true, $templateMgr->fetch($this->getTemplatePath() . 'results.tpl'));
return $json->getString();
case 'export':
$filter = $this->getUserImportExportFilter($request->getContext(), $request->getUser(), false);

$exportXml = $this->exportUsers(
(array) $request->getUserVar('selectedUsers'),
$request->getContext(),
$request->getUser()
$request->getUser(),
$filter
);
import('lib.pkp.classes.file.FileManager');
$fileManager = new FileManager();
Expand All @@ -143,9 +148,12 @@ function display($args, $request) {
$fileManager->deleteFile($exportFileName);
break;
case 'exportAllUsers':
$filter = $this->getUserImportExportFilter($request->getContext(), $request->getUser(), false);

$exportXml = $this->exportAllUsers(
$request->getContext(),
$request->getUser()
$request->getUser(),
$filter
);
import('lib.pkp.classes.file.FileManager');
$fileManager = new FileManager();
Expand All @@ -164,29 +172,35 @@ function display($args, $request) {
* Get the XML for all of users.
* @param $context Context
* @param $user User
* @param $filter Filter byRef parameter - import/export filter used
* @return string XML contents representing the supplied user IDs.
*/
function exportAllUsers($context, $user) {
function exportAllUsers($context, $user, &$filter) {
$userGroupDao = DAORegistry::getDAO('UserGroupDAO');
$users = $userGroupDao->getUsersByContextId($context->getId());
return $this->exportUsers($users->toArray(), $context, $user);
if (!$filter) {
$filter = $this->getUserImportExportFilter($context, $user, false);
}

return $this->exportUsers($users->toArray(), $context, $user, $filter);
}

/**
* Get the XML for a set of users.
* @param $ids array mixed Array of users or user IDs
* @param $context Context
* @param $user User
* @param $filter Filter byRef parameter - import/export filter used
* @return string XML contents representing the supplied user IDs.
*/
function exportUsers($ids, $context, $user) {
function exportUsers($ids, $context, $user, &$filter) {
$userDao = DAORegistry::getDAO('UserDAO');
$xml = '';
$filterDao = DAORegistry::getDAO('FilterDAO');
$userExportFilters = $filterDao->getObjectsByGroup('user=>user-xml');
assert(count($userExportFilters) == 1); // Assert only a single serialization filter
$exportFilter = array_shift($userExportFilters);
$exportFilter->setDeployment(new PKPUserImportExportDeployment($context, $user));

if (!$filter) {
$filter = $this->getUserImportExportFilter($context, $user, false);
}

$users = array();
foreach ($ids as $id) {
if (is_a($id, 'User')) {
Expand All @@ -196,7 +210,9 @@ function exportUsers($ids, $context, $user) {
if ($user) $users[] = $user;
}
}
$userXml = $exportFilter->execute($users);


$userXml = $filter->execute($users);
if ($userXml) $xml = $userXml->saveXml();
else fatalError('Could not convert users.');
return $xml;
Expand All @@ -207,17 +223,38 @@ function exportUsers($ids, $context, $user) {
* @param $importXml string XML contents to import
* @param $context Context
* @param $user User
* @param $filter Filter byRef parameter - import/export filter used
* @return array Set of imported users
*/
function importUsers($importXml, $context, $user) {
function importUsers($importXml, $context, $user, &$filter) {
if (!$filter) {
$filter = $this->getUserImportExportFilter($context, $user);
}

return $filter->execute($importXml);
}

/**
* Return user filter for import purposes
* @param $context Context
* @param $user User
* @param $isImport bool return Import Filter if true - export if false
* @return Filter
*/
function getUserImportExportFilter($context, $user, $isImport = true) {
$filterDao = DAORegistry::getDAO('FilterDAO');
$userImportFilters = $filterDao->getObjectsByGroup('user-xml=>user');
assert(count($userImportFilters) == 1); // Assert only a single unserialization filter
$importFilter = array_shift($userImportFilters);
$importFilter->setDeployment(new PKPUserImportExportDeployment($context, $user));

$importFilter->execute($importXml);
return $importFilter;
if ($isImport) {
$userFilters = $filterDao->getObjectsByGroup('user-xml=>user');
} else {
$userFilters = $filterDao->getObjectsByGroup('user=>user-xml');
}

assert(count($userFilters) == 1); // Assert only a single unserialization filter
$filter = array_shift($userFilters);
$filter->setDeployment(new PKPUserImportExportDeployment($context, $user));

return $filter;
}
}

Expand Down

0 comments on commit 3affbd2

Please sign in to comment.