Skip to content

Commit

Permalink
fixed deep linking issue #1229
Browse files Browse the repository at this point in the history
  • Loading branch information
nadar committed Mar 13, 2017
1 parent fdbdf23 commit e2df744
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 105 deletions.
11 changes: 11 additions & 0 deletions modules/admin/src/ngrest/base/NgRestModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ public function scenarios()
$scenarios[RestActiveController::SCENARIO_RESTUPDATE] = $scenarios[self::SCENARIO_DEFAULT];
return $scenarios;
}

/**
* Whether a field is i18n or not.
*
* @param string $fieldName The name of the field which is
* @return boolean
*/
public function isI18n($fieldName)
{
return in_array($fieldName, $this->i18n) ? true : false;
}

/**
* Define an array with filters you can select from the crud list.
Expand Down
5 changes: 3 additions & 2 deletions modules/admin/src/ngrest/base/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,10 @@ public function getServiceName($name)
*
* The above service data can be used when creating the tags with `$this->getServiceName('titles')`.
*
* @param \yii\base\Event $event The event sender which triggers the event.
* @return boolean|array
*/
public function serviceData()
public function serviceData($event)
{
return false;
}
Expand Down Expand Up @@ -457,7 +458,7 @@ public function onBeforeCollectServiceData($event)
public function onCollectServiceData($event)
{
if ($this->onBeforeCollectServiceData($event)) {
$data = $this->serviceData();
$data = $this->serviceData($event);
if (!empty($data)) {
$event->sender->addNgRestServiceData($this->name, $data);
}
Expand Down
6 changes: 4 additions & 2 deletions modules/admin/src/ngrest/plugins/CheckboxList.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
* ];
* }
* ```
*
*
* The plugin stores the value of the selected checkbox items as json into the database.
*
* @author Basil Suter <[email protected]>
*/
class CheckboxList extends Plugin
Expand Down Expand Up @@ -66,7 +68,7 @@ protected function getItems()
/**
* @inheritdoc
*/
public function serviceData()
public function serviceData($event)
{
return ['checkboxitems' => $this->getItems()];
}
Expand Down
47 changes: 26 additions & 21 deletions modules/admin/src/ngrest/plugins/CheckboxRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use luya\admin\ngrest\base\NgRestModel;
use luya\admin\ngrest\base\Plugin;
use luya\rest\ActiveController;
use luya\helpers\ArrayHelper;
use luya\admin\helpers\I18n;

/**
* Checkbox Selector via relation table.
Expand Down Expand Up @@ -87,8 +89,8 @@ class CheckboxRelation extends Plugin
* @var array A list of fields which should be used for the display template. Can also be a callable function to build the field with the template
*
* ```php
* 'labelFields' => function($model) {
* return $model->firstname . ' ' . $model->lastname;
* 'labelFields' => function($array) {
* return $array['firstname'] . ' ' . $array['lastname'];
* }
* ```
*/
Expand Down Expand Up @@ -185,30 +187,37 @@ public function renderUpdate($id, $ngModel)
*
* @return array
*/
private function getOptionsData()
private function getOptionsData($event)
{
$items = [];

foreach ($this->model->find()->all() as $item) {
foreach ($this->model->find()->asArray(true)->all() as $item) {
if (is_callable($this->labelFields, false)) {
$label = call_user_func($this->labelFields, $item);
} else {
$array = $item->getAttributes($this->labelFields);
$array = ArrayHelper::filter($item, $this->labelFields);

foreach ($array as $key => $value) {
if ($event->sender->isI18n($key)) {
$array[$key] = I18n::decodeActive($value);
}
}

$label = $this->labelTemplate ? vsprintf($this->labelTemplate, $array) : implode(', ', $array);
}

$items[] = ['value' => $item[$this->modelPrimaryKey], 'label' => $label];
$items[] = ['value' => (int) $item[$this->modelPrimaryKey], 'label' => $label];
}

return ['items' => $items];
}

/**
* @inheritdoc
*/
public function serviceData()
public function serviceData($event)
{
return ['relationdata' => $this->getOptionsData()];
return ['relationdata' => $this->getOptionsData($event)];
}

/**
Expand All @@ -217,20 +226,16 @@ public function serviceData()
public function onBeforeExpandFind($event)
{
$data = [];
foreach ($this->model->find()->leftJoin($this->refJoinTable, $this->model->tableName().'.id='.$this->refJoinTable.'.'.$this->refJoinPkId)->where([$this->refJoinTable.'.'.$this->refModelPkId => $event->sender->id])->each() as $item) {
$data[] = ['value' => $item->getAttribute($this->getModelPrimaryKey())];
foreach ($this->model->find()
->leftJoin($this->refJoinTable, $this->model->tableName().'.id='.$this->refJoinTable.'.'.$this->refJoinPkId)
->where([$this->refJoinTable.'.'.$this->refModelPkId => $event->sender->id])
->asArray(true)
->each() as $item) {
$data[] = ['value' => $item[$this->getModelPrimaryKey()]];
}
$event->sender->{$this->name} = $data;
}

/**
* @inheritdoc
*/
public function onBeforeListFind($event)
{
$event->sender->{$this->name} = $this->getRelationData($event);
}

/**
* @inheritdoc
*/
Expand Down
2 changes: 1 addition & 1 deletion modules/admin/src/ngrest/plugins/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function renderUpdate($id, $ngModel)
return $this->renderCreate($id, $ngModel);
}

public function serviceData()
public function serviceData($event)
{
return ['selectdata' => $this->data];
}
Expand Down
42 changes: 0 additions & 42 deletions modules/admin/src/ngrest/plugins/SelectHasMany.php

This file was deleted.

7 changes: 7 additions & 0 deletions modules/admin/src/ngrest/plugins/SelectModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
* return $model->firstname . ' ' . $model->lastname;
* }
* ```
*
* You can also use the quick mode which finds the primary key by itself, therfore just keep valueField empty.
* @author Basil Suter <[email protected]>
*/
class SelectModel extends Select
Expand Down Expand Up @@ -156,7 +158,12 @@ public function getData()
$class = $class::className();
}

