<?php
class ButtonService extends BaseSerevice
{
private $keyboard;
private $buttonsData;
function init()
{
parent::init();
$this->buttonsData = [];
$this->initButtons();
$this->keyboard['inline_keyboard'] = [];
}
private function initButtons()
{
foreach ($this->buttonsData as $buttonObj) {
// "text": "\u062e\u0648\u0628",
// "callback_data": "likeWord:c7"
$callBackData = explode(":", $buttonObj[0]['callback_data']);
$button = new Button();
$button->setText($buttonObj[0]['text']);
$button->setKey($callBackData[0]);
$button->setData($callBackData[1]);
$this->addButton($button);
}
}
public function getImageButtons()
{
$this->addKeyboardMarkup("💬 استخراج متن ", ExtractTextCallbackHandler::$key . ":" . "null");
$markup = json_encode($this->getKeyboard(), true);
return $markup;
}
public function getVoiceButton()
{
$this->addKeyboardMarkup("💬🇩🇪 تشخیص آلمانی ", SpeechToTextCallbackHandler::$key . ":" . "de");
$this->addKeyboardMarkup("💬🇮🇷 تشخیص فارسی ", SpeechToTextCallbackHandler::$key . ":" . "fa");
$markup = json_encode($this->getKeyboard(), true);
return $markup;
}
public function createMarkUpButtons()
{
$verbConjId = 0;
if ($this->getTG()->translated && !$this->getTG()->isPersianLanguage($this->getTG()->word) && $this->getTG()->lookForSound) {
try {
$soundFile = "something";
} catch (Exception $ex) {
$this->getTG()->sendMessageLog(var_export($ex, true));
}
}
if ($this->getTG()->translated && !$this->getTG()->isPersianLanguage($this->getTG()->word)) {
if ($verbId = $this->getTG()->getDB()->isVerb($this->getTG()->word))
$verbConjId = $verbId;
}
if (!isset($soundFile)) {
$soundFile = 0;
}
$markup = $this->buildKeyboardMarkup($soundFile, $this->getTG()->word, $verbConjId);
return $markup;
}
public function buildKeyboardMarkup($soundFile, $word, $verbConjId = 0)
{
$this->addSoundKeyboard($word, $soundFile);
$this->addConjunctionkeyboard($word, $verbConjId);
$this->addTranslatorsKeys($word);
$markup = json_encode($this->getKeyboard(), true);
return $markup;
}
function clearKeyboard()
{
$this->keyboard['inline_keyboard'] = [];
}
function addTranslatorsKeys($word)
{
$isUserTranslator = $this->getTG()->getUser()->hasRole(EnumRole::TRANSLATOR);
}
public function getButtons()
{
return $this->buttons;
}
public function addButton(Button $button)
{
$this->buttons[] = $button;
}
function addSoundKeyboard($word, $soundFile)
{
if (!empty($soundFile)) {
$this->addKeyboardMarkup("📣 تلفظ کلمه $word", EnumSpeech::SPEECH_GET_SOUND . ":" . $word);
}
}
function addConjunctionkeyboard($word, $verbConjId)
{
if ($verbConjId > 0) {
$this->addKeyboardMarkup("🖐 صرف فعل $word", EnumConjuction::GET_CONJUCTION . ":" . $verbConjId);
}
}
function addKeyboardMarkup($text, $callbackData)
{
$this->keyboard['inline_keyboard'][] = [['text' => $text, 'callback_data' => $callbackData]];
return $this;
}
function getKeyboard()
{
return $this->keyboard;
}
function getKeyboardMarkedup()
{
return json_encode(
$this->getKeyboard(),
true
);
}
/**
* Set the value of buttonsData
*
* @return self
*/
public function setButtonsData($buttonsData)
{
$this->buttonsData = $buttonsData;
$this->initButtons();
}
public function findButton($key): Button
{
$buttons = &$this->getButtons();
foreach ($buttons as &$button) {
if ($button->getKey() == $key) {
return $button;
}
}
$button = new Button();
$button->setKey($key);
$button->setText("not found");
$button->setUsed(false);
return $button;
}
public function deleteButton($key)
{
$buttons = &$this->getButtons();
foreach ($buttons as $arrayKey => $button) {
if ($button->getKey() == $key) {
unset($this->buttons[$arrayKey]);
}
}
}
public function removeAllButtons()
{
$this->buttons = [];
$this->clearKeyboard();
}
public function rebuildKeyboard()
{
$buttons = $this->getButtons();
$this->clearKeyboard();
foreach ($buttons as $button) {
$this->addKeyboardMarkup($button->getText(), $button->getKey() . ':' . $button->getData());
}
}
}