-
Notifications
You must be signed in to change notification settings - Fork 251
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
478 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
|
||
namespace app\backend\actions; | ||
|
||
use app; | ||
use yii; | ||
use yii\base\Action; | ||
use yii\base\InvalidConfigException; | ||
use yii\helpers\Json; | ||
|
||
/** | ||
* Universal action for editable updates | ||
* | ||
* How to use: | ||
* | ||
* Add to your controller's action: | ||
* | ||
* ``` | ||
* | ||
* | ||
* ``` | ||
* | ||
* Allowed attributes is the array of attribute name as key and callable as value. | ||
* Callable is the function that returns the result of editable change. | ||
* | ||
* @package app\backend\actions | ||
*/ | ||
class UpdateEditable extends Action | ||
{ | ||
/** | ||
* @var string Model name, ie. `Product::className()` | ||
*/ | ||
public $modelName = null; | ||
|
||
public $allowedAttributes = []; | ||
|
||
public function init() | ||
{ | ||
if (!isset($this->modelName)) { | ||
throw new InvalidConfigException("Model name should be set in controller actions"); | ||
} | ||
if (!class_exists($this->modelName)) { | ||
throw new InvalidConfigException("Model class does not exists"); | ||
} | ||
|
||
$newAllowedAttributes = []; | ||
foreach ($this->allowedAttributes as $key => $value) { | ||
if (is_callable($value) === true) { | ||
$newAllowedAttributes[$key] = $value; | ||
|
||
} else { | ||
$newAllowedAttributes[$value] = | ||
function(yii\db\ActiveRecord $model, $attribute) { | ||
return $model->getAttribute($attribute); | ||
}; | ||
} | ||
} | ||
$this->allowedAttributes = $newAllowedAttributes; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function run() | ||
{ | ||
/** @var \yii\db\ActiveRecord $modelName fake type for PHPStorm (: */ | ||
$modelName = $this->modelName; | ||
|
||
if (Yii::$app->request->post('hasEditable')) { | ||
$modelId = Yii::$app->request->post('editableKey'); | ||
$model = $modelName::findOne($modelId); | ||
|
||
if ($model === null) { | ||
throw new yii\web\NotFoundHttpException; | ||
} | ||
|
||
$formName = $model->formName(); | ||
|
||
$out = Json::encode(['output'=>'', 'message'=>'']); | ||
|
||
$post = []; | ||
$posted = current($_POST[$formName]); | ||
$post[$formName] = $posted; | ||
|
||
// load model like any single model validation | ||
if ($model->load($post)) { | ||
// can save model or do something before saving model | ||
$model->save(); | ||
if ($model->hasMethod('invalidateTags')) { | ||
$model->invalidateTags(); | ||
} | ||
|
||
$output = ''; | ||
|
||
|
||
foreach ($this->allowedAttributes as $attribute=>$callable) { | ||
if (isset($posted[$attribute])) { | ||
$output = call_user_func($callable, $model, $attribute); | ||
|
||
break; | ||
} | ||
} | ||
|
||
$out = Json::encode(['output'=>$output, 'message'=>'']); | ||
|
||
} | ||
echo $out; | ||
|
||
} | ||
return; | ||
} | ||
} |
110 changes: 110 additions & 0 deletions
110
application/backend/controllers/CurrenciesController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php | ||
|
||
namespace app\backend\controllers; | ||
|
||
use app\backend\actions\DeleteOne; | ||
use app\backend\actions\MultipleDelete; | ||
use app\backend\actions\UpdateEditable; | ||
use app\models\Currency; | ||
use Yii; | ||
use yii\filters\AccessControl; | ||
use yii\web\Controller; | ||
|
||
class CurrenciesController extends Controller | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function behaviors() | ||
{ | ||
return [ | ||
'access' => [ | ||
'class' => AccessControl::className(), | ||
'rules' => [ | ||
[ | ||
'allow' => true, | ||
'roles' => ['content manage'], | ||
], | ||
], | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function actions() | ||
{ | ||
return [ | ||
'remove-all' => [ | ||
'class' => MultipleDelete::className(), | ||
'modelName' => Currency::className(), | ||
], | ||
'delete' => [ | ||
'class' => DeleteOne::className(), | ||
'modelName' => Currency::className(), | ||
], | ||
'update-editable' => [ | ||
'class' => UpdateEditable::className(), | ||
'modelName' => Currency::className(), | ||
'allowedAttributes' => [ | ||
'currency_rate_provider_id' => function(Currency $model, $attribute) { | ||
if ($model === null || $model->rateProvider === null || $model->currency_rate_provider_id===0) { | ||
return null; | ||
} | ||
return \yii\helpers\Html::tag('div', $model->rateProvider->name, ['class' => $model->rateProvider->name]); | ||
}, | ||
], | ||
], | ||
]; | ||
} | ||
|
||
public function actionIndex() | ||
{ | ||
$searchModel = new Currency(); | ||
$dataProvider = $searchModel->search($_GET); | ||
|
||
return $this->render( | ||
'index', | ||
[ | ||
'dataProvider' => $dataProvider, | ||
'searchModel' => $searchModel, | ||
] | ||
); | ||
} | ||
|
||
public function actionEdit($id = null) | ||
{ | ||
$model = new Currency; | ||
$model->loadDefaultValues(); | ||
|
||
if ($id !== null) { | ||
$model = Currency::findOne($id); | ||
} | ||
|
||
|
||
|
||
$post = \Yii::$app->request->post(); | ||
|
||
if ($model->load($post) && $model->validate() && !isset($_GET['Currency'])) { | ||
|
||
$save_result = $model->save(); | ||
if ($save_result) { | ||
Yii::$app->session->setFlash('info', Yii::t('app', 'Object saved')); | ||
return $this->redirect(['/backend/currencies/edit', 'id' => $model->id]); | ||
} else { | ||
\Yii::$app->session->setFlash('error', Yii::t('app', 'Cannot update data')); | ||
} | ||
|
||
|
||
} | ||
|
||
return $this->render( | ||
'currency-form', | ||
[ | ||
'model' => $model, | ||
] | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
/* @var $this yii\web\View */ | ||
/* @var $model app\models\Currency */ | ||
|
||
use app\backend\widgets\BackendWidget; | ||
use kartik\helpers\Html; | ||
use kartik\icons\Icon; | ||
use kartik\widgets\ActiveForm; | ||
|
||
$this->title = $model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'); | ||
$this->params['breadcrumbs'] = [ | ||
['label' => Yii::t('app', 'Currencies'), 'url' => ['index']], | ||
$this->params['breadcrumbs'][] = $this->title, | ||
]; | ||
|
||
?> | ||
<?php $form = ActiveForm::begin(['type'=>ActiveForm::TYPE_VERTICAL]); ?> | ||
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6"> | ||
|
||
<?php | ||
BackendWidget::begin( | ||
[ | ||
'icon' => 'gear', | ||
'title'=> Yii::t('app', 'Currency'), | ||
'footer' => Html::submitButton( | ||
Icon::show('save') . Yii::t('app', 'Save'), | ||
['class' => 'btn btn-primary'] | ||
), | ||
] | ||
); | ||
?> | ||
<?= $form->field($model, 'name')->textInput(['maxlength' => 255]) ?> | ||
<?= $form->field($model, 'iso_code')->textInput(['maxlength' => 4]) ?> | ||
<?= $form->field($model, 'is_main')->textInput()->widget(\kartik\widgets\SwitchInput::className()) ?> | ||
<?= $form->field($model, 'convert_nominal') ?> | ||
<?= $form->field($model, 'currency_rate_provider_id')->dropDownList( | ||
[0=>'-']+app\components\Helper::getModelMap(\app\models\CurrencyRateProvider::className(), 'id', 'name') | ||
) ?> | ||
<?= $form->field( | ||
$model, | ||
'convert_rate', | ||
[ | ||
'addon' => [ | ||
'append' => [ | ||
'content' => | ||
Html::a( | ||
Icon::show('question-circle'), | ||
'#', | ||
[ | ||
'data-toggle' => 'popover', | ||
'data-trigger' => 'focus', | ||
'data-content' => Yii::t('app', 'Convert rate is updated automatically if currency rate provider is set and includes additional rate and nominal.'), | ||
] | ||
) | ||
], | ||
], | ||
] | ||
) ?> | ||
|
||
<?= $form->field($model, 'additional_rate') ?> | ||
<?= $form->field($model, 'additional_nominal') ?> | ||
|
||
<?= $form->field($model, 'sort_order') ?> | ||
|
||
|
||
<?php BackendWidget::end(); ?> | ||
|
||
</div> | ||
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6"> | ||
|
||
<?php | ||
BackendWidget::begin( | ||
[ | ||
'icon' => 'gear', | ||
'title'=> Yii::t('app', 'Currency formatting'), | ||
'footer' => Html::submitButton( | ||
Icon::show('save') . Yii::t('app', 'Save'), | ||
['class' => 'btn btn-primary'] | ||
), | ||
] | ||
); | ||
?> | ||
<?= $form->field($model, 'intl_formatting')->textInput()->widget(\kartik\widgets\SwitchInput::className()) ?> | ||
<?= $form->field($model, 'min_fraction_digits') ?> | ||
<?= $form->field($model, 'max_fraction_digits') ?> | ||
<?= $form->field($model, 'dec_point') ?> | ||
<?= $form->field($model, 'thousands_sep')->dropDownList([ | ||
'' => 'Don\'t separate', | ||
' ' => 'Space', | ||
'.' => 'Dot', | ||
',' => 'Dash', | ||
]) ?> | ||
<?= $form->field($model, 'format_string') ?> | ||
<?php BackendWidget::end(); ?> | ||
|
||
</div> | ||
<?php ActiveForm::end(); ?> | ||
|
||
|
||
<div class="clearfix"></div> |
Oops, something went wrong.