Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Itemgroupadmin often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Itemgroupadmin, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
33 | class Itemgroupadmin extends Base |
||
34 | { |
||
35 | /** |
||
36 | * @var \PDO |
||
37 | */ |
||
38 | private $db; |
||
39 | |||
40 | /** |
||
41 | * Itemgroupadmin constructor. |
||
42 | * @param ServiceManager $serviceManager |
||
43 | */ |
||
44 | public function __construct(ServiceManager $serviceManager) |
||
45 | { |
||
46 | parent::__construct($serviceManager); |
||
47 | $this->db = $serviceManager->get('db'); |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * |
||
52 | */ |
||
53 | public function preparePage() |
||
54 | { |
||
55 | $this->P = new \HaaseIT\HCSF\CorePage($this->serviceManager); |
||
56 | $this->P->cb_pagetype = 'content'; |
||
57 | $this->P->cb_subnav = 'admin'; |
||
58 | |||
59 | $this->P->cb_customcontenttemplate = 'shop/itemgroupadmin'; |
||
60 | |||
61 | $return = ''; |
||
62 | if (isset($_REQUEST['action']) && $_REQUEST['action'] === 'insert_lang') { |
||
63 | $sql = 'SELECT itmg_id FROM itemgroups_base WHERE itmg_id = :gid'; |
||
64 | $hResult = $this->db->prepare($sql); |
||
65 | $hResult->bindValue(':gid', $_REQUEST['gid']); |
||
66 | $hResult->execute(); |
||
67 | $iNumRowsBasis = $hResult->rowCount(); |
||
68 | |||
69 | $sql = 'SELECT itmgt_id FROM itemgroups_text WHERE itmgt_pid = :gid AND itmgt_lang = :lang'; |
||
70 | $hResult = $this->db->prepare($sql); |
||
71 | $hResult->bindValue(':gid', $_REQUEST['gid']); |
||
72 | $hResult->bindValue(':lang', HelperConfig::$lang); |
||
73 | $hResult->execute(); |
||
74 | $iNumRowsLang = $hResult->rowCount(); |
||
75 | |||
76 | if ($iNumRowsBasis == 1 && $iNumRowsLang == 0) { |
||
77 | $iGID = filter_var($_REQUEST['gid'], FILTER_SANITIZE_NUMBER_INT); |
||
78 | $aData = [ |
||
79 | 'itmgt_pid' => $iGID, |
||
80 | 'itmgt_lang' => HelperConfig::$lang, |
||
81 | ]; |
||
82 | $sql = DBTools::buildPSInsertQuery($aData, 'itemgroups_text'); |
||
83 | $hResult = $this->db->prepare($sql); |
||
84 | foreach ($aData as $sKey => $sValue) { |
||
85 | $hResult->bindValue(':'.$sKey, $sValue); |
||
86 | } |
||
87 | $hResult->execute(); |
||
88 | \HaaseIT\HCSF\Helper::redirectToPage('/_admin/itemgroupadmin.html?gid='.$iGID.'&action=editgroup'); |
||
89 | } |
||
90 | } |
||
91 | |||
92 | if (isset($_REQUEST['action']) && $_REQUEST['action'] === 'editgroup') { |
||
93 | if (isset($_REQUEST['do']) && $_REQUEST['do'] === 'true') { |
||
94 | $this->P->cb_customdata['updatestatus'] = $this->admin_updateGroup(\HaaseIT\HCSF\Helper::getPurifier('itemgroup')); |
||
95 | } |
||
96 | |||
97 | $iGID = filter_var($_REQUEST['gid'], FILTER_SANITIZE_NUMBER_INT); |
||
98 | $aGroup = $this->admin_getItemgroups($iGID); |
||
99 | if (isset($_REQUEST['added'])) { |
||
100 | $this->P->cb_customdata['groupjustadded'] = true; |
||
101 | } |
||
102 | $this->P->cb_customdata['showform'] = 'edit'; |
||
103 | $this->P->cb_customdata['group'] = $this->admin_prepareGroup('edit', $aGroup[0]); |
||
104 | } elseif (isset($_REQUEST['action']) && $_REQUEST['action'] === 'addgroup') { |
||
105 | $aErr = []; |
||
106 | if (isset($_REQUEST['do']) && $_REQUEST['do'] === 'true') { |
||
107 | $sName = filter_var($_REQUEST['name'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW); |
||
108 | $sGNo = filter_var($_REQUEST['no'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW); |
||
109 | $sImg = filter_var($_REQUEST['img'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW); |
||
110 | |||
111 | if (strlen($sName) < 3) { |
||
112 | $aErr['nametooshort'] = true; |
||
113 | } |
||
114 | if (strlen($sGNo) < 3) { |
||
115 | $aErr['grouptooshort'] = true; |
||
116 | } |
||
117 | if (count($aErr) == 0) { |
||
118 | $sql = 'SELECT itmg_no FROM itemgroups_base WHERE itmg_no = :no'; |
||
119 | $hResult = $this->db->prepare($sql); |
||
120 | $hResult->bindValue(':no', $sGNo); |
||
121 | $hResult->execute(); |
||
122 | if ($hResult->rowCount() > 0) { |
||
123 | $aErr['duplicateno'] = true; |
||
124 | } |
||
125 | } |
||
126 | if (count($aErr) == 0) { |
||
127 | $aData = [ |
||
128 | 'itmg_name' => $sName, |
||
129 | 'itmg_no' => $sGNo, |
||
130 | 'itmg_img' => $sImg, |
||
131 | ]; |
||
132 | $sql = DBTools::buildPSInsertQuery($aData, 'itemgroups_base'); |
||
133 | $hResult = $this->db->prepare($sql); |
||
134 | foreach ($aData as $sKey => $sValue) { |
||
135 | $hResult->bindValue(':'.$sKey, $sValue); |
||
136 | } |
||
137 | $hResult->execute(); |
||
138 | $iLastInsertID = $this->db->lastInsertId(); |
||
139 | \HaaseIT\HCSF\Helper::redirectToPage('/_admin/itemgroupadmin.html?action=editgroup&added&gid='.$iLastInsertID)); |
||
140 | } else { |
||
141 | $this->P->cb_customdata['err'] = $aErr; |
||
142 | $this->P->cb_customdata['showform'] = 'add'; |
||
143 | $this->P->cb_customdata['group'] = $this->admin_prepareGroup('add'); |
||
144 | } |
||
145 | } else { |
||
146 | $this->P->cb_customdata['showform'] = 'add'; |
||
147 | $this->P->cb_customdata['group'] = $this->admin_prepareGroup('add'); |
||
148 | } |
||
149 | } else { |
||
150 | if (!$return .= $this->admin_showItemgroups($this->admin_getItemgroups(''))) { |
||
151 | $this->P->cb_customdata['err']['nogroupsavaliable'] = true; |
||
152 | } |
||
153 | } |
||
154 | $this->P->oPayload->cl_html = $return; |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * @param $purifier |
||
159 | * @return string |
||
160 | */ |
||
161 | private function admin_updateGroup( $purifier) |
||
162 | { |
||
163 | $sql = 'SELECT * FROM itemgroups_base WHERE itmg_id != :id AND itmg_no = :gno'; |
||
164 | $hResult = $this->db->prepare($sql); |
||
165 | $iGID = filter_var($_REQUEST['gid'], FILTER_SANITIZE_NUMBER_INT); |
||
166 | $sGNo = filter_var($_REQUEST['no'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW); |
||
167 | $hResult->bindValue(':id', $iGID); |
||
168 | $hResult->bindValue(':gno', $sGNo); |
||
169 | $hResult->execute(); |
||
170 | $iNumRows = $hResult->rowCount(); |
||
171 | |||
172 | if ($iNumRows > 0) { |
||
173 | return 'duplicateno'; |
||
174 | } |
||
175 | |||
176 | $aData = [ |
||
177 | 'itmg_name' => filter_var($_REQUEST['name'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW), |
||
178 | 'itmg_no' => $sGNo, |
||
179 | 'itmg_img' => filter_var($_REQUEST['img'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW), |
||
180 | 'itmg_id'=> $iGID, |
||
181 | ]; |
||
182 | |||
183 | $sql = DBTools::buildPSUpdateQuery($aData, 'itemgroups_base', 'itmg_id'); |
||
184 | $hResult = $this->db->prepare($sql); |
||
185 | foreach ($aData as $sKey => $sValue) { |
||
186 | $hResult->bindValue(':' . $sKey, $sValue); |
||
187 | } |
||
188 | $hResult->execute(); |
||
189 | |||
190 | $sql = 'SELECT itmgt_id FROM itemgroups_text WHERE itmgt_pid = :gid AND itmgt_lang = :lang'; |
||
191 | $hResult = $this->db->prepare($sql); |
||
192 | $hResult->bindValue(':gid', $iGID); |
||
193 | $hResult->bindValue(':lang', HelperConfig::$lang, \PDO::PARAM_STR); |
||
194 | $hResult->execute(); |
||
195 | |||
196 | $iNumRows = $hResult->rowCount(); |
||
197 | |||
198 | if ($iNumRows == 1) { |
||
199 | $aRow = $hResult->fetch(); |
||
200 | $aData = [ |
||
201 | 'itmgt_shorttext' => $purifier->purify($_REQUEST['shorttext']), |
||
202 | 'itmgt_details' => $purifier->purify($_REQUEST['details']), |
||
203 | 'itmgt_id' => $aRow['itmgt_id'], |
||
204 | ]; |
||
205 | $sql = DBTools::buildPSUpdateQuery($aData, 'itemgroups_text', 'itmgt_id'); |
||
206 | $hResult = $this->db->prepare($sql); |
||
207 | foreach ($aData as $sKey => $sValue) { |
||
208 | $hResult->bindValue(':' . $sKey, $sValue); |
||
209 | } |
||
210 | $hResult->execute(); |
||
211 | } |
||
212 | |||
213 | return 'success'; |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * @param string $sPurpose |
||
218 | * @param array $aData |
||
219 | * @return array |
||
220 | */ |
||
221 | private function admin_prepareGroup($sPurpose = 'none', $aData = []) |
||
222 | { |
||
223 | $aGData = [ |
||
224 | 'formaction' => Tools::makeLinkHRefWithAddedGetVars('/_admin/itemgroupadmin.html'), |
||
225 | 'id' => isset($aData['itmg_id']) ? $aData['itmg_id'] : '', |
||
226 | 'name' => isset($aData['itmg_name']) ? $aData['itmg_name'] : '', |
||
227 | 'no' => isset($aData['itmg_no']) ? $aData['itmg_no'] : '', |
||
228 | 'img' => isset($aData['itmg_img']) ? $aData['itmg_img'] : '', |
||
229 | ]; |
||
230 | |||
231 | if ($sPurpose === 'edit') { |
||
232 | if ($aData['itmgt_id'] != '') { |
||
233 | $aGData['lang'] = [ |
||
234 | 'shorttext' => isset($aData['itmgt_shorttext']) ? $aData['itmgt_shorttext'] : '', |
||
235 | 'details' => isset($aData['itmgt_details']) ? $aData['itmgt_details'] : '', |
||
236 | ]; |
||
237 | } |
||
238 | } |
||
239 | |||
240 | return $aGData; |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * @param string $iGID |
||
245 | * @return mixed |
||
246 | */ |
||
247 | private function admin_getItemgroups($iGID = '') |
||
248 | { |
||
249 | $sql = 'SELECT * FROM itemgroups_base ' |
||
250 | . 'LEFT OUTER JOIN itemgroups_text ON itemgroups_base.itmg_id = itemgroups_text.itmgt_pid' |
||
251 | . ' AND itemgroups_text.itmgt_lang = :lang'; |
||
252 | if ($iGID != '') { |
||
253 | $sql .= ' WHERE itmg_id = :gid'; |
||
254 | } |
||
255 | $sql .= ' ORDER BY itmg_no'; |
||
256 | $hResult = $this->db->prepare($sql); |
||
257 | $hResult->bindValue(':lang', HelperConfig::$lang); |
||
258 | if ($iGID != '') { |
||
259 | $hResult->bindValue(':gid', $iGID); |
||
260 | } |
||
261 | $hResult->execute(); |
||
262 | |||
263 | return $hResult->fetchAll(); |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * @param $aGroups |
||
268 | * @return bool|mixed |
||
269 | */ |
||
270 | private function admin_showItemgroups($aGroups) |
||
271 | { |
||
272 | $aList = [ |
||
273 | ['title' => HardcodedText::get('itemgroupadmin_list_no'), 'key' => 'gno', 'width' => 80, 'linked' => false, 'style-data' => 'padding: 5px 0;'], |
||
274 | ['title' => HardcodedText::get('itemgroupadmin_list_name'), 'key' => 'gname', 'width' => 350, 'linked' => false, 'style-data' => 'padding: 5px 0;'], |
||
275 | ['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;'], |
||
276 | ]; |
||
277 | if (count($aGroups) > 0) { |
||
278 | $aData = []; |
||
279 | foreach ($aGroups as $aValue) { |
||
280 | $aData[] = [ |
||
281 | 'gid' => $aValue['itmg_id'], |
||
282 | 'gno' => $aValue['itmg_no'], |
||
283 | 'gname' => $aValue['itmg_name'], |
||
284 | ]; |
||
285 | } |
||
286 | return Tools::makeListtable($aList, $aData, $this->serviceManager->get('twig')); |
||
287 | } else { |
||
288 | return false; |
||
289 | } |
||
290 | } |
||
291 | } |