diff --git a/modules/admin/src/ngrest/base/NgRestModel.php b/modules/admin/src/ngrest/base/NgRestModel.php index 9e6a94a7c..85e3af45d 100644 --- a/modules/admin/src/ngrest/base/NgRestModel.php +++ b/modules/admin/src/ngrest/base/NgRestModel.php @@ -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. diff --git a/modules/admin/src/ngrest/base/Plugin.php b/modules/admin/src/ngrest/base/Plugin.php index 984dd3b81..95f938790 100644 --- a/modules/admin/src/ngrest/base/Plugin.php +++ b/modules/admin/src/ngrest/base/Plugin.php @@ -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; } @@ -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); } diff --git a/modules/admin/src/ngrest/plugins/CheckboxList.php b/modules/admin/src/ngrest/plugins/CheckboxList.php index 374439386..a9a1c0653 100644 --- a/modules/admin/src/ngrest/plugins/CheckboxList.php +++ b/modules/admin/src/ngrest/plugins/CheckboxList.php @@ -19,7 +19,9 @@ * ]; * } * ``` - * + * + * The plugin stores the value of the selected checkbox items as json into the database. + * * @author Basil Suter */ class CheckboxList extends Plugin @@ -66,7 +68,7 @@ protected function getItems() /** * @inheritdoc */ - public function serviceData() + public function serviceData($event) { return ['checkboxitems' => $this->getItems()]; } diff --git a/modules/admin/src/ngrest/plugins/CheckboxRelation.php b/modules/admin/src/ngrest/plugins/CheckboxRelation.php index c049c2881..3e759a5ac 100644 --- a/modules/admin/src/ngrest/plugins/CheckboxRelation.php +++ b/modules/admin/src/ngrest/plugins/CheckboxRelation.php @@ -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. @@ -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']; * } * ``` */ @@ -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)]; } /** @@ -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 */ diff --git a/modules/admin/src/ngrest/plugins/Select.php b/modules/admin/src/ngrest/plugins/Select.php index 95dea17ba..d3f1dab7a 100644 --- a/modules/admin/src/ngrest/plugins/Select.php +++ b/modules/admin/src/ngrest/plugins/Select.php @@ -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]; } diff --git a/modules/admin/src/ngrest/plugins/SelectHasMany.php b/modules/admin/src/ngrest/plugins/SelectHasMany.php deleted file mode 100644 index f611c88e9..000000000 --- a/modules/admin/src/ngrest/plugins/SelectHasMany.php +++ /dev/null @@ -1,42 +0,0 @@ - ['hasMany', 'relation' => 'cars], - * ]; - * } - * - * public function getCars() - * { - * return $this->hasMany(Car::class, ['car_id' => 'id']); - * } - * ``` - * - * - * @author Basil Suter - */ -class SelectHasMany extends Select -{ - public $relation = null; - - public function init() - { - parent::init(); - - $this->removeEvent(NgRestModel::EVENT_AFTER_FIND); - } - - public function getData() - { - } -} diff --git a/modules/admin/src/ngrest/plugins/SelectModel.php b/modules/admin/src/ngrest/plugins/SelectModel.php index 44f7a0f34..4e476a14b 100644 --- a/modules/admin/src/ngrest/plugins/SelectModel.php +++ b/modules/admin/src/ngrest/plugins/SelectModel.php @@ -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 */ class SelectModel extends Select @@ -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), diff --git a/modules/admin/src/ngrest/plugins/SortRelation.php b/modules/admin/src/ngrest/plugins/SortRelation.php index 37f07546c..65ff73257 100644 --- a/modules/admin/src/ngrest/plugins/SortRelation.php +++ b/modules/admin/src/ngrest/plugins/SortRelation.php @@ -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(), diff --git a/modules/admin/tests/admin/ngrest/plugins/CheckboxRelationTest.php b/modules/admin/tests/admin/ngrest/plugins/CheckboxRelationTest.php index a7e36134e..93c7872fb 100644 --- a/modules/admin/tests/admin/ngrest/plugins/CheckboxRelationTest.php +++ b/modules/admin/tests/admin/ngrest/plugins/CheckboxRelationTest.php @@ -36,7 +36,7 @@ public function testGetServiceDataConfiguration() $this->assertSame([ 0 => ['value' => 1, 'label' => 'John Doe (john@luya.io)'], 1 => ['value' => 2, 'label' => 'Jane Doe (jane@luya.io)'] - ], $plugin->serviceData()['relationdata']['items']); + ], $plugin->serviceData($event)['relationdata']['items']); } public function testGetServiceDataNoTemplateConfiguration() @@ -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() @@ -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']); } } diff --git a/modules/admin/tests/admin/ngrest/plugins/SelectHasManyTest.php b/modules/admin/tests/admin/ngrest/plugins/SelectHasManyTest.php deleted file mode 100644 index b1e3e0a8b..000000000 --- a/modules/admin/tests/admin/ngrest/plugins/SelectHasManyTest.php +++ /dev/null @@ -1,32 +0,0 @@ -load(); - - $user = $model->getModel('user1'); - - $event = new Event(); - $event->sender = $user; - - $plugin = new SelectHasMany([ - 'alias' => 'alias', - 'name' => 'firstname', - 'i18n' => false, - ]); - - $this->assertArrayNotHasKey('afterFind', $plugin->events()); - } -} diff --git a/modules/admin/tests/admin/ngrest/plugins/SelectModelTest.php b/modules/admin/tests/admin/ngrest/plugins/SelectModelTest.php index 8c2e10c6e..088c08901 100644 --- a/modules/admin/tests/admin/ngrest/plugins/SelectModelTest.php +++ b/modules/admin/tests/admin/ngrest/plugins/SelectModelTest.php @@ -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); + } }