PageRenderTime 82ms CodeModel.GetById 24ms RepoModel.GetById 7ms app.codeStats 0ms

/ojs/ojs-2.2.2/plugins/importexport/users/UserImportExportPlugin.inc.php

https://github.com/mcrider/pkpUpgradeTestSuite
PHP | 301 lines | 231 code | 34 blank | 36 comment | 19 complexity | e64862ad31340b43a5fbb9eb9298ace2 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @file UserImportExportPlugin.inc.php
  4. *
  5. * Copyright (c) 2003-2008 John Willinsky
  6. * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  7. *
  8. * @class UserImportExportPlugin
  9. * @ingroup plugins_importexport_users
  10. *
  11. * @brief Users import/export plugin
  12. */
  13. // $Id: UserImportExportPlugin.inc.php,v 1.26 2008/07/01 01:16:14 asmecher Exp $
  14. import('classes.plugins.ImportExportPlugin');
  15. import('xml.XMLCustomWriter');
  16. class UserImportExportPlugin extends ImportExportPlugin {
  17. /**
  18. * Called as a plugin is registered to the registry
  19. * @param $category String Name of category plugin was registered to
  20. * @return boolean True iff plugin initialized successfully; if false,
  21. * the plugin will not be registered.
  22. */
  23. function register($category, $path) {
  24. $success = parent::register($category, $path);
  25. $this->addLocaleData();
  26. return $success;
  27. }
  28. /**
  29. * Get the name of this plugin. The name must be unique within
  30. * its category.
  31. * @return String name of plugin
  32. */
  33. function getName() {
  34. return 'UserImportExportPlugin';
  35. }
  36. function getDisplayName() {
  37. return Locale::translate('plugins.importexport.users.displayName');
  38. }
  39. function getDescription() {
  40. return Locale::translate('plugins.importexport.users.description');
  41. }
  42. function display(&$args) {
  43. $templateMgr = &TemplateManager::getManager();
  44. parent::display($args);
  45. $templateMgr->assign('roleOptions', array(
  46. '' => 'manager.people.doNotEnroll',
  47. 'manager' => 'user.role.manager',
  48. 'editor' => 'user.role.editor',
  49. 'sectionEditor' => 'user.role.sectionEditor',
  50. 'layoutEditor' => 'user.role.layoutEditor',
  51. 'reviewer' => 'user.role.reviewer',
  52. 'copyeditor' => 'user.role.copyeditor',
  53. 'proofreader' => 'user.role.proofreader',
  54. 'author' => 'user.role.author',
  55. 'reader' => 'user.role.reader',
  56. 'subscriptionManager' => 'user.role.subscriptionManager'
  57. ));
  58. $roleDao = &DAORegistry::getDAO('RoleDAO');
  59. $journal = &Request::getJournal();
  60. switch (array_shift($args)) {
  61. case 'confirm':
  62. $this->import('UserXMLParser');
  63. $templateMgr->assign('helpTopicId', 'journal.users.importUsers');
  64. $sendNotify = (bool) Request::getUserVar('sendNotify');
  65. $continueOnError = (bool) Request::getUserVar('continueOnError');
  66. import('file.FileManager');
  67. if (($userFile = FileManager::getUploadedFilePath('userFile')) !== false) {
  68. // Import the uploaded file
  69. $parser = &new UserXMLParser($journal->getJournalId());
  70. $users = &$parser->parseData($userFile);
  71. $i = 0;
  72. $usersRoles = array();
  73. foreach ($users as $user) {
  74. $usersRoles[$i] = array();
  75. foreach ($user->getRoles() as $role) {
  76. array_push($usersRoles[$i], $role->getRoleName());
  77. }
  78. $i++;
  79. }
  80. $templateMgr->assign_by_ref('users', $users);
  81. $templateMgr->assign_by_ref('usersRoles', $usersRoles);
  82. $templateMgr->assign('sendNotify', $sendNotify);
  83. $templateMgr->assign('continueOnError', $continueOnError);
  84. $templateMgr->assign('errors', $parser->errors);
  85. // Show confirmation form
  86. $templateMgr->display($this->getTemplatePath() . 'importUsersConfirm.tpl');
  87. }
  88. break;
  89. case 'import':
  90. $this->import('UserXMLParser');
  91. $userKeys = Request::getUserVar('userKeys');
  92. if (!is_array($userKeys)) $userKeys = array();
  93. $sendNotify = (bool) Request::getUserVar('sendNotify');
  94. $continueOnError = (bool) Request::getUserVar('continueOnError');
  95. $users = array();
  96. foreach ($userKeys as $i) {
  97. $newUser = &new ImportedUser();
  98. $newUser->setFirstName(Request::getUserVar($i.'_firstName'));
  99. $newUser->setMiddleName(Request::getUserVar($i.'_middleName'));
  100. $newUser->setLastName(Request::getUserVar($i.'_lastName'));
  101. $newUser->setUsername(Request::getUserVar($i.'_username'));
  102. $newUser->setEmail(Request::getUserVar($i.'_email'));
  103. $locales = array();
  104. if (Request::getUserVar($i.'_locales') != null || is_array(Request::getUserVar($i.'_locales'))) {
  105. foreach (Request::getUserVar($i.'_locales') as $locale) {
  106. array_push($locales, $locale);
  107. }
  108. }
  109. $newUser->setLocales($locales);
  110. $newUser->setSignature(Request::getUserVar($i.'_signature'), null);
  111. $newUser->setBiography(Request::getUserVar($i.'_biography'), null);
  112. $newUser->setInterests(Request::getUserVar($i.'_interests'), null);
  113. $newUser->setCountry(Request::getUserVar($i.'_country'));
  114. $newUser->setMailingAddress(Request::getUserVar($i.'_mailingAddress'));
  115. $newUser->setFax(Request::getUserVar($i.'_fax'));
  116. $newUser->setPhone(Request::getUserVar($i.'_phone'));
  117. $newUser->setUrl(Request::getUserVar($i.'_url'));
  118. $newUser->setAffiliation(Request::getUserVar($i.'_affiliation'));
  119. $newUser->setGender(Request::getUserVar($i.'_gender'));
  120. $newUser->setInitials(Request::getUserVar($i.'_initials'));
  121. $newUser->setSalutation(Request::getUserVar($i.'_salutation'));
  122. $newUser->setPassword(Request::getUserVar($i.'_password'));
  123. $newUser->setMustChangePassword(Request::getUserVar($i.'_mustChangePassword'));
  124. $newUser->setUnencryptedPassword(Request::getUserVar($i.'_unencryptedPassword'));
  125. $newUserRoles = Request::getUserVar($i.'_roles');
  126. if (is_array($newUserRoles) && count($newUserRoles) > 0) {
  127. foreach ($newUserRoles as $newUserRole) {
  128. if ($newUserRole != '') {
  129. $role = &new Role();
  130. $role->setRoleId(RoleDAO::getRoleIdFromPath($newUserRole));
  131. $newUser->AddRole($role);
  132. }
  133. }
  134. }
  135. array_push($users, $newUser);
  136. }
  137. $parser = &new UserXMLParser($journal->getJournalId());
  138. $parser->setUsersToImport($users);
  139. if (!$parser->importUsers($sendNotify, $continueOnError)) {
  140. // Failures occurred
  141. $templateMgr->assign('isError', true);
  142. $templateMgr->assign('errors', $parser->getErrors());
  143. }
  144. $templateMgr->assign('importedUsers', $parser->getImportedUsers());
  145. $templateMgr->display($this->getTemplatePath() . 'importUsersResults.tpl');
  146. break;
  147. case 'exportAll':
  148. $this->import('UserExportDom');
  149. $users = &$roleDao->getUsersByJournalId($journal->getJournalId());
  150. $users = &$users->toArray();
  151. $doc = &UserExportDom::exportUsers($journal, $users);
  152. header("Content-Type: application/xml");
  153. header("Cache-Control: private");
  154. header("Content-Disposition: attachment; filename=\"users.xml\"");
  155. echo XMLCustomWriter::getXML($doc);
  156. break;
  157. case 'exportByRole':
  158. $this->import('UserExportDom');
  159. $users = array();
  160. $rolePaths = array();
  161. foreach (Request::getUserVar('roles') as $rolePath) {
  162. $roleId = $roleDao->getRoleIdFromPath($rolePath);
  163. $thisRoleUsers = &$roleDao->getUsersByRoleId($roleId, $journal->getJournalId());
  164. foreach ($thisRoleUsers->toArray() as $user) {
  165. $users[$user->getUserId()] = $user;
  166. }
  167. $rolePaths[] = $rolePath;
  168. }
  169. $users = array_values($users);
  170. $doc = &UserExportDom::exportUsers($journal, $users, $rolePaths);
  171. header("Content-Type: application/xml");
  172. header("Cache-Control: private");
  173. header("Content-Disposition: attachment; filename=\"users.xml\"");
  174. echo XMLCustomWriter::getXML($doc);
  175. break;
  176. default:
  177. $this->setBreadcrumbs();
  178. $templateMgr->display($this->getTemplatePath() . 'index.tpl');
  179. }
  180. }
  181. /**
  182. * Execute import/export tasks using the command-line interface.
  183. * @param $args Parameters to the plugin
  184. */
  185. function executeCLI($scriptName, &$args) {
  186. $command = array_shift($args);
  187. $xmlFile = array_shift($args);
  188. $journalPath = array_shift($args);
  189. $flags = &$args;
  190. $journalDao = &DAORegistry::getDAO('JournalDAO');
  191. $userDao = &DAORegistry::getDAO('UserDAO');
  192. $journal = &$journalDao->getJournalByPath($journalPath);
  193. if (!$journal) {
  194. if ($journalPath != '') {
  195. echo Locale::translate('plugins.importexport.users.import.errorsOccurred') . ":\n";
  196. echo Locale::translate('plugins.importexport.users.unknownJournal', array('journalPath' => $journalPath)) . "\n\n";
  197. }
  198. $this->usage($scriptName);
  199. return;
  200. }
  201. switch ($command) {
  202. case 'import':
  203. $this->import('UserXMLParser');
  204. $sendNotify = in_array('send_notify', $flags);
  205. $continueOnError = in_array('continue_on_error', $flags);
  206. import('file.FileManager');
  207. // Import the uploaded file
  208. $parser = &new UserXMLParser($journal->getJournalId());
  209. $users = &$parser->parseData($xmlFile);
  210. if (!$parser->importUsers($sendNotify, $continueOnError)) {
  211. // Failure.
  212. echo Locale::translate('plugins.importexport.users.import.errorsOccurred') . ":\n";
  213. foreach ($parser->getErrors() as $error) {
  214. echo "\t$error\n";
  215. }
  216. return false;
  217. }
  218. // Success.
  219. echo Locale::translate('plugins.importexport.users.import.usersWereImported') . ":\n";
  220. foreach ($parser->getImportedUsers() as $user) {
  221. echo "\t" . $user->getUserName() . "\n";
  222. }
  223. return true;
  224. break;
  225. case 'export':
  226. $this->import('UserExportDom');
  227. $roleDao = &DAORegistry::getDAO('RoleDAO');
  228. $rolePaths = null;
  229. if (empty($args)) {
  230. $users = &$roleDao->getUsersByJournalId($journal->getJournalId());
  231. $users = &$users->toArray();
  232. } else {
  233. $users = array();
  234. $rolePaths = array();
  235. foreach ($args as $rolePath) {
  236. $roleId = $roleDao->getRoleIdFromPath($rolePath);
  237. $thisRoleUsers = &$roleDao->getUsersByRoleId($roleId, $journal->getJournalId());
  238. foreach ($thisRoleUsers->toArray() as $user) {
  239. $users[$user->getUserId()] = $user;
  240. }
  241. $rolePaths[] = $rolePath;
  242. }
  243. $users = array_values($users);
  244. }
  245. $doc = &UserExportDom::exportUsers($journal, $users, $rolePaths);
  246. if (($h = fopen($xmlFile, 'wb'))===false) {
  247. echo Locale::translate('plugins.importexport.users.export.errorsOccurred') . ":\n";
  248. echo Locale::translate('plugins.importexport.users.export.couldNotWriteFile', array('fileName' => $xmlFile)) . "\n";
  249. return false;
  250. }
  251. fwrite($h, XMLCustomWriter::getXML($doc));
  252. fclose($h);
  253. return true;
  254. }
  255. $this->usage($scriptName);
  256. }
  257. /**
  258. * Display the command-line usage information
  259. */
  260. function usage($scriptName) {
  261. echo Locale::translate('plugins.importexport.users.cliUsage', array(
  262. 'scriptName' => $scriptName,
  263. 'pluginName' => $this->getName()
  264. )) . "\n";
  265. }
  266. }
  267. ?>