Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Admin.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
14 {
18  protected $_salesConfig;
19 
23  protected $_storeManager;
24 
28  protected $priceCurrency;
29 
33  protected $escaper;
34 
42  public function __construct(
43  \Magento\Framework\App\Helper\Context $context,
45  \Magento\Sales\Model\Config $salesConfig,
47  \Magento\Framework\Escaper $escaper
48  ) {
49  $this->priceCurrency = $priceCurrency;
50  $this->_storeManager = $storeManager;
51  $this->_salesConfig = $salesConfig;
52  $this->escaper = $escaper;
53  parent::__construct($context);
54  }
55 
65  public function displayPriceAttribute($dataObject, $code, $strong = false, $separator = '<br/>')
66  {
67  // Fix for 'bs_customer_bal_total_refunded' attribute
68  $baseValue = $dataObject->hasData('bs_' . $code)
69  ? $dataObject->getData('bs_' . $code)
70  : $dataObject->getData('base_' . $code);
71  return $this->displayPrices(
72  $dataObject,
73  $baseValue,
74  $dataObject->getData($code),
75  $strong,
76  $separator
77  );
78  }
79 
90  public function displayPrices($dataObject, $basePrice, $price, $strong = false, $separator = '<br/>')
91  {
92  if ($dataObject instanceof \Magento\Sales\Model\Order) {
93  $order = $dataObject;
94  } else {
95  $order = $dataObject->getOrder();
96  }
97 
98  if ($order && $order->isCurrencyDifferent()) {
99  $res = '<strong>';
100  $res .= $order->formatBasePrice($basePrice);
101  $res .= '</strong>' . $separator;
102  $res .= '[' . $order->formatPrice($price) . ']';
103  } elseif ($order) {
104  $res = $order->formatPrice($price);
105  if ($strong) {
106  $res = '<strong>' . $res . '</strong>';
107  }
108  } else {
109  $res = $this->priceCurrency->format($price);
110  if ($strong) {
111  $res = '<strong>' . $res . '</strong>';
112  }
113  }
114  return $res;
115  }
116 
124  {
125  $productTypes = $this->_salesConfig->getAvailableProductTypes();
126  foreach ($collection->getItems() as $key => $item) {
127  if ($item instanceof \Magento\Catalog\Model\Product) {
128  $type = $item->getTypeId();
129  } elseif ($item instanceof \Magento\Sales\Model\Order\Item) {
130  $type = $item->getProductType();
131  } elseif ($item instanceof \Magento\Quote\Model\Quote\Item) {
132  $type = $item->getProductType();
133  } else {
134  $type = '';
135  }
136  if (!in_array($type, $productTypes)) {
137  $collection->removeItemByKey($key);
138  }
139  }
140  return $collection;
141  }
142 
150  public function escapeHtmlWithLinks($data, $allowedTags = null)
151  {
152  if (!empty($data) && is_array($allowedTags) && in_array('a', $allowedTags)) {
153  $links = [];
154  $i = 1;
155  $data = str_replace('%', '%%', $data);
156  $regexp = "#(?J)<a"
157  ."(?:(?:\s+(?:(?:href\s*=\s*(['\"])(?<link>.*?)\\1\s*)|(?:\S+\s*=\s*(['\"])(.*?)\\3)\s*)*)|>)"
158  .">?(?:(?:(?<text>.*?)(?:<\/a\s*>?|(?=<\w))|(?<text>.*)))#si";
159  while (preg_match($regexp, $data, $matches)) {
160  $text = '';
161  if (!empty($matches['text'])) {
162  $text = str_replace('%%', '%', $matches['text']);
163  }
164  $url = $this->filterUrl($matches['link'] ?? '');
165  //Recreate a minimalistic secure a tag
166  $links[] = sprintf(
167  '<a href="%s">%s</a>',
168  htmlspecialchars($url, ENT_QUOTES, 'UTF-8', false),
169  $this->escaper->escapeHtml($text)
170  );
171  $data = str_replace($matches[0], '%' . $i . '$s', $data);
172  ++$i;
173  }
174  $data = $this->escaper->escapeHtml($data, $allowedTags);
175  return vsprintf($data, $links);
176  }
177  return $this->escaper->escapeHtml($data, $allowedTags);
178  }
179 
186  private function filterUrl(string $url): string
187  {
188  if ($url) {
189  //Revert the sprintf escaping
190  $url = str_replace('%%', '%', $url);
191  $urlScheme = parse_url($url, PHP_URL_SCHEME);
192  $urlScheme = $urlScheme ? strtolower($urlScheme) : '';
193  if ($urlScheme !== 'http' && $urlScheme !== 'https') {
194  $url = null;
195  }
196  }
197 
198  if (!$url) {
199  $url = '#';
200  }
201 
202  return $url;
203  }
204 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
escapeHtmlWithLinks($data, $allowedTags=null)
Definition: Admin.php:150
$order
Definition: order.php:55
$storeManager
endifif( $block->getLastPageNum()>1)( 'Page') ?></strong >< ul class $text
Definition: pager.phtml:43
$price
displayPriceAttribute($dataObject, $code, $strong=false, $separator='< br/>')
Definition: Admin.php:65
$type
Definition: item.phtml:13
displayPrices($dataObject, $basePrice, $price, $strong=false, $separator='< br/>')
Definition: Admin.php:90
__construct(\Magento\Framework\App\Helper\Context $context, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Sales\Model\Config $salesConfig, \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency, \Magento\Framework\Escaper $escaper)
Definition: Admin.php:42
$i
Definition: gallery.phtml:31
applySalableProductTypesFilter($collection)
Definition: Admin.php:123
$code
Definition: info.phtml:12