1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* PAYONE Magento 2 Connector is free software: you can redistribute it and/or modify |
5
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
6
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
7
|
|
|
* (at your option) any later version. |
8
|
|
|
* |
9
|
|
|
* PAYONE Magento 2 Connector is distributed in the hope that it will be useful, |
10
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
11
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12
|
|
|
* GNU Lesser General Public License for more details. |
13
|
|
|
* |
14
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
15
|
|
|
* along with PAYONE Magento 2 Connector. If not, see <http://www.gnu.org/licenses/>. |
16
|
|
|
* |
17
|
|
|
* PHP version 5 |
18
|
|
|
* |
19
|
|
|
* @category Payone |
20
|
|
|
* @package Payone_Magento2_Plugin |
21
|
|
|
* @author FATCHIP GmbH <[email protected]> |
22
|
|
|
* @copyright 2003 - 2016 Payone GmbH |
23
|
|
|
* @license <http://www.gnu.org/licenses/> GNU Lesser General Public License |
24
|
|
|
* @link http://www.payone.de |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
namespace Payone\Core\Block\Adminhtml\Config\Form\Field; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Block class for status-mapping grid-element |
31
|
|
|
*/ |
32
|
|
|
class StatusMapping extends \Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* Element factory |
36
|
|
|
* |
37
|
|
|
* @var \Magento\Framework\Data\Form\Element\Factory |
38
|
|
|
*/ |
39
|
|
|
protected $elementFactory; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* List of all possible OrderStatus types |
43
|
|
|
* |
44
|
|
|
* @var \Magento\Sales\Model\Config\Source\Order\Status |
45
|
|
|
*/ |
46
|
|
|
protected $orderStatus; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* List of all possible TransactionStatus types |
50
|
|
|
* |
51
|
|
|
* @var \Payone\Core\Model\Source\TransactionStatus |
52
|
|
|
*/ |
53
|
|
|
protected $transactionStatus; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Constructor |
57
|
|
|
* |
58
|
|
|
* @param \Magento\Backend\Block\Template\Context $context |
59
|
|
|
* @param \Magento\Framework\Data\Form\Element\Factory $elementFactory |
60
|
|
|
* @param \Magento\Sales\Model\Config\Source\Order\Status $orderStatus |
61
|
|
|
* @param \Payone\Core\Model\Source\TransactionStatus $transactionStatus |
62
|
|
|
* @param array $data |
63
|
|
|
*/ |
64
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
65
|
|
|
\Magento\Backend\Block\Template\Context $context, |
66
|
|
|
\Magento\Framework\Data\Form\Element\Factory $elementFactory, |
67
|
|
|
\Magento\Sales\Model\ResourceModel\Order\Status\Collection $orderStatus, |
68
|
|
|
\Payone\Core\Model\Source\TransactionStatus $transactionStatus, |
69
|
|
|
array $data = [] |
70
|
|
|
) { |
71
|
|
|
parent::__construct($context, $data); |
72
|
|
|
$this->elementFactory = $elementFactory; |
73
|
|
|
$this->orderStatus = $orderStatus; |
|
|
|
|
74
|
|
|
$this->transactionStatus = $transactionStatus; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Initialise form fields |
79
|
|
|
* |
80
|
|
|
* @return void |
81
|
|
|
*/ |
82
|
|
View Code Duplication |
protected function _construct() |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
$this->addColumn('txaction', ['label' => __('Transactionstatus-message')]); // set column name for txaction |
85
|
|
|
$this->addColumn('state_status', ['label' => __('Magento-status')]); // set column name for state_status |
86
|
|
|
$this->addAfter = false; // dont add "add after" button |
87
|
|
|
$this->addButtonLabel = __('Add Statusmapping'); // set the label text of the button |
88
|
|
|
parent::_construct(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Render array cell for prototypeJS template |
93
|
|
|
* |
94
|
|
|
* @param string $columnName |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
View Code Duplication |
public function renderCellTemplate($columnName) |
|
|
|
|
98
|
|
|
{ |
99
|
|
|
if ($columnName == 'txaction' && isset($this->_columns[$columnName])) { |
100
|
|
|
$aOptions = $this->transactionStatus->toOptionArray(); // add transction status action options to dropdown |
101
|
|
|
} elseif ($columnName == 'state_status' && isset($this->_columns[$columnName])) { |
102
|
|
|
$aOptions = $this->orderStatus->toOptionArray(); // add state_status options to dropdown |
103
|
|
|
} else { |
104
|
|
|
return parent::renderCellTemplate($columnName); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$oElement = $this->elementFactory->create('select'); |
108
|
|
|
$oElement->setForm($this->getForm()); |
109
|
|
|
$oElement->setName($this->_getCellInputElementName($columnName)); |
110
|
|
|
$oElement->setHtmlId($this->_getCellInputElementId('<%- _id %>', $columnName)); |
111
|
|
|
$oElement->setValues($aOptions); |
112
|
|
|
return str_replace("\n", '', $oElement->getElementHtml()); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Obtain existing data from form element |
117
|
|
|
* |
118
|
|
|
* Each row will be instance of \Magento\Framework\DataObject |
119
|
|
|
* |
120
|
|
|
* @return array |
121
|
|
|
*/ |
122
|
|
|
public function getArrayRows() |
123
|
|
|
{ |
124
|
|
|
$result = []; |
125
|
|
|
/** @var \Magento\Framework\Data\Form\Element\AbstractElement */ |
126
|
|
|
$element = $this->getElement(); |
127
|
|
|
$aValue = $element->getValue(); // get values |
128
|
|
|
if (!is_array($aValue)) { // values given as array? |
129
|
|
|
$aValue = unserialize($aValue); // convert string to array |
130
|
|
|
} |
131
|
|
|
if ($aValue && is_array($aValue)) { |
132
|
|
|
foreach ($aValue as $rowId => $row) { |
133
|
|
|
$rowColumnValues = []; |
134
|
|
|
foreach ($row as $key => $value) { |
135
|
|
|
$row[$key] = $value; |
136
|
|
|
$rowColumnValues[$this->_getCellInputElementId($rowId, $key)] = $row[$key]; // add value the row |
137
|
|
|
} |
138
|
|
|
$row['_id'] = $rowId; |
139
|
|
|
$row['column_values'] = $rowColumnValues; |
140
|
|
|
$result[$rowId] = new \Magento\Framework\DataObject($row); |
141
|
|
|
$this->_prepareArrayRow($result[$rowId]); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
return $result; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
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.