<?php

/**
 * Nieko's script to practise Russian :).
 *
 * Most things are stored as PHP arrays, and
 * then serialized and stored/fetched and
 * deserialized.
 *
 * The idea is that the application shows a self-made
 * sentence in Russian, with a hidden field in Dutch.
 * When pressing the answer button, the Dutch translation
 * follows.
 *
 * Obviously, self-made sentences from a computer have
 * a high chance of being stupid :P, but so be it.
 *
 * GET-parameters:
 * hide=true|false
 *  Hide Dutch sentences
 * lang=ru|nl
 *  Show Russian/Dutch sentences first
 *
 *
 *  Copyright (c) 2005 Nieko Maatjes (nieko.net)
 *
 *  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.
 *
 *  See http://www.gnu.org/licenses/gpl.txt
 */

/*
 * Word types
 * English       Dutch
 * adjective     bijvoeglijk naamwoord
 * adverb        bijwoord
 * participle    deelwoord
 * particle      partikel
 * numeral       telwoord
 * interjection  tussenwerpsel
 * pronoun       voornaamwoord
 * conjunction   voegwoord
 * preposition   voorzetsel
 * verb          werkwoord
 * noun          zelfstandig naamwoord
 * name          naam (with capital letter ;)
 */

class Russian extends Module
{
    public function 
run()
    {
        global 
$gem;
        global 
$cases;

        
$gem->css[] = 'russian.css';
        
$gem->js[] = 'russian.js';

        
$cases = ['1' => 'nom''gen''dat''acc''ins''pos'];

        
//use: printf($format, <id>, <ru>, <nl>);
        
$hide = isset($_GET['hide']) && $_GET['hide'] == 'false' 'block' 'none';
        
$hidetext $hide == 'none' 'open' 'close';
        
$format = <<<HTML
        <p class='border'>
            <span class='left'>
                %2\$s
            </span>
            <span class='right'>
                [<a href='javascript:switchblock(%1\$d);' id='a%1\$d'>
$hidetext</a>]
            </span>
            <br />
            <span id='block%1\$d' style='display: 
$hide'>
                %3\$s
            </span>
        </p>
HTML;

        
$langtrue = isset($_GET['lang']) && $_GET['lang'] == 'nl';
        
$hidetrue = isset($_GET['hide']) && $_GET['hide'] == 'false';
        if (
$langtrue) {
            
$lang $hidetrue '?hide=false&amp;lang=ru' '?hide=true&amp;lang=ru';
            
$langtext 'Russian';
        }
        else {
            
$lang $hidetrue '?hide=false&amp;lang=nl' '?hide=true&amp;lang=nl';
            
$langtext 'Dutch';
        }
        if (
$hidetrue) {
            
$hide $langtrue '?hide=true&amp;lang=nl' '?hide=true&amp;lang=ru';
            
$hidetext 'Hide translations';
        }
        else {
            
$hide $langtrue '?hide=false&amp;lang=nl' '?hide=false&amp;lang=ru';
            
$hidetext 'Show translations';
        }

        
$output '<h1>'.html::escape($gem->struct->cur_page['info'][$gem->i18n->language])."</h1>\n";
        
$output .= "<p><a href='$hide'>$hidetext</a> | <a href='$lang'>$langtext</a></p>\n";

        for (
$i 1$i <= 5; ++$i) {
            list(
$ru$nl) = $this->_sentence();
            if (isset(
$_GET['lang']) && $_GET['lang'] == 'nl') {
                
$output .= sprintf($format$i$nl$ru);
            }
            else {
                
$output .= sprintf($format$i$ru$nl);
            }
        }

        return [
$output'Html5', []];
    }

