· 6 years ago · Jul 22, 2019, 09:58 AM
1<?php defined('_PS_VERSION_') or exit;
2
3
4class Inpli_Ip extends Module {
5
6 public function __construct() {
7 $this->name = 'inpli_ip';
8 $this->tab = 'other';
9 $this->version = '1.0.0';
10 $this->author = 'tobieniepodam@gmail.com';
11 $this->need_instance = 0;
12
13 parent::__construct();
14
15 $this->displayName = 'Inpli IP';
16 $this->description = 'IP customer store.';
17 //$this->confirmUninstall = 'Are You sure you want to uninstall?';
18 //$this->ps_versions_compliancy = ['min' => '1.7.1.0', 'max' => _PS_VERSION_);
19 }
20
21 public function install() {
22 if (!parent::install()
23 || !$this->registerHook('actionOrderStatusUpdate')
24 || !$this->install_db()
25 ) {
26 return false;
27 }
28
29 return true;
30 }
31
32 private function install_db() {
33 $sql = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'inpli_ip` ('
34 . ' `id_order` INT(10) NOT NULL PRIMARY KEY,'
35 . ' `ip` VARCHAR(15) NOT NULL)'
36 . ' ENGINE = InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci;';
37 return Db::getInstance()->execute($sql);
38 }
39
40 public function uninstall() {
41 if (!parent::uninstall()
42 || !$this->uninstall_db()
43 ) {
44 return false;
45 }
46
47 return true;
48 }
49
50 private function uninstall_db() {
51 $sql = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'inpli_ip`;';
52 return Db::getInstance()->execute($sql);
53 }
54
55 public function hookActionOrderStatusUpdate($data) {
56 if(Tools::getValue('controller') != 'validation') {
57 return;
58 }
59 Db::getInstance()->insert('inpli_ip', [
60 'id_order' => (int)$data['newOrderStatus']->id,
61 'ip' => $_SERVER['REMOTE_ADDR'],
62 ]);
63 }
64
65 public function getContent() {
66 $output = '<h1>List</h1>';
67
68 $t1 = _DB_PREFIX_ . 'inpli_ip';
69 $t2 = _DB_PREFIX_ . 'orders';
70 $sql = 'SELECT `' . $t1 . '`.`id_order`, `' . $t1 . '`.`ip`, `' . $t2 . '`.`reference` FROM `' . $t1 . '`'
71 . ' INNER JOIN `' . $t2 . '` ON `' . $t2 . '`.`id_order` = `' . $t1 . '`.`id_order`';
72
73 if($list = Db::getInstance()->ExecuteS($sql)) {
74 $output .=
75 '<table class="table">'
76 . '<tr><th>id order</th><th>reference</th><th>ip</th></tr>';
77 foreach ($list as $row) {
78 $output .= '<tr><td>' . $row['id_order'] . '</td><td>' . $row['reference'] . '</td><td>' . $row['ip'] . '</td></tr>';
79 }
80
81 $output .= '</table>';
82 }
83 else {
84 $output .= 'Empty..';
85 }
86
87 return $output;
88 }
89}