/** * Adds or updates model item in the models storage * * @param Currency | CurrencyRateProvider $toUpdate * @throws InvalidParamException * @return bool */ public static function updateStorage($toUpdate) { if (false === $toUpdate instanceof BaseFileModel) { throw new InvalidParamException(Yii::t('dotplant.currencies', 'Method {method} expects instance of {model}. {type} given', ['method' => __METHOD__, 'model' => BaseFileModel::className(), 'type' => gettype($toUpdate)])); } $current = CurrenciesModule::module()->getData($toUpdate::className(), true, false); $current[$toUpdate->name] = $toUpdate->attributes; return self::generateStorage($current, $toUpdate->getStorage(), $toUpdate::className()); }
public function testGenerateStorageDefaultData() { $storage = (new Currency())->getStorage(); FunctionalHelper::flushStorage($storage); $this->assertFileNotExists($storage); CurrenciesModule::module()->getData(Currency::className()); $this->assertFileExists($storage); $a = []; if (true === file_exists($storage)) { $a = (include $storage); } $this->assertEquals($a, CurrenciesModule::module()->getDefaults(Currency::className())); return $storage; }
public function actionChange() { $isoCode = Yii::$app->request->post('isoCode'); if ($isoCode === null) { throw new BadRequestHttpException(); } $newUserCurrency = CurrencyHelper::findCurrencyByIso($isoCode); if ($newUserCurrency !== null) { $oldUserCurrency = CurrencyHelper::getUserCurrency(); CurrencyHelper::setUserCurrency($newUserCurrency); $event = new AfterUserCurrencyChangeEvent(); $event->oldUserCurrency = $oldUserCurrency; $event->newUserCurrency = $newUserCurrency; CurrenciesModule::module()->trigger(CurrenciesModule::AFTER_USER_CURRENCY_CHANGE, $event); } return $this->redirect(Yii::$app->request->referrer); }
/** * Find all CurrencyRateProvider and placing them into cache * * @return array Currency */ public static function findAll() { if (0 == count(static::$models)) { $models = Yii::$app->cache->get(static::$cacheKey); if (false === $models) { $models = []; $items = CurrenciesModule::module()->getData(static::className(), true, false); foreach ($items as $item) { $model = new static(); $model->setDefaults(); $model->setAttributes($item); $models[$model->name] = $model; } if (false === empty($models)) { Yii::$app->cache->set(static::$cacheKey, $models, 86400); } } static::$models = $models; } return static::$models; }
/** * @inheritdoc */ public function init() { parent::init(); self::$cacheKey = CurrenciesModule::module()->providersCacheKey; self::$storage = Yii::getAlias(CurrenciesModule::module()->providersStorage); }
/** * @inheritdoc */ public function actions() { return ['edit' => ['class' => ItemEditAction::className(), 'className' => Currency::className(), 'itemName' => Yii::t('dotplant.currencies', 'currency'), 'editView' => '@vendor/dotplant/currencies/src/views/currency-edit', 'storage' => CurrenciesModule::module()->currenciesStorage], 'delete' => ['class' => ItemDeleteAction::className(), 'className' => Currency::className(), 'itemName' => Yii::t('dotplant.currencies', 'currency'), 'storage' => CurrenciesModule::module()->currenciesStorage], 'reset' => ['class' => ResetAction::className(), 'className' => Currency::className(), 'itemName' => Yii::t('dotplant.currencies', 'currencies'), 'storage' => CurrenciesModule::module()->currenciesStorage]]; }
/** * Returns array of module configuration that should be stored in application config. * Array should be ready to merge in app config. * Used both for web and console. * * @return array */ public function commonApplicationAttributes() { return ['components' => ['i18n' => ['translations' => ['dotplant.currencies' => ['class' => 'yii\\i18n\\PhpMessageSource', 'basePath' => dirname(__DIR__) . DIRECTORY_SEPARATOR . 'messages']]]], 'modules' => ['currencies' => ['class' => CurrenciesModule::className(), 'currenciesStorage' => $this->currenciesStorage, 'currenciesCacheKey' => $this->currenciesCacheKey, 'providersStorage' => $this->providersStorage, 'providersCacheKey' => $this->providersCacheKey]]]; }
<?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')]]]]]);
<?php use DotPlant\Currencies\CurrenciesModule; use DotPlant\Currencies\models\Currency; use DevGroup\AdminUtils\Helper; use yii\grid\GridView; use kartik\icons\Icon; use yii\helpers\Html; $currencies = CurrenciesModule::module()->getData(Currency::className()); $currenciesProvider = new \yii\data\ArrayDataProvider(['allModels' => Currency::findAll(), 'pagination' => ['pageSize' => 10]]); $currencyButtons = Html::tag('div', Html::a(Icon::show('plus') . ' ' . Yii::t('dotplant.currencies', 'Add currency'), ['/currencies/currencies-manage/edit', 'returnUrl' => Helper::returnUrl()], ['role' => 'button', 'class' => 'btn btn-success']) . Html::a(Icon::show('eraser') . ' ' . Yii::t('dotplant.currencies', 'Reset currencies'), ['/currencies/currencies-manage/reset', 'returnUrl' => Helper::returnUrl()], ['role' => 'button', 'class' => 'btn btn-danger']), ['class' => 'btn-group pull-right', 'role' => 'group', 'aria-label' => 'Currencies 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-list', 'dataProvider' => $currenciesProvider, 'layout' => str_replace('[button]', $currencyButtons, $gridTpl), 'tableOptions' => ['class' => 'table table-bordered table-hover table-responsive'], 'columns' => ['name', 'iso_code', ['attribute' => 'is_main', 'content' => function ($data) { return Yii::$app->formatter->asBoolean($data->is_main);