<?php
class ProcessNewWordStepHandler extends CallBackhandler {
public static $key = EnumSteps::USER_ASKED_FOR_INSERTING_NEW_WORD;
private $message;
private $lng;
function execute(){
$contribution = $this->getDB()->getContributionByUserId($this->getTG()->chat_id);
if($this->isSameLanguage($contribution)) {
$this->getTG()->getButtonService()->clearKeyboard();
$cancelButton = new Button();
$cancelButton->setData("-");
$cancelButton->setKey(CancelProcessCallbackHandler::$key);
$cancelButton->setText('بی خیال');
$continueButton = new Button();
$continueButton->setData("-");
$continueButton->setKey(RewriteMeaningCallbackHandler::$key);
$continueButton->setText('معنیش رو اضافه می کنم');
$this->getTG()->getButtonService()->addButton($cancelButton);
$this->getTG()->getButtonService()->addButton($continueButton);
$this->getTG()->getButtonService()->rebuildKeyboard();
$answerArray = $this->getTG()->getButtonService()->getKeyboardMarkedup();
$this->getTG()->sendMarkedupMessage($this->message, $answerArray);
}
else if($this->isInDatabase($contribution['word'], $this->getTG()->message['text'])){
$this->getTG()->sendMessage("این ترجمه قبلا توی لغتنامه وارد شده. یه معنی دیگه بنویس",$this->getTG()->chat_id);
}
else if ($contribution['type'] == "verb" && !$this->validateVerb($this->getTG()->message['text'])){
// && !$this->validateVerb($this->getTG()->message['text']
// $this->validateVerb($this->getTG()->message['text'];
$validationMsg = "فعل باید به صورت مصدری";
$validationMsg .= " یا ";
$validationMsg .= " infinitive ";
$validationMsg .= " نوشته بشه ";
$validationMsg .= " مثلا فعل ";
$validationMsg .= " به چیزی نگاه کردن ";
$validationMsg .= " anschauen : ";
$validationMsg .= " هردو در انتها حرف ن یا ";
$validationMsg .= " n ";
$validationMsg .= " رو دارن حالا سعی کن مصدر فعل رو بنویسی";
$this->getTG()->sendMessage($validationMsg, $this->getTG()->chat_id);
}
else if(false && !$this->getTG()->isPersianLanguage($this->getTG()->message['text']) && $this->isInOnlineDictionaries($this->getTG()->message['text'])){
$this->getTG()->sendMessage("it is not in dict maybe false spelled", $this->getTG()->chat_id);
}
else{
if(isset($contribution['word'])){
$word1 = $contribution['word'];
$this->lng = EditNewWordCallbackHandler::$langDE;
}
else {
$word1 = $contribution['translation'];
$this->lng = EditNewWordCallbackHandler::$langFA;
}
$this->askToConfirmNewWord($word1);
$this->updateTranslation();
}
return true;
}
private function updateTranslation(){
if($this->getTG()->isPersianLanguage($this->getTG()->message['text'])){
$this->getDB()->setTranslatedWord($this->getTG()->message['text'], $this->getTG()->chat_id);
}
else {
$this->getDB()->setTranslatedWordForPersian($this->getTG()->message['text'], $this->getTG()->chat_id);
}
}
private function getTG(): TranslateGerman{
return $this->getSM();
}
public function isInOnlineDictionaries($word){
$dict = new DictCC($this->getTG());
$dict->checkWord($word);
$ling = new Linguee($this->getTG());
$ling->checkWord($word);
return false;
}
public function isSameLanguage($contribution){
if(!empty($contribution['word'])){
$word1 = $contribution['word'];
}
else {
$word1 = $contribution['translation'];
}
$word2= $this->getTG()->message['text'];
$wordPart = explode($word2, " ");
// if(is_array($wordPart) && !empty($wordPart[0]))
// $word2= $wordPart[0];
if(strpos($word2, " ")){
$wordPart = explode(" ", $word2 );
$word2 = $wordPart[0];
}
if(strpos($word1, " ")){
$wordPart = explode(" ", $word1 );
$word1 = $wordPart[0];
}
$isSame = $this->getTG()->isPersianLanguage($word1) == $this->getTG()->isPersianLanguage($word2);
if($isSame){
$questionText = "عزیزم این کلمه ";
$questionText .= $word1;
$questionText .= " با معنیش ";
$questionText .= $word2;
$questionText .= " به یک زبان هستن. اینجوری که نمی شه. ";
$questionText .= " لطفا معنی درستش رو تایپ کن پایین و بفرست ";
$questionText .=PHP_EOL. "و اگه معنیش رو نمی دونی دکمه بی خیال رو فشار بده";
$this->message = $questionText;
return true;
}
else {
return false;
}
}
public function askToConfirmNewWord($word){
$qText = " 👇 پس می شه گفت معنی کلمه به صورت زیر نوشته می شه ؟";
$qText.=PHP_EOL;
$qText.= $word." : ". $this->getTG()->message['text'];
$this->getDB()->setUserStep($this->getTG()->chat_id, EnumSteps::USER_ADDED_NEW_WORD);
// $this->getTG()->setTranslatedWord($this->getTG()->message['text'],$this->getTG()->chat_id );
// $answerArray = [
// ['text'=>'تائید می کنم', 'id'=>ٍEnumSteps::USER_ASKED_FOR_CONFIRMING_HIS_TRANSLATION],
// ['text'=>'نیاز به اصلاح داره', 'id'=>EnumSteps::USER_WANTS_INSERT_MEANING],
// ['text'=>'بی خیال', 'id'=>EnumSteps::USER_IS_TRANSLATING]
// ];
$answerArray = [
['text'=>'تائید می کنم', 'id'=>ConfirmSelfAddedWordCallbackHandler::$key.":".$word],
['text'=>'نیاز به اصلاح داره', 'id'=>EditNewWordCallbackHandler::$key.":".$this->lng],
['text'=>'بی خیال', 'id'=>CancelProcessCallbackHandler::$key.":".$word]
];
$this->getTG()->askUserMultipleChoiceQuestion($qText, $answerArray, $this->getTG()->chat_id);
}
public function isInDatabase($word1, $meaning){
return $this->getDB()->isInDatabase($word1, $meaning);
}
private function getDB(): LearnSystemDatabase {
return $this->getParent()->getDB();
}
public function getParent() : LearnSystem{
return parent::getParent();
}
public function validateVerb($verb){
// substr($verb, -1..
mb_internal_encoding("UTF-8");
$len = mb_strlen($verb);
$verbEnd = mb_substr($verb,$len-1,1);
if(mb_strpos($verbEnd, "ن") === 0 || mb_strpos($verbEnd, "n") === 0 ){
return true;
}
else {
return false;
}
// if($this->getTG()->isPersianLanguage($word))
// return true;
}
}