Skip to content

Commit

Permalink
Reuse selector
Browse files Browse the repository at this point in the history
  • Loading branch information
VicDeo committed Jul 6, 2018
1 parent 1add7c1 commit 616e1ea
Show file tree
Hide file tree
Showing 8 changed files with 211 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/private/Helper/LocaleHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ public function getNormalizedLanguages(IFactory $langFactory, $activeLangCode) {

// TRANSLATORS this is a self-name of your language for the language switcher
$endonym = (string)$l->t('__language_name__');
//Check if the language name is in the translation file
// Fallback to hardcoded language name if translation is
// Check if the language name is in the translation file
// Fallback to hardcoded language name if it isn't
$languageName = ($l->getLanguageCode() === $languageCode
&& \substr($endonym, 0, 1) !== '_'
) ? $endonym
Expand Down
34 changes: 34 additions & 0 deletions settings/Panels/Admin/FileSharing.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

namespace OC\Settings\Panels\Admin;

use OC\Helper\LocaleHelper;
use OC\Settings\Panels\Helper;
use OCP\IConfig;
use OCP\Settings\ISettings;
Expand All @@ -47,6 +48,38 @@ public function getPriority() {
}

public function getPanel() {
$this->lfactory = \OC::$server->getL10NFactory();
$activeLangCode = $this->config->getAppValue(
'core',
'shareapi_public_notification_lang',
'owner'
);
$this->localeHelper = new LocaleHelper();
list($userLang, $commonLanguages, $languages) = $this->localeHelper->getNormalizedLanguages(
$this->lfactory,
$activeLangCode
);

// Allow reset to the defaults when mail notification is sent in the lang of owner
if ($userLang['code'] === "owner") {
$userLang['name'] = $this->l->t("Owner language");
} else {
\array_push(
$commonLanguages,
[
'code' => 'owner',
'name' => $this->l->t("Owner language")
]
);
}

$selector = new Template('settings', 'language');
$selector->assign('selectName', 'shareapi_public_notification_lang');
$selector->assign('selectId', 'shareapiPublicNotificationLang');
$selector->assign('activelanguage', $userLang);
$selector->assign('commonlanguages', $commonLanguages);
$selector->assign('languages', $languages);

$template = new Template('settings', 'panels/admin/filesharing');
$template->assign('allowResharing', $this->config->getAppValue('core', 'shareapi_allow_resharing', 'yes'));
$template->assign('shareAPIEnabled', $this->config->getAppValue('core', 'shareapi_enabled', 'yes'));
Expand All @@ -57,6 +90,7 @@ public function getPanel() {
$template->assign('enforceLinkPasswordWriteOnly', $this->config->getAppValue('core', 'shareapi_enforce_links_password_write_only', 'no'));
$template->assign('shareDefaultExpireDateSet', $this->config->getAppValue('core', 'shareapi_default_expire_date', 'no'));
$template->assign('allowPublicMailNotification', $this->config->getAppValue('core', 'shareapi_allow_public_notification', 'no'));
$template->assign('publicMailNotificationLang', $selector->fetchPage());
$template->assign('allowSocialShare', $this->config->getAppValue('core', 'shareapi_allow_social_share', 'yes'));
$template->assign('allowGroupSharing', $this->config->getAppValue('core', 'shareapi_allow_group_sharing', 'yes'));
$template->assign('onlyShareWithGroupMembers', $this->helper->shareWithGroupMembersOnly());
Expand Down
1 change: 1 addition & 0 deletions settings/Panels/Personal/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public function getPanel() {
);

$selector = new Template('settings', 'language');
$selector->assign('selectName', 'lang');
$selector->assign('selectId', 'languageinput');
$selector->assign('activelanguage', $userLang);
$selector->assign('commonlanguages', $commonLanguages);
Expand Down
13 changes: 13 additions & 0 deletions settings/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,19 @@ $(document).ready(function(){
$('#setDefaultExpireDate').toggleClass('hidden', !(this.checked && $('#shareapiDefaultExpireDate')[0].checked));
});

$('#allowPublicMailNotification').change(function() {
$("#publicMailNotificationLang").toggleClass('hidden', !this.checked);
});

$('#shareapiPublicNotificationLang').change(function() {
var value = $(this).val();
if (value === 'owner') {
OC.AppConfig.deleteKey('core', $(this).attr('name'));
} else {
OC.AppConfig.setValue('core', $(this).attr('name'), $(this).val());
}
});


$('#allowGroupSharing').change(function() {
$('#allowGroupSharing').toggleClass('hidden', !this.checked);
Expand Down
2 changes: 1 addition & 1 deletion settings/templates/language.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<select id="<?php p($_['selectId'])?>" name="lang" data-placeholder="<?php p($l->t('Language'));?>">
<select id="<?php p($_['selectId'])?>" name="<?php p($_['selectName'])?>" data-placeholder="<?php p($l->t('Language'));?>">
<option value="<?php p($_['activelanguage']['code']);?>">
<?php p($_['activelanguage']['name']);?>
</option>
Expand Down
8 changes: 8 additions & 0 deletions settings/templates/panels/admin/filesharing.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@
<input type="checkbox" name="shareapi_allow_public_notification" id="allowPublicMailNotification" class="checkbox"
value="1" <?php if ($_['allowPublicMailNotification'] == 'yes') print_unescaped('checked="checked"'); ?> />
<label for="allowPublicMailNotification"><?php p($l->t('Allow users to send mail notification for shared files'));?></label><br/>
<span id="publicMailNotificationLang" <?php if ($_['allowPublicMailNotification'] == 'no') {
print_unescaped('class="hidden"');
} ?>>
<label><?php p($l->t('Language used for public mail notifications for shared files'));?></label>
<?php print_unescaped($_['publicMailNotificationLang']); ?>
<br>
</span>


<input type="checkbox" name="shareapi_allow_social_share" id="allowSocialShare" class="checkbox"
value="1" <?php if ($_['allowSocialShare'] == 'yes') print_unescaped('checked="checked"'); ?> />
Expand Down
2 changes: 1 addition & 1 deletion tests/Settings/Panels/Personal/ProfileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function testGetPanel() {
$this->assertContains('[email protected]', $templateHtml);
$this->assertContains('<div id="groups" class="section">', $templateHtml);
$this->assertContains('group2', $templateHtml);
$this->assertContains('<form id="language" class="section">', $templateHtml);
$this->assertContains('<select id="languageinput" name="lang"', $templateHtml);
}

}
151 changes: 151 additions & 0 deletions tests/lib/Helper/LocaleHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php
/**
* @author Viktar Dubiniuk <[email protected]>
*
* @copyright Copyright (c) 2018, ownCloud GmbH
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace Test\Helper;

use OC\Helper\LocaleHelper;
use OCP\IL10N;
use OCP\L10N\IFactory;
use Test\TestCase;

/**
* Class LocaleHelperTest
*
* @package Test\Helper
*/
class LocaleHelperTest extends TestCase {
/**
* @var LocaleHelper
*/
protected $localeHelper;

protected function setUp() {
parent::setUp();
$this->localeHelper = new LocaleHelper();
}

public function langDataProvider() {
return [
[
[ 'hi', 'ro', 'php', 'de', 'java' ],
'en',
// en was omited in the available lang list above. it will have
// no translation and will not be available for selection
[
'code' => 'en',
'name' => 'en',
],
// Only de is common. en was omitted in the available lang list above
[
3 => [
'code' => 'de',
'name' => 'de',
]
],
// sorted by name alphabetically but if code was not resolved
// to name the item should has less priority
[
[
'code' => 'ro',
'name' => 'română',
],
[
'code' => 'hi',
'name' => 'हिन्दी',
],
[
'code' => 'java',
'name' => 'java',
],
[
'code' => 'php',
'name' => 'php',
],
]
],
[
[ 'hi', 'ro', 'php', 'de', 'java', 'en'],
'ro',
// Now we have ro in a list. It will be translated
[
'code' => 'ro',
'name' => 'română',
],
// de and en are common. en should go first
[
0 => [
'code' => 'en',
'name' => 'English',
],
3 => [
'code' => 'de',
'name' => 'de',
]
],
// sorted by name alphabetically but if code was not resolved
// to name the item should has less priority
[
[
'code' => 'hi',
'name' => 'हिन्दी',
],
[
'code' => 'java',
'name' => 'java',
],
[
'code' => 'php',
'name' => 'php',
],
]
]
];
}

/**
* @dataProvider langDataProvider
*/
public function testNormalization($availableCodes,
$currentCode,
$expectedUserLang,
$expectedCommonLanguages,
$expectedLanguages
) {
$l10n = $this->createMock(IL10N::class);

$langFactory = $this->createMock(IFactory::class);
$langFactory->expects($this->any())
->method('findAvailableLanguages')
->willReturn($availableCodes);
$langFactory->expects($this->any())
->method('get')
->willReturn($l10n);

list(
$userLang,
$commonLanguages,
$languages
) = $this->localeHelper->getNormalizedLanguages($langFactory, $currentCode);
$this->assertEquals($expectedUserLang, $userLang);
$this->assertEquals($expectedCommonLanguages, $commonLanguages);
$this->assertEquals($expectedLanguages, $languages);
}
}

0 comments on commit 616e1ea

Please sign in to comment.