    private function 
_sentence()
    {
        global 
$gem$cases;

        list(
$subject) = ($gem->db->query("SELECT * FROM `russian` WHERE `type` IN ('pronoun', 'noun', 'name') ORDER BY RAND() LIMIT 0,1"));
        list(
$verb) = ($gem->db->query("SELECT * FROM `russian` WHERE `type`='verb' ORDER BY RAND() LIMIT 0,1"));
        list(
$object) = ($gem->db->query("SELECT * FROM `russian` WHERE `type` IN ('pronoun', 'name', 'noun') ORDER BY RAND() LIMIT 0,1"));
        list(
$prepos) = ($gem->db->query("SELECT * FROM `russian` WHERE `type`='preposition' ORDER BY RAND() LIMIT 0,1"));
        list(
$other) = ($gem->db->query("SELECT * FROM `russian` WHERE `type` IN ('pronoun', 'name', 'noun') ORDER BY RAND() LIMIT 0,1"));
        
$subject['ru_info'] = unserialize($subject['ru_info']);
        
$subject['nl_info'] = unserialize($subject['nl_info']);
        
$verb['ru_info'] = unserialize($verb['ru_info']);
        
$verb['nl_info'] = unserialize($verb['nl_info']);
        
$object['ru_info'] = unserialize($object['ru_info']);
        
$object['nl_info'] = unserialize($object['nl_info']);
        
$prepos['ru_info'] = unserialize($prepos['ru_info']);
        
$prepos['nl_info'] = unserialize($prepos['nl_info']);
        
$other['ru_info'] = unserialize($other['ru_info']);
        
$other['nl_info'] = unserialize($other['nl_info']);

        
$ru '';
        
$nl '';
        
$rand rand(01) > 0.5;

        
//SUBJECT
        
if ($subject['type'] == 'noun' && $subject['ru_info']['number'] == 'both') {
            
$singular $rand;
        }
        elseif (
$subject['type'] == 'noun' && $subject['ru_info']['number'] == 'singular') {
            
$singular true;
        }
        elseif (
$subject['type'] == 'noun' && $subject['ru_info']['number'] == 'plural') {
            
$singular false;
        }
        elseif (
$subject['type'] == 'pronoun' && is_array($subject['ru_info']['declension'][0])) {
            
$singular true;
        }
        elseif (
$subject['type'] == 'pronoun' && is_array($subject['ru_info']['declension'][1])) {
            
$singular false;
        }
        elseif (
$subject['type'] == 'name') {
            
$singular true;
        }
        
$ru .= $this->_stress($subject['ru_info']['declension'][$singular 1]['nom']);
        
//VERB
        
if ($rand) {
            
//present tense

            
if ($subject['type'] == 'noun') {
                
$person $singular 6;
            }
            elseif (
$subject['type'] == 'name') {
                
$person 3;
            }
            else {
                
$person $subject['ru_info']['follows'];
            }
            
$ru .= ' '.$this->_stress($verb['ru_info']['conjugation']['present'][$person]);
        }
        else {
            
//past tense

            
switch ($subject['ru_info']['gender']) {
                case 
'male':    $gender 1; break;
                case 
'female':  $gender 2; break;
                case 
'neutral'$gender 3; break;
                default: 
$gender 4; break;
            }
            
$gender $singular $gender 4;
            
$ru .= ' '.$this->_stress($verb['ru_info']['conjugation']['past'][$gender]);
        }
        
//OBJECT
        
if ($verb['ru_info']['active'] == true) {
            if (
is_array($object['ru_info']['declension'][0]) && is_array($object['ru_info']['declension'][1])) {
                
$ru .= ' '.$this->_stress($object['ru_info']['declension'][$rand 0]['acc']);
            }
            elseif (
is_array($object['ru_info']['declension'][0])) {
                
$ru .= ' '.$this->_stress($object['ru_info']['declension'][0]['acc']);
            }
            elseif (
is_array($object['ru_info']['declension'][1])) {
                
$ru .= ' '.$this->_stress($object['ru_info']['declension'][1]['acc']);
            }
        }
        
//PREPOSITION AND OTHER
        
$ru .= ' '.$this->_stress($prepos['ru']);
        
$n $other['type'] == 'pronoun' && ($other['ru_info']['follows'] == || $other['ru_info']['follows'] == 6) ? 'н' '';
        if (
is_array($other['ru_info']['declension'][0]) && is_array($other['ru_info']['declension'][1])) {
            
$ru .= ' '.$this->_stress($n.$other['ru_info']['declension'][$rand 0][$cases[$prepos['ru_info']['follows']]]);
        }
        elseif (
is_array($other['ru_info']['declension'][0])) {
            
$ru .= ' '.$this->_stress($n.$other['ru_info']['declension'][0][$cases[$prepos['ru_info']['follows']]]);
        }
        elseif (
is_array($other['ru_info']['declension'][1])) {
            
$ru .= ' '.$this->_stress($n.$other['ru_info']['declension'][1][$cases[$prepos['ru_info']['follows']]]);
        }

        
//weird Russian thingies :P
        
$ru str_replace(" к мн<span class='underline'>е</span>"" ко мн<span class='underline'>е</span>"$ru);
        
$ru str_replace(" п<span class='underline'>о</span>д мн<span class='underline'>о</span>й"" подо мн<span class='underline'>о</span>й"$ru);
        
$ru str_replace(" н<span class='underline'>а</span>д мн<span class='underline'>о</span>й"" надо мн<span class='underline'>о</span>й"$ru);
        
$ru str_replace(" п<span class='underline'>е</span>ред мн<span class='underline'>о</span>й"" передо мн<span class='underline'>о</span>й"$ru);
        
$ru str_replace(" с мн<span class='underline'>о</span>й"" со мн<span class='underline'>о</span>й"$ru);
        
$ru str_replace(" о мн<span class='underline'>е</span>"" <span class='underline'>о</span>бо мн<span class='underline'>е</span>"$ru);

        
//SUBJECT
        
if ($subject['type'] == 'noun' && $subject['ru_info']['number'] == 'both') {
            
$singular $rand;
        }
        elseif (
$subject['type'] == 'noun' && $subject['ru_info']['number'] == 'singular') {
            
$singular true;
        }
        elseif (
$subject['type'] == 'noun' && $subject['ru_info']['number'] == 'plural') {
            
$singular false;
        }
        elseif (
$subject['type'] == 'pronoun' && is_array($subject['ru_info']['declension'][0])) {
            
$singular true;
        }
        elseif (
$subject['type'] == 'pronoun' && is_array($subject['ru_info']['declension'][1])) {
            
$singular false;
        }
        if (
$subject['type'] == 'name') {
            
$singular true;
            
$nl .= $this->_stress($subject['nl']);
        }
        elseif (
$subject['type'] == 'pronoun') {
            
$nl .= $this->_stress($subject['nl']);
        }
        else {
            
$nl .= $this->_stress($subject['nl_info']['declension'][$singular 1]);
        }
        
//VERB
        
if ($rand) {
            
//present tense

            
if ($subject['type'] == 'noun') {
                
$person $singular 6;
            }
            elseif (
$subject['type'] == 'name') {
                
$person 3;
            }
            else {
                
$person $subject['nl_info']['follows'];
            }
            
$nl .= ' '.$this->_stress($verb['nl_info']['conjugation']['present'][$person]);
        }
        else {
            
//past tense

            
$nl .= ' '.$this->_stress($verb['nl_info']['conjugation']['past'][$singular 2]);
        }
        
//OBJECT
        
if ($verb['nl_info']['active'] == true) {   //pronoun
            
if (is_array($object['nl_info']['declension'][0])) {
                
$nl .= ' '.$this->_stress($object['nl_info']['declension'][0]['acc']);
            }
            elseif (
is_array($object['nl_info']['declension'][1])) {
                
$nl .= ' '.$this->_stress($object['nl_info']['declension'][1]['acc']);
            }
            else {
                if (
$object['type'] == 'noun') {
                    
$nl .= ' '.$this->_stress($object['nl_info']['declension'][$rand 0]);
                }
                else {
                    
//name
                    
$nl .= ' '.$this->_stress($object['nl_info']['declension']);
                }
            }
        }
        
//PREPOSITION AND OTHER
        
$nl .= ' '.$this->_stress($prepos['nl']);
        
//pronoun
        
if (is_array($other['nl_info']['declension'][0])) {
            
$nl .= ' '.$this->_stress($other['nl_info']['declension'][0][$cases[$prepos['ru_info']['follows']]]);
        }
        elseif (
is_array($other['nl_info']['declension'][1])) {
            
$nl .= ' '.$this->_stress($other['nl_info']['declension'][1][$cases[$prepos['ru_info']['follows']]]);
        }
        else {
            if (
$other['type'] == 'noun') {
                
$nl .= ' '.$this->_stress($other['nl_info']['declension'][$rand 0]);
            }
            else {
                
//name
                
$nl .= ' '.$this->_stress($other['nl_info']['declension']);
            }
        }

        return [
$ru$nl];
    }

