-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEvent.php
More file actions
99 lines (86 loc) · 3.08 KB
/
Event.php
File metadata and controls
99 lines (86 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
namespace Plugin\LowStockAlert;
use Eccube\Event\RenderEvent;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\CssSelector\CssSelector;
use Symfony\Component\DomCrawler\Crawler;
/**
* Event of plugin Low Stock Alert
* @author Dung Le
* @param FilterResponseEvent $event
*/
class Event
{
private $app;
public function __construct($app)
{
$this->app = $app;
}
/**
* Front-end: product detail render event
* @param FilterResponseEvent $event
*/
public function onRenderProductsDetailBefore(FilterResponseEvent $event)
{
if ($event->getRequest()->getMethod() === 'GET') {
$app = $this->app;
// product
$id = $app['request']->attributes->get('id');
$Product = $app['eccube.repository.product']->find($id);
// low stock number
$LowStockAlert = $app['eccube.plugin.low_stock_alert.repository.low_stock_alert']->findOneBy(array());
if (is_null($LowStockAlert)) {
$LowStockAlert = new \Plugin\LowStockAlert\Entity\LowStockAlert();
}
$low_stock_number = $LowStockAlert->getId();
$lowStocks = array();
// check stock for each product class
foreach ($Product->getProductClasses() as $key => $ProductClass) {
$classId = $ProductClass->getId();
// array low stock output
$lowStocks[$classId] = false;
if ($ProductClass->getStockUnlimited() === 0) {
if ($ProductClass->getStock() < $low_stock_number) {
$lowStocks[$classId] = true;
}
}
}
// template
$twig = $app->renderView(
'LowStockAlert/Resource/template/default/low_stock_alert.twig',
array(
'LowStockAlert' => $LowStockAlert,
'low_stocks' => $lowStocks,
'Product' => $Product,
)
);
// show view
$response = $event->getResponse();
$html = $response->getContent();
$crawler = new Crawler($html);
$oldElement = $crawler->filter('#item_detail_area .extra-form');
$oldHtml = $oldElement->html();
$oldHtml = html_entity_decode($oldHtml, ENT_NOQUOTES, 'UTF-8');
$newHtml = $oldHtml.$twig;
$html = $this->getHtml($crawler);
$html = str_replace($oldHtml, $newHtml, $html);
$response->setContent($html);
$event->setResponse($response);
}
}
/**
* html decode
*
* @param Crawler $crawler
* @return string
*/
private function getHtml(Crawler $crawler)
{
$html = '';
foreach ($crawler as $domElement) {
$domElement->ownerDocument->formatOutput = true;
$html .= $domElement->ownerDocument->saveHTML();
}
return html_entity_decode($html, ENT_NOQUOTES, 'UTF-8');
}
}