PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/ojs/ojs-2.1.0-1/plugins/importexport/users/UserImportExportPlugin.inc.php

https://github.com/mcrider/pkpUpgradeTestSuite
PHP | 280 lines | 213 code | 31 blank | 36 comment | 28 complexity | ae656f70cad547e7011a2510e045b01e MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * UserImportExportPlugin.inc.php
  4. *
  5. * Copyright (c) 2003-2005 The Public Knowledge Project
  6. * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  7. *
  8. * @package plugins
  9. *
  10. * Users import/export plugin
  11. *
  12. * $Id: UserImportExportPlugin.inc.php,v 1.10 2006/01/24 20:38:04 alec Exp $
  13. */
  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();
  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. $journal = &Request::getJournal();
  70. $parser = &new UserXMLParser($journal->getJournalId());
  71. $users = &$parser->parseData($userFile);
  72. $i = 0;
  73. $usersRoles = array();
  74. foreach ($users as $user) {
  75. $usersRoles[$i] = array();
  76. foreach ($user->getRoles() as $role) {
  77. array_push($usersRoles[$i], $role->getRoleName());
  78. }
  79. $i++;
  80. }
  81. $templateMgr->assign_by_ref('users', $users);
  82. $templateMgr->assign_by_ref('usersRoles', $usersRoles);
  83. $templateMgr->assign('sendNotify', $sendNotify);
  84. $templateMgr->assign('continueOnError', $continueOnError);
  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. if (($firstName = Request::getUserVar($i.'_firstName')) !== '') $newUser->setFirstName($firstName);
  99. if (($middleName = Request::getUserVar($i.'_middleName')) !== '') $newUser->setMiddleName($middleName);
  100. if (($lastName = Request::getUserVar($i.'_lastName')) !== '') $newUser->setLastName($lastName);
  101. if (($username = Request::getUserVar($i.'_username')) !== '') $newUser->setUsername($username);
  102. if (($password = Request::getUserVar($i.'_password')) !== '') $newUser->setPassword($password);
  103. if (($biography = Request::getUserVar($i.'_biography')) !== '') $newUser->setBiography($biography);
  104. if (($affiliation = Request::getUserVar($i.'_affiliation')) !== '') $newUser->setAffiliation($affiliation);
  105. if (($phone = Request::getUserVar($i.'_phone')) !== '') $newUser->setPhone($phone);
  106. if (($fax = Request::getUserVar($i.'_fax')) !== '') $newUser->setFax($fax);
  107. if (($mailingaddress = Request::getUserVar($i.'_mailingAddress')) !== '') $newUser->setMailingAddress($mailingaddress);
  108. if (($unencryptedPassword = Request::getUserVar($i.'_unencryptedPassword')) !== '') $newUser->setUnencryptedPassword($unencryptedPassword);
  109. if (($email = Request::getUserVar($i.'_email')) !== '') $newUser->setEmail($email);
  110. $newUserRoles = Request::getUserVar($i.'_roles');
  111. if (is_array($newUserRoles) && count($newUserRoles) > 0) {
  112. foreach ($newUserRoles as $newUserRole) {
  113. if ($newUserRole != '') {
  114. $role = &new Role();
  115. $role->setRoleId(RoleDAO::getRoleIdFromPath($newUserRole));
  116. $newUser->AddRole($role);
  117. }
  118. }
  119. }
  120. array_push($users, $newUser);
  121. }
  122. $journal = &Request::getJournal();
  123. $parser = &new UserXMLParser($journal->getJournalId());
  124. $parser->setUsersToImport($users);
  125. if (!$parser->importUsers($sendNotify, $continueOnError)) {
  126. // Failures occurred
  127. $templateMgr->assign('isError', true);
  128. $templateMgr->assign('errors', $parser->getErrors());
  129. }
  130. $templateMgr->assign('importedUsers', $parser->getImportedUsers());
  131. $templateMgr->display($this->getTemplatePath() . 'importUsersResults.tpl');
  132. break;
  133. case 'exportAll':
  134. $this->import('UserExportDom');
  135. $users = &$roleDao->getUsersByJournalId($journal->getJournalId());
  136. $users = &$users->toArray();
  137. $doc = &UserExportDom::exportUsers($journal, $users);
  138. header("Content-Type: application/xml");
  139. echo XMLCustomWriter::getXML($doc);
  140. break;
  141. case 'exportByRole':
  142. $this->import('UserExportDom');
  143. $users = array();
  144. $rolePaths = array();
  145. foreach (Request::getUserVar('roles') as $rolePath) {
  146. $roleId = $roleDao->getRoleIdFromPath($rolePath);
  147. $thisRoleUsers = &$roleDao->getUsersByRoleId($roleId, $journal->getJournalId());
  148. foreach ($thisRoleUsers->toArray() as $user) {
  149. $users[$user->getUserId()] = $user;
  150. }
  151. $rolePaths[] = $rolePath;
  152. }
  153. $users = array_values($users);
  154. $doc = &UserExportDom::exportUsers($journal, $users, $rolePaths);
  155. header("Content-Type: application/xml");
  156. echo XMLCustomWriter::getXML($doc);
  157. break;
  158. default:
  159. $this->setBreadcrumbs();
  160. $templateMgr->display($this->getTemplatePath() . 'index.tpl');
  161. }
  162. }
  163. /**
  164. * Execute import/export tasks using the command-line interface.
  165. * @param $args Parameters to the plugin
  166. */
  167. function executeCLI($scriptName, &$args) {
  168. $command = array_shift($args);
  169. $xmlFile = array_shift($args);
  170. $journalPath = array_shift($args);
  171. $flags = &$args;
  172. $journalDao = &DAORegistry::getDAO('JournalDAO');
  173. $userDao = &DAORegistry::getDAO('UserDAO');
  174. $journal = &$journalDao->getJournalByPath($journalPath);
  175. if (!$journal) {
  176. if ($journalPath != '') {
  177. echo Locale::translate('plugins.importexport.users.import.errorsOccurred') . ":\n";
  178. echo Locale::translate('plugins.importexport.users.unknownJournal', array('journalPath' => $journalPath)) . "\n\n";
  179. }
  180. $this->usage($scriptName);
  181. return;
  182. }
  183. switch ($command) {
  184. case 'import':
  185. $this->import('UserXMLParser');
  186. $sendNotify = in_array('send_notify', $flags);
  187. $continueOnError = in_array('continue_on_error', $flags);
  188. import('file.FileManager');
  189. // Import the uploaded file
  190. $parser = &new UserXMLParser($journal->getJournalId());
  191. $users = &$parser->parseData($xmlFile);
  192. if (!$parser->importUsers($sendNotify, $continueOnError)) {
  193. // Failure.
  194. echo Locale::translate('plugins.importexport.users.import.errorsOccurred') . ":\n";
  195. foreach ($parser->getErrors() as $error) {
  196. echo "\t$error\n";
  197. }
  198. return false;
  199. }
  200. // Success.
  201. echo Locale::translate('plugins.importexport.users.import.usersWereImported') . ":\n";
  202. foreach ($parser->getImportedUsers() as $user) {
  203. echo "\t" . $user->getUserName() . "\n";
  204. }
  205. return true;
  206. break;
  207. case 'export':
  208. $this->import('UserExportDom');
  209. $roleDao = &DAORegistry::getDAO('RoleDAO');
  210. $rolePaths = null;
  211. if (empty($args)) {
  212. $users = &$roleDao->getUsersByJournalId($journal->getJournalId());
  213. $users = &$users->toArray();
  214. } else {
  215. $users = array();
  216. $rolePaths = array();
  217. foreach ($args as $rolePath) {
  218. $roleId = $roleDao->getRoleIdFromPath($rolePath);
  219. $thisRoleUsers = &$roleDao->getUsersByRoleId($roleId, $journal->getJournalId());
  220. foreach ($thisRoleUsers->toArray() as $user) {
  221. $users[$user->getUserId()] = $user;
  222. }
  223. $rolePaths[] = $rolePath;
  224. }
  225. $users = array_values($users);
  226. }
  227. $doc = &UserExportDom::exportUsers($journal, $users, $rolePaths);
  228. if (($h = fopen($xmlFile, 'w'))===false) {
  229. echo Locale::translate('plugins.importexport.users.export.errorsOccurred') . ":\n";
  230. echo Locale::translate('plugins.importexport.users.export.couldNotWriteFile', array('fileName' => $xmlFile)) . "\n";
  231. return false;
  232. }
  233. fwrite($h, XMLCustomWriter::getXML($doc));
  234. fclose($h);
  235. return true;
  236. }
  237. $this->usage($scriptName);
  238. }
  239. /**
  240. * Display the command-line usage information
  241. */
  242. function usage($scriptName) {
  243. echo Locale::translate('plugins.importexport.users.cliUsage', array(
  244. 'scriptName' => $scriptName,
  245. 'pluginName' => $this->getName()
  246. )) . "\n";
  247. }
  248. }
  249. ?>