<?php
namespace SYSTEM\SAI;

class saimod_sys_locale extends \SYSTEM\SAI\SaiModule {
    
    public static function getLanguages(){
        return \SYSTEM\CONFIG\config::get(\SYSTEM\CONFIG\config_ids::SYS_CONFIG_LANGS);        
    }

    public static function sai_mod__SYSTEM_SAI_saimod_sys_locale(){
        $result =   '<h3>Locale String</h3>'.
                    //<input type="submit" value="Add" class="btn add_form">
                    '<br><table class="table table-hover table-condensed" style="overflow: auto;">'.        
                    '<tr>'.'<th>'.'ID'.'</th>'.'<th>'.'Category'.'</th>';
                    
                    foreach (self::getLanguages() as $lang){
                        $result .= '<th>'.$lang.'</th>';
                        $languages[] = $lang;
                    }
                    
                    $result .= '</tr>';        
        
        $con = new \SYSTEM\DB\Connection(\SYSTEM\system::getSystemDBInfo());
        if(\SYSTEM\system::isSystemDbInfoPG()){
            $res = $con->query('SELECT * FROM system.locale_string ORDER BY "category" ASC;');
        } else {
            $res = $con->query('SELECT * FROM system_locale_string ORDER BY category ASC;');
        }
        while($r = $res->next()){
            $result .= '<tr>'.'<td style="padding-bottom: 5px;">'.$r["id"].'<br><br><input type="submit" class="btn-danger delete_content" value="delete" id="'.$r["id"].'">'.'<input type="submit" class="btn" value="edit" style="margin-left: 3px;" name="'.$r["id"].'">'.'</td>'.'<td>'.$r["category"].'</td>';
                    foreach ($languages as $columns){
                        //echo "+tututututututut:".$r[$columns]."nochmal tututututututut";
                        $result .= '<td>'.utf8_encode($r[$columns]).'</td>';
                        //$_POST[$r["id"]] = $r[$columns];
                    }
                    
            $result .= '</tr>';
            
            }
        $result .= '</table>';
               
        return $result;
    }
    
    public static function sai_mod__SYSTEM_SAI_saimod_sys_locale_action_edit($id, $lang, $newtext){
         $charset = 'utf-8';
         $con = new \SYSTEM\DB\Connection(\SYSTEM\system::getSystemDBInfo());
         $res = null;
        if(\SYSTEM\system::isSystemDbInfoPG()){
            $res = $con->prepare('newText' ,'UPDATE system.locale_string SET "'.$lang.'"=$1 WHERE id=$2;', array($newtext, $id));
        } else {
            $res = $con->prepare('newText' ,'UPDATE system_locale_string SET '.$lang.'=? WHERE id=?;', array(utf8_decode($newtext), $id));
        }
        return $res->affectedRows() == 0 ? \SYSTEM\LOG\JsonResult::error(new \SYSTEM\LOG\WARNING("no rows affected")) : \SYSTEM\LOG\JsonResult::ok();
    }
    
    public static function sai_mod__SYSTEM_SAI_saimod_sys_locale_action_add($id, $lang, $newtext){
         $con = new \SYSTEM\DB\Connection(\SYSTEM\system::getSystemDBInfo());
         $res = null;
        if(\SYSTEM\system::isSystemDbInfoPG()){
            throw new \SYSTEM\LOG\ERROR("action_edit failed");
        } else {
                $res = $con->prepare('addText' ,'INSERT INTO system_locale_string (id, '.$lang.', category) VALUES (?, ?, 100);', array($id, $newtext));
        }
        return $res->affectedRows() == 0 ? \SYSTEM\LOG\JsonResult::error(new \SYSTEM\LOG\WARNING("no data added")) : \SYSTEM\LOG\JsonResult::ok();
    }
    public static function sai_mod__SYSTEM_SAI_saimod_sys_locale_action_addcontent(){
         $result = "<h3>Add new text</h3><br>";
         $result .= '<input type="text" id="new" value=""><br><select id="langselect" size="1">'; 
         foreach (self::getLanguages() as $lang){
                        $result .= '<option>'.$lang.'</option>';
                        $languages[] = $lang;
                    }
         $result .= '</select><br><textarea class="tinymce" name="content" id="areacontent" style="width: 100%"></textarea><br><input type="submit" class="btn add" value="save">'.'<script>tinymce.init({selector:\'textarea\'});</script>';
         return $result;
    }
    
