|
1
|
|
|
<?php |
|
2
|
|
|
namespace app\modules\shop\widgets; |
|
3
|
|
|
|
|
4
|
|
|
use Yii; |
|
5
|
|
|
use yii\base\Widget; |
|
6
|
|
|
use yii\helpers\Html; |
|
7
|
|
|
use yii\helpers\Url; |
|
8
|
|
|
use kartik\icons\Icon; |
|
9
|
|
|
use app\modules\shop\assets\PacketPriceEditAsset; |
|
10
|
|
|
use app\modules\shop\models\Currency; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Packet edit the prices |
|
14
|
|
|
* @param $context string |
|
15
|
|
|
* @param $btnHtmlOptions [] |
|
16
|
|
|
* @param $gridSelector string |
|
17
|
|
|
* @param $modalFormId string |
|
18
|
|
|
* @param $modalView string |
|
19
|
|
|
*/ |
|
20
|
|
|
class PacketPriceEditButton extends Widget |
|
21
|
|
|
{ |
|
22
|
|
|
const PACKET_PRICE_EDIT = 'edit_prices'; |
|
23
|
|
|
|
|
24
|
|
|
public $context; |
|
25
|
|
|
public $btnHtmlOptions = []; |
|
26
|
|
|
public $gridSelector = '.grid-view'; |
|
27
|
|
|
public $modalFormId = 'packet-price-edit-form'; |
|
28
|
|
|
public $modalView = '@app/modules/shop/widgets/views/PacketPriceEditModal'; |
|
29
|
|
|
|
|
30
|
|
|
public function init() |
|
31
|
|
|
{ |
|
32
|
|
|
$this->btnHtmlOptions['data-action'] = self::PACKET_PRICE_EDIT; |
|
33
|
|
|
if (!isset($this->btnHtmlOptions['class'])) |
|
34
|
|
|
$this->btnHtmlOptions['class'] = 'btn btn-default'; |
|
35
|
|
|
|
|
36
|
|
|
$this->regJS(); |
|
37
|
|
|
PacketPriceEditAsset::register($this->view); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function run() |
|
41
|
|
|
{ |
|
42
|
|
|
// render modal form |
|
43
|
|
|
$modal = $this->renderModal(); |
|
44
|
|
|
|
|
45
|
|
|
// render button |
|
46
|
|
|
$button = Html::button( |
|
47
|
|
|
Icon::show('usd') . ' ' . |
|
48
|
|
|
\Yii::t('app', 'Edit prices'), |
|
49
|
|
|
$this->btnHtmlOptions |
|
50
|
|
|
); |
|
51
|
|
|
|
|
52
|
|
|
return $button . "\n" . $modal; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
protected function renderModal() { |
|
|
|
|
|
|
56
|
|
|
$currencies = []; |
|
57
|
|
|
$arCurrency = Currency::find()->orderBy(['is_main' => SORT_DESC])->all(); |
|
58
|
|
|
foreach ($arCurrency as $currency) |
|
59
|
|
|
$currencies[$currency->id] = $currency->iso_code; |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
return $this->view->render($this->modalView, [ |
|
63
|
|
|
'modalFormId' => $this->modalFormId, |
|
64
|
|
|
'currencies' => $currencies, |
|
65
|
|
|
'contextId' => $this->context |
|
66
|
|
|
]); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
protected function regJS() { |
|
70
|
|
|
$lang = [ |
|
71
|
|
|
'confirm_edit' => Yii::t('app', 'The action is irreversible. Do you want to continue?'), |
|
72
|
|
|
'wait' => Yii::t('app', 'Waiting'), |
|
73
|
|
|
'total' => Yii::t('app', 'Total items'), |
|
74
|
|
|
'updated' => Yii::t('app', 'Updated at'), |
|
75
|
|
|
'missed' => Yii::t('app', 'Missed (difference between the currencies)'), |
|
76
|
|
|
'errors' => Yii::t('app', 'Number of errors'), |
|
77
|
|
|
]; |
|
78
|
|
|
|
|
79
|
|
|
$this->view->registerJs(" |
|
80
|
|
|
$(\"button[data-action='" . self::PACKET_PRICE_EDIT . "']\").click(function(){ |
|
81
|
|
|
var items = $('{$this->gridSelector}').yiiGridView('getSelectedRows'); |
|
82
|
|
|
if (items.length) { |
|
83
|
|
|
var modal = $('#{$this->modalFormId}'), |
|
84
|
|
|
action = $(this).data('action'); |
|
85
|
|
|
|
|
86
|
|
|
modal.data('url', '" . Url::toRoute(['packet-price-edit']) . "') |
|
87
|
|
|
.data('items', items) |
|
88
|
|
|
.data('price-edit-action', action) |
|
89
|
|
|
.data('lang', " . json_encode($lang) . ") |
|
90
|
|
|
.modal('show'); |
|
91
|
|
|
} |
|
92
|
|
|
return false; |
|
93
|
|
|
}); |
|
94
|
|
|
"); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.