Login   Register  
PHP Classes
elePHPant
Icontem

File: tools/i18n_dbm_editor.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Alan H. Lake  >  I18N class  >  tools/i18n_dbm_editor.php  >  Download  
File: tools/i18n_dbm_editor.php
Role: Application script
Content type: text/plain
Description: Program to edit DBM data
Class: I18N class
Get translation texts from different containers
Author: By
Last change: Added subdirectory to file name.
Date: 2005-09-05 22:07
Size: 11,773 bytes
 

Contents

Class file image Download
<?php

    /*
       i18n_dbm_editor.php - XL() Translation file (DBM) editor
       Copyright (C) 2005  Alan Lake

         This program is free software; you can redistribute it and/or modify
         it under the terms of the GNU General Public License as published by
         the Free Software Foundation; either version 2 of the License, or
         (at your option) any later version.

         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 General Public License for more details.

         You should have received a copy of the GNU General Public License
         along with this program; if not, write to the Free Software
         Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

       Current version: 1.0.0 (August 23, 2005)
    */


    $ScriptName = "i18n_dbm_editor.php";


/* -------------------------------------------------------------------------- */
    function OpenLocaleFile ($localepath, $locale) {
        // Open an existing locale file for writing
        $filename = $localepath . $locale . ".dbm";
        $dbi = dbmopen ($filename, "c");
        return $dbi;
    }


    function CloseLocaleFile ($dbi) {
        dbmclose ($dbi);
    }


    function ReadLocaleFile ($dbi) {
        $key = dbmfirstkey ($dbi);
        while ($key) {
            $result[$key] = stripslashes (unserialize (urldecode 
                                (dbmfetch ($dbi, $key))));
            $key = dbmnextkey ($dbi, $key);
        }
        return $result;
    }


    function AddLocaleEntry ($localepath, $selectedlocale, $entry, $value) {
        $dbi = OpenLocaleFile ($localepath, $selectedlocale);
        dbminsert ($dbi, $entry, urlencode (serialize ($value)));
        CloseLocaleFile ($dbi);
    }


    function EditLocaleEntry ($localepath, $selectedlocale, $entry, $value) {
        $dbi = OpenLocaleFile ($localepath, $selectedlocale);
        dbmreplace ($dbi, $entry, urlencode (serialize ($value)));
        CloseLocaleFile ($dbi);
    }


    function DeleteLocaleEntry ($localepath, $selectedlocale, $entry) {
        $dbi = OpenLocaleFile ($localepath, $selectedlocale);
        dbmdelete ($dbi, $entry);
        CloseLocaleFile ($dbi);
    }


    function RenameLocaleEntry ($localepath, $selectedlocale, $oldentry, 
                                $entry) {

        $oldvalue = GetLocaleEntry ($localepath, $selectedlocale, $oldentry);
        AddLocaleEntry ($localepath, $selectedlocale, $entry, $oldvalue);
        DeleteLocaleEntry ($localepath, $selectedlocale, $oldentry);
    }


    function GetLocaleEntry ($localepath, $selectedlocale, $entry) {
        $dbi = OpenLocaleFile ($localepath, $selectedlocale);
        $value = stripslashes (unserialize (urldecode 
                     (dbmfetch ($dbi, $entry))));
        CloseLocaleFile ($dbi);
        return $value;
    }
        

    function DisplayLocalePathForm() {
        // Display the form requesting locale path and storage type

        // Global variables
        global $ScriptName;

        // Display the form
        echo "<p><b>Enter configuration information about this STPhp enabled ",
             "application.</b>\n";
        echo "<p><br><form action=\"$ScriptName\" method=POST>\n";
        echo "Please enter the path to the locale files.\n";
        echo "<input maxlen=255 size=50 name=localepath><br><br>\n";
        echo "<br><br><input type=\"SUBMIT\" value=\"Continue\">";
        echo "</form>\n";
    }


    function DisplayLocaleSelectionForm ($localepath) {
        // Display form permitting the user to select the locale they
        // wish to edit

        // Global variables
        global $ScriptName;

        echo "<p><b>Please enter the identifier for the locale you wish ",
             "to edit.</b>\n";
        echo "<form action=\"$ScriptName\" method=POST>\n";
        echo "<input type=\"HIDDEN\" name=localepath value=$localepath>\n";
        echo "Locale identifier: <input maxlen=10 size=10 ",
             "name=selectedlocale><br>\n\n";
        echo "<br><br><input type=\"SUBMIT\" value=\"Continue\">";
        echo "</form>\n";
    }


    function DisplayLocaleData ($localepath, $selectedlocale) {
        $dbi = OpenLocaleFile ($localepath, $selectedlocale);
        $localedata = ReadLocaleFile ($dbi);
        CloseLocaleFile ($dbi);

        if (!empty ($localedata)) {
            ksort ($localedata);
            reset ($localedata);

            echo "<table cellpadding=2 cellspacing=1 border=1>\n";
            echo "<tr><th>Message Identifier</th><th>Message Value</th>",
                 "<th>&nbsp;</th></tr>\n";
            while (list ($key, $value) = each ($localedata)) {
                echo "<tr><td>$key</td><td>$value</td><td>";
                ShowTagEditEntry ($localepath, $selectedlocale, $key);
                ShowTagRenameEntry ($localepath, $selectedlocale, $key, "<br>");
                ShowTagDeleteEntry ($localepath, $selectedlocale, $key, "<br>");
                echo "</td></tr>\n";
            }
            echo "</table>\n";
        } else {
            echo "<p>The selected locale's translation file is empty.",
                 "<br>\n";
        }

        ShowTagAddEntry ($localepath, $selectedlocale, "<br>", "<br>");
    }


    function DisplayFormAddEditEntry ($command, $localepath, $selectedlocale,
                                      $entry) {

        // Global variables
        global $ScriptName;

        echo "<p><b>Please enter the value for this entry ($entry)</b>\n";
        echo "<form action=\"$ScriptName\" method=POST>\n";
        echo "<input type=\"HIDDEN\" name=localepath value=$localepath>\n";
        echo "<input type=\"HIDDEN\" name=selectedlocale ",
             "value=$selectedlocale>\n";
        echo "<input type=\"HIDDEN\" name=entry value=$entry>\n";
        if ($command == "ADD") {
            echo "Value: <input maxlen=255 size=50 ",
                 "name=value><br>\n\n";
            echo "<input type=\"HIDDEN\" name=submit_new_entry value=1>\n";
            echo "<br><br><input type=\"SUBMIT\" value=\"Add\">";
        } elseif ($command == "EDIT") {
            $value = GetLocaleEntry ($localepath, $selectedlocale, $entry);
            echo "Value: <input maxlen=255 size=50 ",
                 "name=value value=\"", htmlspecialchars ($value), 
                 "\"><br>\n\n";
            echo "<input type=\"HIDDEN\" name=submit_modified_entry value=1>\n";
            echo "<br><br><input type=\"SUBMIT\" value=\"Edit\">";
        } else {
            die ("Invalid command specified.\n");
        }
        echo "</form>\n";

    }


    function DisplayFormAddEditName ($command, $localepath, $selectedlocale,
                                     $entry="") {

        // Global variables
        global $ScriptName;

        echo "<p><b>Please enter the name for this entry</b>\n";
        echo "<form action=\"$ScriptName\" method=POST>\n";
        echo "<input type=\"HIDDEN\" name=localepath value=$localepath>\n";
        echo "<input type=\"HIDDEN\" name=selectedlocale ",
             "value=$selectedlocale>\n";

        if ($command == "ADD") {
            echo "<input type=\"HIDDEN\" name=add_entry value=1>\n";
            echo "Value: <input maxlen=255 size=50 ",
                 "name=entry><br>\n\n";
            echo "<br><br><input type=\"SUBMIT\" value=\"Add\">";
        } elseif ($command == "EDIT") {
            echo "<input type=\"HIDDEN\" name=oldentry value=$entry>\n";
            echo "<input type=\"HIDDEN\" name=submit_new_name value=1>\n";
            echo "Value: <input maxlen=255 size=50 ",
                 "name=entry value=$entry><br>\n\n";
            echo "<br><br><input type=\"SUBMIT\" value=\"Edit\">";
        } else {
            die ("Invalid command specified.\n");
        }

        echo "</form>\n";

    }


    function ShowTagAddEntry ($localepath, $selectedlocale,
                              $predisplay="", $postdisplay="") {
        // Global variables
        global $ScriptName;

        echo $predisplay;
        echo "<a href=\"$ScriptName?name_entry=1&localepath=$localepath",
             "&selectedlocale=$selectedlocale\">[Add new entry]</a>\n";
        echo $postdisplay;
    }


    function ShowTagEditEntry ($localepath, $selectedlocale, $entry,
                               $predisplay="", $postdisplay="") {
        // Global variables
        global $ScriptName;

        echo $predisplay;
        echo "<a href=\"$ScriptName?modify_entry=1&localepath=$localepath",
             "&selectedlocale=$selectedlocale&entry=$entry\">",
             "[Edit this entry]</a>\n";
        echo $postdisplay;
    }


    function ShowTagRenameEntry ($localepath, $selectedlocale, $entry,
                               $predisplay="", $postdisplay="") {
        // Global variables
        global $ScriptName;

        echo $predisplay;
        echo "<a href=\"$ScriptName?rename_entry=1&localepath=$localepath",
             "&selectedlocale=$selectedlocale&entry=$entry\">",
             "[Rename this entry]</a>\n";
        echo $postdisplay;
    }


    function ShowTagDeleteEntry ($localepath, $selectedlocale, $entry,
                                 $predisplay="", $postdisplay="") {
        // Global variables
        global $ScriptName;

        echo $predisplay;
        echo "<a href=\"$ScriptName?delete_entry=1&localepath=$localepath",
             "&selectedlocale=$selectedlocale&entry=$entry\">",
             "[Delete this entry]</a>\n";
        echo $postdisplay;
    }


