forked from mnaczenski/MNAddEmotionAttributes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MNAddEmotionAttributes.php
137 lines (114 loc) · 4.23 KB
/
MNAddEmotionAttributes.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
<?php
namespace MNAddEmotionAttributes;
use Shopware\Components\Plugin\Context\ActivateContext;
use Shopware\Components\Plugin\Context\DeactivateContext;
use Shopware\Components\Plugin\Context\InstallContext;
use Shopware\Components\Plugin\Context\UninstallContext;
class MNAddEmotionAttributes extends \Shopware\Components\Plugin
{
public function activate(ActivateContext $context)
{
$context->scheduleClearCache(ActivateContext::CACHE_LIST_DEFAULT);
}
public function deactivate(DeactivateContext $context)
{
$context->scheduleClearCache(DeactivateContext::CACHE_LIST_DEFAULT);
}
public static function getSubscribedEvents()
{
return [
'Enlight_Controller_Action_PostDispatchSecure_Frontend_Listing' => 'onFrontendListing',
'Enlight_Controller_Action_PostDispatchSecure_Frontend_Index' => 'onFrontendListing',
'Enlight_Controller_Action_PostDispatchSecure_Frontend_Campaign' => 'onFrontendCampaign',
'Enlight_Controller_Action_PostDispatchSecure_Frontend' => 'onFrontend'
];
}
/**
* @param InstallContext $context
* @throws \Exception
*/
public function install(InstallContext $context)
{
$service = $this->container->get('shopware_attribute.crud_service');
$service->update('s_emotion_attributes', 'mnposition', 'combobox', [
'label' => 'Position der Einkaufswelt',
'displayInBackend' => true,
'arrayStore' => [
['key' => '1', 'value' => 'Vor Listing'],
['key' => '2', 'value' => 'Nach Listing']
],
]);
$service->update('s_emotion_attributes', 'mncssclasses', 'string', [
'label' => 'Eigene CSS Klassen',
'supportText' => 'Mehrere CSS Klassen mit Leerzeichen trennen',
'displayInBackend' => true,
]);
}
/**
* @param InstallContext $context
* @throws \Exception
*/
public function update(InstallContext $context)
{
return $this->install($context);
}
/**
* @param UninstallContext $context
* @throws \Exception
*/
public function uninstall(UninstallContext $context)
{
if ($context->keepUserData()) {
return;
}
$service = $this->container->get('shopware_attribute.crud_service');
$service->delete('s_emotion_attributes', 'mnposition');
$service->delete('s_emotion_attributes', 'mncssclasses');
}
/**
* @param \Enlight_Event_EventArgs $args
* @throws \Exception
*/
public function onFrontendListing(\Enlight_Event_EventArgs $args)
{
/** @var \Enlight_Controller_Action $controller */
$controller = $args->get('subject');
$view = $controller->View();
$emotions = $view->getAssign('emotions');
$this->updateEmotions($emotions);
$view->assign('emotions', $emotions);
}
/**
* @param \Enlight_Event_EventArgs $args
* @throws \Exception
*/
public function onFrontendCampaign(\Enlight_Event_EventArgs $args)
{
/** @var \Enlight_Controller_Action $controller */
$controller = $args->get('subject');
$view = $controller->View();
$landingPage = $view->getAssign('landingPage');
$this->updateEmotions($landingPage['emotions']);
$view->assign('landingPage', $landingPage);
}
public function onFrontend(\Enlight_Event_EventArgs $args)
{
if ( Shopware()->Config()->getByNamespace('MNAddEmotionAttributes', 'useEmotionAttribute') == 'Yes')
{
/** @var \Enlight_Controller_Action $controller */
$controller = $args->get('subject');
$view = $controller->View();
$this->container->get('Template')->addTemplateDir(
$this->getPath() . '/Resources/views/'
);
}
}
private function updateEmotions(&$emotions) {
$service = $this->container->get('shopware_attribute.data_loader');
foreach ($emotions as $key => $emotion)
{
$attributes['attributes'] = $service->load('s_emotion_attributes', $emotion['id']);
$emotions[$key] = array_merge($emotion, $attributes);
}
}
}