00001 <?php
00002
00015
00016
00017
00018 require_once('CustomLocalePlugin.inc.php');
00019 require_once('CustomLocaleAction.inc.php');
00020
00021 class CustomLocaleHandler extends Handler {
00022
00023 function index() {
00024 list($plugin) = CustomLocaleHandler::validate();
00025 CustomLocaleHandler::setupTemplate($plugin, false);
00026
00027 $journal = Request::getJournal();
00028 $rangeInfo = Handler::getRangeInfo('locales');
00029
00030 $templateMgr =& TemplateManager::getManager();
00031 $templateMgr->assign('locales', new ArrayItemIterator($journal->getSupportedLocaleNames(), $rangeInfo->getPage(), $rangeInfo->getCount()));
00032 $templateMgr->assign('masterLocale', MASTER_LOCALE);
00033
00034 $templateMgr->display($plugin->getTemplatePath() . 'index.tpl');
00035 }
00036
00037 function edit($args) {
00038 list($plugin) = CustomLocaleHandler::validate();
00039 CustomLocaleHandler::setupTemplate($plugin, true);
00040
00041 $locale = array_shift($args);
00042 $file = array_shift($args);
00043
00044 if (!Locale::isLocaleValid($locale)) {
00045 $path = array($plugin->getCategory(), $plugin->getName(), 'index');
00046 Request::redirect(null, null, null, $path);
00047 }
00048 $localeFiles = CustomLocaleAction::getLocaleFiles($locale);
00049
00050 $templateMgr =& TemplateManager::getManager();
00051
00052 $localeFilesRangeInfo = Handler::getRangeInfo('localeFiles');
00053
00054 $templateMgr->assign('localeFiles', new ArrayItemIterator($localeFiles, $localeFilesRangeInfo->getPage(), $localeFilesRangeInfo->getCount()));
00055
00056 $templateMgr->assign('locale', $locale);
00057 $templateMgr->assign('masterLocale', MASTER_LOCALE);
00058
00059 $templateMgr->display($plugin->getTemplatePath() . 'locale.tpl');
00060 }
00061
00062 function editLocaleFile($args) {
00063 list($plugin) = CustomLocaleHandler::validate();
00064 CustomLocaleHandler::setupTemplate($plugin, true);
00065
00066 $locale = array_shift($args);
00067 if (!Locale::isLocaleValid($locale)) {
00068 $path = array($plugin->getCategory(), $plugin->getName(), 'index');
00069 Request::redirect(null, null, null, $path);
00070 }
00071
00072 $filename = urldecode(urldecode(array_shift($args)));
00073 if (!CustomLocaleAction::isLocaleFile($locale, $filename)) {
00074 $path = array($plugin->getCategory(), $plugin->getName(), 'edit', $locale);
00075 Request::redirect(null, null, null, $path);
00076 }
00077
00078 $templateMgr =& TemplateManager::getManager();
00079
00080 import('file.FileManager');
00081 import('i18n.EditableLocaleFile');
00082 $journal = Request::getJournal();
00083 $journalId = $journal->getJournalId();
00084 $publicFilesDir = Config::getVar('files', 'public_files_dir');
00085 $customLocaleDir = $publicFilesDir . DIRECTORY_SEPARATOR . 'journals' . DIRECTORY_SEPARATOR . $journalId . DIRECTORY_SEPARATOR . CUSTOM_LOCALE_DIR;
00086 $customLocalePath = $customLocaleDir . DIRECTORY_SEPARATOR . $locale . DIRECTORY_SEPARATOR . $filename;
00087 if (FileManager::fileExists($customLocalePath)) {
00088 $localeContents = EditableLocaleFile::load($customLocalePath);
00089 } else {
00090 $localeContents = null;
00091 }
00092
00093 $referenceLocaleContents = EditableLocaleFile::load($filename);
00094 $referenceLocaleContentsRangeInfo = Handler::getRangeInfo('referenceLocaleContents');
00095
00096
00097 $searchKey = Request::getUserVar('searchKey');
00098 $found = false;
00099 $index = 0;
00100 $pageIndex = 0;
00101 if (!empty($searchKey)) foreach ($referenceLocaleContents as $key => $value) {
00102 if ($index % $referenceLocaleContentsRangeInfo->getCount() == 0) $pageIndex++;
00103 if ($key == $searchKey) {
00104 $found = true;
00105 break;
00106 }
00107 $index++;
00108 }
00109
00110 if ($found) {
00111 $referenceLocaleContentsRangeInfo->setPage($pageIndex);
00112 $templateMgr->assign('searchKey', $searchKey);
00113 }
00114
00115 $templateMgr->assign('filename', $filename);
00116 $templateMgr->assign('locale', $locale);
00117 $templateMgr->assign_by_ref('referenceLocaleContents', new ArrayItemIterator($referenceLocaleContents, $referenceLocaleContentsRangeInfo->getPage(), $referenceLocaleContentsRangeInfo->getCount()));
00118 $templateMgr->assign('localeContents', $localeContents);
00119
00120 $templateMgr->display($plugin->getTemplatePath() . 'localeFile.tpl');
00121 }
00122
00123 function saveLocaleFile($args) {
00124 list($plugin) = CustomLocaleHandler::validate();
00125 CustomLocaleHandler::setupTemplate($plugin, true);
00126
00127 $locale = array_shift($args);
00128 if (!Locale::isLocaleValid($locale)) {
00129 $path = array($plugin->getCategory(), $plugin->getName(), 'index');
00130 Request::redirect(null, null, null, $path);
00131 }
00132
00133 $filename = urldecode(urldecode(array_shift($args)));
00134 if (!CustomLocaleAction::isLocaleFile($locale, $filename)) {
00135 $path = array($plugin->getCategory(), $plugin->getName(), 'edit', $locale);
00136 Request::redirect(null, null, null, $path);
00137 }
00138
00139 $journal = &Request::getJournal();
00140 $journalId = $journal->getJournalId();
00141 $changes = Request::getUserVar('changes');
00142 $customFilesDir = Config::getVar('files', 'public_files_dir') . DIRECTORY_SEPARATOR . 'journals' . DIRECTORY_SEPARATOR . $journalId . DIRECTORY_SEPARATOR . CUSTOM_LOCALE_DIR . DIRECTORY_SEPARATOR . $locale;
00143 $customFilePath = $customFilesDir . DIRECTORY_SEPARATOR . $filename;
00144
00145
00146 import('file.FileManager');
00147 import('i18n.EditableLocaleFile');
00148 if (!FileManager::fileExists($customFilePath)) {
00149 $numParentDirs = substr_count($customFilePath, DIRECTORY_SEPARATOR);
00150 $parentDirs = '';
00151 for ($i=0; $i<$numParentDirs; $i++) {
00152 $parentDirs .= '..' . DIRECTORY_SEPARATOR;
00153 }
00154
00155 $newFileContents = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
00156 $newFileContents .= '<!DOCTYPE locale SYSTEM "' . $parentDirs . 'locale' . DIRECTORY_SEPARATOR . 'locale.dtd' . '">' . "\n";
00157 $newFileContents .= '<locale name="' . $locale . '">' . "\n";
00158 $newFileContents .= '</locale>';
00159 FileManager::writeFile($customFilePath, $newFileContents);
00160 }
00161
00162 $file =& new EditableLocaleFile($locale, $customFilePath);
00163
00164 while (!empty($changes)) {
00165 $key = array_shift($changes);
00166 $value = CustomLocaleHandler::correctCr(array_shift($changes));
00167 if (!empty($value)) {
00168 if (!$file->update($key, $value)) {
00169 $file->insert($key, $value);
00170 }
00171 } else {
00172 $file->delete($key);
00173 }
00174 }
00175 $file->write();
00176
00177 Request::redirectUrl(Request::getUserVar('redirectUrl'));
00178 }
00179
00180 function correctCr($value) {
00181 return str_replace("\r\n", "\n", $value);
00182 }
00183
00184 function setupTemplate(&$plugin, $subclass = true) {
00185 $templateMgr = &TemplateManager::getManager();
00186 $templateMgr->register_function('plugin_url', array(&$plugin, 'smartyPluginUrl'));
00187 $pageHierarchy = array(array(Request::url(null, 'user'), 'navigation.user'), array(Request::url(null, 'manager'), 'user.role.manager'));
00188 if ($subclass) {
00189 $path = array($plugin->getCategory(), $plugin->getName(), 'index');
00190 $pageHierarchy[] = array(Request::url(null, null, null, $path), 'plugins.generic.customLocale.name');
00191 }
00192 $templateMgr->assign('pageHierarchy', $pageHierarchy);
00193 $templateMgr->assign('helpTopicId', 'plugins.generic.CustomLocalePlugin');
00194 }
00195
00196 function validate() {
00197 parent::validate(true);
00198
00199 if (!Validation::isJournalManager()) {
00200 Validation::redirectLogin();
00201 }
00202
00203 $plugin = &PluginRegistry::getPlugin('generic', 'CustomLocalePlugin');
00204 return array(&$plugin);
00205 }
00206
00207 }
00208 ?>