Skip to content

Commit

Permalink
Currencies RBAC - #147
Browse files Browse the repository at this point in the history
  • Loading branch information
bethrezen committed Feb 24, 2015
1 parent 5ca1563 commit 427d002
Show file tree
Hide file tree
Showing 10 changed files with 478 additions and 14 deletions.
112 changes: 112 additions & 0 deletions application/backend/actions/UpdateEditable.php
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 application/backend/controllers/CurrenciesController.php
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,
]
);
}

}
4 changes: 4 additions & 0 deletions application/backend/controllers/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ private function processEditable()
$productId = Yii::$app->request->post('editableKey');
$model = Product::findOne($productId);

if ($model === null) {
throw new NotFoundHttpException;
}

// store a default json response as desired by editable
$out = Json::encode(['output'=>'', 'message'=>'']);

Expand Down
101 changes: 101 additions & 0 deletions application/backend/views/currencies/currency-form.php
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>
Loading

0 comments on commit 427d002

Please sign in to comment.