    private function 
_stress($in)
    {
        return 
preg_replace_callback(
            
"/(\p{L}*?)(\p{Lu}+)(\p{Ll}*?)\b/iu",
            function (
$matches) {
                return 
$matches[1]."<span class='underline'>".strtolower($matches[2]).'</span>'.$matches[3];
            },
            
$in
        
);
    }
}

//Database thingies
//Examples
//
//$ru = "АлИна";
//$nl = "Alina";
//$type = "name";
//$ru_info = ["gender" => "female"
//                ,"number" => "singular"
//                ,"living" => true
//                ,"declension" => [
//                  ["nom" => "АлИна"
//                       ,"gen" => "АлИны"
//                       ,"dat" => "АлИне"
//                       ,"acc" => "АлИну"
//                       ,"ins" => "АлИной"
//                       ,"pos" => "АлИне"],
//                  null
//                  ]];
//
//$nl_info = ["gender" => "female"
//                ,"number" => "singular"
//                ,"declension" => "AlIna"
//                ];
//
//$ru = "Ездить";
//$nl = "rOndrijden";
//$type = "name";
//$ru_info = ["verbtype" => "II"
//                ,"active" => false
//                ,"conjugation" => [
//                  "present" => ["1" => "Езжу"
//                                    ,"Ездишь"
//                                    ,"Ездит"
//                                    ,"Ездим"
//                                    ,"Ездите"
//                                    ,"Ездят"],
//                  "past"    => ["1" => "Ездил"
//                                    ,"Ездила"
//                                    ,"Ездило"
//                                    ,"Ездили"]
//                  ]];
//$nl_info = ["active" => false
//                ,"conjugation" => [
//                  "present" => ["1" => "rIJd rOnd"
//                                    ,"rIJdt rOnd"
//                                    ,"rIJdt rOnd"
//                                    ,"rIJden rOnd"
//                                    ,"rIJden rOnd"
//                                    ,"rIJden rOnd"],
//                  "past"    => ["1" => "rEEd rOnd"
//                                    ,"rEden rOnd"]
//                  ]];
//
//$query = "INSERT INTO russian (`id`,`ru`,`nl`,`type`,`ru_info`,`nl_info`) VALUES('', '%s', '%s', '%s', '%s', '%s')";
//$query = sprintf($query, $ru, $nl, $type, serialize($ru_info), serialize($nl_info));
//print htmlspecialchars($query);
;