Itemgroupadmin::getItemgroups()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 21

Duplication

Lines 21
Ratio 100 %

Importance

Changes 0
Metric Value
dl 21
loc 21
rs 9.584
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
/*
4
    HCSF - A multilingual CMS and Shopsystem
5
    Copyright (C) 2014  Marcus Haase - [email protected]
6
7
    This program is free software: you can redistribute it and/or modify
8
    it under the terms of the GNU General Public License as published by
9
    the Free Software Foundation, either version 3 of the License, or
10
    (at your option) any later version.
11
12
    This program is distributed in the hope that it will be useful,
13
    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
    GNU General Public License for more details.
16
17
    You should have received a copy of the GNU General Public License
18
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
namespace HaaseIT\HCSF\Controller\Admin\Shop;
22
23
24
use HaaseIT\HCSF\HelperConfig;
25
use HaaseIT\Toolbox\Tools;
26
use Zend\ServiceManager\ServiceManager;
27
28
/**
29
 * Class Itemgroupadmin
30
 * @package HaaseIT\HCSF\Controller\Admin\Shop
31
 */
32
class Itemgroupadmin extends Base
33
{
34
    /**
35
     * @var \Doctrine\DBAL\Connection
36
     */
37
    private $dbal;
38
39
    /**
40
     * @var \HaaseIT\HCSF\HardcodedText
41
     */
42
    private $hardcodedtextcats;
43
44
    /**
45
     * Itemgroupadmin constructor.
46
     * @param ServiceManager $serviceManager
47
     */
48
    public function __construct(ServiceManager $serviceManager)
49
    {
50
        parent::__construct($serviceManager);
51
        $this->dbal = $serviceManager->get('dbal');
52
        $this->hardcodedtextcats = $serviceManager->get('hardcodedtextcats');
53
    }
54
55
    /**
56
     *
57
     */
58
    public function preparePage()
59
    {
60
        $this->P = new \HaaseIT\HCSF\CorePage($this->serviceManager, [], 'admin/base.twig');
61
        $this->P->cb_pagetype = 'content';
62
        $this->P->cb_subnav = 'admin';
63
64
        $this->P->cb_customcontenttemplate = 'shop/itemgroupadmin';
65
66
        $return = '';
67
        $iGID = filter_input(INPUT_GET, 'gid', FILTER_SANITIZE_NUMBER_INT);
68
        $getaction = filter_input(INPUT_GET, 'action');
69
        if ($getaction === 'insert_lang') {
70
            $querybuilder = $this->dbal->createQueryBuilder();
71
            $querybuilder
72
                ->select('itmg_id')
73
                ->from('itemgroups_base')
74
                ->where('itmg_id = ?')
75
                ->setParameter(0, $iGID)
76
            ;
77
            $stmt = $querybuilder->execute();
78
79
            $iNumRowsBasis = $stmt->rowCount();
80
81
            $querybuilder = $this->dbal->createQueryBuilder();
82
            $querybuilder
83
                ->select('itmgt_id')
84
                ->from('itemgroups_text')
85
                ->where('itmgt_pid = ? AND itmgt_lang = ?')
86
                ->setParameter(0, $iGID)
87
                ->setParameter(1, $this->config->getLang())
88
            ;
89
            $stmt = $querybuilder->execute();
90
91
            $iNumRowsLang = $stmt->rowCount();
92
93
            if ($iNumRowsBasis === 1 && $iNumRowsLang === 0) {
94
                $querybuilder = $this->dbal->createQueryBuilder();
95
                $querybuilder
96
                    ->insert('itemgroups_text')
97
                    ->setValue('itmgt_pid', '?')
98
                    ->setValue('itmgt_lang', '?')
99
                    ->setParameter(0, $iGID)
100
                    ->setParameter(1, $this->config->getLang())
101
                ;
102
                $querybuilder->execute();
103
                $this->helper->redirectToPage('/_admin/itemgroupadmin.html?gid='.$iGID.'&action=editgroup');
104
            }
105
        }
106
107
        $postdo = filter_input(INPUT_POST, 'do');
108
        if ($getaction === 'editgroup') {
109
            if ($postdo === 'true') {
110
                $this->P->cb_customdata['updatestatus'] = $this->updateGroup();
111
            }
112
113
            $aGroup = $this->getItemgroups($iGID);
114
            if (filter_input(INPUT_GET, 'added') !== null) {
115
                $this->P->cb_customdata['groupjustadded'] = true;
116
            }
117
            $this->P->cb_customdata['showform'] = 'edit';
118
            $this->P->cb_customdata['group'] = $this->prepareGroup('edit', $aGroup[0]);
119
        } elseif ($getaction === 'addgroup') {
120
            $aErr = [];
121
            if ($postdo === 'true') {
122
                $sName = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
123
                $sGNo = filter_input(INPUT_POST, 'no', FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
124
                $sImg = filter_input(INPUT_POST, 'img', FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
125
126
                if (strlen($sName) < 3) {
127
                    $aErr['nametooshort'] = true;
128
                }
129
                if (strlen($sGNo) < 3) {
130
                    $aErr['grouptooshort'] = true;
131
                }
132
                if (count($aErr) == 0) {
133
                    $querybuilder = $this->dbal->createQueryBuilder();
134
                    $querybuilder
135
                        ->select('itmg_no')
136
                        ->from('itemgroups_base')
137
                        ->where('itmg_no = ?')
138
                        ->setParameter(0, $sGNo)
139
                    ;
140
                    $stmt = $querybuilder->execute();
141
142
                    if ($stmt->rowCount() > 0) {
143
                        $aErr['duplicateno'] = true;
144
                    }
145
                }
146
                if (count($aErr) === 0) {
147
                    $querybuilder = $this->dbal->createQueryBuilder();
148
                    $querybuilder
149
                        ->insert('itemgroups_base')
150
                        ->setValue('itmg_name', '?')
151
                        ->setValue('itmg_no', '?')
152
                        ->setValue('itmg_img', '?')
153
                        ->setParameter(0, $sName)
154
                        ->setParameter(1, $sGNo)
155
                        ->setParameter(2, $sImg)
156
                    ;
157
                    $querybuilder->execute();
158
                    $iLastInsertID = $this->dbal->lastInsertId();
159
                    $this->helper->redirectToPage('/_admin/itemgroupadmin.html?action=editgroup&added&gid='.$iLastInsertID);
160 View Code Duplication
                } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
161
                    $this->P->cb_customdata['err'] = $aErr;
162
                    $this->P->cb_customdata['showform'] = 'add';
163
                    $this->P->cb_customdata['group'] = $this->prepareGroup('add');
164
                }
165 View Code Duplication
            } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
166
                $this->P->cb_customdata['showform'] = 'add';
167
                $this->P->cb_customdata['group'] = $this->prepareGroup('add');
168
            }
169
        } else {
170
            if (!$return .= $this->showItemgroups($this->getItemgroups(''))) {
171
                $this->P->cb_customdata['err']['nogroupsavaliable'] = true;
172
            }
173
        }
174
        $this->P->oPayload->cl_html = $return;
175
    }
176
177
    /**
178
     * @return string
179
     */
180
    private function updateGroup()
181
    {
182
        $purifier = false;
183
        if ($this->config->getShop('itemgrouptext_enable_purifier')) {
184
            $purifier = $this->helper->getPurifier('itemgroup');
185
        }
186
187
        $iGID = filter_input(INPUT_GET, 'gid', FILTER_SANITIZE_NUMBER_INT);
188
        $sGNo = filter_input(INPUT_POST, 'no', FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
189
190
        $querybuilder = $this->dbal->createQueryBuilder();
191
        $querybuilder
192
            ->select('*')
193
            ->from('itemgroups_base')
194
            ->where('itmg_id != ? AND itmg_no = ?')
195
            ->setParameter(0, $iGID)
196
            ->setParameter(1, $sGNo)
197
        ;
198
        $stmt = $querybuilder->execute();
199
200
        if ($stmt->rowCount() > 0) {
201
            return 'duplicateno';
202
        }
203
204
        $querybuilder = $this->dbal->createQueryBuilder();
205
        $querybuilder
206
            ->update('itemgroups_base')
207
            ->set('itmg_name', '?')
208
            ->set('itmg_no', '?')
209
            ->set('itmg_img', '?')
210
            ->where('itmg_id = ?')
211
            ->setParameter(0, filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW))
212
            ->setParameter(1, $sGNo)
213
            ->setParameter(2, filter_input(INPUT_POST, 'img', FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW))
214
            ->setParameter(3, $iGID)
215
        ;
216
        $querybuilder->execute();
217
218
        $querybuilder = $this->dbal->createQueryBuilder();
219
        $querybuilder
220
            ->select('itmgt_id')
221
            ->from('itemgroups_text')
222
            ->where('itmgt_pid = ? AND itmgt_lang = ?')
223
            ->setParameter(0, $iGID)
224
            ->setParameter(1, $this->config->getLang())
225
        ;
226
        $stmt = $querybuilder->execute();
227
228
        if ($stmt->rowCount() === 1) {
229
            $shorttext = filter_input(INPUT_POST, 'shorttext', FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW);
230
            $details = filter_input(INPUT_POST, 'details', FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW);
231
232
            $aRow = $stmt->fetch();
233
            $querybuilder = $this->dbal->createQueryBuilder();
234
            $querybuilder
235
                ->update('itemgroups_text')
236
                ->set('itmgt_shorttext', '?')
237
                ->set('itmgt_details', '?')
238
                ->where('itmgt_id = ?')
239
                ->setParameter(0, !empty($this->purifier) ? $purifier->purify($shorttext) : $shorttext)
0 ignored issues
show
Bug introduced by
The property purifier does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
240
                ->setParameter(1, !empty($this->purifier) ? $purifier->purify($details) : $details)
241
                ->setParameter(2, $aRow['itmgt_id'])
242
            ;
243
            $querybuilder->execute();
244
        }
245
246
        return 'success';
247
    }
248
249
    /**
250
     * @param string $sPurpose
251
     * @param array $aData
252
     * @return array
253
     */
254
    private function prepareGroup($sPurpose = 'none', $aData = [])
255
    {
256
        $aGData = [
257
            'formaction' => Tools::makeLinkHRefWithAddedGetVars('/_admin/itemgroupadmin.html'),
258
            'id' => isset($aData['itmg_id']) ? $aData['itmg_id'] : '',
259
            'name' => isset($aData['itmg_name']) ? $aData['itmg_name'] : '',
260
            'no' => isset($aData['itmg_no']) ? $aData['itmg_no'] : '',
261
            'img' => isset($aData['itmg_img']) ? $aData['itmg_img'] : '',
262
        ];
263
264
        if ($sPurpose === 'edit') {
265
            if ($aData['itmgt_id'] != '') {
266
                $aGData['lang'] = [
267
                    'shorttext' => isset($aData['itmgt_shorttext']) ? $aData['itmgt_shorttext'] : '',
268
                    'details' => isset($aData['itmgt_details']) ? $aData['itmgt_details'] : '',
269
                ];
270
            }
271
        }
272
273
        return $aGData;
274
    }
275
276
    /**
277
     * @param string $iGID
278
     * @return mixed
279
     */
280 View Code Duplication
    private function getItemgroups($iGID = '')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
281
    {
282
        $querybuilder = $this->dbal->createQueryBuilder();
283
        $querybuilder
284
            ->select('*')
285
            ->from('itemgroups_base', 'b')
286
            ->leftJoin('b', 'itemgroups_text', 't', 'b.itmg_id = t.itmgt_pid AND t.itmgt_lang = ?')
287
            ->setParameter(0, $this->config->getLang())
288
            ->orderBy('itmg_no')
289
        ;
290
291
        if ($iGID != '') {
292
            $querybuilder
293
                ->where('itmg_id = ?')
294
                ->setParameter(1, $iGID)
295
            ;
296
        }
297
        $stmt = $querybuilder->execute();
298
299
        return $stmt->fetchAll();
300
    }
301
302
    /**
303
     * @param $aGroups
304
     * @return bool|mixed
305
     */
306
    private function showItemgroups($aGroups)
307
    {
308
        $aList = [
309
            ['title' => $this->hardcodedtextcats->get('itemgroupadmin_list_no'), 'key' => 'gno', 'width' => 80, 'linked' => false, 'style-data' => 'padding: 5px 0;'],
310
            ['title' => $this->hardcodedtextcats->get('itemgroupadmin_list_name'), 'key' => 'gname', 'width' => 350, 'linked' => false, 'style-data' => 'padding: 5px 0;'],
311
            ['title' => $this->hardcodedtextcats->get('itemgroupadmin_list_edit'), 'key' => 'gid', 'width' => 30, 'linked' => true, 'ltarget' => '/_admin/itemgroupadmin.html', 'lkeyname' => 'gid', 'lgetvars' => ['action' => 'editgroup'], 'style-data' => 'padding: 5px 0;'],
312
        ];
313
        if (count($aGroups) > 0) {
314
            $aData = [];
315
            foreach ($aGroups as $aValue) {
316
                $aData[] = [
317
                    'gid' => $aValue['itmg_id'],
318
                    'gno' => $aValue['itmg_no'],
319
                    'gname' => $aValue['itmg_name'],
320
                ];
321
            }
322
            return Tools::makeListtable($aList, $aData, $this->serviceManager->get('twig'));
323
        }
324
325
        return false;
326
    }
327
}
328