<?PHP
class language extends engine {

	var $call_sql_class = array('language', 'language_menus', 'templates');
	var $clone_class = array(
		array('language' => 'language_values')
	);
	var $call_class = array('Obj', 'Form', 'File');

	function get_public_menu_data(){

		$this->banners->banners();
		return $this;
	}
	function language_data(){

		$language = $_GET['cur_language'] ? $_GET['cur_language'] : $_SESSION['language'];

		$this->language->limit(100);
		$this->language->useMethod(array($this, 'set_current_value'));
		$this->language->language_group($language,
			($_GET['search'] && !$_GET['equals'] ? "CONCAT(p1.name,' ',p1.value,' ',p1.page) LIKE '%".$_GET['search']."%' " : "").
			($_GET['equals'] ? " p1.name LIKE p1.value " : "")
		);

		$this->language_menus->Select();
		return $this;
	}

	function set_current_value($row){

		$row['current_value'] = $row['value'];

		if($row['value_group']){

			$exp = explode(';', $row['value_group']);

			foreach($exp as $v){

				list($id, $language, $value) = explode('=', $v);
				if($_GET['cur_language'] == $language && $value){

					$row['current_value'] = $value;
					$row['id'] = $id;
					break;
				}
			}
		}
		else{

			if($_GET['cur_language']){

				if($_SESSION['language'] != $_GET['cur_language']){

					$row['current_value'] = '';
				}
			}
		}
		return $row;
	}

	function menu(){

		$this->language_menus->Select(
			"WHERE is_active = 1 ".
			"ORDER BY queue"
		);
		return $this;
	}

	function admin_menu(){

		$this->language_menus->useMethod(array($this, 'modify_row'));
		$this->language_menus->Select(
			"ORDER BY queue"
		);
		return $this;
	}

	function modify_row($row){

		if($this->language_menus->Numrows > 1){

			if($this->i < $this->language_menus->Numrows){

				$row['language_menus']['splitter'] = '|';
			}
		}
		$this-> i++;
		return $row;
	}

	function translator(){

		$this->File->GetAllFiles(_RootFolder.'/app/views');
		return $this;
	}

	function update_language(){

		foreach($_POST['value'] as $id => $value){

			$this->language->Update(array('value' => $value), "WHERE id = '".$id."' ");
		}
		if(is_file(_RootFolder.'/tmp/script/language.js')) unlink(_RootFolder.'/tmp/script/language.js');
	}

	function check(){

		list($name, $file_key, $string_key) = explode('_', $_GET['id']);
		$_SESSION[$name][$file_key][$string_key] = $_GET['is_checked'];
		exit;
	}

	function set_view_translator(){

		$folder = _RootFolder.'/tmp/backup/views/'.date('d_m_Y_H_i_s');
		$this->File->WriteDirectorRecursive($folder);

		system('cp -rf '._RootFolder.'/app/views/* '.$folder);

		foreach($_POST['file'] as $key => $file){

			if($_POST['translate'][$key]){

				$content = file_get_contents($file);
				echo $file.'<br>';

				foreach($_POST['translate'][$key] as $k => $translate){

					if($translate == true){

						$replacement = str_replace($_POST['replacement'][$key][$k], '<?=$this->Select(\''.$_POST['replacement'][$key][$k].'\');?>', $_POST['replace'][$key][$k]);

						/*echo 'replace:'.htmlspecialchars($_POST['replace'][$key][$k]).'<br>';
						echo 'replacement:'.htmlspecialchars($_POST['replacement'][$key][$k]).'<br>';
						echo 'tulemus:'.htmlspecialchars($replacement).'<br><br>';*/

						//$content = str_replace($_POST['replace'][$key][$k], $replacement, $content);
						$content = str_replace('>'.$_POST['replace'][$key][$k].'<', '>'.$replacement.'<', $content);
					}
				}
				file_put_contents($file, $content);
				//die(htmlspecialchars($content));
			}
		}
	}

	function add_language_menu(){

		$this->language_menus->Insert($_POST);
	}

	function update_language_menu(){

		$_POST['is_active'] = $_POST['is_active'] ? 1 : 0;

		$this->language_menus->Update($_POST, "WHERE id = ".$_POST['id']);
	}

	function delete_language_menu(){

		$this->language_menus->Delete("WHERE id = ".$_GET['id']);
	}

	function delete_language(){

		$this->language->Delete("WHERE id IN(".implode($_POST['delete'], ',').")");
	}

	function truncate(){

		$this->language->Query("TRUNCATE TABLE language");
		die($this->Redirect('?model=language&page=language'));
	}
}
?>