/* -------------------------------------------------------------------------- */
    if (isset ($localepath)) {
        if (substr ($localepath, strlen ($localepath) - 1) != "/")
            $localepath .= "/";
        if (!is_dir ($localepath))
            die ("<p>The locale path you specified does not exist or " .
                 "is otherwise invalid. <a href=\"$ScriptName\">" .
                 "Please try again.</a>\n");
    }


    if (!isset ($localepath)) {
        DisplayLocalePathForm();
    } elseif (!isset ($selectedlocale)) {
        DisplayLocaleSelectionForm ($localepath);
    } elseif ($rename_entry) {
        DisplayFormAddEditName ("EDIT", $localepath, $selectedlocale, $entry);
    } elseif ($name_entry) {
        DisplayFormAddEditName ("ADD", $localepath, $selectedlocale);
    } elseif ($add_entry) {
        DisplayFormAddEditEntry ("ADD", $localepath, $selectedlocale, $entry);
    } elseif ($delete_entry) {
        DeleteLocaleEntry ($localepath, $selectedlocale, $entry);
        DisplayLocaleData ($localepath, $selectedlocale);
    } elseif ($modify_entry) {
        DisplayFormAddEditEntry ("EDIT", $localepath, $selectedlocale, $entry);
    } elseif ($submit_new_name) {
        RenameLocaleEntry ($localepath, $selectedlocale, $oldentry, $entry);
        DisplayLocaleData ($localepath, $selectedlocale);
    } elseif ($submit_new_entry) {
        AddLocaleEntry ($localepath, $selectedlocale, $entry, $value);
        DisplayLocaleData ($localepath, $selectedlocale);
    } elseif ($submit_modified_entry) {
        EditLocaleEntry ($localepath, $selectedlocale, $entry, $value);
        DisplayLocaleData ($localepath, $selectedlocale);
    } else {
        DisplayLocaleData ($localepath, $selectedlocale);
    }


?>