summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJose Reyero2010-09-14 11:30:30 (GMT)
committer Jose Reyero2010-09-14 11:30:30 (GMT)
commit3bfcff3078349ee51259ca6dcc42046ce4bba98d (patch)
tree86147f08c8fa2d5939f07e8fae6083a4842adbd8
parent5c0dd8609beca55ab09377fd2718c19857b7f32b (diff)
Moving functions from i18n.module, applying latest patches
-rw-r--r--i18n.module208
-rw-r--r--i18nvariable/i18nvariable.module231
2 files changed, 231 insertions, 208 deletions
diff --git a/i18n.module b/i18n.module
index 84566ef..338561c 100644
--- a/i18n.module
+++ b/i18n.module
@@ -90,6 +90,7 @@ function _i18n_init_mode() {
}
}
}
+
/**
* Implementation of hook_help().
*/
@@ -524,13 +525,6 @@ function i18n_preprocess_page(&$variables) {
}
/**
- * Implementation of hook_exit().
- */
-function i18n_exit() {
- _i18n_variable_exit();
-}
-
-/**
* Implementation of hook_form_alter();
*
* This is the place to add language fields to all forms.
@@ -603,14 +597,6 @@ function i18n_form_alter(&$form, $form_state, $form_id) {
}
}
}
-
- // Multilingual variables in settings form.
- if (isset($form['#theme']) && $form['#theme'] == 'system_settings_form' && $variables = variable_get('i18n_variables', 0)) {
- if ($i18n_variables = i18n_form_alter_settings($form, $variables)) {
- array_unshift($form['#submit'], 'i18n_variable_form_submit');
- $form['#i18n_variables'] = $i18n_variables;
- }
- }
}
}
@@ -650,74 +636,6 @@ function i18n_menu_edit_item_form_submit($form, &$form_state) {
}
/**
- * Check for multilingual variables in form.
- */
-function i18n_form_alter_settings(&$form, &$variables) {
- $result = array();
- foreach (element_children($form) as $field) {
- if (count(element_children($form[$field])) && empty($form[$field]['#tree'])) {
- $result += i18n_form_alter_settings($form[$field], $variables);
- }
- elseif (in_array($field, $variables)) {
- // Add form field class: i18n-variable
- $form[$field]['#attributes']['class'] = !empty($form[$field]['#attributes']['class']) ? $form[$field]['#attributes']['class'] . ' i18n-variable' : 'i18n-variable';
- $form[$field]['#description'] = !empty($form[$field]['#description']) ? $form[$field]['#description'] : '';
- $form[$field]['#description'] .= ' <strong>'. t('This is a multilingual variable.') .'</strong>';
- // Addd field => name to result
- $result[$field] = !empty($form[$field]['#title']) ? $form[$field]['#title'] : $field;
- }
- }
- return $result;
-}
-
-/**
- * Save multilingual variables and remove them from form.
- */
-function i18n_variable_form_submit($form, &$form_state) {
- $op = isset($form_state['values']['op']) ? $form_state['values']['op'] : '';
- $variables = variable_get('i18n_variables', array());
- $language = i18n_get_lang();
-
- foreach ($form_state['values'] as $key => $value) {
- if (in_array($key, $variables)) {
- if ($op == t('Reset to defaults')) {
- i18n_variable_del($key, $language);
- }
- else {
- if (is_array($value) && isset($form_state['values']['array_filter'])) {
- $value = array_keys(array_filter($value));
- }
- i18n_variable_set($key, $value, $language);
- }
- unset($form_state['values'][$key]);
- }
- }
- // Re-submit form
- // system_settings_form_submit($form_id, $form_values);
-}
-
-/**
- * Initialization of multilingual variables.
- *
- * @param $langcode
- * Language to retrieve variables. Defaults to current language.
- */
-function i18n_variable_init($langcode = NULL) {
- global $conf;
- global $i18n_conf;
-
- $langcode = $langcode ? $langcode : i18n_get_lang();
- if (isset($i18n_conf[$langcode])) {
- // If this language was previously set, just switch the array
- $conf = $i18n_conf[$langcode];
- }
- elseif ($variables = _i18n_variable_init($langcode)) {
- $conf = array_merge($conf, $variables);
- $i18n_conf[$langcode] = $conf;
- }
-}
-
-/**
* Get language from context.
*/
function _i18n_get_context_lang() {
@@ -747,53 +665,6 @@ function _i18n_language_select($value ='', $description ='', $weight = -20, $lan
);
}
-/**
- * Load language variables into array.
- */
-function _i18n_variable_init($langcode) {
- static $variables;
-
- if (!isset($variables[$langcode])) {
- $cacheid = 'variables:'. $langcode;
- if ($cached = cache_get($cacheid)) {
- $variables[$langcode] = $cached->data;
- }
- else {
- $result = db_query("SELECT * FROM {i18n_variable} WHERE language = '%s'", $langcode);
- $variables[$langcode] = array();
- while ($variable = db_fetch_object($result)) {
- $variables[$langcode][$variable->name] = unserialize($variable->value);
- }
- cache_set($cacheid, $variables[$langcode]);
- }
- }
-
- return $variables[$langcode];
-}
-
-/**
- * Save multilingual variables that may have been changed by other methods than settings pages.
- */
-function _i18n_variable_exit() {
- global $conf, $i18n_conf;
-
- $langcode = i18n_get_lang();
- if (isset($i18n_conf[$langcode])) {
- $refresh = FALSE;
- // Rewritten because array_diff_assoc may fail with array variables.
- foreach ($i18n_conf[$langcode] as $name => $value) {
- if (isset($conf[$name]) && $value != $conf[$name]) {
- $refresh = TRUE;
- $i18n_conf[$langcode][$name] = $conf[$name];
- db_query("DELETE FROM {i18n_variable} WHERE name='%s' AND language='%s'", $name, $langcode);
- db_query("INSERT INTO {i18n_variable} (language, name, value) VALUES('%s', '%s', '%s')", $langcode, $name, serialize($conf[$name]));
- }
- }
- if ($refresh) {
- cache_set('variables:'. $langcode, $i18n_conf[$langcode]);
- }
- }
-}
/**
* Check whether we are in bootstrap mode.
@@ -855,83 +726,6 @@ function i18n_supported_languages($all = FALSE) {
}
/**
- * Set a persistent language dependent variable.
- *
- * @param $name
- * The name of the variable to set.
- * @param $value
- * The value to set. This can be any PHP data type; these functions take care
- * of serialization as necessary.
- * @param $langcode
- * Language code.
- */
-function i18n_variable_set($name, $value, $langcode) {
- global $conf, $i18n_conf;
-
- $serialized_value = serialize($value);
- db_query("UPDATE {i18n_variable} SET value = '%s' WHERE name = '%s' AND language = '%s'", $serialized_value, $name, $langcode);
- if (!db_affected_rows()) {
- @db_query("INSERT INTO {i18n_variable} (name, language, value) VALUES ('%s', '%s', '%s')", $name, $langcode, $serialized_value);
- }
-
- cache_clear_all('variables:'. $langcode, 'cache');
-
- $conf[$name] = $value;
- $i18n_conf[$name] = $value;
-}
-
-/**
- * Get single multilingual variable
- */
-function i18n_variable_get($name, $langcode, $default = NULL) {
- if ($variables = _i18n_variable_init($langcode)) {
- return isset($variables[$name]) ? $variables[$name] : $default;
- }
- else {
- return $default;
- }
-}
-
-/**
- * Unset a persistent multilingual variable.
- *
- * @param $name
- * The name of the variable to undefine.
- * @param $langcode
- * Language code.
- */
-function i18n_variable_del($name, $langcode) {
- global $conf, $i18n_conf;
-
- db_query("DELETE FROM {i18n_variable} WHERE name = '%s' AND language='%s'", $name, $langcode);
- cache_clear_all('variables:'. $langcode, 'cache');
-
- unset($conf[$name]);
- unset($i18n_conf[$name]);
-}
-
-/**
- * Utility. Get part of array variable.
- */
-function i18n_array_variable_get($name, $element, $default = NULL) {
- if (($values = variable_get($name, array())) && isset($values[$element])) {
- return $values[$element];
- }
- else {
- return $default;
- }
-}
-
-/**
- * Utility. Set part of array variable.
- */
-function i18n_array_variable_set($name, $element, $value) {
- $values = variable_get($name, array());
- $values[$element] = $value;
- variable_set($name, $values);
-}
-
-/**
* @} End of "defgroup i18n_api".
*/
diff --git a/i18nvariable/i18nvariable.module b/i18nvariable/i18nvariable.module
index ae02f91..02c63ef 100644
--- a/i18nvariable/i18nvariable.module
+++ b/i18nvariable/i18nvariable.module
@@ -4,4 +4,233 @@
/**
* @file
* Internationalization (i18n) package. Multilingual variables API.
- */ \ No newline at end of file
+ */
+
+/**
+ * Implementation of hook_boot()
+ *
+ * Initialize variables, that will be used to decide on site_frontpage
+ */
+function i18n_variable_boot() {
+ drupal_bootstrap(DRUPAL_BOOTSTRAP_LANGUAGE);
+ _i18n_init();
+}
+
+/**
+ * Implementation of hook_exit().
+ */
+function i18n_variable_exit() {
+ _i18n_variable_exit();
+}
+
+/**
+ * Implementation of hook_form_alter();
+ *
+ * This is the place to add language fields to all forms.
+ */
+function i18n_variable_form_alter(&$form, $form_state, $form_id) {
+ // Multilingual variables in settings form.
+ if (isset($form['#theme']) && $form['#theme'] == 'system_settings_form' && $variables = i18n_variable_list()) {
+ if ($i18n_variables = i18n_variable_form_alter_settings($form, $variables)) {
+ array_unshift($form['#submit'], 'i18n_variable_form_submit');
+ $form['#i18n_variables'] = $i18n_variables;
+ }
+ }
+}
+
+/**
+ * Get list of multilingual variables or check whether a variable is multilingual
+ */
+function i18n_variable_list($name = NULL) {
+ $variables = variable_get('i18n_variables', array());
+ return $name ? in_array($name, $variables) : $variables;
+}
+
+/**
+ * Check for multilingual variables in form.
+ */
+function i18n_variable_form_alter_settings(&$form, &$variables) {
+ $result = array();
+ foreach (element_children($form) as $field) {
+ if (count(element_children($form[$field])) && empty($form[$field]['#tree'])) {
+ $result += i18n_variable_form_alter_settings($form[$field], $variables);
+ }
+ elseif (in_array($field, $variables)) {
+ // Add form field class: i18n-variable
+ $form[$field]['#attributes']['class'] = !empty($form[$field]['#attributes']['class']) ? $form[$field]['#attributes']['class'] . ' i18n-variable' : 'i18n-variable';
+ $form[$field]['#description'] = !empty($form[$field]['#description']) ? $form[$field]['#description'] : '';
+ $form[$field]['#description'] .= ' <strong>'. t('This is a multilingual variable.') .'</strong>';
+ // Addd field => name to result
+ $result[$field] = !empty($form[$field]['#title']) ? $form[$field]['#title'] : $field;
+ }
+ }
+ return $result;
+}
+
+/**
+ * Save multilingual variables and remove them from form.
+ */
+function i18n_variable_form_submit($form, &$form_state) {
+ $op = isset($form_state['values']['op']) ? $form_state['values']['op'] : '';
+ $variables = i18n_variable_list();
+ $language = i18n_get_lang();
+ $is_default = $language == language_default('language');
+ foreach ($form_state['values'] as $key => $value) {
+ if (i18n_variable_list($key)) {
+ if ($op == t('Reset to defaults')) {
+ i18n_variable_del($key, $language);
+ }
+ else {
+ if (is_array($value) && isset($form_state['values']['array_filter'])) {
+ $value = array_keys(array_filter($value));
+ }
+ i18n_variable_set($key, $value, $language);
+ }
+ // If current is default language, we allow global (without language) variables to be set too
+ if (!$is_default) {
+ unset($form_state['values'][$key]);
+ }
+ }
+ }
+ // The form will go now through system_settings_form_submit()
+}
+
+/**
+ * Initialization of multilingual variables.
+ *
+ * @param $langcode
+ * Language to retrieve variables. Defaults to current language.
+ */
+function i18n_variable_init($langcode = NULL) {
+ global $conf;
+
+ $langcode = $langcode ? $langcode : i18n_get_lang();
+ if ($variables = _i18n_variable_init($langcode)) {
+ $conf = array_merge($conf, $variables);
+ }
+}
+
+/**
+ * Set a persistent language dependent variable.
+ *
+ * @param $name
+ * The name of the variable to set.
+ * @param $value
+ * The value to set. This can be any PHP data type; these functions take care
+ * of serialization as necessary.
+ * @param $langcode
+ * Language code.
+ */
+function i18n_variable_set($name, $value, $langcode) {
+ global $conf, $i18n_conf;
+
+ $serialized_value = serialize($value);
+ db_query("UPDATE {i18n_variable} SET value = '%s' WHERE name = '%s' AND language = '%s'", $serialized_value, $name, $langcode);
+ if (!db_affected_rows()) {
+ @db_query("INSERT INTO {i18n_variable} (name, language, value) VALUES ('%s', '%s', '%s')", $name, $langcode, $serialized_value);
+ }
+
+ cache_clear_all('variables:'. $langcode, 'cache');
+
+ $conf[$name] = $value;
+ $i18n_conf[$name] = $value;
+}
+
+/**
+ * Get single multilingual variable
+ */
+function i18n_variable_get($name, $langcode, $default = NULL) {
+ if ($variables = _i18n_variable_init($langcode)) {
+ return isset($variables[$name]) ? $variables[$name] : $default;
+ }
+ else {
+ return $default;
+ }
+}
+
+/**
+ * Unset a persistent multilingual variable.
+ *
+ * @param $name
+ * The name of the variable to undefine.
+ * @param $langcode
+ * Language code.
+ */
+function i18n_variable_del($name, $langcode) {
+ global $conf, $i18n_conf;
+
+ db_query("DELETE FROM {i18n_variable} WHERE name = '%s' AND language='%s'", $name, $langcode);
+ cache_clear_all('variables:'. $langcode, 'cache');
+
+ unset($conf[$name]);
+ unset($i18n_conf[$name]);
+}
+
+/**
+ * Utility. Get part of array variable.
+ */
+function i18n_array_variable_get($name, $element, $default = NULL) {
+ if (($values = variable_get($name, array())) && isset($values[$element])) {
+ return $values[$element];
+ }
+ else {
+ return $default;
+ }
+}
+
+/**
+ * Utility. Set part of array variable.
+ */
+function i18n_array_variable_set($name, $element, $value) {
+ $values = variable_get($name, array());
+ $values[$element] = $value;
+ variable_set($name, $values);
+}
+
+/**
+ * Load language variables into array.
+ */
+function _i18n_variable_init($langcode) {
+ global $i18n_conf;
+
+ if (!isset($i18n_conf[$langcode])) {
+ $cacheid = 'variables:'. $langcode;
+ if ($cached = cache_get($cacheid)) {
+ $i18n_conf[$langcode] = $cached->data;
+ }
+ else {
+ $result = db_query("SELECT * FROM {i18n_variable} WHERE language = '%s'", $langcode);
+ $i18n_conf[$langcode] = array();
+ while ($variable = db_fetch_object($result)) {
+ $i18n_conf[$langcode][$variable->name] = unserialize($variable->value);
+ }
+ cache_set($cacheid, $i18n_conf[$langcode]);
+ }
+ }
+
+ return $i18n_conf[$langcode];
+}
+
+/**
+ * Save multilingual variables that may have been changed by other methods than settings pages.
+ */
+function _i18n_variable_exit() {
+ global $conf, $i18n_conf;
+
+ $langcode = i18n_get_lang();
+ if (isset($i18n_conf[$langcode])) {
+ $refresh = FALSE;
+ // Rewritten because array_diff_assoc may fail with array variables.
+ foreach (i18n_variable_list() as $name) {
+ if (isset($conf[$name]) && isset($i18n_conf[$langcode][$name]) && $conf[$name] != $i18n_conf[$langcode][$name]) {
+ $refresh = TRUE;
+ $i18n_conf[$langcode][$name] = $conf[$name];
+ db_query("DELETE FROM {i18n_variable} WHERE name='%s' AND language='%s'", $name, $langcode);
+ db_query("INSERT INTO {i18n_variable} (language, name, value) VALUES('%s', '%s', '%s')", $langcode, $name, serialize($conf[$name]));
+ }
+ }
+ if ($refresh) {
+ cache_set('variables:'. $langcode, $i18n_conf[$langcode]);
+ }
+ }
+}