-
Notifications
You must be signed in to change notification settings - Fork 0
/
LuigisboxSearchSuiteShopware5.php
160 lines (122 loc) · 4.34 KB
/
LuigisboxSearchSuiteShopware5.php
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
namespace LuigisboxSearchSuiteShopware5;
use Shopware\Components\Plugin;
use LuigisboxSearchSuiteShopware5\Models\Helper as H;
class LuigisboxSearchSuiteShopware5 extends Plugin
{
const SCRIPT_URL_KEY = 'luigisBoxScriptUrl';
const SYNC_ENABLED_KEY = 'luigisBoxCatalogSynchronization';
const API_KEY = 'luigisBoxApiKey';
const TRACKER_ID_KEY = 'luigisBoxTrackerID';
const LOGFILE = 'update.txt';
private $indexer;
public function install(Plugin\Context\InstallContext $context)
{
parent::install($context);
H::setIndexInvalidationTimestamp($this->getLogFilePath());
}
public static function getSubscribedEvents()
{
return [
'Enlight_Controller_Action_PreDispatch' => 'addScript',
'Shopware_CronJob_UpdateLuigisBoxApi' => 'updateLastModifiedProducts',
'Shopware_CronJob_SendToLuigisBoxApi' => 'sendProductsToLB',
];
}
public function isConfigured()
{
if (!$this->isEnabled()) {
return false;
}
$trackerId = $this->getTrackerId();
if (empty($trackerId)) {
return false;
}
$apiKey = $this->getApiKey();
if (empty($apiKey)) {
return false;
}
return true;
}
public function isEnabled()
{
return Shopware()->Config()->getByNamespace('LuigisboxSearchSuiteShopware5', self::SYNC_ENABLED_KEY);
}
public function getApiKey()
{
return Shopware()->Config()->getByNamespace('LuigisboxSearchSuiteShopware5', self::API_KEY);
}
public function getTrackerId()
{
return Shopware()->Config()->getByNamespace('LuigisboxSearchSuiteShopware5', self::TRACKER_ID_KEY);
}
public function getScriptUrl()
{
return Shopware()->Config()->getByNamespace('LuigisboxSearchSuiteShopware5', self::SCRIPT_URL_KEY);
}
public function getLogFilePath()
{
return $this->getPath() . '/' . self::LOGFILE;
}
public function addScript(\Enlight_Controller_ActionEventArgs $args)
{
$controller = $args->get('subject');
$view = $controller->View();
$view->addTemplateDir($this->getPath() . '/Resources/views');
$scriptURL = $this->getScriptUrl();
$view->assign('luigisBoxScriptUrl', $scriptURL);
}
public function singleProductUpdate(\Enlight_Event_EventArgs $args)
{
$request = $args->getRequest();
if ($request->getActionName() !== 'save') {
return;
}
$articleId = $request->getParam('id', null);
if (!is_numeric($articleId) || !$this->isConfigured()) {
return;
}
try {
$filePath = $this->getLogFilePath();
$this->indexer = $this->container->get('luigisbox_search_suite.indexer');
$this->indexer->setFilePath($filePath);
$this->indexer->singleContentUpdate($articleId);
} catch (\Exception $exception) {
return;
}
}
public function updateLastModifiedProducts(\Shopware_Components_Cron_CronJob $job)
{
$filePath = $this->getLogFilePath();
$fromDate = $job->get('job')->end; // last execution
if (!$this->isConfigured() || H::isIndexRunning($filePath)) {
$this->writeLog('Prevent updating.');
return;
}
try {
$filePath = $this->getLogFilePath();
$this->indexer = $this->container->get('luigisbox_search_suite.indexer');
$this->indexer->setFilePath($filePath);
$this->indexer->updateLastChanged($fromDate);
} catch (\Exception $exception) {
$this->writeLog($exception->getMessage());
}
}
public function sendProductsToLB(\Shopware_Components_Cron_CronJob $job)
{
$filePath = $this->getLogFilePath();
if (!$this->isConfigured() || H::isIndexRunning($filePath)) {
$this->writeLog('Prevent indexing.');
return;
}
H::markIndexRunning($filePath);
$this->indexer = $this->container->get('luigisbox_search_suite.indexer');
$this->indexer->setFilePath($filePath);
$this->indexer->allContentUpdate();
H::markIndexFinished($filePath);
}
private function writeLog($msg)
{
Shopware()->Container()->get('pluginlogger')->info($msg);
}
}