{
private $inlines = array();
- function __construct($data=null, $model=null, $modelName='', $extra=array())
+ function __construct($data=null, $model=null, $modelClass='', $extra=array())
{
- if ($model) {
- // edit
- $extra['model'] = $model;
- $extra['initial'] = $this->getFormData($model);
- } else {
- // add
- $extra['model'] = new $modelName;
+ $editMode = $model !== null;
+
+ if (!$model)
+ $model = new $modelClass;
+
+ $extra['model'] = $model;
+
+ if ($extra['inlines']) {
+ foreach ($extra['inlines'] as $inlineClassName) {
+ $this->inlines[] = new $inlineClassName($model);
+ }
+ }
+
+ if ($editMode) {
+ $extra['initial'] = $this->getFormData($model, $this->inlines);
}
parent::__construct($data, $extra);
{
parent::initFields($extra);
- if ($extra['inlines']) {
+ if ($this->inlines) {
$this->field_groups[] = array('fields' => array_keys($this->fields));
- foreach ($extra['inlines'] as $inlineClassName) {
- $this->inlines[] = new $inlineClassName($extra['model']);
- }
}
foreach ($this->inlines as $inline) {
}
}
- private function getFormData($o)
+ private function getFormData($o, $inlines)
{
$data = $o->getData();
foreach ($o->getTable()->getRelations() as $rname => $rel) {
}
}
- foreach ($this->inlines() as $inlineName => $inlineClassName) {
- $inlineInstance = new $inlineClassName($o, null);
-
+ foreach ($inlines as $inline) {
$objs = array();
- foreach ($inlineInstance->getObjects() as $io) {
+ foreach ($inline->getObjects() as $io) {
$d = $io->getData();
$d['id'] = $io->pk();
$objs[] = $d;
}
- $data[$inlineName] = $objs;
+ $data[$inline->getModelName()] = $objs;
}
return $data;