-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMultiEmbedded.php
51 lines (41 loc) · 1.4 KB
/
MultiEmbedded.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
namespace consultnn\widgets;
use consultnn\multiInput\MultiInput;
use yii\base\InvalidConfigException;
use yii\helpers\Html;
use yii\helpers\Json;
class MultiEmbedded extends MultiInput
{
public $embeddedModelClassName;
public $embeddedFormName;
public function init()
{
if (empty($this->rowView)) {
throw new InvalidConfigException('rowView required');
}
if (empty($this->embeddedModelClassName)) {
throw new InvalidConfigException('embeddedModelClassName required');
}
if (isset($this->embeddedFormName) && count($this->model->{$this->attribute}) > 0) {
foreach ($this->model->{$this->attribute} as $embeddedModel) {
/** @var \consultnn\embedded\EmbeddedDocument $embeddedModel */
$embeddedModel->setFormName($this->embeddedFormName);
}
}
parent::init();
}
private function getEmptyModel()
{
$emptyModel = new $this->embeddedModelClassName;
$emptyModel->setFormName($this->embeddedFormName);
return $emptyModel;
}
protected function initEmpty($a = null)
{
$this->model->{$this->attribute} = [$this->getEmptyModel()];
}
protected function renderTemplateRow()
{
return Json::encode(Html::tag('div', $this->renderRow('#index#', $this->getEmptyModel()), ['class' => 'template']));
}
}