Completed
Pull Request — master (#358)
by Stefan
02:49
created

ArticleList::extendBackendArticleList()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

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