|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* (c) shopware AG <[email protected]> |
|
4
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
5
|
|
|
* file that was distributed with this source code. |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace ShopwarePlugins\Connect\Subscribers; |
|
9
|
|
|
|
|
10
|
|
|
use Enlight\Event\SubscriberInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Implements a 'connect' filter for the article list |
|
14
|
|
|
* |
|
15
|
|
|
* Class ArticleList |
|
16
|
|
|
* @package ShopwarePlugins\Connect\Subscribers |
|
17
|
|
|
*/ |
|
18
|
|
|
class ArticleList implements SubscriberInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
private $pluginPath; |
|
24
|
|
|
/** |
|
25
|
|
|
* @var \Shopware_Components_Snippet_Manager |
|
26
|
|
|
*/ |
|
27
|
|
|
private $snippetManager; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param $pluginPath |
|
31
|
|
|
* @param \Shopware_Components_Snippet_Manager $snippetManager |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct($pluginPath, \Shopware_Components_Snippet_Manager $snippetManager) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->pluginPath = $pluginPath; |
|
36
|
|
|
$this->snippetManager = $snippetManager; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* {@inheritdoc} |
|
41
|
|
|
*/ |
|
42
|
|
|
public static function getSubscribedEvents() |
|
43
|
|
|
{ |
|
44
|
|
|
return [ |
|
45
|
|
|
'Shopware_Controllers_Backend_ArticleList_SQLParts' => 'onFilterArticle', |
|
46
|
|
|
'Enlight_Controller_Action_PostDispatch_Backend_ArticleList' => 'extendBackendArticleList' |
|
47
|
|
|
]; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* If the 'connect' filter is checked, only show products imported from connect |
|
52
|
|
|
* |
|
53
|
|
|
* @param \Enlight_Event_EventArgs $args |
|
54
|
|
|
*/ |
|
55
|
|
|
public function onFilterArticle(\Enlight_Event_EventArgs $args) |
|
56
|
|
|
{ |
|
57
|
|
|
$subject = $args->getSubject(); |
|
58
|
|
|
$filterBy = $subject->Request()->getParam('filterBy'); |
|
59
|
|
|
|
|
60
|
|
|
list($sqlParams, $filterSql, $categorySql, $imageSQL, $order) = $args->getReturn(); |
|
61
|
|
|
|
|
62
|
|
|
if ($filterBy === 'connect') { |
|
63
|
|
|
$imageSQL = ' |
|
64
|
|
|
LEFT JOIN s_plugin_connect_items as connect_items |
|
65
|
|
|
ON connect_items.article_id = articles.id |
|
66
|
|
|
'; |
|
67
|
|
|
|
|
68
|
|
|
$filterSql .= ' AND connect_items.shop_id > 0 '; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return [$sqlParams, $filterSql, $categorySql, $imageSQL, $order]; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Extends the product list in the backend in order to have a special hint for connect products |
|
76
|
|
|
* |
|
77
|
|
|
* @event Enlight_Controller_Action_PostDispatch_Backend_ArticleList |
|
78
|
|
|
* @param \Enlight_Event_EventArgs $args |
|
79
|
|
|
*/ |
|
80
|
|
|
public function extendBackendArticleList(\Enlight_Event_EventArgs $args) |
|
81
|
|
|
{ |
|
82
|
|
|
/** @var $subject \Enlight_Controller_Action */ |
|
83
|
|
|
$subject = $args->getSubject(); |
|
84
|
|
|
$request = $subject->Request(); |
|
85
|
|
|
|
|
86
|
|
|
switch ($request->getActionName()) { |
|
87
|
|
View Code Duplication |
case 'load': |
|
|
|
|
|
|
88
|
|
|
$subject->View()->addTemplateDir($this->pluginPath . '/Views', 'connect'); |
|
89
|
|
|
$this->snippetManager->addConfigDir($this->pluginPath . '/Snippets'); |
|
90
|
|
|
$subject->View()->extendsTemplate( |
|
91
|
|
|
'backend/article_list/connect.js' |
|
92
|
|
|
); |
|
93
|
|
|
break; |
|
94
|
|
|
case 'list': |
|
95
|
|
|
case 'filter': |
|
96
|
|
|
$subject->View()->data = $this->markConnectProducts( |
|
97
|
|
|
$subject->View()->data |
|
98
|
|
|
); |
|
99
|
|
|
break; |
|
100
|
|
|
case 'columnConfig': |
|
101
|
|
|
$subject->View()->data = $this->addConnectColumn( |
|
102
|
|
|
$subject->View()->data |
|
103
|
|
|
); |
|
104
|
|
|
break; |
|
105
|
|
|
default: |
|
106
|
|
|
break; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Helper method which adds an additional 'connect' field to article objects in order to indicate |
|
112
|
|
|
* if they are connect articles or not |
|
113
|
|
|
* |
|
114
|
|
|
* @param $data |
|
115
|
|
|
* @return mixed |
|
116
|
|
|
*/ |
|
117
|
|
|
private function markConnectProducts($data) |
|
118
|
|
|
{ |
|
119
|
|
|
$articleIds = array_map(function ($row) { |
|
120
|
|
|
if ((int) $row['Article_id'] > 0) { |
|
121
|
|
|
return (int) $row['Article_id']; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return (int) $row['articleId']; |
|
125
|
|
|
}, $data); |
|
126
|
|
|
|
|
127
|
|
|
if (empty($articleIds)) { |
|
128
|
|
|
return $data; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
$sql = 'SELECT article_id |
|
132
|
|
|
FROM s_plugin_connect_items |
|
133
|
|
|
WHERE article_id IN (' . implode(', ', $articleIds) . ') |
|
134
|
|
|
AND source_id IS NOT NULL |
|
135
|
|
|
AND shop_id IS NOT NULL'; |
|
136
|
|
|
|
|
137
|
|
|
$connectArticleIds = array_map(function ($row) { |
|
138
|
|
|
return $row['article_id']; |
|
139
|
|
|
}, Shopware()->Db()->fetchAll($sql)); |
|
140
|
|
|
|
|
141
|
|
|
foreach ($data as $idx => $row) { |
|
142
|
|
|
if ((int) $row['Article_id'] > 0) { |
|
143
|
|
|
$articleId = $row['Article_id']; |
|
144
|
|
|
} else { |
|
145
|
|
|
$articleId = $row['articleId']; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
$data[$idx]['connect'] = in_array($articleId, $connectArticleIds); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
return $data; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Adds connect field to the ExtJS model for article_list |
|
156
|
|
|
* That was changed in SW5, because the model is dynamically created |
|
157
|
|
|
* |
|
158
|
|
|
* @param $data |
|
159
|
|
|
* @return array |
|
160
|
|
|
*/ |
|
161
|
|
|
private function addConnectColumn($data) |
|
162
|
|
|
{ |
|
163
|
|
|
$data[] = [ |
|
164
|
|
|
'entity' => 'Article', |
|
165
|
|
|
'field' => 'connect', |
|
166
|
|
|
'type' => 'boolean', |
|
167
|
|
|
'alias' => 'connect', |
|
168
|
|
|
'allowInGrid' => true, |
|
169
|
|
|
'nullable' => false, |
|
170
|
|
|
|
|
171
|
|
|
]; |
|
172
|
|
|
|
|
173
|
|
|
return $data; |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.