Browse Source

Code reformat 2

master
Egorka 6 years ago
parent
commit
6e48642575
  1. 189
      backend/components/ToggleAction.php
  2. 237
      backend/components/ToggleColumn.php
  3. 60
      backend/components/menu/widgets/MenuEditorWidget.php
  4. 1
      backend/forms/rbac/RbacCreatePermissionForm.php
  5. 1
      backend/forms/rbac/RbacCreateRoleForm.php
  6. 1
      backend/forms/rbac/RbacUpdateChildren.php

189
backend/components/ToggleAction.php

@ -14,100 +14,101 @@ use Yii;
class ToggleAction extends Action class ToggleAction extends Action
{ {
/** /**
* @var string name of the model * @var string name of the model
*/ */
public $modelClass; public $modelClass;
/** /**
* @var string model attribute * @var string model attribute
*/ */
public $attribute = 'active'; public $attribute = 'active';
/** /**
* @var string scenario model * @var string scenario model
*/ */
public $scenario = null; public $scenario = null;
/** /**
* @var string|array additional condition for loading the model * @var string|array additional condition for loading the model
*/ */
public $andWhere; public $andWhere;
/** /**
* @var string|int|boolean|Expression what to set active models to * @var string|int|boolean|Expression what to set active models to
*/ */
public $onValue = 1; public $onValue = 1;
/** /**
* @var string|int|boolean what to set inactive models to * @var string|int|boolean what to set inactive models to
*/ */
public $offValue = 0; public $offValue = 0;
/** /**
* @var bool whether to set flash messages or not * @var bool whether to set flash messages or not
*/ */
public $setFlash = false; public $setFlash = false;
/** /**
* @var string flash message on success * @var string flash message on success
*/ */
public $flashSuccess = "Model saved"; public $flashSuccess = 'Model saved';
/** /**
* @var string flash message on error * @var string flash message on error
*/ */
public $flashError = "Error saving Model"; public $flashError = 'Error saving Model';
/** /**
* @var string|array URL to redirect to * @var string|array URL to redirect to
*/ */
public $redirect; public $redirect;
/** /**
* @var string pk field name * @var string pk field name
*/ */
public $primaryKey = 'id'; public $primaryKey = 'id';
public function run($id) public function run($id)
{ {
if (!Yii::$app->request->getIsPost()) { if (!Yii::$app->request->getIsPost()) {
throw new MethodNotAllowedHttpException(); throw new MethodNotAllowedHttpException();
} }
$id = (int)$id; $id = (int)$id;
$result = null; $result = null;
if (empty($this->modelClass) || !class_exists($this->modelClass)) { if (empty($this->modelClass) || !class_exists($this->modelClass)) {
throw new InvalidConfigException("Model class doesn't exist"); throw new InvalidConfigException("Model class doesn't exist");
} }
/* @var $modelClass \yii\db\ActiveRecord */ /* @var $modelClass \yii\db\ActiveRecord */
$modelClass = $this->modelClass; $modelClass = $this->modelClass;
$attribute = $this->attribute; $attribute = $this->attribute;
$model = $modelClass::find()->where([$this->primaryKey => $id]); $model = $modelClass::find()->where([$this->primaryKey => $id]);
if (!empty($this->andWhere)) { if (!empty($this->andWhere)) {
$model->andWhere($this->andWhere); $model->andWhere($this->andWhere);
} }
$model = $model->one(); $model = $model->one();
if (!is_null($this->scenario)) { if (!is_null($this->scenario)) {
$model->scenario = $this->scenario; $model->scenario = $this->scenario;
} }
if (!$model->hasAttribute($this->attribute)) { if (!$model->hasAttribute($this->attribute)) {
throw new InvalidConfigException("Attribute doesn't exist"); throw new InvalidConfigException("Attribute doesn't exist");
} }
if ($model->$attribute == $this->onValue) { if ($model->$attribute == $this->onValue) {
$model->$attribute = $this->offValue; $model->$attribute = $this->offValue;
} elseif ($this->onValue instanceof Expression && $model->$attribute != $this->offValue) { } elseif ($this->onValue instanceof Expression && $model->$attribute != $this->offValue) {
$model->$attribute = $this->offValue; $model->$attribute = $this->offValue;
} else { } else {
$model->$attribute = $this->onValue; $model->$attribute = $this->onValue;
} }
if ($model->save()) { if ($model->save()) {
if ($this->setFlash) { if ($this->setFlash) {
Yii::$app->session->setFlash('success', $this->flashSuccess); Yii::$app->session->setFlash('success', $this->flashSuccess);
} }
} else { } else {
if ($this->setFlash) { if ($this->setFlash) {
Yii::$app->session->setFlash('error', $this->flashError); Yii::$app->session->setFlash('error', $this->flashError);
} }
} }
if (Yii::$app->request->getIsAjax()) { if (Yii::$app->request->getIsAjax()) {
Yii::$app->end(); Yii::$app->end();
} }
/* @var $controller \yii\web\Controller */ /* @var $controller \yii\web\Controller */
$controller = $this->controller; $controller = $this->controller;
if (!empty($this->redirect)) { if (!empty($this->redirect)) {
return $controller->redirect($this->redirect); return $controller->redirect($this->redirect);
} }
return $controller->redirect(Yii::$app->request->getReferrer());
} return $controller->redirect(Yii::$app->request->getReferrer());
}
} }

