<?php
 namespace Engine\Form\Input; class Matrix extends \Engine\Form\Input\ElementAbstract { protected $class = "matrix"; protected $childs = []; protected $url = null; protected $saveHandler = null; protected $rowRenderHandler = null; protected $disabledRows = []; public function __construct($label=null, $name=null, $value=null) { parent::__construct($label, $name, $value); $this->settings['minItems'] = 1; $this->settings['showCounter'] = false; $this->settings['addLocation'] = 'bottom'; $this->settings['operations'] = []; $this->settings['operations']['add'] = true; $this->settings['operations']['remove'] = true; $this->style['startIndex'] = 0; $this->style['render_rowIndex'] = true; } public function addRow(\Engine\Form\Input\Matrix\Row $row) { $row->setParent($this); $row->setStyle('operations_add', $this->settings['operations']['add']); $row->setStyle('operations_remove', $this->settings['operations']['remove']); $row->setStyle('render_rowIndex', $this->getStyle('render_rowIndex')); $this->childs[get_class($row)] = $row; return $this; } public function getRow($type) { return $this->childs[$type]; } public function setUrl($url) { $this->url = $url; return $this; } public function getUrl() { return $this->url; } public function setValue($value) { if (!$value) { $value = []; } return parent::setValue(array_values($value)); } public function setDisabledRows($keys) { $this->disabledRows = $keys; return $this; } protected function getTemplateParams($templateClass = null, $parentElement = null) { $params = parent::getTemplateParams($templateClass, null); $params['disabledRows'] = $this->disabledRows; $params['rowRenderHandler'] = $this->rowRenderHandler; return $params; } public function setSaveHandler($callback) { $this->saveHandler = $callback; return $this; } public function setRenderRowCallback($callback) { $this->rowRenderHandler = $callback; return $this; } public function getRenderRowCallback() { return $this->rowRenderHandler; } public function render($templateClass = null, $parentElement = null) { if ($this->saveHandler) { $svh = $this->saveHandler; $this->saveHandler = null; $rrh = $this->rowRenderHandler; $this->rowRenderHandler = null; call_user_func_array($svh, [$this]); $this->saveHandler = $svh; $this->rowRenderHandler = $rrh; } return parent::render($templateClass, $parentElement); } } 