<?php
 namespace Modules\Utils\RemarksAssistedInputs; class Model extends \App\Mvc\ModelAbstract { private $remarksType = ''; private $remarksBulk = ''; public function __construct($remarksType, $remarksBulk) { if (!$remarksType) { throw new \RuntimeException("The remarks type is not set"); } if ($remarksType) { if (!in_array($remarksType, \App\Config\Remarks::getInstance()->getValidRemarkTypes())) { throw new \RuntimeException("The remarks type is not valid"); } if (!\App\Config\Remarks::getInstance()->getAllRemarksFromCategory($remarksType)) { throw new \RuntimeException("There is no remark configured"); } if (!\App\Config\Remarks::getInstance()->remarkTypeHasAssistedRemarks($remarksType)) { throw new \RuntimeException("There is no assisted remark configured"); } $this->remarksType = $remarksType; } if ($remarksBulk) { $this->remarksBulk = $remarksBulk; } parent::__construct(); } public function fillRemarksValues(Array $remarksValues) { $remarksProcessObj = \App\Config\Remarks::getInstance(); $remarksConfig = $remarksProcessObj->getAllRemarksFromCategory($this->remarksType); $finalRemarksList = []; $remarksLines = explodeClean("\n", $this->remarksBulk); $remarkReplacingValues_vars = array_map(function($remarkName) { return "[".$remarkName."]";}, array_keys($remarksValues)); $remarkReplacingValues_values = array_values($remarksValues); $assistedRemarksValues = $remarksValues; foreach ($remarksLines as $remarkLine) { $remarkLine = trim($remarkLine); if (!$remarkLine) { break; } $remarkLineVars = $remarksProcessObj->getProcessedRemarksDetailed($remarkLine, $remarksConfig); $finalRemarkLine = $remarkLine; foreach ($remarkLineVars as $remarkLineVarName => $remarkLineVar) { if (isset($assistedRemarksValues[$remarkLineVarName])) { if ($remarkLineVar['var']['type'] == 'blank' && $assistedRemarksValues[$remarkLineVarName] == 0) { $finalRemarkLine = ''; } else { $finalRemarkLine = str_replace($remarkReplacingValues_vars, $remarkReplacingValues_values, $remarkLineVar['originalRemark']['format']); } unset($assistedRemarksValues[$remarkLineVar['var']['name']]); } } if ($finalRemarkLine) { $finalRemarksList[]= trim($finalRemarkLine); } } if ($assistedRemarksValues) { foreach ($remarksConfig as $thisRemark) { if ($thisRemark['hasAssistedInputs']) { $foundRemarkVar = false; $onlyDefaultValueTypeRemark = false; foreach ($thisRemark['vars'] as $remarkVar) { if (isset($assistedRemarksValues[$remarkVar['name']])) { if ($remarkVar['type'] == 'blank' && $assistedRemarksValues[$remarkVar['name']] == 0) { $onlyDefaultValueTypeRemark = true; } $foundRemarkVar = true; break; } } if ($foundRemarkVar) { if (!$onlyDefaultValueTypeRemark) { $finalRemarkLine = str_replace($remarkReplacingValues_vars, $remarkReplacingValues_values, $thisRemark['format']); $finalRemarksList[]= trim($finalRemarkLine); } unset($assistedRemarksValues[$remarkVar['name']]); } } if (!$assistedRemarksValues) { break; } } } return implode("\r\n", array_unique($finalRemarksList)); } }