<?php
class DictionaryEnglishDatabaseHandler extends DatabaseHandler {
private $wordArrayResult;
private $databaseName = "musiceri_english_bot";
function __construct($botUsername){
parent::__construct($botUsername);
$this->wordArrayResult="";
}
public function addWordsToDb($wordArrayResult){
$catchedWord = $this->getWordFromDb($word);
if(empty($catchedNews['id'])){
$this->insertWordsArrayToDb($wordArrayResult);
}
return $catchedNews['id'];
}
public function getTrnslations($word, $ln) {
$sql = "SELECT * FROM `".$this->databaseName."`.`dictionary_english` where searchedWord='" . $word . "' AND status='".$ln."'";
$this->connection->query ( "SET NAMES utf8" );
$result = $this->connection->query ( $sql ) or die ( $this->connection->error );
while ($wordArrayResult[] = mysqli_fetch_assoc($result));
if(count ($wordArrayResult) > 0 && !empty($wordArrayResult[0]) )
$this->wordArrayResult = $wordArrayResult;
return $this->wordArrayResult;
}
public function getNewWords($number){
$sql = "SELECT * FROM `musiceri_english_dictionary`.`entries` where seen = 0 GROUP BY word ORDER BY RAND() LIMIT $number";
$result = $this->connection->query ( $sql ) or die ( $this->connection->error );
$words = [];
while($word = mysqli_fetch_assoc($result)){
$words[] = $word['word'];
};
return $words;
}
public function makeTranslationTextMessage() {
$translated ="";
$finished = false;
foreach ( $this->wordArrayResult as $word ) {
if($word['word'] && ($word['category']=="words" || $word['category']=="mainMeanEnglishArray" || $word['category']=="mainMean")){
// $translated.=trim($word['category']) ." - ". trim($word['word']).": ".$word['translation'].PHP_EOL;
$translated .= trim($word['word']).": ".$word['translation'].PHP_EOL;
if(strlen($translated) <= 3800 ){
$result = $translated;
}
}
}
// $testMessage = var_export(stripslashes($translated), true);
// file_put_contents("testTranslatedResult.html", $testMessage, FILE_APPEND | LOCK_EX);
// $translated = str_replace("'", "", $translated);
return $result. " 😊".PHP_EOL. "@TranslateEnglish_bot";
}
public function getWordById($wordId) {
$sql = "SELECT * FROM `".$this->databaseName."`.`dictionary_english` where id=$wordId";
$result = $this->connection->query ( $sql ) or die ( $this->connection->error );
$news = mysqli_fetch_assoc($result);
return $news;
}
public function insertWordsWithTranslation($translationsArray, $ln, $searchedWord){
$status = $ln;
foreach ($translationsArray as $key=>$wordArea){
if($key=="mainMean" && $wordArea !="" ){
$this->insertWord($searchedWord,$wordArea,$category ="mainMean",$wordType="",$status, $searchedWord);
}
else if($key=="mainMeanEnglishArray" && $wordArea !="" ){
foreach($wordArea as $word){
$this->insertWord($word,$searchedWord, $category ="mainMeanEnglishArray",$wordType="" ,$status, $searchedWord);
}
}
else if($key=="words" && $wordArea !="" ){
foreach($wordArea as $word){
$this->insertWord($word['word'],$word['mean'], $category ="words",$wordType="" ,$status, $searchedWord);
}
}
else if($key=="synWords" && $wordArea !="" ){
foreach($wordArea as $word){
echo $wordType;
$this->insertWord($word['word'],$word['mean'], $category ="syn" ,$word['meanType'] ,$status, $searchedWord);
}
}
else if($key=="simWords" && $wordArea !="" ){
foreach($wordArea as $word){
$this->insertWord($word['word'],$word['mean'], $category ="sim",$wordType="" ,$status, $searchedWord);
}
}
}
$this->seen($searchedWord);
}
public function seen($word){
$word = addslashes($word);
$sql= "UPDATE `musiceri_english_dictionary`.`entries` set seen = 1 where word like '".$word."' ";
$result = $this->connection->query ( $sql ) or die ("seen error ".$this->connection->error );
}
public function isWordAndTranslation($word,$translation, $ln, $searchedWord){
$translation= addslashes($translation);
$word= addslashes($word);
$searchedWord= addslashes($searchedWord);
// $searchedWord = str_replace("'", "\'", $searchedWord);
$sql = "SELECT * FROM `".$this->databaseName."`.`dictionary_english` WHERE word='".$word."'"
." AND translation='".$translation."' AND searchedword='".$searchedWord."' AND status='".$ln."'";
$this->connection->query ( "SET NAMES utf8" );
$result = $this->connection->query ( $sql ) or die ( "error check ".$this->connection->error );
$words = mysqli_fetch_assoc($result);
if(count($words)>0)
return true;
else
return false;
}
public function replaceApast(){
}
public function insertWord($word,$translation,$category,$wordType ,$ln , $searchedWord){
if($this->isWordAndTranslation($word,$translation, $ln , $searchedWord))
return false;
$word = addslashes($word);
$searchedWord = addslashes($searchedWord);
$translation = addslashes($translation);
$sql = "INSERT INTO `".$this->databaseName."`.`dictionary_english` (`word`,`translation`,`category`,`wordType`,`status` ,`searchedword`)
VALUES ('"
. $word."' ,'"
. $translation."' ,'"
. $category."' ,'"
. $wordType."' ,'"
. $ln."' ,'"
. $searchedWord."'"
.");";
$this->connection->query ( "SET NAMES utf8" );
$result = $this->connection->query ( $sql ) or die( "error insert ".$this->connection->error );
return true;
}
public function addTranslationToWord($wordId,$translation){
$status ="translated";
$sql = "UPDATE `".$this->databaseName."`.`dictionary_english` set translation ='".$translation
."' AND status='ready' where id=".$wordId;
$result = $this->connection->query ( $sql );
$this->connection->error();
}
public function isUser($userId){
$sql = "SELECT * FROM `".$this->databaseName."`.`dicusers_english` WHERE userid='".$userId."'";
$this->connection->query ( "SET NAMES utf8" );
$result = $this->connection->query ( $sql ) or die ( $this->connection->error );
$words = mysqli_fetch_assoc($result);
if(count($words)>0)
return true;
else
return false;
}
public function insertUser($userId,$userName,$name ,$lastname){
if($this->isUser($userId))
return false;
$sql = "INSERT INTO `".$this->databaseName."`.`dicusers_english` (`userid`,`username`,`name`,`lastname`)
VALUES ('"
. $userId."' ,'"
. $userName."' ,'"
. $name."' ,'"
. $lastname."'"
.");";
$this->connection->query ( "SET NAMES utf8" );
$result = $this->connection->query ( $sql ) or die( $this->connection->error );
return true;
}
public function insertUserActivity($userId,$searchedWord,$translated){
$sql = "INSERT INTO `".$this->databaseName."`.`user_activities_english` (`userid`,`searchedWord`,`translated`,`trdate`)
VALUES ('"
. $userId."' ,'"
. $searchedWord."' ,'"
. $translated."' ,"
."now()"
.");";
$this->connection->query ( "SET NAMES utf8" );
$result = $this->connection->query ( $sql ) or die( $this->connection->error );
return true;
}
public function isOverUsage($userId,$limit){
//$sql = "SELECT * FROM `user_activities_english` WHERE userid = 232333396 AND trdate = CURDATE() LIMIT 0, 30 ";
$sql = "SELECT * FROM `".$this->databaseName."`.`user_activities_english` WHERE userid='".$userId."' AND trdate = CURDATE() LIMIT 0, 30";
$this->connection->query ( "SET NAMES utf8" );
$result = $this->connection->query ( $sql ) or die ( $this->connection->error );
$words = mysqli_fetch_assoc($result);
if(count($words)>$limit)
return true;
else
return false;
}
public function isUserSuggested($newSuggestedUserId){
$sql = "SELECT * FROM `".$this->databaseName."`.`dic_suggested_users_english` WHERE suggestedid='".$newSuggestedUserId."'";
$this->connection->query ( "SET NAMES utf8" );
$result = $this->connection->query ( $sql ) or die ( $this->connection->error );
$words = mysqli_fetch_assoc($result);
if(count($words)>0)
return true;
else
return false;
}
public function insertSuggested($userId,$newSuggestedUserId){
if($this->isUserSuggested($newSuggestedUserId))
return false;
$sql = "INSERT INTO `".$this->databaseName."`.`dic_suggested_users_english` (`userid`,`suggestedid`,`Datum`)
VALUES ('"
. $userId."' ,'"
. $newSuggestedUserId."' ,"
."now()"
.");";
$this->connection->query ( "SET NAMES utf8" );
$result = $this->connection->query ( $sql ) or die( $this->connection->error );
return true;
}
}