|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Finance module for HiPanel |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/hiqdev/hipanel-module-finance |
|
6
|
|
|
* @package hipanel-module-finance |
|
7
|
|
|
* @license BSD-3-Clause |
|
8
|
|
|
* @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace hipanel\modules\finance\widgets; |
|
12
|
|
|
|
|
13
|
|
|
use hipanel\modules\finance\models\ExchangeRate; |
|
14
|
|
|
use hipanel\widgets\ArraySpoiler; |
|
15
|
|
|
use Tuck\Sort\Sort; |
|
16
|
|
|
use Yii; |
|
17
|
|
|
use yii\base\Widget; |
|
18
|
|
|
use yii\helpers\Html; |
|
19
|
|
|
|
|
20
|
|
|
class ExchangeRatesLine extends Widget |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var ExchangeRate[] |
|
24
|
|
|
*/ |
|
25
|
|
|
public $rates; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var string[] pairs that should be shown first (if exist) |
|
29
|
|
|
*/ |
|
30
|
|
|
public $priorityPairCodes = [ |
|
31
|
|
|
'USD/EUR', |
|
32
|
|
|
'EUR/USD', |
|
33
|
|
|
'USB/BTC', |
|
34
|
|
|
]; |
|
35
|
|
|
|
|
36
|
|
|
public function run() |
|
37
|
|
|
{ |
|
38
|
|
|
if (!Yii::$app->user->can('manage') || empty($this->rates)) { |
|
39
|
|
|
return ''; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
return $this->renderLine(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param ExchangeRate[] $rates not sorted rates array |
|
47
|
|
|
* @return ExchangeRate[] sorted rates |
|
48
|
|
|
*/ |
|
49
|
|
|
private function sortRates(array $rates): array |
|
50
|
|
|
{ |
|
51
|
|
|
$chain = Sort::chain() |
|
52
|
|
|
->asc(function (ExchangeRate $rate) { |
|
53
|
|
|
$pos = array_search($rate->pairCode(), $this->priorityPairCodes, true); |
|
54
|
|
|
|
|
55
|
|
|
return $pos !== false ? $pos : INF; |
|
56
|
|
|
}) |
|
57
|
|
|
->compare(function (ExchangeRate $a, ExchangeRate $b) { |
|
58
|
|
|
return strnatcasecmp($a->pairCode(), $b->pairCode()); |
|
59
|
|
|
}); |
|
60
|
|
|
|
|
61
|
|
|
return $chain->values($rates); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
protected function renderLine() |
|
65
|
|
|
{ |
|
66
|
|
|
return Html::tag('span', ArraySpoiler::widget([ |
|
67
|
|
|
'data' => $this->sortRates($this->rates), |
|
68
|
|
|
'visibleCount' => 3, |
|
69
|
|
|
'button' => [ |
|
70
|
|
|
'label' => '+{count}', |
|
71
|
|
|
'popoverOptions' => ['html' => true], |
|
72
|
|
|
], |
|
73
|
|
|
'hiddenDelimiter' => '<br />', |
|
74
|
|
|
'formatter' => function (ExchangeRate $model) { |
|
75
|
|
|
return Html::tag('span', $model->pairCode(), ['style' => 'font-weight: 400']) . ': ' . $model->rate; |
|
76
|
|
|
}, |
|
77
|
|
|
]), ['style' => 'padding-left: 20px; color: #737272;']); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|