    public static function sai_mod__SYSTEM_SAI_saimod_sys_locale_action_delete($id){
         $con = new \SYSTEM\DB\Connection(\SYSTEM\system::getSystemDBInfo());
         $res = null;
        if(\SYSTEM\system::isSystemDbInfoPG()){
            throw new \SYSTEM\LOG\ERROR("action_delete failed");
        } else {
            $res = $con->prepare('deleteText' ,'DELETE FROM system_locale_string WHERE id=?;', array($id));
        }
        return $res->affectedRows() == 0 ? \SYSTEM\LOG\JsonResult::error(new \SYSTEM\LOG\WARNING("could not delete the permitted data")) : \SYSTEM\LOG\JsonResult::ok();
    }
    
    public static function sai_mod__SYSTEM_SAI_saimod_sys_locale_action_editmode($entry){
        $result =   '<h3>'.$entry.'</h3>'.                    
                    '<table class="table table-hover table-condensed" style="overflow: auto;">'.        
                    '<tr>';
                    
                    foreach (self::getLanguages() as $lang){
                        $result .= '<th>'.$lang.'</th>';
                        $languages[] = $lang;
                    }
                    
                    $result .= '</tr>';                
        $con = new \SYSTEM\DB\Connection(\SYSTEM\system::getSystemDBInfo());
        $res = null;
        if(\SYSTEM\system::isSystemDbInfoPG()){
            $res = $con->prepare(   'edit',
                                    'SELECT * FROM system.locale_string WHERE id = $1 ORDER BY "category" ASC;',
                                    array($entry));
        } else {
            $res = $con->prepare(   'edit',
                                    'SELECT * FROM system_locale_string WHERE id = ? ORDER BY "category" ASC;',
                                    array($entry));
        }       
        while($r = $res->next()){
            $result .= "<tr>";
            $fu = 0;
            foreach ($languages as $columns){                        
                $result .= '<td>
                            <div class="dialog" style="padding-bottom: 5px;">'.
                                '<textarea name="content" class="tinymce" style="width: 100%" id="edit_field_'.$r["id"].'_'.$columns.'">'.utf8_encode($r[$columns]).'</textarea>'.
                            '</div>
                            <input type="submit" class="btn edit_content" value="edit" lang="'.$columns.'" name="'.$r["id"].'"><br></td>';
            }
            $result .= "</tr></table>";
        }
        $result .=  '<br><input type="submit" class="btn localeMain" value="back">'.'<script>tinymce.init({
    selector: "textarea",
    entity_encoding : "raw",
    theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
    plugins: [
        "advlist autolink lists link image charmap print preview anchor",
        "searchreplace visualblocks code fullscreen",
        "insertdatetime media table contextmenu paste moxiemanager",
        "textcolor"
    ],
    toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent"
});</script>';
        return $result; 
    }
    
    public static function html_li_menu(){return '<li><a href="#" saimenu=".SYSTEM.SAI.saimod_sys_locale">DB Text</a></li>';}
    public static function right_public(){return false;}    
    public static function right_right(){return \SYSTEM\SECURITY\Security::check(\SYSTEM\SECURITY\RIGHTS::SYS_SAI);}
    
    public static function sai_mod__SYSTEM_SAI_saimod_sys_locale_flag_css(){}
    public static function sai_mod__SYSTEM_SAI_saimod_sys_locale_flag_js(){
        return \SYSTEM\LOG\JsonResult::toString(
            array(  \SYSTEM\WEBPATH(new \SYSTEM\PSAI(),'modules/saimod_sys_locale/saimod_sys_locale_submit.js')));}
}