PageRenderTime 25ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/generic/customLocale/CustomLocaleHandler.inc.php

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