Commit 4d43241f authored by MBoretto's avatar MBoretto

check mysql connection before execute a command that need it

parent a4f2c71f
......@@ -23,7 +23,21 @@ class SendtoallCommand extends Command
protected $version = '1.2.0';
protected $enabled = true;
protected $public = true;
//need Mysql
protected $need_mysql = true;
public function executeFail()
{
//Database not setted or without connection
//Preparing message
$message = $this->getMessage();
$chat_id = $message->getChat()->getId();
$data = array();
$data['chat_id'] = $chat_id;
$data['text'] = 'Sorry no database connection, unable to execute '.$this->name.' command.';
return Request::sendMessage($data);
}
public function execute()
{
......
......@@ -23,6 +23,7 @@ abstract class Command
protected $description = 'Command help';
protected $usage = 'Command usage';
protected $version = '1.0.0';
protected $need_mysql = false;
protected $enabled = true;
protected $name = '';
......@@ -42,8 +43,25 @@ abstract class Command
return $this;
}
public function preExecute()
{
if (!$this->need_mysql |
$this->need_mysql & $this->telegram->isDbEnabled() & $this->telegram->isDbConnected()
) {
return $this->execute();
}
return $this->executeFail();
}
abstract public function execute();
//this methods is executed if $need_mysql is true but DB connection for some reason is not avaiable
public function executeFail(){
}
public function getUpdate()
{
return $this->update;
......
......@@ -86,7 +86,7 @@ class Telegram
*
* @var boolean
*/
protected $mysql_enabled;
protected $mysql_enabled = false;
/**
* MySQL credentials
......@@ -345,13 +345,11 @@ class Telegram
return $this->executeCommand('Newchattitle', $update);
break;
case 'delete_chat_photo':
// trigger delete_chat_photo
return $this->executeCommand('Deletechatphoto', $update);
break;
case 'group_chat_created':
// trigger group_chat_created
return $this->executeCommand('Groupchatcreated', $update);
......@@ -378,8 +376,9 @@ class Telegram
return false;
}
return $class->execute();
//execute methods will be execute after preexecution
//this for prevent to execute db query witout connection
return $class->preExecute();
}
/**
......@@ -507,16 +506,45 @@ class Telegram
}
/**
* Insert request in db
* check id user require the db connection
*
* @return bool
*/
// protected function insertRequest(Update $update)
public function insertRequest(Update $update)
public function isDbEnabled()
{
if ($this->mysql_enabled) {
return true;
} else {
return false;
}
}
/**
* check if database Connection has been created
*
* @return bool
*/
public function isDbConnected()
{
if (empty($this->pdo)) {
return false;
} else {
return true;
}
}
/**
* Insert request in db
*
* @return bool
*/
public function insertRequest(Update $update)
{
if (!$this->isDbConnected()) {
return false;
}
$message = $update->getMessage();
......@@ -686,6 +714,9 @@ class Telegram
++$a;
}
}
$query .= ' ORDER BY '.TB_CHATS.'.`updated_at` ASC';
//echo $query."\n";
$sth = $this->pdo->prepare($query);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment