Completed
Pull Request — master (#358)
by Simon
06:06 queued 01:52
created

ArticleList   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 137
rs 10
c 1
b 0
f 0
wmc 14
lcom 0
cbo 0

5 Methods

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