<?php

// security check - must be included in all scripts

if (!$GLOBALS['kewl_entry_point_run'])

{

    die("You cannot view this page directly");

}

// end security check



/**

* The class that demonstrates how to use the KEWL.NextGen

* architecture by providing a hello wold example.

*

* @author Jeremy O'Connor

* @author Nic Appleby

*

*

* $Id: pofile_class_inc.php,v 1.3 2007-04-24 09:58:35 joconnor Exp $

*

*/

class pofile extends dbTable {



    var $objConfig;



    function init() {

        parent::init('tbl_translationmanager_languagelist');

        $this->objConfig = $this->getObject('altconfig','config');

    }

    function tableExists($table) {
        //Get an array of tables
        $ar = $this->listDbTables();
        $numElements = count($ar);
        for($counter=0; $counter < $numElements; $counter++) {
           if($ar[$counter] == $table) {
               return TRUE;
           }
        }
        return FALSE;
    }


    function getLangFile($langName) {

        $rec = $this->getRow('name',$langName);

        $path = $this->objConfig->contentBasePath()."pofiles/{$rec['code']}.po";

        return $path;

    }



    function parse($str) {

        $result = '';

        $len = strlen($str);

        $i = 0;

        while($i < $len){

            $char = $str[$i];

            if ($char == '"') {

                $result .= '\\'.'"';

            } else if (ord($char)==10) {

                   } else if (ord($char)==13) {

                        } else {

                            $result .= $char;

                        }

            $i++;

        } // while

        return $result;

    }



    function export($langName) {

        //$langFile = $this->getLangFile($langName);

        if (!$this->tableExists('tbl_'.$langName)) {

            echo "No language '$langName' exists.";
            return; //FALSE;

        } else {
        $stmt = "

            SELECT

                tbl_$langName.id AS code,

                tbl_$langName.en AS Content,

                tbl_languagetext.description AS description

            FROM

                tbl_$langName, tbl_languagetext

            WHERE

                tbl_$langName.id = tbl_languagetext.code

            ";

        $array = $this->getArray($stmt);

        /*

        $element = $array[0];

        $content = $element['Content'];

        for ($i=0;$i<strlen($content);$i++) {

            echo '['.ord($content[$i]).']';

        }

        */

        //$fd = fopen($langFile,'w');

        foreach ($array as $element)

        {

            $msgid = $element['code'];

            $msgstr = $element['content'];

            $description = $element['description'];

            $str =

            '# '.$this->parse($description).chr(10)

            .'msgid "'.$this->parse($msgid).'"'.chr(10)

            .'msgstr "'.$this->parse($msgstr).'"'.chr(10)

            .chr(10);

            //fputs($fd, $str);
            echo $str;

        }

        //fclose ($fd);

        //echo "OK";

        return; //TRUE;

        }

    }



    function import($langName, $langFile) {
        //echo "BP1";
        $objDBLanguageList =& $this->getObject('dblanguagelist');
        $objDBLanguageList->query("DELETE FROM tbl_languagelist WHERE languageCode='tbl_$langName'");
        $objDBLanguageList->query("INSERT INTO tbl_languagelist SET languageCode='tbl_$langName', languageName='".ucfirst($langName)."'");
        //echo "BP2";

        //$langFile = $this->getLangFile($langName);

        if ($this->tableExists('tbl_'.$langName)) {

            //die("Table `tbl_$langName` already exists.");

            $this->query("DROP TABLE `tbl_$langName`");

        } //else {
          //$this->query("DROP TABLE `tbl_$langName`");

        $this->query(

            "

            CREATE TABLE `tbl_$langName` (

            `code` varchar(50) NOT NULL default '',

            `Content` mediumtext,

            `isInNextGen` tinyint(1) default NULL,

            `dateCreated` datetime default '2006-04-01 12:00:00',

            `creatorUserId` varchar(25) default '1',

            `dateLastModified` datetime default NULL,

            `modifiedByUserId` varchar(25) default NULL,

            PRIMARY KEY  (`code`)

            ) TYPE=InnoDB

            "

            );

        //}

        $fd = fopen($langFile,'r');

        define("STATE_NULL",0);

        define("STATE_IN_MSGID",1);

        define("STATE_IN_MSGSTR",2);

        $state = STATE_NULL;

        $rescan = FALSE;

        while(!feof($fd)){

            if (!$rescan) {

                $line = fgets($fd, 65535);

                if (feof($fd)) {

                    break;

                }

                /*
                //Strip trailing CR/LF
                // Windows
                if (preg_match('/(.*)\r\n/', $line, $matches)) {
                    $line = $matches[1];
                }
                // Linux/Unix
                else if (preg_match('/(.*)\n/', $line, $matches)) {
                    $line = $matches[1];
                }
                // Max OS
                else if (preg_match('/(.*)\r/', $line, $matches)) {
                    $line = $matches[1];
                }
                // Unknown
                else {
                    $line = '';
                }
                */
                //Strip whitespace (incl. trailing CR/LF)
                $line = rtrim($line);

                //echo "\"$line\"<br/>";

                /*

                if (strlen($line)>0) {

                   for ($i=0;$i<strlen($line);$i++) {

                       echo '['.ord($line[$i]).']';

                   }

                   echo('<br/>');

             }

             */

            } else {

                $rescan = FALSE;

            }

            if ($line == '') {

                $state = STATE_NULL;

                //echo 'BLANK LINE<br/>';

                // Insert the record into the database here...

                // Skip the first entry

                if ($msgid=='') {

                    continue;

                }

                $sql = "SELECT COUNT(*) AS fCount FROM tbl_$langName WHERE code = '".addslashes($msgid)."'";
                $rs = $this->query($sql);
                if (!$rs) {
                    $ret = false;
                } else {
                    $line = $rs->fetchRow();
                    if ($line['fCount'] > 0) {
                        $ret = true;
                    } else {
                        $ret = false;
                    }
                }
                //return $ret;

                if (!$ret) {

                    $msgid = addslashes($msgid);

                    $msgstr = addslashes($msgstr);

                    $this->query(

                    "

                    INSERT INTO tbl_$langName SET

                    code='$msgid',

                    Content='$msgstr'

                    "

                    );

                }


                continue;

            }

            else if ($line[0]=='#') {

                //$state = STATE_NULL;

                continue;

            }

            // Implement a finite state machine

            //echo "($state)";

            switch($state){

                case STATE_NULL:

                    if (preg_match('/^msgid "(.*)"/', $line, $matches)) {

                        $msgid = $matches[1];

                        //echo "msgid==[$msgid]<br/>";

                        $state = STATE_IN_MSGID;

                    } else if (preg_match('/^msgstr "(.*)"/', $line, $matches)) {

                        $msgstr = $matches[1];

                        //echo "msgstr==[$msgstr]<br/>";

                        $state = STATE_IN_MSGSTR;

                    }

                    break;

                case STATE_IN_MSGID:

                    if (preg_match('/^"(.*)"/', $line, $matches)) {

                        $msgid .= $matches[1];

                        //echo "msgid==[$msgid]<br/>";

                    } else {

                        $state = STATE_NULL;

                        $rescan = TRUE;

                    }

                    break;

                case STATE_IN_MSGSTR:

                    if (preg_match('/^"(.*)"/', $line, $matches)) {

                        $msgstr .= $matches[1];

                        //echo "msgstr==[$msgstr]<br/>";

                    } else {

                        $state = STATE_NULL;

                    }

                    break;

                default:

                    ;

            } // switch

        } // while

        fclose ($fd);

        //echo "OK";

    }

}

?>