Index: stringoverrides.admin.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/stringoverrides/Attic/stringoverrides.admin.inc,v retrieving revision 1.1.2.12 diff -u -p -r1.1.2.12 stringoverrides.admin.inc --- stringoverrides.admin.inc 8 Mar 2009 10:37:14 -0000 1.1.2.12 +++ stringoverrides.admin.inc 16 Dec 2009 04:44:44 -0000 @@ -9,7 +9,7 @@ /** * Menu callback for the String Overrides module to display its administration */ -function stringoverrides_admin($form_state = NULL, $lang = NULL) { +function stringoverrides_admin($form, $form_state, $lang = NULL) { // See which language we're modifying if (empty($lang)) { global $language; @@ -17,47 +17,64 @@ function stringoverrides_admin($form_sta } // Setup the form - $form = array('#cache' => TRUE); + $form['#cache'] = TRUE; $form['lang'] = array( '#type' => 'hidden', '#value' => $lang, ); $form['string'] = array( + '#tree' => TRUE, + '#prefix' => '
', + '#suffix' => '
', '#theme' => 'stringoverrides_strings', ); - // Retrieve the words to display + $words = array( + FALSE => variable_get("locale_custom_disabled_strings_$lang", array()), + TRUE => variable_get("locale_custom_strings_$lang", array()) + ); $strings = array(); - $custom_strings = array( - FALSE => variable_get('locale_custom_disabled_strings_'. $lang, array()), - TRUE => variable_get('locale_custom_strings_'. $lang, array()) - ); - foreach ($custom_strings as $enabled => $replacements) { - foreach ($replacements as $original => $replacement) { - $strings[] = array('enabled' => $enabled, 'original' => $original, 'replacement' => $replacement); + foreach ($words as $enabled => $custom_strings) { + foreach ($custom_strings as $context => $translations) { + foreach ($translations as $source => $translation) { + $strings[] = array( + 'enabled' => $enabled, + 'context' => $context, + 'source' => $source, + 'translation' => $translation, + ); + } } } + if (isset($form_state['string_count'])) { + $string_count = $form_state['string_count']; + } + else { + $string_count = max(3, count($strings)); + } + // Sort the strings and display them in the form usort($strings, 'stringoverrides_admin_word_sort'); - $delta = 0; - foreach($strings as $string){ - $form['string'][$delta] = stringoverrides_textbox_combo($delta, $string['enabled'], $string['original'], $string['replacement']); - $delta++; - } - for ($index = 0; $index < 3; $index++) { // Add placeholder rows - $form['string'][$delta] = stringoverrides_textbox_combo($delta, -1); - $delta++; + for ($index = 0; $index < $string_count; $index++) { + if (isset($strings[$index])) { + $string = $strings[$index]; + $form['string'][$index] = stringoverrides_textbox_combo($index, $string['enabled'], $string['context'], $string['source'], $string['translation']); + } + else { + $form['string'][$index] = stringoverrides_textbox_combo($index, -1); + } } // Add the buttons to the form $form['more_strings'] = array( - '#type' => 'button', + '#type' => 'submit', '#value' => t('Add row'), '#description' => t("If the amount of boxes above isn't enough, click here to add more choices."), '#weight' => 2, - '#ahah' => array( - 'path' => 'admin/settings/stringoverrides/js', + '#submit' => array('stringoverrides_more_strings_submit'), + '#ajax' => array( + 'callback' => 'stringoverrides_ajax', 'wrapper' => 'stringoverrides-wrapper', 'method' => 'replace', 'effect' => 'fade', @@ -82,13 +99,19 @@ function stringoverrides_admin($form_sta * Triggered when the user submits the administration page */ function stringoverrides_admin_submit($form, &$form_state) { + if (!in_array($form_state['clicked_button']['#id'], array('edit-save', 'edit-remove'))) { + // Submit the form only for save and remove buttons. + return; + } + // Format the words correctly so that they're put into the database correctly $words = array(FALSE => array(), TRUE => array()); foreach ($form_state['values']['string'] as $index => $string) { - if (!empty($string['original'])) { + if (!empty($string['source'])) { + $context = check_plain($string['context']); // Get rid of carriage returns. - list($orig, $replace) = str_replace("\r", '', array($string['original'], $string['replacement'])); - $words[$string['enabled']][$orig] = $replace; + list($source, $translation) = str_replace("\r", '', array($string['source'], $string['translation'])); + $words[$string['enabled']][$context][$source] = $translation; } } @@ -98,45 +121,47 @@ function stringoverrides_admin_submit($f global $language; $lang = $language->language; } - variable_set('locale_custom_strings_'. $lang, $words[1]); + variable_set("locale_custom_strings_$lang", $words[1]); // Save the values and display a message to the user depend - switch ($form_state['values']['op']) { - case t('Save configuration'): - variable_set('locale_custom_disabled_strings_'. $lang, $words[0]); - drupal_set_message('Your changes have been saved.'); - break; - case t('Remove disabled strings'): - variable_del('locale_custom_disabled_strings_'. $lang); - drupal_set_message('The disabled strings have been removed.'); - break; + switch ($form_state['clicked_button']['#id']) { + case 'edit-save': + variable_set("locale_custom_disabled_strings_$lang", $words[0]); + drupal_set_message('Your changes have been saved.'); + break; + + case 'edit-remove': + variable_del("locale_custom_disabled_strings_$lang"); + drupal_set_message('The disabled strings have been removed.'); + break; } } /** * Function to return a textbox combo form */ -function stringoverrides_textbox_combo($delta = 0, $enabled = TRUE, $original = '', $replacement = '') { - $form = array( - '#tree' => TRUE, - ); +function stringoverrides_textbox_combo($delta = 0, $enabled = TRUE, $context = '', $source = '', $translation = '') { + $form['#tree'] = TRUE; $form['enabled'] = array( '#type' => 'checkbox', '#default_value' => ($enabled == -1) ? TRUE : $enabled, - '#parents' => array('string', $delta, 'enabled'), '#access' => $enabled != -1, // Have access if it's not a placeholder value ); - $form['original'] = array( + $form['context'] = array( + '#type' => 'textfield', + '#default_value' => $context, + '#size' => 20, + '#maxlength' => 255, + ); + $form['source'] = array( '#type' => 'textarea', - '#default_value' => $original, + '#default_value' => $source, '#rows' => 1, - '#parents' => array('string', $delta, 'original'), ); - $form['replacement'] = array( + $form['translation'] = array( '#type' => 'textarea', - '#default_value' => $replacement, + '#default_value' => $translation, '#rows' => 1, - '#parents' => array('string', $delta, 'replacement'), ); return $form; } @@ -144,8 +169,9 @@ function stringoverrides_textbox_combo($ /** * Theme the enabled box and the two text box strings */ -function theme_stringoverrides_strings($form) { - drupal_add_css(drupal_get_path('module', 'stringoverrides') .'/stringoverrides.css', 'module', NULL, NULL, FALSE); +function theme_stringoverrides_strings($variables) { + $form = $variables['form']; + drupal_add_css(drupal_get_path('module', 'stringoverrides') . '/stringoverrides.css', 'module', NULL, NULL, FALSE); $rows = array(); foreach (element_children($form) as $key) { @@ -153,8 +179,9 @@ function theme_stringoverrides_strings($ $rows[$key] = array( 'data' => array( array('data' => drupal_render($form[$key]['enabled']), 'class' => 'stringoverrides-enabled'), - array('data' => drupal_render($form[$key]['original']), 'class' => 'stringoverrides-original'), - array('data' => drupal_render($form[$key]['replacement']), 'class' => 'stringoverrides-replacement'), + array('data' => drupal_render($form[$key]['context']), 'class' => 'stringoverrides-context'), + array('data' => drupal_render($form[$key]['source']), 'class' => 'stringoverrides-source'), + array('data' => drupal_render($form[$key]['translation']), 'class' => 'stringoverrides-translation'), ), ); // Add any attributes on the element to the row, such as the ahah class. @@ -162,54 +189,30 @@ function theme_stringoverrides_strings($ $rows[$key] = array_merge($rows[$key], $form[$key]['#attributes']); } } - $headers = array( - sizeof($rows) > 3 ? t('Enabled') : NULL, // theme('table_select_header_cell'), + $header = array( + ($form[0]['enabled']['#access']) ? t('Enabled') : NULL, + t('Context'), t('Original'), t('Replacement'), ); - $output = ''; - $output .= '
'; - $output .= theme('table', $headers, $rows); - $output .= '
'; - $output .= drupal_render($form); + $output = theme('table', array('header' => $header, 'rows' => $rows)); + $output .= drupal_render_children($form); return $output; } /** * Menu callback for the String Overrides module to display a new string override */ -function stringoverrides_js() { - $delta = count($_POST['string']); - - // Build our new form element. - $form_element = stringoverrides_textbox_combo($delta); - drupal_alter('form', $form_element, array(), 'stringoverrides_js'); - - // Build the new form. - $form_state = array('submitted' => FALSE); - $form_build_id = $_POST['form_build_id']; - // Add the new element to the stored form. Without adding the element to the - // form, Drupal is not aware of this new elements existence and will not - // process it. We retreive the cached form, add the element, and resave. - $form = form_get_cache($form_build_id, $form_state); - $form['string'][$delta] = $form_element; - form_set_cache($form_build_id, $form, $form_state); - $form += array( - '#post' => $_POST, - '#programmed' => FALSE, - ); - - // Rebuild the form. - $form = form_builder('stringoverrides_admin', $form, $form_state); - - // Render the new output. - $string_form = $form['string']; - $string_form[$delta]['enabled']['#value'] = TRUE; - $string_form[$delta]['#attributes']['class'] = empty($string_form[$delta]['#attributes']['class']) ? 'ahah-new-content' : $string_form[$delta]['#attributes']['class'] .' ahah-new-content'; - $output = theme('status_messages') . drupal_render($string_form); +function stringoverrides_ajax($form, $form_state) { + return $form['string']; +} - drupal_json(array('status' => TRUE, 'data' => $output)); +/** + * Submit handler for the "Add row" button. + */ +function stringoverrides_more_strings_submit($form, &$form_state) { + $form_state['string_count'] = count($form_state['values']['string']) + 1; } /** @@ -239,85 +242,46 @@ function stringoverrides_admin_import() } /** - * Triggered when the user imports data + * Triggered when the user imports data. */ function stringoverrides_admin_import_submit($form, &$form_state) { // Check if the file uploaded correctly - if ($file = file_save_upload('file')) { - $overrides = array(); + $file = file_save_upload('file'); + if (!$file) { + form_set_error('file', t('A file to import is required.')); + return; + } - // Start reading the file - $handle = @fopen($file->filepath, "r"); - if ($handle) { - $currentoverride = NULL; - $currentstring = NULL; - $operation = 'msgstr'; - - // Loop through the whole file - while (!feof($handle)) { - // Retrieve a single line - $buffer = trim(fgets($handle)); - // Skip empty or comment lines - if (empty($buffer) || $buffer[0] == '#') { - continue; - } - // Continued string - if ($buffer[0] == '"') { - // See what we're reading in - $string = trim(substr($buffer, 1, -1)); - if ($operation == 'msgstr' && !empty($string)) { - $currentstring .= $string; - } - else { - $currentoverride .= $string; - } - } - else if (substr($buffer, 0, 6) == 'msgstr') { - // Retrieve the override string - $operation = 'msgstr'; - $currentstring = substr($buffer, 8, -1); - } - else if (substr($buffer, 0, 5) == 'msgid') { // New string - // Save old string - if (!empty($currentoverride)) { - $overrides[$currentoverride] = $currentstring; - $currentoverride = $currentstring = ''; - } - // Read what's next - $operation = 'msgid'; - $currentoverride = substr($buffer, 7, -1); - } - } + // Try to allocate enough time to parse and import the data. + drupal_set_time_limit(240); + $lang = $form_state['values']['lang']; - // Save old string - if (!empty($currentstring) && !empty($currentoverride)) { - $overrides[$currentoverride] = $currentstring; - } - - // Clean up and save the imported data - fclose($handle); - file_delete($file->filepath); - variable_set('locale_custom_strings_'. $form_state['values']['lang'], $overrides); - drupal_set_message(t('The overrides have been imported.')); - } - } - else { - form_set_error('file', t('A file to import is required.')); + // Get strings from file (returns on failure after a partial import, or on success) + $status = _locale_import_read_po('mem-store', $file, LOCALE_IMPORT_OVERWRITE, $lang, 'stringoverrides'); + if ($status === FALSE) { + // Error messages are set in _locale_import_read_po(). + return FALSE; } + + // Get the import result. + $strings = _locale_import_one_string('mem-report'); + + file_delete($file); + variable_set("locale_custom_strings_$lang", $strings); + drupal_set_message(t('The overrides have been imported.')); } /** * Ability to export a *.po file. */ function stringoverrides_admin_export() { - $form = array(); $languages = module_exists('locale') ? locale_language_list() : array('en' => t('English')); $form['lang'] = array( '#type' => 'select', '#title' => t('Language'), '#description' => t('The language you would like to export.'), '#options' => $languages, - '#required' => TRUE + '#required' => TRUE, ); $form['export'] = array( '#type' => 'submit', @@ -332,29 +296,45 @@ function stringoverrides_admin_export() * Submit-handler for stringoverrides_admin_export. */ function stringoverrides_admin_export_submit($form, &$form_state) { - drupal_set_header('Content-Type: text/plain'); - drupal_set_header('Content-Disposition: attachment; filename: "stringoverrides.po"'); - $export = stringoverrides_admin_export_text($form_state['values']['lang']); - drupal_set_header('Content-Length: '. strlen($export)); + $lang = $form_state['values']['lang']; + $export = stringoverrides_admin_export_text($lang); + $filename = "my-string-overrides.$lang.po"; + $headers = array( + 'Content-Type' => 'text/plain; charset=UTF-8', + 'Content-Length' => strlen($export), + 'Content-Disposition' => 'attachment; filename="' . $filename . '"', + 'Cache-Control' => 'private', + ); + foreach ($headers as $name => $value) { + drupal_add_http_header($name, $value); + } echo $export; - $form_state['redirect'] = FALSE; + drupal_exit(); } /** * Returns the exported *.po text from the given language. */ function stringoverrides_admin_export_text($lang = 'en') { - $export = "# String Overrides Export\n"; - $overrides = variable_get('locale_custom_strings_'. $lang, array()); - foreach ($overrides as $override => $string) { - $export .= 'msgid "'. $override ."\"\nmsgstr \"$string\"\n"; + $languages = language_list(); + + $custom_strings = variable_get("locale_custom_strings_$lang", array()); + foreach ($custom_strings as $context => $translations) { + foreach ($translations as $source => $translation) { + $strings[] = array( + 'context' => $context, + 'source' => $source, + 'translation' => $translation, + 'comment' => '', + ); + } } - return $export; + return _locale_export_po_generate($languages[$lang], $strings); } /** - * Sorts two words based on their original text. + * Sorts two words based on their source text. */ function stringoverrides_admin_word_sort($word1, $word2) { - return strcasecmp($word1['original'], $word2['original']); + return strcasecmp($word1['source'], $word2['source']); } Index: stringoverrides.css =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/stringoverrides/Attic/stringoverrides.css,v retrieving revision 1.1.4.3 diff -u -p -r1.1.4.3 stringoverrides.css --- stringoverrides.css 11 May 2008 13:42:23 -0000 1.1.4.3 +++ stringoverrides.css 16 Dec 2009 03:55:14 -0000 @@ -5,11 +5,11 @@ height: 1.7em; } -#stringoverrides-admin .stringoverrides-original { +#stringoverrides-admin .stringoverrides-source { width: 50%; } -#stringoverrides-admin .stringoverrides-replacement { +#stringoverrides-admin .stringoverrides-translation { width: 50%; } Index: stringoverrides.info =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/stringoverrides/Attic/stringoverrides.info,v retrieving revision 1.2 diff -u -p -r1.2 stringoverrides.info --- stringoverrides.info 21 Nov 2007 09:41:32 -0000 1.2 +++ stringoverrides.info 15 Dec 2009 22:01:28 -0000 @@ -1,4 +1,7 @@ ; $Id: stringoverrides.info,v 1.2 2007/11/21 09:41:32 robloach Exp $ name = String Overrides description = Provides a quick and easy way of replacing text. -core = 6.x +core = 7.x +files[] = stringoverrides.module +files[] = stringoverrides.install +files[] = stringoverrides.admin.inc Index: stringoverrides.install =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/stringoverrides/Attic/stringoverrides.install,v retrieving revision 1.3.2.4 diff -u -p -r1.3.2.4 stringoverrides.install --- stringoverrides.install 10 Sep 2008 21:03:33 -0000 1.3.2.4 +++ stringoverrides.install 16 Dec 2009 03:26:19 -0000 @@ -2,10 +2,20 @@ // $Id: stringoverrides.install,v 1.3.2.4 2008/09/10 21:03:33 robloach Exp $ /** - * Implementation of hook_uninstall() + * Implements hook_uninstall(). */ function stringoverrides_uninstall() { // Remove all stored string replacements db_query('DELETE FROM {variable} WHERE name LIKE "locale_custom_strings_%"'); db_query('DELETE FROM {variable} WHERE name LIKE "locale_custom_disabled_strings_%"'); } + +/** + * Update to Drupal 7. + * Will be solved later. Set the version number for new installs for now. + */ +function stringoverrides_update_7000() { + $ret = array(); + + return $ret; +} Index: stringoverrides.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/stringoverrides/Attic/stringoverrides.module,v retrieving revision 1.5.2.8 diff -u -p -r1.5.2.8 stringoverrides.module --- stringoverrides.module 18 Feb 2009 19:59:57 -0000 1.5.2.8 +++ stringoverrides.module 16 Dec 2009 04:34:21 -0000 @@ -7,64 +7,65 @@ */ /** - * Implementation of hook_help() + * Implements hook_help(). */ function stringoverrides_help($path, $arg) { - $output = ''; - if ($path == 'admin/help#stringoverrides') { - $output = '

'. t('The String Overrides module provides a quick and easy way of replacing text.') .'

'; - $output .= '

'. t('To replace a string, enter the complete string that is passed through the t() function. String Overrides cannot translate user-defined content, it can only replace strings wrapped in the t() function. To find the strings you can actually change, open up a module and look for t() function calls. Places where %, @, or ! are used means that the translation contains dynamic information (such as the node type or title in the above examples); these are not translated while the text around them is.', array('@t' => 'http://api.drupal.org/api/function/t')) .'

'; - $output .= '

'. t('For example:') .'

'; - $output .= theme('item_list', array( - '"The %post has been updated." → "You just updated the %post."', - '"Are you sure you want to delete %title?" → "Do you want to delete %title?"', - )); - $output .= '

'. t('Remember, you must replace the entire string, not just a portion of it.') .'

'; + switch ($path) { + case 'admin/help#stringoverrides': + $output = '

' . t('The String Overrides module provides a quick and easy way of replacing text.') . '

'; + $output .= '

' . t('To replace a string, enter the complete string that is passed through the t() function. String Overrides cannot translate user-defined content, it can only replace strings wrapped in the t() function. To find the strings you can actually change, open up a module and look for t() function calls. Places where %, @, or ! are used means that the translation contains dynamic information (such as the node type or title in the above examples); these are not translated while the text around them is.', array('@t' => 'http://api.drupal.org/api/function/t')) . '

'; + $output .= '

' . t('For example:') . '

'; + $output .= theme('item_list', array( + '"The %post has been updated." → "You just updated the %post."', + '"Are you sure you want to delete %title?" → "Do you want to delete %title?"', + )); + $output .= '

' . t('Remember, you must replace the entire string, not just a portion of it.') . '

'; + return $output; + + case 'admin/config/regional/stringoverrides/import': + $output = '

' . t('Upload a *.po file here to import a collection of strings.') . '

'; + $output .= '

' . t('The current string overrides for the selected language will be overwritten with the content of the file.') . '

'; + return $output; + + case 'admin/config/regional/stringoverrides/export': + return '

' . t('The following is a generated *.po file. You can use this feature to backup the current String Overrides.') . '

'; } - else if ($arg[0] == 'admin' && $arg[1] == 'settings' && $arg[2] == 'stringoverrides') { - switch ($arg[3]) { - case 'import': - $output = '

'. t('Upload a *.po file here to import a collection of strings.') .'

'; - break; - case 'export': - $output = '

'. t('The following is a generated *.po file. You can use this feature to backup the current String Overrides.') .'

'; - break; - case NULL: - $output = '

'. t('The following provides a quick and easy way of replacing text.') .'

'; - break; - default: - if (module_exists('locale')) { - $lang = locale_language_name($arg[3]); - $output = '

'. t('The following provides a quick and easy way of replacing text in @lang.', array('@lang' => $lang)) .'

'; - } - break; + + if ($path == 'admin/config/regional/stringoverrides' || strpos($path, 'admin/config/regional/stringoverrides/manage/') !== FALSE) { + $languages = language_list(); + if (isset($arg[5]) && isset($languages[$arg[5]])) { + $lang = $languages[$arg[5]]->native; + return '

' . t('The following provides a quick and easy way of replacing text in @lang.', array('@lang' => $lang)) . '

'; } + return '

' . t('The following provides a quick and easy way of replacing text.') . '

'; } - return $output; } /** - * Implementation of hook_perm() + * Implements hook_permission(). */ -function stringoverrides_perm() { - return array('administer string overrides'); +function stringoverrides_permission() { + return array( + 'administer string overrides' => array( + 'title' => t('Administer string overrides'), + ), + ); } /** - * Implementation of hook_menu() + * Implements hook_menu(). */ function stringoverrides_menu() { - $items = array(); - $items['admin/settings/stringoverrides'] = array( + $items['admin/config/regional/stringoverrides'] = array( 'title' => 'String overrides', 'description' => 'Provides a quick and easy way of replacing text on the site.', 'page callback' => 'drupal_get_form', - 'page arguments' => array('stringoverrides_admin', 3), + 'page arguments' => array('stringoverrides_admin'), 'access arguments' => array('administer string overrides'), 'type' => MENU_NORMAL_ITEM, 'file' => 'stringoverrides.admin.inc', ); - $items['admin/settings/stringoverrides/import'] = array( + $items['admin/config/regional/stringoverrides/import'] = array( 'title' => 'Import', 'description' => 'Import a set of overrides from a *.po file.', 'page arguments' => array('stringoverrides_admin_import'), @@ -72,7 +73,7 @@ function stringoverrides_menu() { 'type' => MENU_LOCAL_TASK, 'weight' => 9, ); - $items['admin/settings/stringoverrides/export'] = array( + $items['admin/config/regional/stringoverrides/export'] = array( 'title' => 'Export', 'description' => 'Import a set of overrides from a *.po file.', 'page arguments' => array('stringoverrides_admin_export'), @@ -80,13 +81,13 @@ function stringoverrides_menu() { 'type' => MENU_LOCAL_TASK, 'weight' => 10, ); - + // Add the language tabs if there are other languages if (module_exists('locale')) { global $language; $languages = locale_language_list(); foreach ($languages as $code => $name) { - $items["admin/settings/stringoverrides/$code"] = array( + $items["admin/config/regional/stringoverrides/manage/$code"] = array( 'title' => '@lang', 'title arguments' => array('@lang' => $name), 'page arguments' => array('stringoverrides_admin', $code), @@ -96,7 +97,7 @@ function stringoverrides_menu() { } } else { - $items['admin/settings/stringoverrides/en'] = array( + $items['admin/config/regional/stringoverrides/manage/en'] = array( 'title' => 'Overrides', 'page callback' => 'drupal_get_form', 'page arguments' => array('stringoverrides_admin', 'en'), @@ -105,24 +106,17 @@ function stringoverrides_menu() { 'file' => 'stringoverrides.admin.inc', ); } - - $items['admin/settings/stringoverrides/js'] = array( - 'title' => 'String Overrides Javascript', - 'page callback' => 'stringoverrides_js', - 'type' => MENU_CALLBACK, - 'file' => 'stringoverrides.admin.inc', - 'access arguments' => array('administer string overrides'), - ); + return $items; } /** - * Implementation of hook_theme() + * Implements hook_theme(). */ function stringoverrides_theme() { return array( 'stringoverrides_strings' => array( - 'arguments' => array('form' => NULL), + 'render element' => 'form', ), ); }