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

/ojs/ojs-2.3.2-1/plugins/generic/customLocale/CustomLocaleHandler.inc.php

https://github.com/mcrider/pkpUpgradeTestSuite
PHP | 221 lines | 161 code | 42 blank | 18 comment | 20 complexity | ad67eeeab1923ce8c847445cfb64b79c MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @file CustomLocaleHandler.inc.php
  4. *
  5. * Copyright (c) 2003-2009 John Willinsky
  6. * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  7. *
  8. * @class CustomLocaleHandler
  9. * @ingroup plugins_generic_customLocale
  10. *
  11. * @brief This handles requests for the customLocale plugin.
  12. */
  13. // $Id$
  14. require_once('CustomLocalePlugin.inc.php');
  15. require_once('CustomLocaleAction.inc.php');
  16. import('handler.Handler');
  17. class CustomLocaleHandler extends Handler {
  18. /** Plugin associated with the request */
  19. var $plugin;
  20. /**
  21. * Constructor
  22. **/
  23. function CustomLocaleHandler() {
  24. parent::Handler();
  25. $this->addCheck(new HandlerValidatorJournal($this));
  26. $this->addCheck(new HandlerValidatorRoles($this, true, null, null, array(ROLE_ID_SITE_ADMIN, ROLE_ID_JOURNAL_MANAGER)));
  27. $plugin =& PluginRegistry::getPlugin('generic', 'CustomLocalePlugin');
  28. $this->plugin =& $plugin;
  29. }
  30. function index() {
  31. $this->validate();
  32. $plugin =& PluginRegistry::getPlugin('generic', 'CustomLocalePlugin');
  33. $this->setupTemplate($plugin, false);
  34. $journal = Request::getJournal();
  35. $rangeInfo = Handler::getRangeInfo('locales');
  36. $templateMgr =& TemplateManager::getManager();
  37. import('core.ArrayItemIterator');
  38. $templateMgr->assign('locales', new ArrayItemIterator($journal->getSupportedLocaleNames(), $rangeInfo->getPage(), $rangeInfo->getCount()));
  39. $templateMgr->assign('masterLocale', MASTER_LOCALE);
  40. $templateMgr->display($plugin->getTemplatePath() . 'index.tpl');
  41. }
  42. function edit($args) {
  43. $this->validate();
  44. $plugin =& PluginRegistry::getPlugin('generic', 'CustomLocalePlugin');
  45. $this->setupTemplate($plugin, true);
  46. $locale = array_shift($args);
  47. $file = array_shift($args);
  48. if (!Locale::isLocaleValid($locale)) {
  49. $path = array($plugin->getCategory(), $plugin->getName(), 'index');
  50. Request::redirect(null, null, null, $path);
  51. }
  52. $localeFiles = CustomLocaleAction::getLocaleFiles($locale);
  53. $templateMgr =& TemplateManager::getManager();
  54. $localeFilesRangeInfo = Handler::getRangeInfo('localeFiles');
  55. import('core.ArrayItemIterator');
  56. $templateMgr->assign('localeFiles', new ArrayItemIterator($localeFiles, $localeFilesRangeInfo->getPage(), $localeFilesRangeInfo->getCount()));
  57. $templateMgr->assign('locale', $locale);
  58. $templateMgr->assign('masterLocale', MASTER_LOCALE);
  59. $templateMgr->display($plugin->getTemplatePath() . 'locale.tpl');
  60. }
  61. function editLocaleFile($args) {
  62. $this->validate();
  63. $plugin =& PluginRegistry::getPlugin('generic', 'CustomLocalePlugin');
  64. $this->setupTemplate($plugin, true);
  65. $locale = array_shift($args);
  66. if (!Locale::isLocaleValid($locale)) {
  67. $path = array($plugin->getCategory(), $plugin->getName(), 'index');
  68. Request::redirect(null, null, null, $path);
  69. }
  70. $filename = urldecode(urldecode(array_shift($args)));
  71. if (!CustomLocaleAction::isLocaleFile($locale, $filename)) {
  72. $path = array($plugin->getCategory(), $plugin->getName(), 'edit', $locale);
  73. Request::redirect(null, null, null, $path);
  74. }
  75. $templateMgr =& TemplateManager::getManager();
  76. import('file.FileManager');
  77. import('file.EditableLocaleFile');
  78. $journal = Request::getJournal();
  79. $journalId = $journal->getJournalId();
  80. $publicFilesDir = Config::getVar('files', 'public_files_dir');
  81. $customLocaleDir = $publicFilesDir . DIRECTORY_SEPARATOR . 'journals' . DIRECTORY_SEPARATOR . $journalId . DIRECTORY_SEPARATOR . CUSTOM_LOCALE_DIR;
  82. $customLocalePath = $customLocaleDir . DIRECTORY_SEPARATOR . $locale . DIRECTORY_SEPARATOR . $filename;
  83. if (FileManager::fileExists($customLocalePath)) {
  84. $localeContents = EditableLocaleFile::load($customLocalePath);
  85. } else {
  86. $localeContents = null;
  87. }
  88. $referenceLocaleContents = EditableLocaleFile::load($filename);
  89. $referenceLocaleContentsRangeInfo = Handler::getRangeInfo('referenceLocaleContents');
  90. // Handle a search, if one was executed.
  91. $searchKey = Request::getUserVar('searchKey');
  92. $found = false;
  93. $index = 0;
  94. $pageIndex = 0;
  95. if (!empty($searchKey)) foreach ($referenceLocaleContents as $key => $value) {
  96. if ($index % $referenceLocaleContentsRangeInfo->getCount() == 0) $pageIndex++;
  97. if ($key == $searchKey) {
  98. $found = true;
  99. break;
  100. }
  101. $index++;
  102. }
  103. if ($found) {
  104. $referenceLocaleContentsRangeInfo->setPage($pageIndex);
  105. $templateMgr->assign('searchKey', $searchKey);
  106. }
  107. $templateMgr->assign('filename', $filename);
  108. $templateMgr->assign('locale', $locale);
  109. import('core.ArrayItemIterator');
  110. $templateMgr->assign_by_ref('referenceLocaleContents', new ArrayItemIterator($referenceLocaleContents, $referenceLocaleContentsRangeInfo->getPage(), $referenceLocaleContentsRangeInfo->getCount()));
  111. $templateMgr->assign('localeContents', $localeContents);
  112. $templateMgr->display($plugin->getTemplatePath() . 'localeFile.tpl');
  113. }
  114. function saveLocaleFile($args) {
  115. $this->validate();
  116. $plugin =& PluginRegistry::getPlugin('generic', 'CustomLocalePlugin');
  117. $this->setupTemplate($plugin, true);
  118. $locale = array_shift($args);
  119. if (!Locale::isLocaleValid($locale)) {
  120. $path = array($plugin->getCategory(), $plugin->getName(), 'index');
  121. Request::redirect(null, null, null, $path);
  122. }
  123. $filename = urldecode(urldecode(array_shift($args)));
  124. if (!CustomLocaleAction::isLocaleFile($locale, $filename)) {
  125. $path = array($plugin->getCategory(), $plugin->getName(), 'edit', $locale);
  126. Request::redirect(null, null, null, $path);
  127. }
  128. $journal =& Request::getJournal();
  129. $journalId = $journal->getJournalId();
  130. $changes = Request::getUserVar('changes');
  131. $customFilesDir = Config::getVar('files', 'public_files_dir') . DIRECTORY_SEPARATOR . 'journals' . DIRECTORY_SEPARATOR . $journalId . DIRECTORY_SEPARATOR . CUSTOM_LOCALE_DIR . DIRECTORY_SEPARATOR . $locale;
  132. $customFilePath = $customFilesDir . DIRECTORY_SEPARATOR . $filename;
  133. // Create empty custom locale file if it doesn't exist
  134. import('file.FileManager');
  135. import('file.EditableLocaleFile');
  136. if (!FileManager::fileExists($customFilePath)) {
  137. $numParentDirs = substr_count($customFilePath, DIRECTORY_SEPARATOR);
  138. $parentDirs = '';
  139. for ($i=0; $i<$numParentDirs; $i++) {
  140. $parentDirs .= '..' . DIRECTORY_SEPARATOR;
  141. }
  142. $newFileContents = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
  143. $newFileContents .= '<!DOCTYPE locale SYSTEM "' . $parentDirs . 'lib' . DIRECTORY_SEPARATOR . 'pkp' . DIRECTORY_SEPARATOR . 'dtd' . DIRECTORY_SEPARATOR . 'locale.dtd' . '">' . "\n";
  144. $newFileContents .= '<locale name="' . $locale . '">' . "\n";
  145. $newFileContents .= '</locale>';
  146. FileManager::writeFile($customFilePath, $newFileContents);
  147. }
  148. $file = new EditableLocaleFile($locale, $customFilePath);
  149. while (!empty($changes)) {
  150. $key = array_shift($changes);
  151. $value = $this->correctCr(array_shift($changes));
  152. if (!empty($value)) {
  153. if (!$file->update($key, $value)) {
  154. $file->insert($key, $value);
  155. }
  156. } else {
  157. $file->delete($key);
  158. }
  159. }
  160. $file->write();
  161. Request::redirectUrl(Request::getUserVar('redirectUrl'));
  162. }
  163. function correctCr($value) {
  164. return str_replace("\r\n", "\n", $value);
  165. }
  166. function setupTemplate(&$plugin, $subclass = true) {
  167. parent::setupTemplate();
  168. $templateMgr =& TemplateManager::getManager();
  169. $templateMgr->register_function('plugin_url', array($plugin, 'smartyPluginUrl'));
  170. $pageHierarchy = array(array(Request::url(null, 'user'), 'navigation.user'), array(Request::url(null, 'manager'), 'user.role.manager'));
  171. if ($subclass) {
  172. $path = array($plugin->getCategory(), $plugin->getName(), 'index');
  173. $pageHierarchy[] = array(Request::url(null, null, null, $path), 'plugins.generic.customLocale.name');
  174. }
  175. $templateMgr->assign('pageHierarchy', $pageHierarchy);
  176. $templateMgr->assign('helpTopicId', 'plugins.generic.CustomLocalePlugin');
  177. }
  178. }
  179. ?>