|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ABM\Wasabi; |
|
4
|
|
|
|
|
5
|
|
|
use Ratchet\MessageComponentInterface; |
|
6
|
|
|
use Ratchet\ConnectionInterface; |
|
7
|
|
|
use Analog\Analog; |
|
8
|
|
|
|
|
9
|
|
|
class Product implements MessageComponentInterface |
|
10
|
|
|
{ |
|
11
|
|
|
protected $clients; |
|
12
|
|
|
protected $dbConn; |
|
13
|
|
|
|
|
14
|
|
|
public function __construct() |
|
15
|
|
|
{ |
|
16
|
|
|
$this->clients = new \SplObjectStorage(); |
|
17
|
|
|
// Now connect to PS DB using Simplon on Composer |
|
18
|
|
|
$this->dbConn = new \Simplon\Mysql\Mysql( |
|
19
|
|
|
'127.0.0.1', |
|
20
|
|
|
_DB_USER_, |
|
21
|
|
|
_DB_PASSWD_, |
|
22
|
|
|
_DB_NAME_ |
|
23
|
|
|
); |
|
24
|
|
|
$log_file = 'ws-prod.log'; |
|
25
|
|
|
Analog::handler(\Analog\Handler\File::init($log_file)); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function onOpen(ConnectionInterface $conn) |
|
29
|
|
|
{ |
|
30
|
|
|
// Store the new connection to send messages to later |
|
31
|
|
|
$this->clients->attach($conn); |
|
32
|
|
|
|
|
33
|
|
|
Analog::log("New connection: $conn->resourceId"); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function onMessage(ConnectionInterface $from, $msg) |
|
37
|
|
|
{ |
|
38
|
|
|
foreach ($this->clients as $client) { |
|
39
|
|
|
if ($from == $client) { |
|
40
|
|
|
$category = (int) substr($msg, 0, strpos($msg, ',')); |
|
41
|
|
|
if ($category != 0) { |
|
42
|
|
|
$products = $this->getProducts($category); |
|
43
|
|
|
} else { |
|
44
|
|
|
$product = substr($msg, strpos($msg, ',') + 1); |
|
45
|
|
|
$products = $this->getProducts($product); |
|
46
|
|
|
} |
|
47
|
|
|
Analog::log("Product variables: $msg"); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
// Basic test - fire the correct product back via Websocket |
|
52
|
|
|
$client->send(json_encode($products)); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
private function getProducts($category) |
|
56
|
|
|
{ |
|
57
|
|
|
$product_ids = $this->getProductIDs($category); |
|
58
|
|
|
$products = $this->getProduct($product_ids); |
|
59
|
|
|
|
|
60
|
|
|
return $products; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
private function getProductIDs($category) |
|
64
|
|
|
{ |
|
65
|
|
|
$sql = 'SELECT DISTINCT p.id_product |
|
66
|
|
|
from '._DB_PREFIX_.'product as p |
|
67
|
|
|
LEFT JOIN '._DB_PREFIX_.'image AS i ON i.id_product = p.id_product |
|
68
|
|
|
LEFT JOIN '._DB_PREFIX_.'product_lang as pl ON pl.id_product = p.id_product |
|
69
|
|
|
WHERE p.active = 1 |
|
70
|
|
|
AND p.id_category_default = '.(int) $category.' |
|
71
|
|
|
GROUP BY p.id_product'; |
|
72
|
|
|
$pcats = $this->dbConn->fetchRowMany($sql); |
|
73
|
|
|
$ids = ''; |
|
74
|
|
|
if (is_array($pcats) && !empty($pcats) { |
|
|
|
|
|
|
75
|
|
|
foreach ($pcats as $row) { |
|
76
|
|
|
$ids .= $row['id_product'].','; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$ids = rtrim($ids, ','); |
|
81
|
|
|
|
|
82
|
|
|
return $ids; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param string $ids |
|
87
|
|
|
*/ |
|
88
|
|
|
private function getProduct($ids) |
|
89
|
|
|
{ |
|
90
|
|
|
$sql = 'SELECT p.id_product, p.id_supplier, p.ean13, p.upc, p.price, p.wholesale_price, p.on_sale, p.quantity, p.id_category_default, p.show_price, p.available_for_order, p.minimal_quantity, p.customizable, |
|
91
|
|
|
p.out_of_stock, pl.link_rewrite, pl.name, i.id_image, il.legend, |
|
92
|
|
|
cl.name AS category_default, cl.id_category AS cat_id, |
|
93
|
|
|
ps.price AS orderprice |
|
94
|
|
|
FROM '._DB_PREFIX_.'product as p |
|
95
|
|
|
LEFT JOIN '._DB_PREFIX_.'image AS i ON i.id_product = p.id_product |
|
96
|
|
|
LEFT JOIN '._DB_PREFIX_.'product_shop as ps ON ps.id_product = p.id_product |
|
97
|
|
|
LEFT JOIN '._DB_PREFIX_.'product_lang as pl ON pl.id_product = p.id_product |
|
98
|
|
|
LEFT JOIN '._DB_PREFIX_.'image_lang as il ON i.id_image = il.id_image |
|
99
|
|
|
LEFT JOIN '._DB_PREFIX_.'category_lang cl ON p.id_category_default = cl.id_category |
|
100
|
|
|
WHERE p.id_product IN ('.$ids.') |
|
101
|
|
|
AND i.cover = 1 |
|
102
|
|
|
AND p.active = 1 |
|
103
|
|
|
GROUP BY p.id_product |
|
104
|
|
|
ORDER BY p.price ASC'; |
|
105
|
|
|
|
|
106
|
|
|
$result = $this->dbConn->fetchRowMany($sql); |
|
107
|
|
|
|
|
108
|
|
|
return $result; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function onClose(ConnectionInterface $conn) |
|
112
|
|
|
{ |
|
113
|
|
|
// The connection is closed, remove it, as we can no longer send it messages |
|
114
|
|
|
$this->clients->detach($conn); |
|
115
|
|
|
Analog::log('Connection '.$conn->resourceId.' has disconnected'); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function onError(ConnectionInterface $conn, \Exception $e) |
|
119
|
|
|
{ |
|
120
|
|
|
echo "An error has occurred: {$e->getMessage()}\n"; |
|
121
|
|
|
Analog::log('Error: '.$e->getMessage().''); |
|
122
|
|
|
$conn->close(); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|