Skip to content

Commit

Permalink
Now can set $clientRoute as array in InputFile
Browse files Browse the repository at this point in the history
  • Loading branch information
alexantr committed Aug 27, 2018
1 parent 3a316e7 commit d0568ef
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
34 changes: 24 additions & 10 deletions src/InputFile.php
Expand Up @@ -14,7 +14,7 @@
class InputFile extends InputWidget
{
/**
* @var string Route to elFinder client
* @var string|array Route to elFinder client
*/
public $clientRoute;
/**
Expand Down Expand Up @@ -71,8 +71,6 @@ class InputFile extends InputWidget
*/
public $preview;

private $url;

/**
* {@inheritdoc}
* @throws InvalidConfigException
Expand All @@ -96,18 +94,12 @@ public function init()
$this->previewOptions['id'] = $this->options['id'] . '_preview';
}

$route = [$this->clientRoute];
$route['id'] = $this->options['id'];
if (!empty($this->filter)) {
$route['filter'] = $this->filter;
$this->options['data']['filter'] = is_string($this->filter) ? $this->filter : Json::encode($this->filter);
}
if ($this->multiple) {
$route['multiple'] = 1;
$this->options['data']['multiple'] = '1';
}

$this->url = Url::toRoute($route);
}

/**
Expand Down Expand Up @@ -155,8 +147,30 @@ public function run()
$view = $this->getView();
HelperAsset::register($view);

$view->registerJs("alexantr.elFinder.registerSelectButton('{$this->buttonOptions['id']}', '{$this->url}');", View::POS_END);
$buttonId = $this->buttonOptions['id'];

$route = $this->createRoute();
$url = Url::toRoute($route);

$view->registerJs("alexantr.elFinder.registerSelectButton('$buttonId', '$url');", View::POS_END);

return strtr($this->template, $replace);
}

/**
* Creates route to elFinder client
* @return array
*/
protected function createRoute()
{
$route = (array)$this->clientRoute;
$route['id'] = $this->options['id'];
if (!empty($this->filter)) {
$route['filter'] = $this->filter;
}
if ($this->multiple) {
$route['multiple'] = 1;
}
return $route;
}
}
20 changes: 20 additions & 0 deletions tests/InputFileTest.php
Expand Up @@ -179,4 +179,24 @@ public function testMultipleParam()
$expected = "alexantr.elFinder.registerSelectButton('post-image_button', '/index.php?r=elfinder%2Findex&id=post-image&filter=image&multiple=1');";
$this->assertContains($expected, $out);
}

public function testClientRouteAsArray()
{
$view = $this->mockView();

$model = new Post();
$widget = InputFile::widget([
'view' => $view,
'model' => $model,
'attribute' => 'image',
'clientRoute' => ['/elfinder/index', 'section' => 'test'],
]);

$out = $view->renderFile('@tests/data/views/layout.php', [
'content' => $widget,
]);

$expected = "alexantr.elFinder.registerSelectButton('post-image_button', '/index.php?r=elfinder%2Findex&section=test&id=post-image');";
$this->assertContains($expected, $out);
}
}

0 comments on commit d0568ef

Please sign in to comment.