Code: Select all
<?php
namespace Opencart\Admin\Controller\Extension\CanaryExtension\Module;
class CanaryExtension extends \Opencart\System\Engine\Controller {
public function index(): void {
// Load the language file for this extension
$this->load->language('extension/canary_extension/module/canary_extension');
// Set the document title
$this->document->setTitle($this->language->get('heading_title'));
// Load the model for this extension
$this->load->model('setting/setting');
// Save the settings if the form is submitted
if ($this->request->server['REQUEST_METHOD'] == 'POST') {
$this->model_setting_setting->editSetting('module_canary_extension', $this->request->post);
$this->session->data['success'] = $this->language->get('text_success');
$this->response->redirect($this->url->link('extension/module', 'user_token=' . $this->session->data['user_token'], true));
}
// Set data for view
$data['action'] = $this->url->link('extension/canary_extension/module/canary_extension', 'user_token=' . $this->session->data['user_token'], true);
$data['cancel'] = $this->url->link('extension/module', 'user_token=' . $this->session->data['user_token'], true);
$data['user_token'] = $this->session->data['user_token'];
// Retrieve the status of the module
$data['module_canary_extension_status'] = $this->config->get('module_canary_extension_status');
// Load the view
$data['header'] = $this->load->controller('common/header');
$data['column_left'] = $this->load->controller('common/column_left');
$data['footer'] = $this->load->controller('common/footer');
$this->response->setOutput($this->load->view('extension/canary_extension/module/canary_extension', $data));
}
public function install(): void {
$this->load->model('setting/event');
$this->model_setting_event->addEvent([
'code' => 'canary_extension_order_add',
'description' => 'Canary Extension: Add Order',
'trigger' => 'catalog/model/checkout/order/addOrder/after',
'action' => 'extension/canary_extension/module/canary_extension/onOrderAdd',
'status' => 1,
'sort_order' => 1
]);
$this->model_setting_event->addEvent([
'code' => 'canary_extension_order_status_update',
'description' => 'Canary Extension: Update Order Status',
'trigger' => 'catalog/model/checkout/order/addHistory/after',
'action' => 'extension/canary_extension/module/canary_extension/onOrderStatusUpdate',
'status' => 1,
'sort_order' => 1
]);
}
public function uninstall(): void {
$this->load->model('setting/event');
$this->model_setting_event->deleteEventByCode('canary_extension_order_add');
$this->model_setting_event->deleteEventByCode('canary_extension_order_status_update');
}
public function onOrderAdd(): void {
$this->db->query("CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "add_order_new` (
`tf_courier_id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`status` int(2) NOT NULL,
PRIMARY KEY (`tf_courier_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci");
$data = [
'order' => 1,
'customer' => 1
];
file_put_contents('order_status_update_log.txt', print_r($data, true) . "\n", FILE_APPEND);
$this->sendDataToWebsite($data);
}
public function onOrderStatusUpdate(): void {
$this->db->query("CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "update_order_new` (
`tf_courier_id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`status` int(2) NOT NULL,
PRIMARY KEY (`tf_courier_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci");
$data = [
'order' => 1,
'customer' => 1,
'status' => 1
];
file_put_contents('order_status_update_log.txt', print_r($data, true) . "\n", FILE_APPEND);
$this->sendDataToWebsite($data);
}
private function sendDataToWebsite($data) {
$ch = curl_init('https://bot.taqnyat.sa/webhook_response/webhook.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
}
?>