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 ...@@ -23,7 +23,21 @@ class SendtoallCommand extends Command
protected $version = '1.2.0'; protected $version = '1.2.0';
protected $enabled = true; protected $enabled = true;
protected $public = 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() public function execute()
{ {
......
...@@ -23,6 +23,7 @@ abstract class Command ...@@ -23,6 +23,7 @@ abstract class Command
protected $description = 'Command help'; protected $description = 'Command help';
protected $usage = 'Command usage'; protected $usage = 'Command usage';
protected $version = '1.0.0'; protected $version = '1.0.0';
protected $need_mysql = false;
protected $enabled = true; protected $enabled = true;
protected $name = ''; protected $name = '';
...@@ -42,8 +43,25 @@ abstract class Command ...@@ -42,8 +43,25 @@ abstract class Command
return $this; 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(); 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() public function getUpdate()
{ {
return $this->update; return $this->update;
......
...@@ -86,7 +86,7 @@ class Telegram ...@@ -86,7 +86,7 @@ class Telegram
* *
* @var boolean * @var boolean
*/ */
protected $mysql_enabled; protected $mysql_enabled = false;
/** /**
* MySQL credentials * MySQL credentials
...@@ -345,13 +345,11 @@ class Telegram ...@@ -345,13 +345,11 @@ class Telegram
return $this->executeCommand('Newchattitle', $update); return $this->executeCommand('Newchattitle', $update);
break; break;
case 'delete_chat_photo': case 'delete_chat_photo':
// trigger delete_chat_photo // trigger delete_chat_photo
return $this->executeCommand('Deletechatphoto', $update); return $this->executeCommand('Deletechatphoto', $update);
break; break;
case 'group_chat_created': case 'group_chat_created':
// trigger group_chat_created // trigger group_chat_created
return $this->executeCommand('Groupchatcreated', $update); return $this->executeCommand('Groupchatcreated', $update);
...@@ -378,8 +376,9 @@ class Telegram ...@@ -378,8 +376,9 @@ class Telegram
return false; return false;
} }
//execute methods will be execute after preexecution
return $class->execute(); //this for prevent to execute db query witout connection
return $class->preExecute();
} }
/** /**
...@@ -507,16 +506,45 @@ class Telegram ...@@ -507,16 +506,45 @@ class Telegram
} }
/** /**
* Insert request in db * check id user require the db connection
* *
* @return bool * @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)) { if (empty($this->pdo)) {
return false; return false;
} else {
return true;
}
}
/**
* Insert request in db
*
* @return bool
*/
public function insertRequest(Update $update)
{
if (!$this->isDbConnected()) {
return false;
} }
$message = $update->getMessage(); $message = $update->getMessage();
...@@ -686,6 +714,9 @@ class Telegram ...@@ -686,6 +714,9 @@ class Telegram
++$a; ++$a;
} }
} }
$query .= ' ORDER BY '.TB_CHATS.'.`updated_at` ASC';
//echo $query."\n"; //echo $query."\n";
$sth = $this->pdo->prepare($query); $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