|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2016 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Paypal\Model\Config\Structure; |
| 7 | + |
| 8 | +/** |
| 9 | + * PayPal change structure of payment methods configuration in admin panel. |
| 10 | + */ |
| 11 | +class PaymentSectionModifier |
| 12 | +{ |
| 13 | + /** |
| 14 | + * Identifiers of special payment method configuration groups |
| 15 | + * |
| 16 | + * @var array |
| 17 | + */ |
| 18 | + private static $specialGroups = [ |
| 19 | + 'account', |
| 20 | + 'recommended_solutions', |
| 21 | + 'other_paypal_payment_solutions', |
| 22 | + 'other_payment_methods', |
| 23 | + ]; |
| 24 | + |
| 25 | + /** |
| 26 | + * Returns changed section structure. |
| 27 | + * |
| 28 | + * Payment configuration has predefined special blocks: |
| 29 | + * - Account information (id = account), |
| 30 | + * - Recommended Solutions (id = recommended_solutions), |
| 31 | + * - Other PayPal paymnt solution (id = other_paypal_payment_solutions), |
| 32 | + * - Other payment methods (id = other_payment_methods). |
| 33 | + * All payment methods configuration should be moved to one of this group. |
| 34 | + * To move payment method to specific configuration group specify "displayIn" |
| 35 | + * attribute in system.xml file equals to any id of predefined special group. |
| 36 | + * If "displayIn" attribute is not specified then payment method moved to "Other payment methods" group |
| 37 | + * |
| 38 | + * @param array $initialStructure |
| 39 | + * @return array |
| 40 | + */ |
| 41 | + public function modify(array $initialStructure) |
| 42 | + { |
| 43 | + $changedStructure = array_fill_keys(self::$specialGroups, []); |
| 44 | + |
| 45 | + foreach ($initialStructure as $childSection => $childData) { |
| 46 | + if (in_array($childSection, self::$specialGroups)) { |
| 47 | + if (isset($changedStructure[$childSection]['children'])) { |
| 48 | + $children = $changedStructure[$childSection]['children']; |
| 49 | + if (isset($childData['children'])) { |
| 50 | + $children += $childData['children']; |
| 51 | + } |
| 52 | + $childData['children'] = $children; |
| 53 | + unset($children); |
| 54 | + } |
| 55 | + $changedStructure[$childSection] = $childData; |
| 56 | + } else { |
| 57 | + $moveInstructions = $this->getMoveInstructions($childSection, $childData); |
| 58 | + if (!empty($moveInstructions)) { |
| 59 | + foreach ($moveInstructions as $moveInstruction) { |
| 60 | + unset($childData['children'][$moveInstruction['section']]); |
| 61 | + unset($moveInstruction['data']['displayIn']); |
| 62 | + $changedStructure |
| 63 | + [$moveInstruction['parent']] |
| 64 | + ['children'] |
| 65 | + [$moveInstruction['section']] = $moveInstruction['data']; |
| 66 | + } |
| 67 | + } |
| 68 | + if (!isset($moveInstructions[$childSection])) { |
| 69 | + $changedStructure['other_payment_methods']['children'][$childSection] = $childData; |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return $changedStructure; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Recursively collect groups that should be moved to special section |
| 79 | + * |
| 80 | + * @param string $section |
| 81 | + * @param array $data |
| 82 | + * @return array |
| 83 | + */ |
| 84 | + private function getMoveInstructions($section, $data) |
| 85 | + { |
| 86 | + $moved = []; |
| 87 | + |
| 88 | + if (array_key_exists('children', $data)) { |
| 89 | + foreach ($data['children'] as $childSection => $childData) { |
| 90 | + $movedChildren = $this->getMoveInstructions($childSection, $childData); |
| 91 | + if (isset($movedChildren[$childSection])) { |
| 92 | + unset($data['children'][$childSection]); |
| 93 | + } |
| 94 | + $moved = array_merge($moved, $movedChildren); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + if (isset($data['displayIn']) && in_array($data['displayIn'], self::$specialGroups)) { |
| 99 | + $moved = array_merge( |
| 100 | + [ |
| 101 | + $section => [ |
| 102 | + 'parent' => $data['displayIn'], |
| 103 | + 'section' => $section, |
| 104 | + 'data' => $data |
| 105 | + ] |
| 106 | + ], |
| 107 | + $moved |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + return $moved; |
| 112 | + } |
| 113 | +} |
0 commit comments