Conditions | 34 |
Paths | 18242 |
Total Lines | 136 |
Code Lines | 79 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
302 | public function doSaveCreateWiz($is_materialized = false) |
||
303 | { |
||
304 | $data = $this->misc->getDatabaseAccessor(); |
||
305 | |||
306 | // Check that they've given a name and fields they want to select |
||
307 | |||
308 | if (!strlen($_POST['formView'])) { |
||
309 | return $this->doSetParamsCreate($this->lang['strviewneedsname']); |
||
|
|||
310 | } |
||
311 | if (!isset($_POST['formFields']) || !count($_POST['formFields'])) { |
||
312 | return $this->doSetParamsCreate($this->lang['strviewneedsfields']); |
||
313 | } |
||
314 | $selFields = ''; |
||
315 | |||
316 | if (!empty($_POST['dblFldMeth'])) { |
||
317 | $tmpHsh = []; |
||
318 | } |
||
319 | |||
320 | foreach ($_POST['formFields'] as $curField) { |
||
321 | $arrTmp = unserialize($curField); |
||
322 | $data->fieldArrayClean($arrTmp); |
||
323 | $field_arr = [$arrTmp['schemaname'], $arrTmp['tablename'], $arrTmp['fieldname']]; |
||
324 | |||
325 | $field_element = '"'.implode('"."', $field_arr).'"'; |
||
326 | if (empty($_POST['dblFldMeth'])) { |
||
327 | // no doublon control |
||
328 | $selFields .= $field_element.', '; |
||
329 | // doublon control |
||
330 | } elseif (empty($tmpHsh[$arrTmp['fieldname']])) { |
||
331 | // field does not exist |
||
332 | $selFields .= $field_element.', '; |
||
333 | $tmpHsh[$arrTmp['fieldname']] = 1; |
||
334 | } elseif ('rename' == $_POST['dblFldMeth']) { |
||
335 | // field exist and must be renamed |
||
336 | ++$tmpHsh[$arrTmp['fieldname']]; |
||
337 | $selFields .= $field_element.' AS "'.implode('_', $field_arr).'_'.$tmpHsh[$arrTmp['fieldname']].'", '; |
||
338 | } // field already exist, just ignore this one |
||
339 | } |
||
340 | |||
341 | $selFields = substr($selFields, 0, -2); |
||
342 | unset($arrTmp, $tmpHsh); |
||
343 | $linkFields = ''; |
||
344 | $count = 0; |
||
345 | |||
346 | // If we have links, out put the JOIN ... ON statements |
||
347 | if (is_array($_POST['formLink'])) { |
||
348 | // Filter out invalid/blank entries for our links |
||
349 | $arrLinks = []; |
||
350 | foreach ($_POST['formLink'] as $curLink) { |
||
351 | if (strlen($curLink['leftlink']) && strlen($curLink['rightlink']) && strlen($curLink['operator'])) { |
||
352 | $arrLinks[] = $curLink; |
||
353 | } |
||
354 | } |
||
355 | // We must perform some magic to make sure that we have a valid join order |
||
356 | $count = sizeof($arrLinks); |
||
357 | $arrJoined = []; |
||
358 | $arrUsedTbls = []; |
||
359 | } |
||
360 | // If we have at least one join condition, output it |
||
361 | |||
362 | $j = 0; |
||
363 | $this->prtrace('arrLinks ', $arrLinks); |
||
364 | while ($j < $count) { |
||
365 | foreach ($arrLinks as $curLink) { |
||
366 | $arrLeftLink = unserialize($curLink['leftlink']); |
||
367 | $arrRightLink = unserialize($curLink['rightlink']); |
||
368 | $data->fieldArrayClean($arrLeftLink); |
||
369 | $data->fieldArrayClean($arrRightLink); |
||
370 | |||
371 | $tbl1 = "\"{$arrLeftLink['schemaname']}\".\"{$arrLeftLink['tablename']}\""; |
||
372 | $tbl2 = "\"{$arrRightLink['schemaname']}\".\"{$arrRightLink['tablename']}\""; |
||
373 | |||
374 | if (!((!in_array($curLink, $arrJoined, true) && in_array($tbl1, $arrUsedTbls, true)) || !count($arrJoined))) { |
||
375 | continue; |
||
376 | } |
||
377 | // Make sure for multi-column foreign keys that we use a table alias tables joined to more than once |
||
378 | // This can (and should be) more optimized for multi-column foreign keys |
||
379 | $adj_tbl2 = in_array($tbl2, $arrUsedTbls, true) ? "${tbl2} AS alias_ppa_".time() : $tbl2; |
||
380 | |||
381 | $clause1 = "{$curLink['operator']} ${adj_tbl2} ON ({$tbl1}.\"{$arrLeftLink['fieldname']}\" = {$tbl2}.\"{$arrRightLink['fieldname']}\") "; |
||
382 | $clause2 = "${tbl1} {$curLink['operator']} ${adj_tbl2} ON ({$tbl1}.\"{$arrLeftLink['fieldname']}\" = {$tbl2}.\"{$arrRightLink['fieldname']}\") "; |
||
383 | |||
384 | $linkFields .= strlen($linkFields) ? $clause1 : $clause2; |
||
385 | |||
386 | $arrJoined[] = $curLink; |
||
387 | if (!in_array($tbl1, $arrUsedTbls, true)) { |
||
388 | $arrUsedTbls[] = $tbl1; |
||
389 | } |
||
390 | |||
391 | if (!in_array($tbl2, $arrUsedTbls, true)) { |
||
392 | $arrUsedTbls[] = $tbl2; |
||
393 | } |
||
394 | } |
||
395 | ++$j; |
||
396 | } |
||
397 | |||
398 | //if linkFields has no length then either _POST['formLink'] was not set, or there were no join conditions |
||
399 | //just select from all seleted tables - a cartesian join do a |
||
400 | if (!strlen($linkFields)) { |
||
401 | foreach ($_POST['formTables'] as $curTable) { |
||
402 | $arrTmp = unserialize($curTable); |
||
403 | $data->fieldArrayClean($arrTmp); |
||
404 | $linkFields .= (strlen($linkFields) ? ', ' : ' ')."\"{$arrTmp['schemaname']}\".\"{$arrTmp['tablename']}\""; |
||
405 | } |
||
406 | } |
||
407 | |||
408 | $addConditions = ''; |
||
409 | if (is_array($_POST['formCondition'])) { |
||
410 | foreach ($_POST['formCondition'] as $curCondition) { |
||
411 | if (strlen($curCondition['field']) && strlen($curCondition['txt'])) { |
||
412 | $arrTmp = unserialize($curCondition['field']); |
||
413 | $data->fieldArrayClean($arrTmp); |
||
414 | $condition = " \"{$arrTmp['schemaname']}\".\"{$arrTmp['tablename']}\".\"{$arrTmp['fieldname']}\" {$curCondition['operator']} '{$curCondition['txt']}' "; |
||
415 | $addConditions .= (strlen($addConditions) ? ' AND ' : ' ').$condition; |
||
416 | } |
||
417 | } |
||
418 | } |
||
419 | |||
420 | $viewQuery = "SELECT ${selFields} FROM ${linkFields} "; |
||
421 | |||
422 | //add where from additional conditions |
||
423 | if (strlen($addConditions)) { |
||
424 | $viewQuery .= ' WHERE '.$addConditions; |
||
425 | } |
||
426 | |||
427 | try { |
||
428 | $status = $data->createView($_POST['formView'], $viewQuery, false, $_POST['formComment'], $is_materialized); |
||
429 | if (0 == $status) { |
||
430 | $this->misc->setReloadBrowser(true); |
||
431 | |||
432 | return $this->doDefault($this->lang['strviewcreated']); |
||
433 | } |
||
434 | |||
435 | return $this->doSetParamsCreate($this->lang['strviewcreatedbad']); |
||
436 | } catch (\PHPPgAdmin\ADOdbException $e) { |
||
437 | return $this->halt($e->getMessage()); |
||
1 ignored issue
–
show
|
|||
438 | } |
||
441 |