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

237
backend/components/ToggleColumn.php

@ -13,122 +13,88 @@ use Yii;
class ToggleColumn extends DataColumn
{
/**
* Toggle action that will be used as the toggle action in your controller
* @var string
*/
public $action = 'toggle';
/**
* @var string pk field name
*/
public $primaryKey = 'primaryKey';
/**
* Whether to use ajax or not
* @var bool
*/
public $enableAjax = true;
/**
* @var string glyphicon for 'on' value
*/
public $iconOn = 'ok';
/**
* @var string glyphicon for 'off' value
*/
public $iconOff = 'remove';
/**
* @var string text to display on the 'on' link
*/
public $onText;
/**
* @var string text to display on the 'off' link
*/
public $offText;
/**
* @var string text to display next to the 'on' link
*/
public $displayValueText = false;
/**
* @var string text to display next to the 'on' link
*/
public $onValueText;
/**
* @var string text to display next to the 'off' link
*/
public $offValueText;
public function init()
{
if ($this->onText === null) {
$this->onText = Yii::t('main', 'On');
}
if ($this->offText === null) {
$this->offText = Yii::t('main', 'Off');
}
if ($this->onValueText === null) {
$this->onValueText = Yii::t('main', 'Active');
}
if ($this->offValueText === null) {
$this->offValueText = Yii::t('main', 'Inactive');
}
if ($this->enableAjax) {
$this->registerJs();
}
}
/**
* @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}" : "" );
}
/**
* Registers the ajax JS
*/
public function registerJs()
{
if(Yii::$app->request->isAjax) {
return;
}
$js = <<<'JS'
/**
* Toggle action that will be used as the toggle action in your controller
* @var string
*/
public $action = 'toggle';
/**
* @var string pk field name
*/
public $primaryKey = 'primaryKey';
/**
* Whether to use ajax or not
* @var bool
*/
public $enableAjax = true;
/**
* @var string glyphicon for 'on' value
*/
public $iconOn = 'ok';
/**
* @var string glyphicon for 'off' value
*/
public $iconOff = 'remove';
/**
* @var string text to display on the 'on' link
*/
public $onText;
/**
* @var string text to display on the 'off' link
*/
public $offText;
/**
* @var string text to display next to the 'on' link
*/
public $displayValueText = false;
/**
* @var string text to display next to the 'on' link
*/
public $onValueText;
/**
* @var string text to display next to the 'off' link
*/
public $offValueText;
public function init()
{
if ($this->onText === null) {
$this->onText = Yii::t('main', 'On');
}
if ($this->offText === null) {
$this->offText = Yii::t('main', 'Off');
}
if ($this->onValueText === null) {
$this->onValueText = Yii::t('main', 'Active');
}
if ($this->offValueText === null) {
$this->offValueText = Yii::t('main', 'Inactive');
}
if ($this->enableAjax) {
$this->registerJs();
}
}
/**
* Registers the ajax JS
*/
public function registerJs()
{
if (Yii::$app->request->isAjax) {
return;
}
$js = <<<'JS'
$(document.body).on("click", "a.toggle-column", function(e) {
e.preventDefault();
$.post($(this).attr("href"), function(data) {
@ -138,6 +104,41 @@ $(document.body).on("click", "a.toggle-column", function(e) {
return false;
});
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;
use core\entities\menu\Menu;
use core\entities\menu\MenuItem;
use yii\base\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()
{
$menu = Menu::findOne($this->menu_id);
$items = $menu->items;
return $this->render('menu', [
'items' => $this->getMenu($items),
'menu' => $menu,
]);
}
return $this->render('menu', [
'items' => $this->getMenu($items),
'menu' => $menu,
]);
}
/**
* @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);
}
}
/**
* @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;
}
return $array;
}
}

1
backend/forms/rbac/RbacCreatePermissionForm.php

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

1
backend/forms/rbac/RbacCreateRoleForm.php

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

1
backend/forms/rbac/RbacUpdateChildren.php

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

Loading…
Cancel
Save