• Main Page
  • Modules
  • Classes
  • Files
  • File List

controllers/grid/admin/languages/AdminLanguageGridHandler.inc.php

00001 <?php
00002 
00016 import('classes.controllers.grid.languages.LanguageGridHandler');
00017 
00018 import('controllers.grid.languages.LanguageGridRow');
00019 import('controllers.grid.languages.form.InstallLanguageForm');
00020 
00021 class AdminLanguageGridHandler extends LanguageGridHandler {
00025    function AdminLanguageGridHandler() {
00026       parent::LanguageGridHandler();
00027       $this->addRoleAssignment(array(
00028          ROLE_ID_SITE_ADMIN),
00029          array('fetchGrid', 'fetchRow', 'installLocale', 'saveInstallLocale', 'uninstallLocale',
00030             'downloadLocale', 'disableLocale', 'enableLocale', 'reloadLocale', 'setPrimaryLocale'));
00031    }
00032 
00033 
00034    //
00035    // Implement template methods from PKPHandler.
00036    //
00040    function initialize(&$request) {
00041       parent::initialize($request);
00042 
00043       AppLocale::requireComponents(
00044          LOCALE_COMPONENT_OMP_ADMIN,
00045          LOCALE_COMPONENT_PKP_ADMIN
00046       );
00047 
00048       // Grid actions.
00049       $router =& $request->getRouter();
00050 
00051       import('lib.pkp.classes.linkAction.request.AjaxModal');
00052       $this->addAction(
00053          new LinkAction(
00054             'installLocale',
00055             new AjaxModal(
00056                $router->url($request, null, null, 'installLocale', null, null),
00057                __('admin.languages.installLocale'),
00058                null,
00059                true
00060                ),
00061             __('admin.languages.installLocale'),
00062             'add')
00063       );
00064 
00065       $cellProvider = $this->getCellProvider();
00066 
00067       // Columns.
00068       // Enable locale.
00069       $this->addColumn(
00070          new GridColumn(
00071             'enable',
00072             'common.enable',
00073             null,
00074             'controllers/grid/common/cell/selectStatusCell.tpl',
00075             $cellProvider,
00076             array('width' => 10)
00077          )
00078       );
00079 
00080       // Locale name.
00081       $this->addNameColumn();
00082 
00083       // Primary locale.
00084       if ($this->_canManage($request)) {
00085          $primaryId = 'pressPrimary';
00086       } else {
00087          $primaryId = 'sitePrimary';
00088       }
00089       $this->addPrimaryColumn($primaryId);
00090 
00091       if ($this->_canManage($request)) {
00092          $this->addManagementColumns();
00093       }
00094 
00095       if ($this->_canManage($request)) {
00096          $instructions = 'manager.languages.languageInstructions';
00097       } else {
00098          $instructions = 'admin.languages.supportedLocalesInstructions';
00099       }
00100       $this->setInstructions($instructions);
00101       $this->setFootNote('admin.locale.maybeIncomplete');
00102    }
00103 
00104 
00105    //
00106    // Implement methods from GridHandler.
00107    //
00111    function &getRowInstance() {
00112       $row = new LanguageGridRow();
00113       return $row;
00114    }
00115 
00119    function loadData(&$request, $filter) {
00120       $site =& $request->getSite();
00121       $data = array();
00122 
00123       $allLocales = AppLocale::getAllLocales();
00124       $installedLocales = $site->getInstalledLocales();
00125       $supportedLocales = $site->getSupportedLocales();
00126       $primaryLocale = $site->getPrimaryLocale();
00127 
00128       foreach($installedLocales as $localeKey) {
00129          $data[$localeKey] = array();
00130          $data[$localeKey]['name'] = $allLocales[$localeKey];
00131          $data[$localeKey]['incomplete'] = !AppLocale::isLocaleComplete($localeKey);
00132          if (in_array($localeKey, $supportedLocales)) {
00133             $supported = true;
00134          } else {
00135             $supported = false;
00136          }
00137          $data[$localeKey]['supported'] = $supported;
00138 
00139          if ($this->_canManage($request)) {
00140             $press =& $request->getPress();
00141             $primaryLocale = $press->getPrimaryLocale();
00142          }
00143 
00144          if ($localeKey == $primaryLocale) {
00145             $primary = true;
00146          } else {
00147             $primary = false;
00148          }
00149          $data[$localeKey]['primary'] = $primary;
00150       }
00151 
00152       if ($this->_canManage($request)) {
00153          $data = $this->addManagementData($request, $data);
00154       }
00155 
00156       return $data;
00157    }
00158 
00159 
00160    //
00161    // Public grid actions.
00162    //
00168    function installLocale($args, &$request) {
00169       // Form handling.
00170       $installLanguageForm = new InstallLanguageForm();
00171       $installLanguageForm->initData($request);
00172       $json = new JSONMessage(true, $installLanguageForm->fetch($request));
00173 
00174       return $json->getString();
00175    }
00176 
00182    function saveInstallLocale($args, &$request) {
00183       $installLanguageForm = new InstallLanguageForm();
00184       $installLanguageForm->readInputData($request);
00185 
00186       if ($installLanguageForm->validate($request)) {
00187          $installLanguageForm->execute($request);
00188          $this->_updatePressLocaleSettings($request);
00189 
00190          $notificationManager = new NotificationManager();
00191          $user =& $request->getUser();
00192          $notificationManager->createTrivialNotification(
00193             $user->getId(), NOTIFICATION_TYPE_SUCCESS,
00194             array('contents' => __('notification.localeInstalled'))
00195          );
00196       }
00197       return DAO::getDataChangedEvent();
00198    }
00199 
00205    function downloadLocale($args, &$request) {
00206       $this->setupTemplate($request, true);
00207       $locale = $request->getUserVar('locale');
00208 
00209       import('classes.i18n.LanguageAction');
00210       $languageAction = new LanguageAction();
00211 
00212       if (!$languageAction->isDownloadAvailable() || !preg_match('/^[a-z]{2}_[A-Z]{2}$/', $locale)) {
00213          $request->redirect(null, 'admin', 'settings');
00214       }
00215 
00216       $notificationManager = new NotificationManager();
00217       $user =& $request->getUser();
00218       $json = new JSONMessage(true);
00219 
00220       $errors = array();
00221       if (!$languageAction->downloadLocale($locale, $errors)) {
00222          $notificationManager->createTrivialNotification(
00223             $user->getId(),
00224             NOTIFICATION_TYPE_ERROR,
00225             array('contents' => $errors));
00226          $json->setEvent('refreshForm', $this->_fetchReviewerForm($args, $request));
00227       } else {
00228          $notificationManager->createTrivialNotification(
00229             $user->getId(),
00230             NOTIFICATION_TYPE_SUCCESS,
00231             array('contentLocaleKey' => __('admin.languages.localeInstalled'),
00232                 'params' => array('locale' => $locale)));
00233       }
00234 
00235       // Refresh form.
00236       $installLanguageForm = new InstallLanguageForm();
00237       $installLanguageForm->initData($request);
00238       $json->setEvent('refreshForm', $installLanguageForm->fetch($request));
00239       return $json->getString();
00240    }
00241 
00247    function uninstallLocale($args, &$request) {
00248       $site =& $request->getSite();
00249       $locale = $request->getUserVar('rowId');
00250       $gridData = $this->getGridDataElements($request);
00251       $site =& $request->getSite();
00252 
00253       if (array_key_exists($locale, $gridData)) {
00254          $localeData = $gridData[$locale];
00255          if (!$localeData['primary']) {
00256             $installedLocales = $site->getInstalledLocales();
00257 
00258             if (in_array($locale, $installedLocales)) {
00259                $installedLocales = array_diff($installedLocales, array($locale));
00260                $site->setInstalledLocales($installedLocales);
00261                $supportedLocales = $site->getSupportedLocales();
00262                $supportedLocales = array_diff($supportedLocales, array($locale));
00263                $site->setSupportedLocales($supportedLocales);
00264                $siteDao =& DAORegistry::getDAO('SiteDAO');
00265                $siteDao->updateObject($site);
00266 
00267                $this->_updatePressLocaleSettings($request);
00268                AppLocale::uninstallLocale($locale);
00269 
00270                $notificationManager = new NotificationManager();
00271                $user =& $request->getUser();
00272                $notificationManager->createTrivialNotification(
00273                   $user->getId(), NOTIFICATION_TYPE_SUCCESS,
00274                   array('contents' => __('notification.localeUninstalled', array('locale' => $localeData['name'])))
00275                );
00276             }
00277          }
00278 
00279       }
00280 
00281       return DAO::getDataChangedEvent($locale);
00282    }
00283 
00289    function enableLocale($args, &$request) {
00290       $rowId = $request->getUserVar('rowId');
00291       $gridData = $this->getGridDataElements($request);
00292 
00293       if (array_key_exists($rowId, $gridData)) {
00294          $this->_updateLocaleSupportState($request, $rowId, true);
00295 
00296          $notificationManager = new NotificationManager();
00297          $user =& $request->getUser();
00298          $notificationManager->createTrivialNotification(
00299             $user->getId(), NOTIFICATION_TYPE_SUCCESS,
00300             array('contents' => __('notification.localeEnabled'))
00301          );
00302       }
00303 
00304       return DAO::getDataChangedEvent($rowId);
00305    }
00306 
00312    function disableLocale($args, &$request) {
00313       $rowId = $request->getUserVar('rowId');
00314       $gridData = $this->getGridDataElements($request);
00315       $notificationManager = new NotificationManager();
00316       $user =& $request->getUser();
00317 
00318       if (array_key_exists($rowId, $gridData)) {
00319          // Don't disable primary locales.
00320          if ($gridData[$rowId]['primary']) {
00321             $notificationManager->createTrivialNotification(
00322                $user->getId(), NOTIFICATION_TYPE_ERROR,
00323                array('contents' => __('admin.languages.cantDisable'))
00324             );
00325          } else {
00326             $locale = $rowId;
00327             $this->_updateLocaleSupportState($request, $rowId, false);
00328 
00329             $notificationManager->createTrivialNotification(
00330                $user->getId(), NOTIFICATION_TYPE_SUCCESS,
00331                array('contents' => __('notification.localeDisabled'))
00332             );
00333          }
00334       }
00335 
00336       return DAO::getDataChangedEvent($rowId);
00337    }
00338 
00344    function reloadLocale($args, &$request) {
00345       $site =& $request->getSite();
00346       $locale = $request->getUserVar('rowId');
00347 
00348       $gridData = $this->getGridDataElements($request);
00349       if (array_key_exists($locale, $gridData)) {
00350          AppLocale::reloadLocale($locale);
00351          $notificationManager = new NotificationManager();
00352          $user =& $request->getUser();
00353          $notificationManager->createTrivialNotification(
00354             $user->getId(), NOTIFICATION_TYPE_SUCCESS,
00355             array('contents' => __('notification.localeReloaded', array('locale' => $gridData[$locale]['name'])))
00356          );
00357       }
00358 
00359       return DAO::getDataChangedEvent($locale);
00360    }
00361 
00362 
00368    function setPrimaryLocale($args, &$request) {
00369       $rowId = $request->getUserVar('rowId');
00370       $gridData = $this->getGridDataElements($request);
00371       $localeData = $gridData[$rowId];
00372       $notificationManager = new NotificationManager();
00373       $user =& $request->getUser();
00374       $site =& $request->getSite();
00375 
00376       if (array_key_exists($rowId, $gridData)) {
00377          if (AppLocale::isLocaleValid($rowId)) {
00378             $site->setPrimaryLocale($rowId);
00379             $siteDao =& DAORegistry::getDAO('SiteDAO');
00380             $siteDao->updateObject($site);
00381 
00382             $notificationManager->createTrivialNotification(
00383                $user->getId(), NOTIFICATION_TYPE_SUCCESS,
00384                array('contents' => __('notification.primaryLocaleDefined', array('locale' => $localeData['name'])))
00385             );
00386          }
00387       }
00388 
00389       // Need to refresh whole grid to remove the check in others
00390       // primary locale radio buttons.
00391       return DAO::getDataChangedEvent();
00392    }
00393 
00394 
00395    //
00396    // Helper methods.
00397    //
00404    function _updateLocaleSupportState(&$request, $rowId, $enable) {
00405       $newSupportedLocales = array();
00406       $gridData = $this->getGridDataElements($request);
00407 
00408       foreach ($gridData as $locale => $data) {
00409          if ($data['supported']) {
00410             array_push($newSupportedLocales, $locale);
00411          }
00412       }
00413 
00414       if (AppLocale::isLocaleValid($rowId)) {
00415          if ($enable) {
00416             array_push($newSupportedLocales, $rowId);
00417          } else {
00418             $key = array_search($rowId, $newSupportedLocales);
00419             if ($key !== false) unset($newSupportedLocales[$key]);
00420          }
00421       }
00422 
00423       $site =& $request->getSite();
00424       $site->setSupportedLocales($newSupportedLocales);
00425 
00426       $siteDao =& DAORegistry::getDAO('SiteDAO');
00427       $siteDao->updateObject($site);
00428 
00429       $this->_updatePressLocaleSettings($request);
00430    }
00431 
00437    function _updatePressLocaleSettings(&$request) {
00438       $site =& $request->getSite();
00439       $siteSupportedLocales = $site->getSupportedLocales();
00440 
00441       $pressDao =& DAORegistry::getDAO('PressDAO');
00442       $settingsDao =& DAORegistry::getDAO('PressSettingsDAO');
00443       $presses =& $pressDao->getPresses();
00444       $presses =& $presses->toArray();
00445       foreach ($presses as $press) {
00446          $primaryLocale = $press->getPrimaryLocale();
00447          $supportedLocales = $press->getSetting('supportedLocales');
00448 
00449          if (isset($primaryLocale) && !in_array($primaryLocale, $siteSupportedLocales)) {
00450             $press->setPrimaryLocale($site->getPrimaryLocale());
00451             $pressDao->updateObject($press);
00452          }
00453 
00454          if (is_array($supportedLocales)) {
00455             $supportedLocales = array_intersect($supportedLocales, $siteSupportedLocales);
00456             $settingsDao->updateSetting($press->getId(), 'supportedLocales', $supportedLocales, 'object');
00457          }
00458       }
00459    }
00460 
00467    function _canManage($request) {
00468       $pressDao =& DAORegistry::getDAO('PressDAO');
00469       $presses =& $pressDao->getPresses();
00470       $userRoles = $this->getAuthorizedContextObject(ASSOC_TYPE_USER_ROLES);
00471       $press =& $request->getPress();
00472       return ($presses->getCount() == 1 && $press && in_array(ROLE_ID_PRESS_MANAGER, $userRoles));
00473    }
00474 }
00475 ?>

Generated on Mon Sep 17 2012 13:58:55 for Open Monograph Press by  doxygen 1.7.1