/** * Removes given item from storage */ public function run() { if (true === $this->model->delete()) { Yii::$app->session->setFlash('success', mb_convert_case($this->itemName, MB_CASE_TITLE, "UTF-8") . Yii::t('dotplant.currencies', ' {name} successfully deleted!', ['name' => $this->model->name])); return $this->controller->redirect($this->returnUrl); } else { Yii::$app->session->setFlash('error', Yii::t('dotplant.currencies', 'An error occurred while deleting {name}!', ['name' => $this->model->name])); } }
/** * @param CurrencyPair $currencyPair * @return Rate * @throws Exception */ public function fetchRate(CurrencyPair $currencyPair) { $providers = [$this->mainProvider => CurrencyRateProvider::getByName($this->mainProvider), $this->secondProvider => CurrencyRateProvider::getByName($this->secondProvider)]; if (count($providers) !== 2) { throw new Exception('One of providers not found'); } $rates = []; /** @var CurrencyRateProvider $provider */ foreach ($providers as $name => $provider) { if (null === $provider) { throw new Exception("Provider \"{$name}\" not found!"); } try { $providerHandler = $provider->getImplementationInstance($this->httpAdapter); if ($providerHandler !== null) { $swap = new Swap($providerHandler); $rate = $swap->quote($currencyPair->getBaseCurrency() . '/' . $currencyPair->getQuoteCurrency())->getValue(); $rates[] = floatval($rate); } else { throw new Exception('Provider "' . $provider->name . '" not found'); } } catch (\Exception $e) { throw new Exception('One or more currency providers did not return result'); } } $min = min($rates); $max = max($rates); return new Rate($max - $min >= $max * $this->criticalDifference / 100 ? $max : $rates[0], new \DateTime()); }
/** * @inheritdoc */ public function getFooter() { $deleteButton = $this->model->isNewItem() ? '' : Html::a(Icon::show('trash-o') . ' ' . Yii::t('dotplant.currencies', 'Delete'), [$this->controller->id . '/delete', 'id' => $this->model->name, 'returnUrl' => $this->returnUrl], ['class' => 'btn btn-danger']); return Html::tag('div', $deleteButton . Html::a(Yii::t('dotplant.currencies', 'Back'), $this->returnUrl, ['class' => 'btn btn-primary']) . Html::submitButton(Icon::show('floppy-o') . ' ' . Yii::t('dotplant.currencies', 'Save'), ['class' => 'btn btn-primary']), ['class' => 'btn-group pull-right', 'role' => 'group', 'aria-label' => 'Edit buttons']); }
<?php /** * @var \yii\widgets\ActiveForm $form * @var \DotPlant\Currencies\models\Currency $model */ use DotPlant\Currencies\models\CurrencyRateProvider; use kartik\switchinput\SwitchInput; use yii\helpers\ArrayHelper; use kartik\icons\Icon; $providers = ArrayHelper::map(CurrencyRateProvider::findAll(), 'name', 'name'); $switchOptions = [0 => Yii::$app->formatter->asBoolean(0), 1 => Yii::$app->formatter->asBoolean(1)]; ?> <div class="row"> <div class="col-sm-6"> <div class="box box-solid"> <div class="box-header with-border"> <?php echo Icon::show('money'); ?> <h3 class="box-title"><?php echo Yii::t('dotplant.currencies', 'Currency'); ?> </h3> </div> <div class="box-body"> <?php echo $form->field($model, 'is_main')->widget(SwitchInput::classname(), []); ?> <?php echo $form->field($model, 'name')->textInput(['maxlength' => 255, 'disabled' => !$model->isNewItem()]);
/** * Relation to CurrencyRateProvider model * * @return CurrencyRateProvider */ public function getRateProvider() { return CurrencyRateProvider::getByName($this->currency_rate_provider_name); }
/** * @inheritdoc */ public function actions() { return ['edit' => ['class' => ItemEditAction::className(), 'className' => CurrencyRateProvider::className(), 'itemName' => Yii::t('dotplant.currencies', 'currency rate provider'), 'editView' => '@vendor/dotplant/currencies/src/views/provider-edit', 'storage' => CurrenciesModule::module()->providersStorage], 'delete' => ['class' => ItemDeleteAction::className(), 'className' => CurrencyRateProvider::className(), 'itemName' => Yii::t('dotplant.currencies', 'currency rate provider'), 'storage' => CurrenciesModule::module()->providersStorage], 'reset' => ['class' => ResetAction::className(), 'className' => CurrencyRateProvider::className(), 'itemName' => Yii::t('dotplant.currencies', 'currency rate providers'), 'storage' => CurrenciesModule::module()->providersStorage]]; }
<?php use DotPlant\Currencies\models\CurrencyRateProvider; use DotPlant\Currencies\CurrenciesModule; use DevGroup\AdminUtils\Helper; use yii\grid\GridView; use kartik\icons\Icon; use yii\helpers\Html; $currencies = CurrenciesModule::module()->getData(CurrencyRateProvider::className()); $currenciesProvider = new \yii\data\ArrayDataProvider(['allModels' => CurrencyRateProvider::findAll(), 'pagination' => ['pageSize' => 10]]); $currencyButtons = Html::tag('div', Html::a(Icon::show('plus') . ' ' . Yii::t('dotplant.currencies', 'Add provider'), ['/currencies/currency-rate-providers-manage/edit', 'returnUrl' => Helper::returnUrl()], ['role' => 'button', 'class' => 'btn btn-success']) . Html::a(Icon::show('eraser') . ' ' . Yii::t('dotplant.currencies', 'Reset providers'), ['/currencies/currency-rate-providers-manage/reset', 'returnUrl' => Helper::returnUrl()], ['role' => 'button', 'class' => 'btn btn-danger']), ['class' => 'btn-group pull-right', 'role' => 'group', 'aria-label' => 'Providers buttons']); $gridTpl = <<<TPL <div class="box-body"> {items} </div> <div class="box-footer"> <div class="row ext-bottom"> <div class="col-sm-5"> {summary} </div> <div class="col-sm-7"> {pager} </div> </div> <div class="row"> <div class="col-sm-12">[button]</div> </div> </div> TPL; echo GridView::widget(['id' => 'dotplant-currencies-providers-list', 'dataProvider' => $currenciesProvider, 'layout' => str_replace('[button]', $currencyButtons, $gridTpl), 'tableOptions' => ['class' => 'table table-bordered table-hover table-responsive'], 'columns' => ['name', 'class_name', ['class' => 'DevGroup\\AdminUtils\\columns\\ActionColumn', 'options' => ['width' => '95px'], 'buttons' => [['url' => '/currencies/currency-rate-providers-manage/edit', 'icon' => 'pencil', 'class' => 'btn-primary', 'label' => Yii::t('dotplant.currencies', 'Edit')], ['url' => '/currencies/currency-rate-providers-manage/delete', 'icon' => 'trash-o', 'class' => 'btn-danger', 'label' => Yii::t('dotplant.currencies', 'Delete')]]]]]);
/** * @inheritdoc */ public function init() { parent::init(); self::$items = [Currency::className() => [], CurrencyRateProvider::className() => []]; self::$defaults = [Currency::className() => self::$defaultCurrencies, CurrencyRateProvider::className() => self::$defaultProviders]; }