Skip to content

Commit

Permalink
code review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
fornit1917 committed Jan 25, 2016
1 parent c6c765b commit bdddcc5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ Yii Framework 2 Change Log
- Enh #9072: `yii\web\ErrorAction` displays 404 error instead of blank page on direct access (klimov-paul)
- Enh #9149: Print directory migrationPath in a `yii migrate` command error. (RusAlex)
- Enh #9177: Added password hash cost setting to Security component (freezy-sk)
- Enh #9198: Added parameter buttonsVisible for `yii\grid\ActionColimn` (fornit1917)
- Enh #9198: Added parameter visibleButtons for `yii\grid\ActionColimn` (fornit1917)
- Enh #9239: Better handling of `Json` errors (grzegorzkurtyka, samdark)
- Enh #9246: Added `yii\web\UrlRule::getParamRules()` (df2)
- Enh #9249: Added `hashCallback` in `yii\web\AssetManager` to allow custom hash generation for asset directory (petrabarus)
Expand Down
22 changes: 11 additions & 11 deletions framework/grid/ActionColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,13 @@ class ActionColumn extends Column
*/
public $buttons = [];
/** @var array visibility conditions for each button. The array keys are the button names (without curly brackets),
* and the values are the conditions whether this button is visible.Can be boolean value or callback. Defaults to true.
* The callbacks should use the following signature:
* and the values are are the boolean true/false or the anonymous function. The button will be shown,
* when its name is not specified in this array.
* The callbacks must use the following signature:
*
* ```php
* [
* 'update' => function ($model) {
* 'update' => function ($model, $key, $index) {
* return $model->status === 'editable';
* },
* ],
Expand All @@ -105,8 +106,7 @@ class ActionColumn extends Column
* ],
* ```
*/
public $buttonsVisible = [];

public $visibleButtons = [];
/**
* @var callable a callback that creates a button URL using the specified model information.
* The signature of the callback should be the same as that of [[createUrl()]].
Expand Down Expand Up @@ -197,15 +197,15 @@ protected function renderDataCellContent($model, $key, $index)
return preg_replace_callback('/\\{([\w\-\/]+)\\}/', function ($matches) use ($model, $key, $index) {
$name = $matches[1];

if (isset($this->buttonsVisible[$name])) {
$visible = is_callable($this->buttonsVisible[$name])
? call_user_func($this->buttonsVisible[$name], $model)
: $this->buttonsVisible[$name];
if (isset($this->visibleButtons[$name])) {
$isVisible = $this->visibleButtons[$name] instanceof \Closure
? call_user_func($this->visibleButtons[$name], $model, $key, $index)
: $this->visibleButtons[$name];
} else {
$visible = true;
$isVisible = true;
}

if ($visible && isset($this->buttons[$name])) {
if ($isVisible && isset($this->buttons[$name])) {
$url = $this->createUrl($name, $model, $key, $index);
return call_user_func($this->buttons[$name], $url, $model, $key);
} else {
Expand Down
12 changes: 6 additions & 6 deletions tests/framework/grid/ActionColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,29 @@ public function testRenderDataCell()
$this->assertContains('update_button', $columnContents);

//test visible button
$column->buttonsVisible = [
$column->visibleButtons = [
'update' => true
];
$columnContents = $column->renderDataCell(['id' => 1], 1, 0);
$this->assertContains('update_button', $columnContents);

//test visible button (condition is callback)
$column->buttonsVisible = [
'update' => function($model){return $model['id'] == 1;}
$column->visibleButtons = [
'update' => function($model, $key, $index){return $model['id'] == 1;}
];
$columnContents = $column->renderDataCell(['id' => 1], 1, 0);
$this->assertContains('update_button', $columnContents);

//test invisible button
$column->buttonsVisible = [
$column->visibleButtons = [
'update' => false
];
$columnContents = $column->renderDataCell(['id' => 1], 1, 0);
$this->assertNotContains('update_button', $columnContents);

//test invisible button (condition is callback)
$column->buttonsVisible = [
'update' => function($model){return $model['id'] != 1;}
$column->visibleButtons = [
'update' => function($model, $key, $index){return $model['id'] != 1;}
];
$columnContents = $column->renderDataCell(['id' => 1], 1, 0);
$this->assertNotContains('update_button', $columnContents);
Expand Down

0 comments on commit bdddcc5

Please sign in to comment.