Skip to content

Commit

Permalink
Fix TemplateableTrait, Part 2
Browse files Browse the repository at this point in the history
Removed structure values processing in `saveTemplateOptions()`; processing already handled in `ModelStructureProperty::save()`.

Changes:
- Added `translateTemplateOptionsModel()` for recurisvely translating generic models
- Changed `saveTemplateOptions()` to prepare structure only once
- Moved translating of property values on generic models from `saveTemplateOptions()` to `templateOptionsStructure()`
  • Loading branch information
mcaskill committed Dec 15, 2017
1 parent 18e920e commit 74378fe
Showing 1 changed file with 40 additions and 14 deletions.
54 changes: 40 additions & 14 deletions src/Charcoal/Cms/TemplateableTrait.php
Expand Up @@ -13,6 +13,7 @@
use Charcoal\Property\PropertyInterface;
use Charcoal\Property\SelectablePropertyInterface;
use Charcoal\Property\Structure\StructureMetadata;
use Charcoal\Property\ModelStructureProperty;
use Charcoal\Property\TemplateOptionsProperty;
use Charcoal\Property\TemplateProperty;

Expand Down Expand Up @@ -182,6 +183,34 @@ final protected function assertValidTemplateStructureDependencies()
}
}

/**
* Convert the given model's multilingual property values into {@see \Charcoal\Translator\Translation} objects.
*
* @param ModelInterface $obj The object to parse.
* @param boolean $recursive Whether we should traverse structure properties.
* @return ModelInterface The localized object.
*/
protected function translateTemplateOptionsModel(ModelInterface $obj, $recursive = false)
{
foreach ($obj->properties() as $propertyIdent => $property) {
$val = $obj[$propertyIdent];
if ($property->l10n()) {
$val = $this->translator()->translation($obj[$propertyIdent]);
$obj[$propertyIdent] = $val;
} elseif ($property instanceof ModelStructureProperty) {
$o = $prop->structureVal($obj[$propertyIdent]);

if ($o instanceof Model) {
$o = $this->translateTemplateOptionsModel($o);
}

$obj[$propertyIdent] = $o;
}
}

return $obj;
}

/**
* Retrieve the default template propert(y|ies).
*
Expand Down Expand Up @@ -277,22 +306,14 @@ protected function saveTemplateOptions(array $properties = null)
$properties = $this->defaultTemplateProperties();
}

$this->prepareTemplateOptions($properties);
if ($this->areTemplateOptionsFinalized === false) {
$this->areTemplateOptionsFinalized = true;
$this->prepareTemplateOptions();
}

$key = 'template_options';
$prop = $this->property($key);
if ($prop->structureModelClass() === Model::class) {
$struct = $this->propertyValue($key);
$struct = $prop->structureVal($struct, $this->templateOptionsMetadata());
foreach ($struct->properties() as $propertyIdent => $property) {
$val = $struct[$propertyIdent];
if ($property->l10n()) {
$val = $this->translator()->translation($struct[$propertyIdent]);
}

$struct[$propertyIdent] = $property->save($val);
}
}
$prop->setStructureMetadata($this->templateOptionsMetadata());
}

/**
Expand Down Expand Up @@ -325,7 +346,12 @@ public function templateOptionsStructure()
$key = 'template_options';
$prop = $this->property($key);
$val = $this->propertyValue($key);
$obj = $prop->structureVal($val, $this->templateOptionsMetadata());

if ($obj instanceof Model) {
$obj = $this->translateTemplateOptionsModel($obj);
}

return $prop->structureVal($val, $this->templateOptionsMetadata());
return $obj;
}
}

0 comments on commit 74378fe

Please sign in to comment.