4 * @file CustomLocaleHandler.inc.php
6 * Copyright (c) 2003-2008 John Willinsky
7 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
9 * @class CustomLocaleHandler
10 * @ingroup plugins_generic_customLocale
12 * @brief This handles requests for the customLocale plugin.
15 // $Id: CustomLocaleHandler.inc.php,v 1.7 2009/09/22 19:22:11 asmecher Exp $
18 require_once('CustomLocalePlugin.inc.php');
19 require_once('CustomLocaleAction.inc.php');
20 import('handler.Handler');
22 class CustomLocaleHandler
extends Handler
{
25 list($plugin) = CustomLocaleHandler
::validate();
26 CustomLocaleHandler
::setupTemplate($plugin, false);
28 $press = Request
::getPress();
29 $rangeInfo = Handler
::getRangeInfo('locales');
31 $templateMgr =& TemplateManager
::getManager();
32 import('core.ArrayItemIterator');
33 $templateMgr->assign('locales', new ArrayItemIterator($press->getSupportedLocaleNames(), $rangeInfo->getPage(), $rangeInfo->getCount()));
34 $templateMgr->assign('masterLocale', MASTER_LOCALE
);
36 $templateMgr->display($plugin->getTemplatePath() . 'index.tpl');
39 function edit($args) {
40 list($plugin) = CustomLocaleHandler
::validate();
41 CustomLocaleHandler
::setupTemplate($plugin, true);
43 $locale = array_shift($args);
44 $file = array_shift($args);
46 if (!Locale
::isLocaleValid($locale)) {
47 $path = array($plugin->getCategory(), $plugin->getName(), 'index');
48 Request
::redirect(null, null, null, $path);
50 $localeFiles = CustomLocaleAction
::getLocaleFiles($locale);
52 $templateMgr =& TemplateManager
::getManager();
54 $localeFilesRangeInfo = Handler
::getRangeInfo('localeFiles');
56 import('core.ArrayItemIterator');
57 $templateMgr->assign('localeFiles', new ArrayItemIterator($localeFiles, $localeFilesRangeInfo->getPage(), $localeFilesRangeInfo->getCount()));
59 $templateMgr->assign('locale', $locale);
60 $templateMgr->assign('masterLocale', MASTER_LOCALE
);
62 $templateMgr->display($plugin->getTemplatePath() . 'locale.tpl');
65 function editLocaleFile($args) {
66 list($plugin) = CustomLocaleHandler
::validate();
67 CustomLocaleHandler
::setupTemplate($plugin, true);
69 $locale = array_shift($args);
70 if (!Locale
::isLocaleValid($locale)) {
71 $path = array($plugin->getCategory(), $plugin->getName(), 'index');
72 Request
::redirect(null, null, null, $path);
75 $filename = urldecode(urldecode(array_shift($args)));
76 if (!CustomLocaleAction
::isLocaleFile($locale, $filename)) {
77 $path = array($plugin->getCategory(), $plugin->getName(), 'edit', $locale);
78 Request
::redirect(null, null, null, $path);
81 $templateMgr =& TemplateManager
::getManager();
83 import('file.FileManager');
84 import('file.EditableLocaleFile');
85 $press = Request
::getPress();
86 $pressId = $press->getId();
87 $publicFilesDir = Config
::getVar('files', 'public_files_dir');
88 $customLocaleDir = $publicFilesDir . DIRECTORY_SEPARATOR
. 'presses' . DIRECTORY_SEPARATOR
. $pressId . DIRECTORY_SEPARATOR
. CUSTOM_LOCALE_DIR
;
89 $customLocalePath = $customLocaleDir . DIRECTORY_SEPARATOR
. $locale . DIRECTORY_SEPARATOR
. $filename;
90 if (FileManager
::fileExists($customLocalePath)) {
91 $localeContents = EditableLocaleFile
::load($customLocalePath);
93 $localeContents = null;
96 $referenceLocaleContents = EditableLocaleFile
::load($filename);
97 $referenceLocaleContentsRangeInfo = Handler
::getRangeInfo('referenceLocaleContents');
99 // Handle a search, if one was executed.
100 $searchKey = Request
::getUserVar('searchKey');
104 if (!empty($searchKey)) foreach ($referenceLocaleContents as $key => $value) {
105 if ($index %
$referenceLocaleContentsRangeInfo->getCount() == 0) $pageIndex++
;
106 if ($key == $searchKey) {
114 $referenceLocaleContentsRangeInfo->setPage($pageIndex);
115 $templateMgr->assign('searchKey', $searchKey);
118 $templateMgr->assign('filename', $filename);
119 $templateMgr->assign('locale', $locale);
120 import('core.ArrayItemIterator');
121 $templateMgr->assign_by_ref('referenceLocaleContents', new ArrayItemIterator($referenceLocaleContents, $referenceLocaleContentsRangeInfo->getPage(), $referenceLocaleContentsRangeInfo->getCount()));
122 $templateMgr->assign('localeContents', $localeContents);
124 $templateMgr->display($plugin->getTemplatePath() . 'localeFile.tpl');
127 function saveLocaleFile($args) {
128 list($plugin) = CustomLocaleHandler
::validate();
129 CustomLocaleHandler
::setupTemplate($plugin, true);
131 $locale = array_shift($args);
132 if (!Locale
::isLocaleValid($locale)) {
133 $path = array($plugin->getCategory(), $plugin->getName(), 'index');
134 Request
::redirect(null, null, null, $path);
137 $filename = urldecode(urldecode(array_shift($args)));
138 if (!CustomLocaleAction
::isLocaleFile($locale, $filename)) {
139 $path = array($plugin->getCategory(), $plugin->getName(), 'edit', $locale);
140 Request
::redirect(null, null, null, $path);
143 $press =& Request
::getPress();
144 $pressId = $press->getId();
145 $changes = Request
::getUserVar('changes');
146 $customFilesDir = Config
::getVar('files', 'public_files_dir') . DIRECTORY_SEPARATOR
. 'presses' . DIRECTORY_SEPARATOR
. $pressId . DIRECTORY_SEPARATOR
. CUSTOM_LOCALE_DIR
. DIRECTORY_SEPARATOR
. $locale;
147 $customFilePath = $customFilesDir . DIRECTORY_SEPARATOR
. $filename;
149 // Create empty custom locale file if it doesn't exist
150 import('file.FileManager');
151 import('file.EditableLocaleFile');
152 if (!FileManager
::fileExists($customFilePath)) {
153 $numParentDirs = substr_count($customFilePath, DIRECTORY_SEPARATOR
);
155 for ($i=0; $i<$numParentDirs; $i++
) {
156 $parentDirs .= '..' . DIRECTORY_SEPARATOR
;
159 $newFileContents = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
160 $newFileContents .= '<!DOCTYPE locale SYSTEM "' . $parentDirs . 'lib' . DIRECTORY_SEPARATOR
. 'pkp' . DIRECTORY_SEPARATOR
. 'dtd' . DIRECTORY_SEPARATOR
. 'locale.dtd' . '">' . "\n";
161 $newFileContents .= '<locale name="' . $locale . '">' . "\n";
162 $newFileContents .= '</locale>';
163 FileManager
::writeFile($customFilePath, $newFileContents);
166 $file = new EditableLocaleFile($locale, $customFilePath);
168 while (!empty($changes)) {
169 $key = array_shift($changes);
170 $value = CustomLocaleHandler
::correctCr(array_shift($changes));
171 if (!empty($value)) {
172 if (!$file->update($key, $value)) {
173 $file->insert($key, $value);
181 Request
::redirectUrl(Request
::getUserVar('redirectUrl'));
184 function correctCr($value) {
185 return str_replace("\r\n", "\n", $value);
188 function setupTemplate(&$plugin, $subclass = true) {
189 parent
::setupTemplate();
190 $templateMgr =& TemplateManager
::getManager();
191 $templateMgr->register_function('plugin_url', array(&$plugin, 'smartyPluginUrl'));
192 $pageHierarchy = array(array(Request
::url(null, 'user'), 'navigation.user'), array(Request
::url(null, 'manager'), 'user.role.manager'));
194 $path = array($plugin->getCategory(), $plugin->getName(), 'index');
195 $pageHierarchy[] = array(Request
::url(null, null, null, $path), 'plugins.generic.customLocale.name');
197 $templateMgr->assign('pageHierarchy', $pageHierarchy);
198 $templateMgr->assign('helpTopicId', 'plugins.generic.CustomLocalePlugin');
201 function validate() {
202 parent
::validate(true);
204 if (!Validation
::isPressManager()) {
205 Validation
::redirectLogin();
208 $plugin =& PluginRegistry
::getPlugin('generic', 'CustomLocalePlugin');
209 return array(&$plugin);