<?php
namespace Engine\Form\Input\Matrix; class Cell extends \Engine\Form\Input\ElementAbstract { protected $parent = null; protected $assocNewName = null; public function setParent($parent) { $this->parent = $parent; return $this; } public function addElement(\Engine\Form\Input\ElementAbstract $element) { $this->childs[] = $element; return $this; } protected function populateAssocNewNames($element) { if ($element instanceof \Engine\Form\Input\ElementAbstract) { $this->assocNewName[$element->getName()] = $element; } if ($element instanceof \Engine\Form\Input\ElementWithElements) { foreach ($element->getChilds() as $childs) { foreach ($childs as $chd) { $this->populateAssocNewNames($chd); } } return $this; } foreach ($element->getChilds() as $chd) { $this->populateAssocNewNames($chd); } return $this; } public function updateElementsNames($index) { if ($this->assocNewName == null) { $this->assocNewName = []; foreach ($this->getChilds() as $element) { $this->populateAssocNewNames($element); } } foreach ($this->assocNewName as $oldName=>$element) { $newName = sprintf("%s[%s][%s]", $this->parent->getName(), $index, $oldName); $element->setId(null); $element->setName($newName); } return $this; } public function resetElementsNames() { foreach ($this->assocNewName as $oldName=>$element) { $element->setName($oldName); } return $this; } public function render($templateClass = null, $parentElement = null) { if ($this->dontRender) { return ''; } if ($templateClass==null) { $templateClass = $this->class; } $this->loadTranslations($templateClass); $element = $this->getTemplateParams($templateClass, $parentElement); $return = ""; $stats = [ 'elements' => 0, 'containers' => 0, 'pseudo-containers' => 0, 'callbackResults' => [], ]; $this->traverseTree( function($child, $self, $callbackParams) { $child->setDisabled($callbackParams['disabled']); }, $stats, ['disabled'=>$this->getDisabled(),] ); foreach ($element['childs'] as $chd) { $return.= $chd->render(); } return $return; } public function setValue($value) { if ($this->assocNewName) { foreach ($this->assocNewName as $inputName=>$element) { if (isset($value[$inputName])) { $element->setValue($value[$inputName]); } else { $element->setValue(null); } } } return $this; } } 