Completed
Push — master ( 9c9d88...888351 )
by Marcus
02:28
created

Itemgroupadmin::admin_updateGroup()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 60
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 60
rs 9.5555
cc 3
eloc 46
nc 3
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
use HaaseIT\HCSF\HardcodedText;
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
     * Itemgroupadmin constructor.
41
     * @param ServiceManager $serviceManager
42
     */
43
    public function __construct(ServiceManager $serviceManager)
44
    {
45
        parent::__construct($serviceManager);
46
        $this->dbal = $serviceManager->get('dbal');
47
    }
48
49
    /**
50
     *
51
     */
52
    public function preparePage()
0 ignored issues
show
Coding Style introduced by
preparePage uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
53
    {
54
        $this->P = new \HaaseIT\HCSF\CorePage($this->serviceManager);
55
        $this->P->cb_pagetype = 'content';
56
        $this->P->cb_subnav = 'admin';
57
58
        $this->P->cb_customcontenttemplate = 'shop/itemgroupadmin';
59
60
        $return = '';
61
        if (isset($_REQUEST['action']) && $_REQUEST['action'] === 'insert_lang') {
62
            $querybuilder = $this->dbal->createQueryBuilder();
63
            $querybuilder
64
                ->select('itmg_id')
65
                ->from('itemgroups_base')
66
                ->where('itmg_id = ?')
67
                ->setParameter(0, $_REQUEST['gid'])
68
            ;
69
            $stmt = $querybuilder->execute();
70
71
            $iNumRowsBasis = $stmt->rowCount();
72
73
            $querybuilder = $this->dbal->createQueryBuilder();
74
            $querybuilder
75
                ->select('itmgt_id')
76
                ->from('itemgroups_text')
77
                ->where('itmgt_pid = ? AND itmgt_lang = ?')
78
                ->setParameter(0, $_REQUEST['gid'])
79
                ->setParameter(1, HelperConfig::$lang)
80
            ;
81
            $stmt = $querybuilder->execute();
82
83
            $iNumRowsLang = $stmt->rowCount();
84
85
            if ($iNumRowsBasis === 1 && $iNumRowsLang === 0) {
86
                $iGID = filter_var($_REQUEST['gid'], FILTER_SANITIZE_NUMBER_INT);
87
                $querybuilder = $this->dbal->createQueryBuilder();
88
                $querybuilder
89
                    ->insert('itemgroups_text')
90
                    ->setValue('itmgt_pid', '?')
91
                    ->setValue('itmgt_lang', '?')
92
                    ->setParameter(0, $iGID)
93
                    ->setParameter(1, HelperConfig::$lang)
94
                ;
95
                $querybuilder->execute();
96
                \HaaseIT\HCSF\Helper::redirectToPage('/_admin/itemgroupadmin.html?gid='.$iGID.'&action=editgroup');
97
            }
98
        }
99
100
        if (isset($_REQUEST['action']) && $_REQUEST['action'] === 'editgroup') {
101
            if (isset($_REQUEST['do']) && $_REQUEST['do'] === 'true') {
102
                $this->P->cb_customdata['updatestatus'] = $this->admin_updateGroup(\HaaseIT\HCSF\Helper::getPurifier('itemgroup'));
103
            }
104
105
            $iGID = filter_var($_REQUEST['gid'], FILTER_SANITIZE_NUMBER_INT);
106
            $aGroup = $this->admin_getItemgroups($iGID);
107
            if (isset($_REQUEST['added'])) {
108
                $this->P->cb_customdata['groupjustadded'] = true;
109
            }
110
            $this->P->cb_customdata['showform'] = 'edit';
111
            $this->P->cb_customdata['group'] = $this->admin_prepareGroup('edit', $aGroup[0]);
112
        } elseif (isset($_REQUEST['action']) && $_REQUEST['action'] === 'addgroup') {
113
            $aErr = [];
114
            if (isset($_REQUEST['do']) && $_REQUEST['do'] === 'true') {
115
                $sName = filter_var($_REQUEST['name'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
116
                $sGNo = filter_var($_REQUEST['no'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
117
                $sImg = filter_var($_REQUEST['img'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
118
119
                if (strlen($sName) < 3) {
120
                    $aErr['nametooshort'] = true;
121
                }
122
                if (strlen($sGNo) < 3) {
123
                    $aErr['grouptooshort'] = true;
124
                }
125
                if (count($aErr) == 0) {
126
                    $querybuilder = $this->dbal->createQueryBuilder();
127
                    $querybuilder
128
                        ->select('itmg_no')
129
                        ->from('itemgroups_base')
130
                        ->where('itmg_no = ?')
131
                        ->setParameter(0, $sGNo)
132
                    ;
133
                    $stmt = $querybuilder->execute();
134
135
                    if ($stmt->rowCount() > 0) {
136
                        $aErr['duplicateno'] = true;
137
                    }
138
                }
139
                if (count($aErr) === 0) {
140
                    $querybuilder = $this->dbal->createQueryBuilder();
141
                    $querybuilder
142
                        ->insert('itemgroups_base')
143
                        ->setValue('itmg_name', '?')
144
                        ->setValue('itmg_no', '?')
145
                        ->setValue('itmg_img', '?')
146
                        ->setParameter(0, $sName)
147
                        ->setParameter(1, $sGNo)
148
                        ->setParameter(2, $sImg)
149
                    ;
150
                    $querybuilder->execute();
151
                    $iLastInsertID = $this->dbal->lastInsertId();
152
                    \HaaseIT\HCSF\Helper::redirectToPage('/_admin/itemgroupadmin.html?action=editgroup&added&gid='.$iLastInsertID);
153 View Code Duplication
                } else {
154
                    $this->P->cb_customdata['err'] = $aErr;
155
                    $this->P->cb_customdata['showform'] = 'add';
156
                    $this->P->cb_customdata['group'] = $this->admin_prepareGroup('add');
157
                }
158 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...
159
                $this->P->cb_customdata['showform'] = 'add';
160
                $this->P->cb_customdata['group'] = $this->admin_prepareGroup('add');
161
            }
162
        } else {
163
            if (!$return .= $this->admin_showItemgroups($this->admin_getItemgroups(''))) {
164
                $this->P->cb_customdata['err']['nogroupsavaliable'] = true;
165
            }
166
        }
167
        $this->P->oPayload->cl_html = $return;
168
    }
169
170
    /**
171
     * @param $purifier
172
     * @return string
173
     */
174
    private function admin_updateGroup( $purifier)
175
    {
176
        $iGID = filter_var($_REQUEST['gid'], FILTER_SANITIZE_NUMBER_INT);
177
        $sGNo = filter_var($_REQUEST['no'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
178
179
        $querybuilder = $this->dbal->createQueryBuilder();
180
        $querybuilder
181
            ->select('*')
182
            ->from('itemgroups_base')
183
            ->where('itmg_id != ? AND itmg_no = ?')
184
            ->setParameter(0, $iGID)
185
            ->setParameter(1, $sGNo)
186
        ;
187
        $stmt = $querybuilder->execute();
188
189
        if ($stmt->rowCount() > 0) {
190
            return 'duplicateno';
191
        }
192
193
        $querybuilder = $this->dbal->createQueryBuilder();
194
        $querybuilder
195
            ->update('itemgroups_base')
196
            ->set('itmg_name', '?')
197
            ->set('itmg_no', '?')
198
            ->set('itmg_img', '?')
199
            ->where('itmg_id = ?')
200
            ->setParameter(0, filter_var($_REQUEST['name'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW))
201
            ->setParameter(1, $sGNo)
202
            ->setParameter(2, filter_var($_REQUEST['img'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW))
203
            ->setParameter(3, $iGID)
204
        ;
205
        $querybuilder->execute();
206
207
        $querybuilder = $this->dbal->createQueryBuilder();
208
        $querybuilder
209
            ->select('itmgt_id')
210
            ->from('itemgroups_text')
211
            ->where('itmgt_pid = ? AND itmgt_lang = ?')
212
            ->setParameter(0, $iGID)
213
            ->setParameter(1, HelperConfig::$lang)
214
        ;
215
        $stmt = $querybuilder->execute();
216
217
        if ($stmt->rowCount() === 1) {
218
            $aRow = $stmt->fetch();
219
            $querybuilder = $this->dbal->createQueryBuilder();
220
            $querybuilder
221
                ->update('itemgroups_text')
222
                ->set('itmgt_shorttext', '?')
223
                ->set('itmgt_details', '?')
224
                ->where('itmgt_id = ?')
225
                ->setParameter(0, $purifier->purify($_REQUEST['shorttext']))
226
                ->setParameter(1, $purifier->purify($_REQUEST['details']))
227
                ->setParameter(2, $aRow['itmgt_id'])
228
            ;
229
            $querybuilder->execute();
230
        }
231
232
        return 'success';
233
    }
234
235
    /**
236
     * @param string $sPurpose
237
     * @param array $aData
238
     * @return array
239
     */
240
    private function admin_prepareGroup($sPurpose = 'none', $aData = [])
241
    {
242
        $aGData = [
243
            'formaction' => Tools::makeLinkHRefWithAddedGetVars('/_admin/itemgroupadmin.html'),
244
            'id' => isset($aData['itmg_id']) ? $aData['itmg_id'] : '',
245
            'name' => isset($aData['itmg_name']) ? $aData['itmg_name'] : '',
246
            'no' => isset($aData['itmg_no']) ? $aData['itmg_no'] : '',
247
            'img' => isset($aData['itmg_img']) ? $aData['itmg_img'] : '',
248
        ];
249
250
        if ($sPurpose === 'edit') {
251
            if ($aData['itmgt_id'] != '') {
252
                $aGData['lang'] = [
253
                    'shorttext' => isset($aData['itmgt_shorttext']) ? $aData['itmgt_shorttext'] : '',
254
                    'details' => isset($aData['itmgt_details']) ? $aData['itmgt_details'] : '',
255
                ];
256
            }
257
        }
258
259
        return $aGData;
260
    }
261
262
    /**
263
     * @param string $iGID
264
     * @return mixed
265
     */
266 View Code Duplication
    private function admin_getItemgroups($iGID = '')
267
    {
268
        $querybuilder = $this->dbal->createQueryBuilder();
269
        $querybuilder
270
            ->select('*')
271
            ->from('itemgroups_base', 'b')
272
            ->leftJoin('b', 'itemgroups_text', 't', 'b.itmg_id = t.itmgt_pid AND t.itmgt_lang = ?')
273
            ->setParameter(0, HelperConfig::$lang)
274
            ->orderBy('itmg_no')
275
        ;
276
277
        if ($iGID != '') {
278
            $querybuilder
279
                ->where('itmg_id = ?')
280
                ->setParameter(1, $iGID)
281
            ;
282
        }
283
        $stmt = $querybuilder->execute();
284
285
        return $stmt->fetchAll();
286
    }
287
288
    /**
289
     * @param $aGroups
290
     * @return bool|mixed
291
     */
292
    private function admin_showItemgroups($aGroups)
293
    {
294
        $aList = [
295
            ['title' => HardcodedText::get('itemgroupadmin_list_no'), 'key' => 'gno', 'width' => 80, 'linked' => false, 'style-data' => 'padding: 5px 0;'],
296
            ['title' => HardcodedText::get('itemgroupadmin_list_name'), 'key' => 'gname', 'width' => 350, 'linked' => false, 'style-data' => 'padding: 5px 0;'],
297
            ['title' => HardcodedText::get('itemgroupadmin_list_edit'), 'key' => 'gid', 'width' => 30, 'linked' => true, 'ltarget' => '/_admin/itemgroupadmin.html', 'lkeyname' => 'gid', 'lgetvars' => ['action' => 'editgroup'], 'style-data' => 'padding: 5px 0;'],
298
        ];
299
        if (count($aGroups) > 0) {
300
            $aData = [];
301
            foreach ($aGroups as $aValue) {
302
                $aData[] = [
303
                    'gid' => $aValue['itmg_id'],
304
                    'gno' => $aValue['itmg_no'],
305
                    'gname' => $aValue['itmg_name'],
306
                ];
307
            }
308
            return Tools::makeListtable($aList, $aData, $this->serviceManager->get('twig'));
309
        }
310
311
        return false;
312
    }
313
}