Completed
Pull Request — master (#392)
by Christian
03:03
created

ArticleList::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
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
use Enlight_Components_Db_Adapter_Pdo_Mysql;
12
13
/**
14
 * Implements a 'connect' filter for the article list
15
 *
16
 * Class ArticleList
17
 * @package ShopwarePlugins\Connect\Subscribers
18
 */
19
class ArticleList implements SubscriberInterface
20
{
21
    /**
22
     * @var Enlight_Components_Db_Adapter_Pdo_Mysql
23
     */
24
    private $db;
25
26
    /**
27
     * @param Enlight_Components_Db_Adapter_Pdo_Mysql $db
28
     */
29
    public function __construct(Enlight_Components_Db_Adapter_Pdo_Mysql $db)
30
    {
31
        $this->db = $db;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public static function getSubscribedEvents()
38
    {
39
        return [
40
            'Shopware_Controllers_Backend_ArticleList_SQLParts' => 'onFilterArticle',
41
            'Enlight_Controller_Action_PostDispatch_Backend_ArticleList' => 'extendBackendArticleList'
42
        ];
43
    }
44
45
    /**
46
     * If the 'connect' filter is checked, only show products imported from connect
47
     *
48
     * @param \Enlight_Event_EventArgs $args
49
     * @return array
50
     */
51
    public function onFilterArticle(\Enlight_Event_EventArgs $args)
52
    {
53
        $subject = $args->getSubject();
54
        $filterBy = $subject->Request()->getParam('filterBy');
55
56
        list($sqlParams, $filterSql, $categorySql, $imageSQL, $order) = $args->getReturn();
57
58
        if ($filterBy === 'connect') {
59
            $imageSQL = '
60
                LEFT JOIN s_plugin_connect_items as connect_items
61
                ON connect_items.article_id = articles.id
62
            ';
63
64
            $filterSql .= ' AND connect_items.shop_id > 0 ';
65
        }
66
67
        return [$sqlParams, $filterSql, $categorySql, $imageSQL, $order];
68
    }
69
70
    /**
71
     * Extends the product list in the backend in order to have a special hint for connect products
72
     *
73
     * @event Enlight_Controller_Action_PostDispatch_Backend_ArticleList
74
     * @param \Enlight_Event_EventArgs $args
75
     */
76
    public function extendBackendArticleList(\Enlight_Event_EventArgs $args)
77
    {
78
        /** @var $subject \Enlight_Controller_Action */
79
        $subject = $args->getSubject();
80
        $request = $subject->Request();
81
82
        switch ($request->getActionName()) {
83
            case 'load':
84
                $subject->View()->extendsTemplate(
85
                    'backend/article_list/connect.js'
86
                );
87
                break;
88
            case 'list':
89
            case 'filter':
90
                $subject->View()->data = $this->markConnectProducts(
91
                    $subject->View()->data
92
                );
93
                break;
94
            case 'columnConfig':
95
                $subject->View()->data = $this->addConnectColumn(
96
                    $subject->View()->data
97
                );
98
                break;
99
            default:
100
                break;
101
        }
102
    }
103
104
    /**
105
     * Helper method which adds an additional 'connect' field to article objects in order to indicate
106
     * if they are connect articles or not
107
     *
108
     * @param $data
109
     * @return mixed
110
     */
111
    private function markConnectProducts($data)
112
    {
113
        $articleIds = array_map(function ($row) {
114
            if ((int) $row['Article_id'] > 0) {
115
                return (int) $row['Article_id'];
116
            }
117
118
            return (int) $row['articleId'];
119
        }, $data);
120
121
        if (empty($articleIds)) {
122
            return $data;
123
        }
124
125
        $sql = 'SELECT article_id
126
                FROM s_plugin_connect_items
127
                WHERE article_id IN (' . implode(', ', $articleIds) . ')
128
                AND source_id IS NOT NULL
129
                AND shop_id IS NOT NULL';
130
131
        $connectArticleIds = array_map(function ($row) {
132
            return $row['article_id'];
133
        }, $this->db->fetchAll($sql));
134
135
        foreach ($data as $idx => $row) {
136
            if ((int) $row['Article_id'] > 0) {
137
                $articleId = $row['Article_id'];
138
            } else {
139
                $articleId = $row['articleId'];
140
            }
141
142
            $data[$idx]['connect'] = in_array($articleId, $connectArticleIds);
143
        }
144
145
        return $data;
146
    }
147
148
    /**
149
     * Adds connect field to the ExtJS model for article_list
150
     * That was changed in SW5, because the model is dynamically created
151
     *
152
     * @param $data
153
     * @return array
154
     */
155
    private function addConnectColumn($data)
156
    {
157
        $data[] = [
158
            'entity' => 'Article',
159
            'field' => 'connect',
160
            'type' => 'boolean',
161
            'alias' => 'connect',
162
            'allowInGrid' => true,
163
            'nullable' => false,
164
165
        ];
166
167
        return $data;
168
    }
169
}
170