baseline
[omp.pkp.sfu.ca.git] / pages / admin / AdminLanguagesHandler.inc.php
bloba7ca7a0858a96a00a34f2c1529a8b99139feb065
1 <?php
3 /**
4 * @file pages/admin/AdminLanguagesHandler.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 AdminLanguagesHandler
10 * @ingroup pages_admin
12 * @brief Handle requests for changing site language settings.
15 // $Id: AdminLanguagesHandler.inc.php,v 1.8 2009/09/22 19:22:10 asmecher Exp $
17 import('pages.admin.AdminHandler');
19 class AdminLanguagesHandler extends AdminHandler {
20 /**
21 * Constructor
23 function AdminLanguagesHandler() {
24 parent::AdminHandler();
27 /**
28 * Display form to modify site language settings.
30 function languages() {
31 $this->validate();
32 $this->setupTemplate(true);
34 $site =& Request::getSite();
36 $templateMgr =& TemplateManager::getManager();
37 $templateMgr->assign('localeNames', Locale::getAllLocales());
38 $templateMgr->assign('primaryLocale', $site->getPrimaryLocale());
39 $templateMgr->assign('supportedLocales', $site->getSupportedLocales());
40 $localesComplete = array();
41 foreach (Locale::getAllLocales() as $key => $name) {
42 $localesComplete[$key] = Locale::isLocaleComplete($key);
44 $templateMgr->assign('localesComplete', $localesComplete);
46 $templateMgr->assign('installedLocales', $site->getInstalledLocales());
47 $templateMgr->assign('uninstalledLocales', array_diff(array_keys(Locale::getAllLocales()), $site->getInstalledLocales()));
48 $templateMgr->assign('helpTopicId', 'site.siteManagement');
50 import('i18n.LanguageAction');
51 $languageAction = new LanguageAction();
52 if ($languageAction->isDownloadAvailable()) {
53 $templateMgr->assign('downloadAvailable', true);
54 $templateMgr->assign('downloadableLocales', $languageAction->getDownloadableLocales());
57 $templateMgr->display('admin/languages.tpl');
60 /**
61 * Update language settings.
63 function saveLanguageSettings() {
64 $this->validate();
65 $this->setupTemplate(true);
67 $site =& Request::getSite();
69 $primaryLocale = Request::getUserVar('primaryLocale');
70 $supportedLocales = Request::getUserVar('supportedLocales');
72 if (Locale::isLocaleValid($primaryLocale)) {
73 $site->setPrimaryLocale($primaryLocale);
76 $newSupportedLocales = array();
77 if (isset($supportedLocales) && is_array($supportedLocales)) {
78 foreach ($supportedLocales as $locale) {
79 if (Locale::isLocaleValid($locale)) {
80 array_push($newSupportedLocales, $locale);
84 if (!in_array($primaryLocale, $newSupportedLocales)) {
85 array_push($newSupportedLocales, $primaryLocale);
87 $site->setSupportedLocales($newSupportedLocales);
89 $siteDao =& DAORegistry::getDAO('SiteDAO');
90 $siteDao->updateObject($site);
92 AdminLanguagesHandler::removeLocalesFromPresses();
94 $templateMgr =& TemplateManager::getManager();
95 $templateMgr->assign(array(
96 'currentUrl' => Request::url(null, null, 'languages'),
97 'pageTitle' => 'common.languages',
98 'message' => 'common.changesSaved',
99 'backLink' => Request::url(null, 'admin'),
100 'backLinkLabel' => 'admin.siteAdmin'
102 $templateMgr->display('common/message.tpl');
106 * Install a new locale.
108 function installLocale() {
109 $this->validate();
111 $site =& Request::getSite();
112 $installLocale = Request::getUserVar('installLocale');
114 if (isset($installLocale) && is_array($installLocale)) {
115 $installedLocales = $site->getInstalledLocales();
117 foreach ($installLocale as $locale) {
118 if (Locale::isLocaleValid($locale) && !in_array($locale, $installedLocales)) {
119 array_push($installedLocales, $locale);
120 Locale::installLocale($locale);
124 $site->setInstalledLocales($installedLocales);
125 $siteDao =& DAORegistry::getDAO('SiteDAO');
126 $siteDao->updateObject($site);
129 Request::redirect(null, null, 'languages');
133 * Uninstall a locale
135 function uninstallLocale() {
136 $this->validate();
138 $site =& Request::getSite();
139 $locale = Request::getUserVar('locale');
141 if (isset($locale) && !empty($locale) && $locale != $site->getPrimaryLocale()) {
142 $installedLocales = $site->getInstalledLocales();
144 if (in_array($locale, $installedLocales)) {
145 $installedLocales = array_diff($installedLocales, array($locale));
146 $site->setInstalledLocales($installedLocales);
147 $supportedLocales = $site->getSupportedLocales();
148 $supportedLocales = array_diff($supportedLocales, array($locale));
149 $site->setSupportedLocales($supportedLocales);
150 $siteDao =& DAORegistry::getDAO('SiteDAO');
151 $siteDao->updateObject($site);
153 AdminLanguagesHandler::removeLocalesFromPresses();
154 Locale::uninstallLocale($locale);
158 Request::redirect(null, null, 'languages');
162 * Reload data for an installed locale.
164 function reloadLocale() {
165 $this->validate();
167 $site =& Request::getSite();
168 $locale = Request::getUserVar('locale');
170 if (in_array($locale, $site->getInstalledLocales())) {
171 Locale::reloadLocale($locale);
174 Request::redirect(null, null, 'languages');
178 * Helper function to remove unsupported locales from presses.
180 function removeLocalesFromPresses() {
181 $site =& Request::getSite();
182 $siteSupportedLocales = $site->getSupportedLocales();
184 $pressDao =& DAORegistry::getDAO('PressDAO');
185 $settingsDao =& DAORegistry::getDAO('PressSettingsDAO');
186 $presses =& $pressDao->getPresses();
187 $presses =& $presses->toArray();
188 foreach ($presses as $press) {
189 $primaryLocale = $press->getPrimaryLocale();
190 $supportedLocales = $press->getSetting('supportedLocales');
192 if (isset($primaryLocale) && !in_array($primaryLocale, $siteSupportedLocales)) {
193 $press->setPrimaryLocale($site->getPrimaryLocale());
194 $pressDao->updatePress($press);
197 if (is_array($supportedLocales)) {
198 $supportedLocales = array_intersect($supportedLocales, $siteSupportedLocales);
199 $settingsDao->updateSetting($press->getId(), 'supportedLocales', $supportedLocales, 'object');
205 * Download a locale from the PKP web site.
207 function downloadLocale() {
208 $this->validate();
209 $locale = Request::getUserVar('locale');
211 import('i18n.LanguageAction');
212 $languageAction = new LanguageAction();
214 if (!$languageAction->isDownloadAvailable()) Request::redirect(null, null, 'languages');
216 if (!preg_match('/^[a-z]{2}_[A-Z]{2}$/', $locale)) {
217 Request::redirect(null, null, 'languages');
220 $templateMgr =& TemplateManager::getManager();
222 $errors = array();
223 if (!$languageAction->downloadLocale($locale, $errors)) {
224 $templateMgr->assign('errors', $errors);
225 $templateMgr->display('admin/languageDownloadErrors.tpl');
226 return;
228 $templateMgr->assign('messageTranslated', Locale::translate('admin.languages.localeInstalled', array('locale' => $locale)));
229 $templateMgr->assign('backLink', Request::url(null, null, 'languages'));
230 $templateMgr->assign('backLinkLabel', 'admin.languages.languageSettings');
231 $templateMgr->display('common/message.tpl');