if (!$this->valueField) {
$this->valueField = implode("", $class::primaryKey());
}

foreach (static::getDataInstance($class, $this->where) as $item) {

$data[] = [
'value' => (int) $item->{$this->valueField},
'label' => $this->generateLabelField($item),
Expand Down
2 changes: 1 addition & 1 deletion modules/admin/src/ngrest/plugins/SortRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function renderUpdate($id, $ngModel)
return $this->renderCreate($id, $ngModel);
}

public function serviceData()
public function serviceData($event)
{
return [
'sortrelationdata' => $this->getData(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function testGetServiceDataConfiguration()
$this->assertSame([
0 => ['value' => 1, 'label' => 'John Doe ([email protected])'],
1 => ['value' => 2, 'label' => 'Jane Doe ([email protected])']
], $plugin->serviceData()['relationdata']['items']);
], $plugin->serviceData($event)['relationdata']['items']);
}

public function testGetServiceDataNoTemplateConfiguration()
Expand Down Expand Up @@ -64,7 +64,7 @@ public function testGetServiceDataNoTemplateConfiguration()
$this->assertSame([
0 => ['value' => 1, 'label' => 'John, Doe'],
1 => ['value' => 2, 'label' => 'Jane, Doe']
], $plugin->serviceData()['relationdata']['items']);
], $plugin->serviceData($event)['relationdata']['items']);
}

public function testGetServiceDataCallablaeConfiguration()
Expand All @@ -87,13 +87,13 @@ public function testGetServiceDataCallablaeConfiguration()
'refModelPkId' => 'group_id',
'refJoinPkId' => 'user_id',
'labelFields' => function ($model) {
return $model->firstname . "|". $model->lastname;
return $model['firstname'] . "|". $model['lastname'];
}
]);

$this->assertSame([
0 => ['value' => 1, 'label' => 'John|Doe'],
1 => ['value' => 2, 'label' => 'Jane|Doe']
], $plugin->serviceData()['relationdata']['items']);
], $plugin->serviceData($event)['relationdata']['items']);
}
}
32 changes: 0 additions & 32 deletions modules/admin/tests/admin/ngrest/plugins/SelectHasManyTest.php

This file was deleted.

23 changes: 23 additions & 0 deletions modules/admin/tests/admin/ngrest/plugins/SelectModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,27 @@ public function testAfterFindEventWithI18n()

unset($plugin);
}

public function testFindSelfPrimaryKey()
{
$model = new UserFixture();
$model->load();
$plugin = new SelectModel([
'name' => 'test',
'alias' => 'test',
'i18n' => false,
'modelClass' => User::class,
'labelField' => function ($model) {
return $model->firstname . '@' . $model->lastname;
}
]);


$this->assertSame([
0 => ['value' => 2, 'label' => 'Jane@Doe'],
1 => ['value' => 1, 'label' => 'John@Doe'],
], $plugin->getData());

unset($plugin);
}
}

0 comments on commit e2df744

Please sign in to comment.