forked from things4it/JTLShop-Plugin-CategoryImageGeneration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBootstrap.php
223 lines (186 loc) · 9.04 KB
/
Bootstrap.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php declare(strict_types=1);
/**
* @package Plugin\things4it_category_image_generation
* @author Johannes Wendig
*/
namespace Plugin\t4it_category_image_generation;
use JTL\Alert\Alert;
use JTL\Events\Dispatcher;
use JTL\Events\Event;
use JTL\Plugin\Bootstrapper;
use JTL\Shop;
use JTL\Smarty\JTLSmarty;
use Plugin\t4it_category_image_generation\adminmenu\RegenerateCategoryImageTab;
use Plugin\t4it_category_image_generation\src\Constants;
use Plugin\t4it_category_image_generation\src\cron\CategoryImageGenerationCronJob;
use Plugin\t4it_category_image_generation\src\db\dao\CategoryHelperDao;
use Plugin\t4it_category_image_generation\src\db\dao\SettingsDao;
use Plugin\t4it_category_image_generation\src\service\CategoryImageGenerationService;
use Plugin\t4it_category_image_generation\src\service\CategoryImageGenerationServiceInterface;
use Plugin\t4it_category_image_generation\src\service\placementStrategy\offset\ratio1to1\OffsetRatio1to1OneProductImagePlacementStrategy;
use Plugin\t4it_category_image_generation\src\service\placementStrategy\offset\ratio1to1\OffsetRatio1to1ThreeProductImagesPlacementStrategy;
use Plugin\t4it_category_image_generation\src\service\placementStrategy\offset\ratio1to1\OffsetRatio1to1TwoProductImagesPlacementStrategy;
use Plugin\t4it_category_image_generation\src\service\placementStrategy\offset\ratio4to3\OffsetRatio4to3OneProductImagePlacementStrategy;
use Plugin\t4it_category_image_generation\src\service\placementStrategy\offset\ratio4to3\OffsetRatio4to3ThreeProductImagesPlacementStrategy;
use Plugin\t4it_category_image_generation\src\service\placementStrategy\offset\ratio4to3\OffsetRatio4to3TwoProductImagesPlacementStrategy;
use Plugin\t4it_category_image_generation\src\service\placementStrategy\row\flat\RowFlatOneProductImagePlacementStrategy;
use Plugin\t4it_category_image_generation\src\service\placementStrategy\row\flat\RowFlatThreeProductImagesPlacementStrategy;
use Plugin\t4it_category_image_generation\src\service\placementStrategy\row\flat\RowFlatTwoProductImagesPlacementStrategy;
use Plugin\t4it_category_image_generation\src\service\placementStrategy\rowCropped\flat\RowCroppedFlatOneProductImagePlacementStrategy;
use Plugin\t4it_category_image_generation\src\service\placementStrategy\rowCropped\flat\RowCroppedFlatThreeProductImagesPlacementStrategy;
use Plugin\t4it_category_image_generation\src\service\placementStrategy\rowCropped\flat\RowCroppedFlatTwoProductImagesPlacementStrategy;
use Plugin\t4it_category_image_generation\src\utils\CategoryImageGenerator;
/**
* Class Bootstrap
* @package Plugin\things4it_category_image_generation
*/
class Bootstrap extends Bootstrapper
{
/**
* @inheritdoc
*/
public function boot(Dispatcher $dispatcher): void
{
parent::boot($dispatcher);
$this->setupContainer();
$this->addListeners($dispatcher);
}
/**
* @inheritdoc
*/
public function installed()
{
parent::installed();
$this->addCron();
}
/**
* @inheritdoc
*/
public function disabled()
{
parent::disabled();
CategoryHelperDao::removeGeneratedImages($this->getDB());
CategoryImageGenerator::removeGeneratedImages();
}
/**
* @inheritdoc
*/
public function uninstalled(bool $deleteData = true)
{
parent::uninstalled($deleteData);
$this->removeCron();
if ($deleteData) {
CategoryHelperDao::removeGeneratedImages($this->getDB());
CategoryImageGenerator::removeGeneratedImages();
}
}
/**
* @inheritdoc
*/
public function renderAdminMenuTab(string $tabName, int $menuID, JTLSmarty $smarty): string
{
$plugin = $this->getPlugin();
if ($tabName === 'Bild neu generieren (einzeln)') {
return RegenerateCategoryImageTab::handleRequest($plugin, $this->getDB(), $smarty);
}
return parent::renderAdminMenuTab($tabName, $menuID, $smarty);
}
private function setupContainer(): void
{
$container = Shop::Container();
$container->setFactory(CategoryImageGenerationServiceInterface::class, function ($container) {
return new CategoryImageGenerationService($this->getDB(), $this->getPlugin());
});
$this->provideImagePlacementStrategiesOffsetRatio1to1($container);
$this->provideImagePlacementStrategiesOffsetRatio4to3($container);
$this->provideImagePlacementStrategiesRowFlat($container);
$this->provideImagePlacementStrategiesRowCroppedFlat($container);
}
/**
* @param Dispatcher $dispatcher
*/
private function addListeners(Dispatcher $dispatcher): void
{
$dispatcher->listen(Event::MAP_CRONJOB_TYPE, static function (array $args) {
if ($args['type'] === Constants::CRON_JOB_CATEGORY_IMAGE_GENERATION) {
$args['mapping'] = CategoryImageGenerationCronJob::class;
}
});
$dispatcher->listen(Event::GET_AVAILABLE_CRONJOBS, static function (array $args) {
$jobs = &$args['jobs'];
if (is_array($jobs)) {
array_push($jobs, Constants::CRON_JOB_CATEGORY_IMAGE_GENERATION);
}
});
$dispatcher->listen('shop.hook.' . \HOOK_PLUGIN_SAVE_OPTIONS, function (array $args) {
$hasError = $args['hasError'];
$savedPlugin = $args['plugin'];
if ($savedPlugin->getID() == $this->getPlugin()->getID() && $hasError === false) {
Shop::Container()->getAlertService()->addAlert(Alert::TYPE_SUCCESS, __('admin.settings.post-saved.success'), 'infoSettingsChanged');
SettingsDao::updateChangedFlag(true, $this->getDB());
}
});
}
private function addCron(): void
{
$job = new \stdClass();
$job->name = 'Kategorie-Bild generierung';
$job->jobType = Constants::CRON_JOB_CATEGORY_IMAGE_GENERATION;
$job->frequency = 24;
$job->startDate = 'NOW()';
$job->startTime = '00:00:00';
$this->getDB()->insert('tcron', $job);
}
private function removeCron(): void
{
$this->getDB()->delete('tcron', 'jobType', Constants::CRON_JOB_CATEGORY_IMAGE_GENERATION);
}
private function provideImagePlacementStrategiesOffsetRatio1to1(\JTL\Services\DefaultServicesInterface $container): void
{
$container->setFactory(OffsetRatio1to1OneProductImagePlacementStrategy::getCode(), function ($container) {
return new OffsetRatio1to1OneProductImagePlacementStrategy();
});
$container->setFactory(OffsetRatio1to1TwoProductImagesPlacementStrategy::getCode(), function ($container) {
return new OffsetRatio1to1TwoProductImagesPlacementStrategy();
});
$container->setFactory(OffsetRatio1to1ThreeProductImagesPlacementStrategy::getCode(), function ($container) {
return new OffsetRatio1to1ThreeProductImagesPlacementStrategy();
});
}
private function provideImagePlacementStrategiesOffsetRatio4to3(\JTL\Services\DefaultServicesInterface $container): void
{
$container->setFactory(OffsetRatio4to3OneProductImagePlacementStrategy::getCode(), function ($container) {
return new OffsetRatio4to3OneProductImagePlacementStrategy();
});
$container->setFactory(OffsetRatio4to3TwoProductImagesPlacementStrategy::getCode(), function ($container) {
return new OffsetRatio4to3TwoProductImagesPlacementStrategy();
});
$container->setFactory(OffsetRatio4to3ThreeProductImagesPlacementStrategy::getCode(), function ($container) {
return new OffsetRatio4to3ThreeProductImagesPlacementStrategy();
});
}
private function provideImagePlacementStrategiesRowFlat(\JTL\Services\DefaultServicesInterface $container): void
{
$container->setFactory(RowFlatOneProductImagePlacementStrategy::getCode(), function ($container) {
return new RowFlatOneProductImagePlacementStrategy();
});
$container->setFactory(RowFlatTwoProductImagesPlacementStrategy::getCode(), function ($container) {
return new RowFlatTwoProductImagesPlacementStrategy();
});
$container->setFactory(RowFlatThreeProductImagesPlacementStrategy::getCode(), function ($container) {
return new RowFlatThreeProductImagesPlacementStrategy();
});
}
private function provideImagePlacementStrategiesRowCroppedFlat(\JTL\Services\DefaultServicesInterface $container): void
{
$container->setFactory(RowCroppedFlatOneProductImagePlacementStrategy::getCode(), function ($container) {
return new RowCroppedFlatOneProductImagePlacementStrategy();
});
$container->setFactory(RowCroppedFlatTwoProductImagesPlacementStrategy::getCode(), function ($container) {
return new RowCroppedFlatTwoProductImagesPlacementStrategy();
});
$container->setFactory(RowCroppedFlatThreeProductImagesPlacementStrategy::getCode(), function ($container) {
return new RowCroppedFlatThreeProductImagesPlacementStrategy();
});
}
}