From 6b445114da301438f386eeb9853448b53fd9e516 Mon Sep 17 00:00:00 2001 From: patdenice Date: Thu, 25 Mar 2010 20:18:28 +0000 Subject: Feature 1535: Add language manager. git-svn-id: http://piwigo.org/svn/trunk@5357 68402e56-0260-453c-a942-63ccdbb3a9ee --- admin/include/functions.php | 1 + admin/include/languages.class.php | 228 +++++++++++++++++++++ admin/languages_installed.php | 90 ++++++++ admin/themes/default/default-layout.css | 8 +- admin/themes/default/template/admin.tpl | 1 + .../default/template/languages_installed.tpl | 34 +++ 6 files changed, 360 insertions(+), 2 deletions(-) create mode 100644 admin/include/languages.class.php create mode 100644 admin/languages_installed.php create mode 100644 admin/themes/default/template/languages_installed.tpl (limited to 'admin') diff --git a/admin/include/functions.php b/admin/include/functions.php index d16b518c0..f4a332623 100644 --- a/admin/include/functions.php +++ b/admin/include/functions.php @@ -1949,6 +1949,7 @@ function get_active_menu($menu_page) case 'menubar': case 'themes_new': case 'themes_installed': + case 'languages_installed': return 5; } return 0; diff --git a/admin/include/languages.class.php b/admin/include/languages.class.php new file mode 100644 index 000000000..dd65c8abd --- /dev/null +++ b/admin/include/languages.class.php @@ -0,0 +1,228 @@ +fs_languages = $this->get_fs_languages($target_charset); + } + + /** + * Perform requested actions + * @param string - action + * @param string - language id + * @param array - errors + */ + function perform_action($action, $language_id) + { + global $conf; + + if (isset($this->db_languages[$language_id])) + { + $crt_db_language = $this->db_languages[$language_id]; + } + + $errors = array(); + + switch ($action) + { + case 'activate': + if (isset($crt_db_language)) + { + array_push($errors, 'CANNOT ACTIVATE - LANGUAGE IS ALREADY ACTIVATED'); + break; + } + + $query = " +INSERT INTO ".LANGUAGES_TABLE." + SET id = '".$language_id."', + name = '".$this->fs_languages[$language_id]."' +;"; + pwg_query($query); + break; + + case 'deactivate': + if (!isset($crt_db_language)) + { + array_push($errors, 'CANNOT DEACTIVATE - LANGUAGE IS ALREADY DEACTIVATED'); + break; + } + + if ($language_id == get_default_language()) + { + array_push($errors, 'CANNOT DEACTIVATE - LANGUAGE IS DEFAULT LANGUAGE'); + break; + } + + $query = " +DELETE + FROM ".LANGUAGES_TABLE." + WHERE id= '".$language_id."' +;"; + pwg_query($query); + break; + + case 'delete': + if (!empty($crt_db_language)) + { + array_push($errors, 'CANNOT DELETE - LANGUAGE IS ACTIVATED'); + break; + } + if (!isset($this->fs_languages[$language_id])) + { + array_push($errors, 'CANNOT DELETE - LANGUAGE DOES NOT EXIST'); + break; + } + + // Set default language to user who are using this language + $query = ' +UPDATE '.USER_INFOS_TABLE.' + SET language = "'.get_default_language().'" + WHERE language = "'.$language_id.'" +;'; + pwg_query($query); + + if (!$this->deltree(PHPWG_ROOT_PATH.'language/'.$language_id)) + { + $this->send_to_trash(PHPWG_ROOT_PATH.'language/'.$language_id); + } + break; + + case 'set_default': + $query = ' +UPDATE '.USER_INFOS_TABLE.' + SET language = "'.$language_id.'" + WHERE user_id = '.$conf['default_user_id'].' +;'; + pwg_query($query); + break; + } + return $errors; + } + + /** + * Get languages defined in the language directory + */ + function get_fs_languages($target_charset = null) + { + if ( empty($target_charset) ) + { + $target_charset = get_pwg_charset(); + } + $target_charset = strtolower($target_charset); + + $dir = opendir(PHPWG_ROOT_PATH.'language'); + + while ($file = readdir($dir)) + { + $path = PHPWG_ROOT_PATH.'language/'.$file; + if (!is_link($path) and is_dir($path) and file_exists($path.'/iso.txt')) + { + list($language_name) = @file($path.'/iso.txt'); + + $languages[$file] = convert_charset($language_name, $target_charset); + } + } + closedir($dir); + @asort($languages); + + return $languages; + } + + function get_db_languages() + { + $query = ' + SELECT id, name + FROM '.LANGUAGES_TABLE.' + ORDER BY name ASC + ;'; + $result = pwg_query($query); + + while ($row = pwg_db_fetch_assoc($result)) + { + $this->db_languages[ $row['id'] ] = $row['name']; + } + } + + /** + * delete $path directory + * @param string - path + */ + function deltree($path) + { + if (is_dir($path)) + { + $fh = opendir($path); + while ($file = readdir($fh)) + { + if ($file != '.' and $file != '..') + { + $pathfile = $path . '/' . $file; + if (is_dir($pathfile)) + { + $this->deltree($pathfile); + } + else + { + @unlink($pathfile); + } + } + } + closedir($fh); + return @rmdir($path); + } + } + + /** + * send $path to trash directory + * @param string - path + */ + function send_to_trash($path) + { + $trash_path = PHPWG_ROOT_PATH . 'language/trash'; + if (!is_dir($trash_path)) + { + @mkdir($trash_path); + $file = @fopen($trash_path . '/.htaccess', 'w'); + @fwrite($file, 'deny from all'); + @fclose($file); + } + while ($r = $trash_path . '/' . md5(uniqid(rand(), true))) + { + if (!is_dir($r)) + { + @rename($path, $r); + break; + } + } + } +} +?> \ No newline at end of file diff --git a/admin/languages_installed.php b/admin/languages_installed.php new file mode 100644 index 000000000..ec12ae74b --- /dev/null +++ b/admin/languages_installed.php @@ -0,0 +1,90 @@ +set_filenames(array('languages' => 'languages_installed.tpl')); + +$base_url = get_root_url().'admin.php?page='.$page['page']; + +$languages = new languages(); +$languages->get_db_languages(); + +//--------------------------------------------------perform requested actions +if (isset($_GET['action']) and isset($_GET['language']) and !is_adviser()) +{ + $page['errors'] = $languages->perform_action($_GET['action'], $_GET['language']); + + if (empty($page['errors'])) + { + redirect($base_url); + } +} + +// +-----------------------------------------------------------------------+ +// | start template output | +// +-----------------------------------------------------------------------+ +$default_language = get_default_language(); + +foreach($languages->fs_languages as $language_id => $language_name) +{ + $template->append('languages', array( + 'ID' => $language_id, + 'NAME' => $language_name, + 'U_ACTION' => $base_url.'&language='.$language_id, + 'STATE' => isset($languages->db_languages[$language_id]) ? 'active' : '', + 'IS_DEFAULT' => $language_id == $default_language, + ) + ); +} + + +$missing_language_ids = array_diff( + array_keys($languages->db_languages), + array_keys($languages->fs_languages) + ); + +foreach($missing_language_ids as $language_id) +{ + $query = ' +UPDATE '.USER_INFOS_TABLE.' + SET language = "'.get_default_language().'" + WHERE language = "'.$language_id.'" +;'; + pwg_query($query); + + $query = " +DELETE + FROM ".LANGUAGES_TABLE." + WHERE id= '".$language_id."' +;"; + pwg_query($query); +} + +$template->assign_var_from_handle('ADMIN_CONTENT', 'languages'); +?> \ No newline at end of file diff --git a/admin/themes/default/default-layout.css b/admin/themes/default/default-layout.css index d9c71d96a..68cba396e 100644 --- a/admin/themes/default/default-layout.css +++ b/admin/themes/default/default-layout.css @@ -36,13 +36,14 @@ TABLE#detailedStats { width: 99%; } -/* Plugins tables */ +/* Plugins, languages tables */ TABLE.plugins { min-width: 400px; } TABLE.plugins A { border: 0; } TABLE.plugins TR TD { padding: 4px 10px; } TABLE.plugins TR TD.pluginState { padding: 4px 16px; } -TABLE.plugins TR TD.active { +TABLE.plugins TR TD.active, +TABLE.languages TR TD.active { background: url(icon/plugin_active.gif) no-repeat center left; background-color: inherit; /* IE need it */ } @@ -63,6 +64,9 @@ TABLE.plugins ul.pluginsActions { TABLE.plugins ul.pluginsActions li { display: inline; } +TABLE.languages { min-width: 400px; } +TABLE.languages TR TD { text-align: center; padding: 4px 20px; } + /* categoryOrdering */ SELECT.categoryList { width: 100%; diff --git a/admin/themes/default/template/admin.tpl b/admin/themes/default/template/admin.tpl index 23da23182..9f2bde9e0 100644 --- a/admin/themes/default/template/admin.tpl +++ b/admin/themes/default/template/admin.tpl @@ -84,6 +84,7 @@ jQuery().ready(function(){ldelim}
  • {'Options'|@translate}
  • {'Menu'|@translate}
  • {'Templates'|@translate}
  • +
  • {'Languages'|@translate}
  • {'Themes'|@translate}
  • diff --git a/admin/themes/default/template/languages_installed.tpl b/admin/themes/default/template/languages_installed.tpl new file mode 100644 index 000000000..e83f13437 --- /dev/null +++ b/admin/themes/default/template/languages_installed.tpl @@ -0,0 +1,34 @@ +
    +

    {'Languages'|@translate}

    +
    + + + + + + + + + +{foreach from=$languages item=language name=languages_loop} + + + + +{/foreach} +
    {'Language'|@translate}{'Actions'|@translate}
    + {$language.NAME} + {if $language.IS_DEFAULT}({'Default'|@translate}){/if} + + {if !$language.IS_DEFAULT} + {if $language.STATE == 'active' or $language.STATE == 'missing'} + {'Deactivate'|@translate} | + {'Default'|@translate} + {else} + {'Activate'|@translate} | + {'Delete'|@translate} + {/if} + {else} + --- + {/if} +
    \ No newline at end of file -- cgit v1.2.3