first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-06-08 17:09:23 -04:00
commit df3a033196
17887 changed files with 8637778 additions and 0 deletions
@@ -0,0 +1,146 @@
<?php
/**
* @file controllers/grid/languages/LanguageGridCellProvider.php
*
* Copyright (c) 2014-2021 Simon Fraser University
* Copyright (c) 2003-2021 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class LanguageGridCellProvider
*
* @ingroup controllers_grid_languages
*
* @brief Subclass for a language grid column's cell provider
*/
namespace PKP\controllers\grid\languages;
use PKP\controllers\grid\GridCellProvider;
use PKP\controllers\grid\GridHandler;
use PKP\linkAction\LinkAction;
use PKP\linkAction\request\AjaxAction;
use PKP\linkAction\request\RemoteActionConfirmationModal;
class LanguageGridCellProvider extends GridCellProvider
{
/**
* @copydoc GridCellProvider::getTemplateVarsFromRowColumn()
*/
public function getTemplateVarsFromRowColumn($row, $column)
{
$element = $row->getData();
$columnId = $column->getId();
switch ($columnId) {
case 'enable':
return ['selected' => $element['supported'],
'disabled' => false];
case 'locale':
$label = $element['name'];
$returnArray = ['label' => $label];
if (isset($element['incomplete'])) {
$returnArray['incomplete'] = $element['incomplete'];
}
return $returnArray;
case 'code':
$label = $element['code'];
$returnArray = ['label' => $label];
return $returnArray;
case 'sitePrimary':
return ['selected' => $element['primary'],
'disabled' => !$element['supported']];
case 'contextPrimary':
return ['selected' => $element['primary'],
'disabled' => !$element['supported']];
case 'uiLocale':
return ['selected' => $element['supportedLocales'],
'disabled' => !$element['supported']];
case 'formLocale':
return ['selected' => $element['supportedFormLocales'],
'disabled' => !$element['supported']];
case 'submissionLocale':
return ['selected' => $element['supportedSubmissionLocales'],
'disabled' => !$element['supported']];
default:
assert(false);
break;
}
}
/**
* @copydoc GridCellProvider::getCellActions()
*/
public function getCellActions($request, $row, $column, $position = GridHandler::GRID_ACTION_POSITION_DEFAULT)
{
$element = $row->getData();
$router = $request->getRouter();
$actions = [];
$actionArgs = ['rowId' => $row->getId()];
$action = null;
$actionRequest = null;
switch ($column->getId()) {
case 'enable':
$enabled = $element['supported'];
if ($enabled) {
$action = 'disable-' . $row->getId();
$actionRequest = new RemoteActionConfirmationModal(
$request->getSession(),
__('admin.languages.confirmDisable'),
__('common.disable'),
$router->url($request, null, null, 'disableLocale', null, $actionArgs)
);
} else {
$action = 'enable-' . $row->getId();
$actionRequest = new AjaxAction($router->url($request, null, null, 'enableLocale', null, $actionArgs));
}
break;
case 'sitePrimary':
$primary = $element['primary'];
if (!$primary) {
$action = 'setPrimary-' . $row->getId();
$actionRequest = new RemoteActionConfirmationModal(
$request->getSession(),
__('admin.languages.confirmSitePrimaryLocaleChange'),
__('locale.primary'),
$router->url($request, null, null, 'setPrimaryLocale', null, $actionArgs)
);
}
break;
case 'contextPrimary':
$primary = $element['primary'];
if (!$primary) {
$action = 'setPrimary-' . $row->getId();
$actionRequest = new AjaxAction($router->url($request, null, null, 'setContextPrimaryLocale', null, $actionArgs));
}
break;
case 'uiLocale':
$action = 'setUiLocale-' . $row->getId();
$actionArgs['setting'] = 'supportedLocales';
$actionArgs['value'] = !$element['supportedLocales'];
$actionRequest = new AjaxAction($router->url($request, null, null, 'saveLanguageSetting', null, $actionArgs));
break;
case 'formLocale':
$action = 'setFormLocale-' . $row->getId();
$actionArgs['setting'] = 'supportedFormLocales';
$actionArgs['value'] = !$element['supportedFormLocales'];
$actionRequest = new AjaxAction($router->url($request, null, null, 'saveLanguageSetting', null, $actionArgs));
break;
case 'submissionLocale':
$action = 'setSubmissionLocale-' . $row->getId();
$actionArgs['setting'] = 'supportedSubmissionLocales';
$actionArgs['value'] = !$element['supportedSubmissionLocales'];
$actionRequest = new AjaxAction($router->url($request, null, null, 'saveLanguageSetting', null, $actionArgs));
break;
}
if ($action && $actionRequest) {
$linkAction = new LinkAction($action, $actionRequest, null, null);
$actions = [$linkAction];
}
return $actions;
}
}
@@ -0,0 +1,348 @@
<?php
/**
* @file controllers/grid/languages/LanguageGridHandler.php
*
* Copyright (c) 2014-2021 Simon Fraser University
* Copyright (c) 2000-2021 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class LanguageGridHandler
*
* @ingroup classes_controllers_grid_languages
*
* @brief Handle language grid requests.
*/
namespace PKP\controllers\grid\languages;
use APP\core\Application;
use APP\core\Request;
use APP\core\Services;
use APP\notification\NotificationManager;
use PKP\controllers\grid\GridColumn;
use PKP\controllers\grid\GridHandler;
use PKP\core\JSONMessage;
use PKP\facades\Locale;
use PKP\notification\PKPNotification;
use PKP\security\Role;
class LanguageGridHandler extends GridHandler
{
/**
* Constructor
*/
public function __construct()
{
parent::__construct();
$this->addRoleAssignment(
[Role::ROLE_ID_MANAGER, Role::ROLE_ID_SITE_ADMIN],
['saveLanguageSetting', 'setContextPrimaryLocale']
);
}
//
// Implement template methods from PKPHandler.
//
/**
* @copydoc GridHandler::initialize()
*
* @param null|mixed $args
*/
public function initialize($request, $args = null)
{
parent::initialize($request, $args);
// Basic grid configuration.
$this->setTitle('common.languages');
}
/**
* @copydoc GridHandler::getRowInstance()
*/
protected function getRowInstance()
{
return new LanguageGridRow();
}
//
// Public handler methods.
//
/**
* Save language management settings.
*
* @param array $args
* @param Request $request
*
* @return JSONMessage JSON message
*/
public function saveLanguageSetting($args, $request)
{
if (!$request->checkCSRF()) {
return new JSONMessage(false);
}
$locale = (string) $request->getUserVar('rowId');
$settingName = (string) $request->getUserVar('setting');
$settingValue = (bool) $request->getUserVar('value');
$availableLocales = $this->getGridDataElements($request);
$context = $request->getContext();
$contextService = Services::get('context');
$permittedSettings = ['supportedFormLocales', 'supportedSubmissionLocales', 'supportedLocales'];
if (in_array($settingName, $permittedSettings) && $locale) {
$currentSettingValue = (array) $context->getData($settingName);
if (Locale::isLocaleValid($locale) && array_key_exists($locale, $availableLocales)) {
if ($settingValue) {
array_push($currentSettingValue, $locale);
if ($settingName == 'supportedFormLocales') {
// reload localized default context settings
$contextService->restoreLocaleDefaults($context, $request, $locale);
} elseif ($settingName == 'supportedSubmissionLocales') {
// if a submission locale is enabled, and this locale is not in the form locales, add it
$supportedFormLocales = (array) $context->getData('supportedFormLocales');
if (!in_array($locale, $supportedFormLocales)) {
array_push($supportedFormLocales, $locale);
$context = $contextService->edit($context, ['supportedFormLocales' => $supportedFormLocales], $request);
// reload localized default context settings
$contextService->restoreLocaleDefaults($context, $request, $locale);
}
}
} else {
$key = array_search($locale, $currentSettingValue);
if ($key !== false) {
unset($currentSettingValue[$key]);
}
if ($currentSettingValue === []) {
return new JSONMessage(false, __('notification.localeSettingsCannotBeSaved'));
}
if ($settingName == 'supportedFormLocales') {
// if a form locale is disabled, disable it form submission locales as well
$supportedSubmissionLocales = (array) $context->getData('supportedSubmissionLocales');
$key = array_search($locale, $supportedSubmissionLocales);
if ($key !== false) {
unset($supportedSubmissionLocales[$key]);
}
$supportedSubmissionLocales = array_values($supportedSubmissionLocales);
if ($supportedSubmissionLocales == []) {
return new JSONMessage(false, __('notification.localeSettingsCannotBeSaved'));
}
$context = $contextService->edit($context, ['supportedSubmissionLocales' => $supportedSubmissionLocales], $request);
}
if ($settingName == 'supportedSubmissionLocales') {
// If someone tried to disable all submissions checkboxes, we should display an error message.
$supportedSubmissionLocales = (array) $context->getData('supportedSubmissionLocales');
$key = array_search($locale, $supportedSubmissionLocales);
if ($key !== false) {
unset($supportedSubmissionLocales[$key]);
}
$supportedSubmissionLocales = array_values($supportedSubmissionLocales);
if ($supportedSubmissionLocales == []) {
return new JSONMessage(false, __('notification.localeSettingsCannotBeSaved'));
}
}
}
}
}
$context = $contextService->edit($context, [$settingName => array_values(array_unique($currentSettingValue))], $request);
$notificationManager = new NotificationManager();
$user = $request->getUser();
$notificationManager->createTrivialNotification(
$user->getId(),
PKPNotification::NOTIFICATION_TYPE_SUCCESS,
['contents' => __('notification.localeSettingsSaved')]
);
$locales = $context->getSupportedFormLocaleNames();
$locales = array_map(fn (string $locale, string $name) => ['key' => $locale, 'label' => $name], array_keys($locales), $locales);
$json = \PKP\db\DAO::getDataChangedEvent($locale);
$json->setGlobalEvent('set-form-languages', $locales);
return $json;
}
/**
* Set context primary locale.
*
* @param array $args
* @param Request $request
*
* @return JSONMessage JSON object
*/
public function setContextPrimaryLocale($args, $request)
{
if (!$request->checkCSRF()) {
return new JSONMessage(false);
}
$locale = (string) $request->getUserVar('rowId');
$context = $request->getContext();
$availableLocales = $this->getGridDataElements($request);
if (Locale::isLocaleValid($locale) && array_key_exists($locale, $availableLocales)) {
// Make sure at least the primary locale is chosen as available
foreach (['supportedLocales', 'supportedSubmissionLocales', 'supportedFormLocales'] as $name) {
$$name = $context->getData($name);
if (!in_array($locale, $$name)) {
array_push($$name, $locale);
$context->updateSetting($name, $$name);
}
}
$context->setPrimaryLocale($locale);
$contextDao = Application::getContextDAO();
$contextDao->updateObject($context);
$notificationManager = new NotificationManager();
$user = $request->getUser();
$notificationManager->createTrivialNotification(
$user->getId(),
PKPNotification::NOTIFICATION_TYPE_SUCCESS,
['contents' => __('notification.localeSettingsSaved')]
);
}
return \PKP\db\DAO::getDataChangedEvent();
}
//
// Protected methods.
//
/**
* Return an instance of the cell provider
* used by this grid.
*
* @return LanguageGridCellProvider
*/
public function getCellProvider()
{
return new LanguageGridCellProvider();
}
/**
* Add name column.
*/
public function addNameColumn()
{
$cellProvider = $this->getCellProvider();
// Locale name.
$this->addColumn(
new GridColumn(
'locale',
'grid.columns.locale',
null,
'controllers/grid/languages/localeNameCell.tpl',
$cellProvider
)
);
}
/**
* Add locale code column.
*/
public function addLocaleCodeColumn()
{
$cellProvider = $this->getCellProvider();
// Locale code.
$this->addColumn(
new GridColumn(
'code',
'grid.columns.locale.code',
null,
null,
$cellProvider
)
);
}
/**
* Add primary column.
*
* @param string $columnId The column id.
*/
public function addPrimaryColumn($columnId)
{
$cellProvider = $this->getCellProvider();
$this->addColumn(
new GridColumn(
$columnId,
'locale.primary',
null,
'controllers/grid/common/cell/radioButtonCell.tpl',
$cellProvider
)
);
}
/**
* Add columns related to management settings.
*/
public function addManagementColumns()
{
$cellProvider = $this->getCellProvider();
$this->addColumn(
new GridColumn(
'uiLocale',
'manager.language.ui',
null,
'controllers/grid/common/cell/selectStatusCell.tpl',
$cellProvider
)
);
$this->addColumn(
new GridColumn(
'formLocale',
'manager.language.forms',
null,
'controllers/grid/common/cell/selectStatusCell.tpl',
$cellProvider
)
);
$this->addColumn(
new GridColumn(
'submissionLocale',
'manager.language.submissions',
null,
'controllers/grid/common/cell/selectStatusCell.tpl',
$cellProvider
)
);
}
/**
* Add data related to management settings.
*
* @param Request $request
* @param array $data Data already loaded.
*
* @return array Same passed array, but with
* the extra management data inserted.
*/
public function addManagementData($request, $data)
{
$context = $request->getContext();
if (is_array($data)) {
foreach ($data as $locale => $localeData) {
foreach (['supportedFormLocales', 'supportedSubmissionLocales', 'supportedLocales'] as $name) {
$data[$locale][$name] = in_array($locale, $context->getData($name));
// $data[$locale][$name] = in_array($locale, (array) $context->getData($name));
}
}
} else {
assert(false);
}
return $data;
}
}
@@ -0,0 +1,83 @@
<?php
/**
* @file controllers/grid/languages/LanguageGridRow.php
*
* Copyright (c) 2014-2021 Simon Fraser University
* Copyright (c) 2000-2021 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class LanguageGridRow
*
* @ingroup controllers_grid_languages
*
* @brief Language grid row definition
*/
namespace PKP\controllers\grid\languages;
use PKP\controllers\grid\GridRow;
use PKP\linkAction\LinkAction;
use PKP\linkAction\request\RemoteActionConfirmationModal;
use PKP\security\Validation;
class LanguageGridRow extends GridRow
{
//
// Overridden methods from GridRow
//
/**
* @copydoc GridRow::initialize()
*
* @param null|mixed $template
*/
public function initialize($request, $template = null)
{
parent::initialize($request, $template);
// Is this a new row or an existing row?
$rowId = $this->getId();
$rowData = $this->getData();
if (!empty($rowId)) {
// Only add row actions if this is an existing row
$router = $request->getRouter();
$actionArgs = [
'gridId' => $this->getGridId(),
'rowId' => $rowId
];
if (Validation::isSiteAdmin()) {
if (!$request->getContext() && !$rowData['primary']) {
$this->addAction(
new LinkAction(
'uninstall',
new RemoteActionConfirmationModal(
$request->getSession(),
__('admin.languages.confirmUninstall'),
__('grid.action.remove'),
$router->url($request, null, null, 'uninstallLocale', null, $actionArgs)
),
__('grid.action.remove'),
'delete'
)
);
}
if ($request->getContext()) {
$this->addAction(
new LinkAction(
'reload',
new RemoteActionConfirmationModal(
$request->getSession(),
__('manager.language.confirmDefaultSettingsOverwrite'),
__('manager.language.reloadLocalizedDefaultSettings'),
$router->url($request, null, null, 'reloadLocale', null, $actionArgs)
),
__('manager.language.reloadLocalizedDefaultSettings')
)
);
}
}
}
}
}
@@ -0,0 +1,116 @@
<?php
/**
* @file controllers/grid/languages/form/InstallLanguageForm.php
*
* Copyright (c) 2014-2021 Simon Fraser University
* Copyright (c) 2003-2021 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class InstallLanguageForm
*
* @ingroup controllers_grid_languages_form
*
* @brief Form for installing languages.
*/
namespace PKP\controllers\grid\languages\form;
use APP\core\Application;
use APP\template\TemplateManager;
use PKP\db\DAORegistry;
use PKP\facades\Locale;
use PKP\form\Form;
use PKP\i18n\LocaleMetadata;
use PKP\site\SiteDAO;
class InstallLanguageForm extends Form
{
/**
* Constructor.
*/
public function __construct()
{
parent::__construct('controllers/grid/languages/installLanguageForm.tpl');
}
//
// Overridden methods from Form.
//
/**
* @copydoc Form::initData()
*/
public function initData()
{
parent::initData();
$request = Application::get()->getRequest();
$site = $request->getSite();
$this->setData('installedLocales', $site->getInstalledLocales());
}
/**
* @copydoc Form::fetch()
*
* @param null|mixed $template
*/
public function fetch($request, $template = null, $display = false)
{
$allLocales = Locale::getFormattedDisplayNames(null, null, LocaleMetadata::LANGUAGE_LOCALE_WITH, false);
$installedLocales = $this->getData('installedLocales');
$notInstalledLocales = array_diff(array_keys($allLocales), $installedLocales);
$templateMgr = TemplateManager::getManager($request);
$templateMgr->assign([
'allLocales' => $allLocales,
'notInstalledLocales' => $notInstalledLocales,
]);
return parent::fetch($request, $template, $display);
}
/**
* @copydoc Form::readInputData()
*/
public function readInputData()
{
parent::readInputData();
$request = Application::get()->getRequest();
$localesToInstall = $request->getUserVar('localesToInstall');
$this->setData('localesToInstall', $localesToInstall);
}
/**
* @copydoc Form::execute()
*/
public function execute(...$functionArgs)
{
$request = Application::get()->getRequest();
$site = $request->getSite();
$localesToInstall = $this->getData('localesToInstall');
parent::execute(...$functionArgs);
if (isset($localesToInstall) && is_array($localesToInstall)) {
$installedLocales = $site->getInstalledLocales();
$supportedLocales = $site->getSupportedLocales();
foreach ($localesToInstall as $locale) {
if (Locale::isLocaleValid($locale) && !in_array($locale, $installedLocales)) {
array_push($installedLocales, $locale);
// Activate/support by default.
if (!in_array($locale, $supportedLocales)) {
array_push($supportedLocales, $locale);
}
Locale::installLocale($locale);
}
}
$site->setInstalledLocales($installedLocales);
$site->setSupportedLocales($supportedLocales);
$siteDao = DAORegistry::getDAO('SiteDAO'); /** @var SiteDAO $siteDao */
$siteDao->updateObject($site);
}
}
}