working on reworking language options

This commit is contained in:
John Bintz 2009-08-13 22:13:46 -04:00
parent e2c5b30173
commit 852c00f16f
4 changed files with 82 additions and 24 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*~

View File

@ -1,12 +1,16 @@
<?php
class WhatDidTheySayAdmin {
var $default_languages = array(
var $default_options = array(
'languages' => array(
array('code' => 'en', 'default' => true),
'fr',
'es',
'it',
'de'
),
'only_allowed_users' => false,
'users' => array()
);
var $language_file;
@ -20,6 +24,7 @@ class WhatDidTheySayAdmin {
$this->what_did_they_say = $what_did_they_say;
add_action('admin_menu', array(&$this, 'admin_menu'));
wp_enqueue_script('prototype');
if (isset($_POST['wdts'])) {
if (isset($_POST['wdts']['_nonce'])) {
@ -28,6 +33,8 @@ class WhatDidTheySayAdmin {
}
}
}
$this->read_language_file();
}
function _update_options($which, $value) {
@ -37,6 +44,8 @@ class WhatDidTheySayAdmin {
}
function handle_update_languages($language_info) {
$languages = array();
foreach ($language_info as $code => $info) {
if (isset($this->all_languages[$code])) {
@ -90,9 +99,9 @@ class WhatDidTheySayAdmin {
}
function install() {
$languages = get_option('what-did-they-say-languages');
if (empty($languages)) {
update_option('what-did-they-say-languages', $this->default_languages);
$options = get_option('what-did-they-say-options');
if (empty($options)) {
update_option('what-did-they-say-options', $this->default_options);
}
}
@ -121,6 +130,21 @@ class WhatDidTheySayAdmin {
function manage_transcriptions_admin() {
$options = get_option('what-did-they-say-options');
$language_map_pairs = array();
$basic_languages = $all_languages = array();
foreach ($this->all_languages as $code => $name) {
$name = addslashes($name);
$language_map_pairs[] = "'${code}': '${name}'";
$all_languages[] = "'$code'";
if (strlen($code) == 2) {
$basic_languages[] = "'$code'";
}
}
$nonce = wp_create_nonce('what-did-they-say');
$nonce_url = add_query_arg('wdts[_nonce]', $nonce);
include(dirname(__FILE__) . '/admin.inc');
}

View File

@ -3,22 +3,36 @@
<h3>Queued Transcripts</h3>
<h3>Available Languages</h3>
<h3>Languages</h3>
<ul id="language-holder">
<?php foreach ($languages as $language) {
<?php foreach ($options['languages'] as $language) {
$default = false;
if (is_string($language)) { $name = $language; }
if (is_array($language)) {
$name = $language['name'];
$default = $language['default'];
$code = $language['code'];
}
$key = md5($name);
if (is_string($language)) { $code = $language; }
?>
<li>
<input type="text" name="wdts[language][<?php echo $key ?>]" value="<?php echo $name ?>" />
<input type="radio" name="wdts[language][_default]" value="<?php echo $key ?>"
<strong><?php echo $this->all_languages[$code] ?></strong>
| <a class="verify" href="<?php echo add_query_arg(array('wdts[code]' => $code, 'wdts[action]' => 'delete'), $nonce_url) ?>">Delete</a>
</li>
<?php } ?>
</ul>
</div>
<script type="text/javascript">
var basic_languages = [ <?php echo implode(",", $basic_languages) ?> ];
var all_languages = [ <?php echo implode(",", $all_languages) ?> ];
var language_map = { <?php echo implode(",\n", $language_map_pairs) ?> };
$$('a.verify').each(function(a) {
Event.observe(a, 'click', function(e) {
if (!confirm("You are about to delete this language from the available list. Continue?")) {
Event.stop(e);
}
});
});
</script>

View File

@ -15,17 +15,35 @@ class WhatDidTheySayAdminTest extends PHPUnit_Framework_TestCase {
$this->assertTrue(count($admin->read_language_file()) > 0);
}
function testHandleUpdateLanguages() {
function providerTestHandleUpdateLanguages() {
return array(
array(
array(
array('en' => array('name' => 'English'), 'de' => array('name' => 'German')),
array('code' => 'en', 'action' => 'delete'),
array('de' => array('name' => 'German'))
)
)
);
}
/**
* @dataProvider providerTestHandleUpdateLanguages
*/
function testHandleUpdateLanguages($original_options, $form_submission, $expected_results) {
$admin = new WhatDidTheySayAdmin();
$admin->all_languages = array(
'en' => 'English',
'de' => 'German'
'de' => 'German',
'fr' => 'French'
);
$admin->handle_update_languages(array('en' => array(), 'de' => array('default' => 'yes'), 'meow' => array()));
update_option('what-did-they-say-options', array('languages', $original_options));
$admin->handle_update_languages($form_submission);
$options = get_option('what-did-they-say-options');
$this->assertEquals(array('en', array('code' => 'de', 'default' => true)), $options['languages']);
$this->assertEquals($expected_results, $options['languages']);
}
function testHandleUpdateAllowedUsers() {