<?php
	
declare(strict_types=1);

namespace D3\ManufacturerInformation\Application\Controller\Admin;

use D3\ManufacturerInformation\Application\Model\Constants;
use D3\ManufacturerInformation\Modules\Application\Model\D3ManufacturerLongDesc;
use OxidEsales\Eshop\Application\Controller\Admin\AdminDetailsController;
use OxidEsales\Eshop\Application\Model\Manufacturer;
use OxidEsales\Eshop\Core\Registry;

class ManufacturerInfoLongdesc extends AdminDetailsController
{
	/**
	 * @return string
	 */
	public function render()
	{
		parent::render();
		
		$this->_aViewData['edit'] = $oManufacturer = oxNew(\OxidEsales\Eshop\Application\Model\Manufacturer::class);
		
		$sOXID = $this->_aViewData["oxid"] = $this->getEditObjectId();
		if (isset($sOXID) && $sOXID != "-1") {
			// load object
			$d3manufacturerlang = \OxidEsales\Eshop\Core\Registry::getConfig()->getRequestParameter("d3manufacturerlang");
			
			if (!isset($d3manufacturerlang)) {
				$d3manufacturerlang = $this->_iEditLang;
			}
			
			$this->_aViewData["d3manufacturerlang"] = $d3manufacturerlang;
			
			$oManufacturer->loadInLang($d3manufacturerlang, $sOXID);
			

			
			//Disable editing for derived items
			if ($oManufacturer->isDerived()) {
				$this->_aViewData['readonly'] = true;
			}
			
			foreach (\OxidEsales\Eshop\Core\Registry::getLang()->getLanguageNames() as $id => $language) {
				$oLang = new \stdClass();
				$oLang->sLangDesc = $language;
				$oLang->selected = ($id == $this->_iEditLang);
				$this->_aViewData["otherlang"][$id] = clone $oLang;
			}
		}
		
		$this->_aViewData["editor"] = $this->generateTextEditor("100%", 280, $oManufacturer, "oxmanufacturers__D3DESCRIPTION", "list.tpl.css");
		
		return "d3manufacturerlongdesc.tpl";
	}
	
	/**
	 * @return void
	 * @throws \Exception
	 */
	public function save()
	{
		parent::save();
		
		$sOXID = $this->getEditObjectId();
		$this->setEditObjectId($sOXID);
		
		if ($sOXID == "-1") {
			return;
		}
		
		/** @var Manufacturer|D3ManufacturerLongDesc $oManufacturer */
		$oManufacturer = oxNew(Manufacturer::class);
		$oManufacturer->setLanguage($this->_iEditLang);
		
		if (!$oManufacturer->load($sOXID)) {
			return;
		}
		
		$aParams = Registry::getRequest()->getRequestEscapedParameter("editval");
		$oManufacturer->assign($aParams);
		
		// default longdesc can parsed only, if longtext #1 is selected, all other longtext selections don't contains its value
		if (false === is_null($this->d3GetLongdescParamValue())) {
			$oManufacturer->d3SetLongdesc($this->d3GetLongdescParamValue());
			$oManufacturer->save();
		}
	}
	
	/**
	 * Returns string which must be edited by editor
	 *
	 * @param \OxidEsales\Eshop\Core\Model\BaseModel $oObject object with field will be used for editing
	 * @param string                                 $sField  name of editable field
	 *
	 * @return string
	 * @deprecated underscore prefix violates PSR12, will be renamed to "getEditValue" in next major
	 */
	protected function _getEditValue($oObject, $sField) // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore
	{
		$sEditObjectValue = '';
		if ($oObject) {
			$sEditObjectValue = $this->_processEditValue($oObject->getFieldData(Constants::D3_OXMANUFACTURERS_LONGDESC_FIELDNAME));
		}
		
		return $sEditObjectValue;
	}
	
	/**
	 * @return string
	 */
	public function d3GetLongdescParamValue() :string
	{
		return $aParams = Registry::getRequest()->getRequestEscapedParameter("editval")['oxmanufacturers__D3DESCRIPTION'];
	}
	
	/**
	 * inhaltliche Uebernahme analog Article_Main
	 * wird genutzt zum Abspeichern der Artikel-Langbeschreibung
	 *
	 * @param $sValue
	 *
	 * @return mixed
	 */
	protected function _processLongDesc($sValue)
	{
		$aSearch  = ['&amp;nbsp;', '&amp;', '&quot;', '&lang=', '<p>&nbsp;</p>', '<p>&nbsp; </p>'];
		$aReplace = ['&nbsp;', '&', '"', '&amp;lang=', '', ''];
		
		return str_replace($aSearch, $aReplace, $sValue);
	}
}