Installments.php 4.22 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
<?php
/**
 *                       ######
 *                       ######
 * ############    ####( ######  #####. ######  ############   ############
 * #############  #####( ######  #####. ######  #############  #############
 *        ######  #####( ######  #####. ######  #####  ######  #####  ######
 * ###### ######  #####( ######  #####. ######  #####  #####   #####  ######
 * ###### ######  #####( ######  #####. ######  #####          #####  ######
 * #############  #############  #############  #############  #####  ######
 *  ############   ############  #############   ############  #####  ######
 *                                      ######
 *                               #############
 *                               ############
 *
 * Adyen Payment module (https://www.adyen.com/)
 *
 * Copyright (c) 2015 Adyen BV (https://www.adyen.com/)
 * See LICENSE.txt for license details.
 *
 * Author: Adyen <magento@adyen.com>
 */

namespace Adyen\Payment\Block\Adminhtml\System\Config\Field;

class Installments extends \Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray
{

    /**
     * @var \Adyen\Payment\Block\Adminhtml\System\Config\Field\Installment
     */
    protected $_installmentRenderer = null;

34

35
    /**
36
     * @var \Adyen\Payment\Block\Adminhtml\System\Config\Field\Cctypes
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
     */
    protected $_ccTypesRenderer = null;

    /**
     * Return renderer for installments
     *
     * @return Installment|\Magento\Framework\View\Element\BlockInterface
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    protected function getNumberOfInstallmentsRenderer()
    {
        if (!$this->_installmentRenderer) {
            $this->_installmentRenderer = $this->getLayout()->createBlock(
                '\Adyen\Payment\Block\Adminhtml\System\Config\Field\Installment',
                '',
                ['data' => ['is_render_to_js_template' => true]]
            );
        }
        return $this->_installmentRenderer;
    }

    /**
     * Returns renderer for country element
60
     *
61 62
     * @return \Magento\Framework\View\Element\BlockInterface|null
     * @throws \Magento\Framework\Exception\LocalizedException
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
     */
    protected function getCcTypesRenderer()
    {
        if (!$this->_ccTypesRenderer) {
            $this->_ccTypesRenderer = $this->getLayout()->createBlock(
                '\Adyen\Payment\Block\Adminhtml\System\Config\Field\Cctypes',
                '',
                ['data' => ['is_render_to_js_template' => true]]
            );
        }
        return $this->_ccTypesRenderer;
    }

    /**
     * Prepare to render
     * @return void
     */
    protected function _prepareToRender()
    {
        $this->addColumn(
            'amount',
            [
Alessio Zampatti's avatar
Alessio Zampatti committed
85
                'label'     => __('Amount Range'),
86 87 88 89 90 91
                'renderer'  => false,
            ]
        );
        $this->addColumn(
            'installments',
            [
Alessio Zampatti's avatar
Alessio Zampatti committed
92
                'label'     => __('Number Of Installments'),
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
                'renderer'  => $this->getNumberOfInstallmentsRenderer(),
            ]
        );
        $this->addColumn(
            'cc_types',
            [
                'label' => __('Allowed Credit Card Types'),
                'renderer'  => $this->getCcTypesRenderer(),
            ]
        );
        $this->_addAfter = false;
        $this->_addButtonLabel = __('Add Rule');
    }

    /**
     * Prepare existing row data object
     *
     * @param \Magento\Framework\DataObject $row
111
     * @throws \Magento\Framework\Exception\LocalizedException
112 113 114
     */
    protected function _prepareArrayRow(\Magento\Framework\DataObject $row)
    {
Alessio Zampatti's avatar
Alessio Zampatti committed
115
        $installments = $row->getInstallments();
116 117

        $options = [];
Alessio Zampatti's avatar
Alessio Zampatti committed
118 119
        if ($installments) {
            $options['option_' . $this->getNumberOfInstallmentsRenderer()->calcOptionHash($installments)]
120 121 122 123 124 125 126 127 128 129 130 131 132 133
                = 'selected="selected"';

            $ccTypes = $row->getCcTypes();
            if (!is_array($ccTypes)) {
                $ccTypes = [$ccTypes];
            }
            foreach ($ccTypes as $cardType) {
                $options['option_' . $this->getCcTypesRenderer()->calcOptionHash($cardType)]
                    = 'selected="selected"';
            }
        }
        $row->setData('option_extra_attrs', $options);
    }
}