<?php
class SelectImagePlugin
{
    private $table = "_plugin_selectimage";

    private $database = DB_WARPIT_WEBCATI_BASE;

    private $id_plugin = 100;

    private $data;

    private $db;

    public function __construct($db)
    {
        $this->db = $db;
    }

    public function load($idProject, $idQuestion)
    {
        $sql = "SELECT * FROM " . $this->database . "." . $this->table . "
            WHERE id_question = " . $idQuestion . " AND id_project = " . $idProject . " AND deleted = 0";
         $res= $this->db->SQLexecute($sql);
         $this->data = $this->db->fetchAssoc($res);

         if(empty($this->data))
         {
            $sql = "SELECT * FROM " .  $this->database . "._Plugin_label
            WHERE id_plugin = " . $this->id_plugin;
            $res= $this->db->SQLexecute($sql);
            while ($dat = $this->db->fetchAssoc($res)) {
                $this->data[$dat['name']] = $dat['default_value'];
            }
         }
    }

    public function parseLanguage($text)
    {
        $text = mysql_real_escape_string($text);
        $language = $_SESSION['id_selectedQuestionLng'];
        $labelsArray = explode('/', $text);
        $langLabel = array();
       if(count($labelsArray) > 1)
        {
            foreach($labelsArray as $label)
            {
                $exploded = explode(' ', $label, 2);
                $langLabel[$exploded[0]] = $exploded[1];
            }
        }
        else
        {
            return $text;
        }

        if(!key_exists($language, $langLabel))
            $language = key(current($langLabel));

        return $langLabel[$language];
    }

    public function getVal($key)
    {
        //TODO popravi left pa right text da bo delalo če bo vseboval link
        $textVaraibles = array('no_answer_text', /*'left_text', 'right_text',*/ 'default_text', 'no_answer_text');

        if(in_array($key, $textVaraibles))  return $this->parseLanguage($this->data[$key]);
        return $this->data[$key];
    }
}

?>