237
backend/components/ToggleColumn.php

@ -13,122 +13,88 @@ use Yii;
class ToggleColumn extends DataColumn class ToggleColumn extends DataColumn
{ {
/** /**
* Toggle action that will be used as the toggle action in your controller * Toggle action that will be used as the toggle action in your controller
* @var string * @var string
*/ */
public $action = 'toggle'; public $action = 'toggle';
/** /**
* @var string pk field name * @var string pk field name
*/ */
public $primaryKey = 'primaryKey'; public $primaryKey = 'primaryKey';
/** /**
* Whether to use ajax or not * Whether to use ajax or not
* @var bool * @var bool
*/ */
public $enableAjax = true; public $enableAjax = true;
/** /**
* @var string glyphicon for 'on' value * @var string glyphicon for 'on' value
*/ */
public $iconOn = 'ok'; public $iconOn = 'ok';
/** /**
* @var string glyphicon for 'off' value * @var string glyphicon for 'off' value
*/ */
public $iconOff = 'remove'; public $iconOff = 'remove';
/** /**
* @var string text to display on the 'on' link * @var string text to display on the 'on' link
*/ */
public $onText; public $onText;
/** /**
* @var string text to display on the 'off' link * @var string text to display on the 'off' link
*/ */
public $offText; public $offText;
/** /**
* @var string text to display next to the 'on' link * @var string text to display next to the 'on' link
*/ */
public $displayValueText = false; public $displayValueText = false;
/** /**
* @var string text to display next to the 'on' link * @var string text to display next to the 'on' link
*/ */
public $onValueText; public $onValueText;
/** /**
* @var string text to display next to the 'off' link * @var string text to display next to the 'off' link
*/ */
public $offValueText; public $offValueText;
public function init() public function init()
{ {
if ($this->onText === null) { if ($this->onText === null) {
$this->onText = Yii::t('main', 'On'); $this->onText = Yii::t('main', 'On');
} }
if ($this->offText === null) { if ($this->offText === null) {
$this->offText = Yii::t('main', 'Off'); $this->offText = Yii::t('main', 'Off');
} }
if ($this->onValueText === null) { if ($this->onValueText === null) {
$this->onValueText = Yii::t('main', 'Active'); $this->onValueText = Yii::t('main', 'Active');
} }
if ($this->offValueText === null) { if ($this->offValueText === null) {
$this->offValueText = Yii::t('main', 'Inactive'); $this->offValueText = Yii::t('main', 'Inactive');
} }
if ($this->enableAjax) { if ($this->enableAjax) {
$this->registerJs(); $this->registerJs();
} }
} }
/** /**
* @inheritdoc * Registers the ajax JS
*/ */
protected function renderDataCellContent($model, $key, $index) public function registerJs()
{ {
$url = [$this->action, 'id' => $model->{$this->primaryKey}]; if (Yii::$app->request->isAjax) {
return;
$attribute = $this->attribute; }
$value = $model->$attribute; $js = <<<'JS'
if ($value === null || $value == true) {
$icon = $this->iconOn;
$title = $this->offText;
$valueText = $this->onValueText;
$color = 'green';
} else {
$icon = $this->iconOff;
$title = $this->onText;
$valueText = $this->offValueText;
$color = 'red';
}
return Html::a(
'<span class="glyphicon glyphicon-' . $icon . '"></span>',
$url,
[
'title' => $title,
'class' => 'toggle-column',
'style' => 'color:' . $color,
'data-method' => 'post',
'data-pjax' => '0',
]
) . ( $this->displayValueText ? " {$valueText}" : "" );
}
/**
* Registers the ajax JS
*/
public function registerJs()
{
if(Yii::$app->request->isAjax) {
return;
}
$js = <<<'JS'
$(document.body).on("click", "a.toggle-column", function(e) { $(document.body).on("click", "a.toggle-column", function(e) {
e.preventDefault(); e.preventDefault();
$.post($(this).attr("href"), function(data) { $.post($(this).attr("href"), function(data) {
@ -138,6 +104,41 @@ $(document.body).on("click", "a.toggle-column", function(e) {
return false; return false;
}); });
JS; JS;
$this->grid->view->registerJs($js, View::POS_READY, 'zx-toggle-column'); $this->grid->view->registerJs($js, View::POS_READY, 'zx-toggle-column');
} }
/**
* @inheritdoc
*/
protected function renderDataCellContent($model, $key, $index)
{
$url = [$this->action, 'id' => $model->{$this->primaryKey}];
$attribute = $this->attribute;
$value = $model->$attribute;
if ($value === null || $value == true) {
$icon = $this->iconOn;
$title = $this->offText;
$valueText = $this->onValueText;
$color = 'green';
} else {
$icon = $this->iconOff;
$title = $this->onText;
$valueText = $this->offValueText;
$color = 'red';
}
return Html::a(
'<span class="glyphicon glyphicon-' . $icon . '"></span>',
$url,
[
'title' => $title,
'class' => 'toggle-column',
'style' => 'color:' . $color,
'data-method' => 'post',
'data-pjax' => '0',
]
) . ($this->displayValueText ? " {$valueText}" : '');
}
} }

60
backend/components/menu/widgets/MenuEditorWidget.php

@ -6,43 +6,43 @@
namespace backend\components\menu\widgets; namespace backend\components\menu\widgets;
use core\entities\menu\Menu; use core\entities\menu\Menu;
use core\entities\menu\MenuItem; use core\entities\menu\MenuItem;
use yii\base\Widget; use yii\base\Widget;
class MenuEditorWidget extends Widget class MenuEditorWidget extends Widget
{ {
public $menu_id; public $menu_id;
public function run()
{
$menu = Menu::findOne($this->menu_id);
$items = $menu->items;
public function run() return $this->render('menu', [
{ 'items' => $this->getMenu($items),
$menu = Menu::findOne($this->menu_id); 'menu' => $menu,
$items = $menu->items; ]);
}
return $this->render('menu', [ /**
'items' => $this->getMenu($items), * @param $items MenuItem[]
'menu' => $menu, *
]); * @return array
} */
private function getMenu($items, $paren_id = null)
{
$array = [];
foreach ($items as $item) {
if ($item->parent_id != $paren_id) {
continue;
}
$array[$item->id]['item'] = $item;
if ($children = $item->children) {
$array[$item->id]['children'] = $this->getMenu($children, $item->id);
}
}
/** return $array;
* @param $items MenuItem[] }
*
* @return array
*/
private function getMenu($items, $paren_id = null)
{
$array = [];
foreach ($items as $item) {
if ($item->parent_id != $paren_id) {
continue;
}
$array[$item->id]['item'] = $item;
if ($children = $item->children) {
$array[$item->id]['children'] = $this->getMenu($children, $item->id);
}
}
return $array;
}
} }

1
backend/forms/rbac/RbacCreatePermissionForm.php

@ -6,7 +6,6 @@
namespace backend\forms\rbac; namespace backend\forms\rbac;
use yii\base\Model; use yii\base\Model;
use Yii; use Yii;

1
backend/forms/rbac/RbacCreateRoleForm.php

@ -6,7 +6,6 @@
namespace backend\forms\rbac; namespace backend\forms\rbac;
use yii\base\Model; use yii\base\Model;
use Yii; use Yii;

1
backend/forms/rbac/RbacUpdateChildren.php

@ -6,7 +6,6 @@
namespace backend\forms\rbac; namespace backend\forms\rbac;
use yii\base\Model; use yii\base\Model;
class RbacUpdateChildren extends Model class RbacUpdateChildren extends Model

Loading…
Cancel
Save