<?php
namespace App\Config; class SupplierProductFields extends XmlBasicAbstract { protected $fileName = CONFIG_FILE_SUPPLIERPRODUCT_FIELDS; private $allowedFields= ['category', 'productName', 'localization', 'chain', 'address', 'zipCode', 'departure', 'destination']; protected function getAll_rawData_extractDataFromFile(\SimpleXMLElement $xml) { $result = []; foreach($xml->service as $serviceFields) { $serviceCode = (string)$serviceFields['code']; foreach ($serviceFields->field as $field) { $result[$serviceCode][(string)$field['code']] = (string)$field['type']; } foreach ($this->allowedFields as $fld) { if (!array_key_exists($fld, $result[$serviceCode])) { $result[$serviceCode][$fld] = 'invisible'; } } } return $result; } protected function save_check() { foreach ($this->itemsList as $serviceCode => $fieldsList) { if (\App\Config\Services_Basic::getInstance()->checkService($serviceCode)) { $serviceName = \App\Config\Services_Basic::getInstance()->getServiceName($serviceCode); if ($fieldsList) { foreach ($fieldsList as $key=>$value) { if (!in_array($key, $this->allowedFields)) { $this->addErrorMessage(sprintf('The field `%s` is not correct.', $key)); } if (!in_array($value, ['invisible', 'optional', 'mandatory'])) { $this->addErrorMessage(sprintf('The value `%s` set for `%s` is not accepted.', $value, $key)); } if (in_array($key, $this->allowedFields) ){ $generalAttributes = \App\Config\Services_GeneralAttributes::getInstance()->get($serviceCode); if (in_array($value, ['optional', 'mandatory']) and $generalAttributes[$key]['type']=='none') { $this->addErrorMessage(sprintf('You can not set `%s` as `%s`, because the field is set as `none` (`%s`).', $key, $value, $serviceName)); } } } } } } } protected function save_write_do_getXml() { $xml=simplexml_load_string('<?xml version="1.0" encoding="UTF-8"?><supplierProductFields/>'); foreach ($this->itemsList as $serviceCode => $serviceDetails) { if ($serviceDetails and (in_array('optional', $serviceDetails) or in_array('mandatory', $serviceDetails)) and \App\Config\Services_Basic::getInstance()->checkService($serviceCode)) { $serviceXml = $xml->addChild('service'); $serviceXml['code'] = $serviceCode; foreach ($serviceDetails as $category => $type) { if ($type != 'invisible') { $fieldXml = $serviceXml->addChild('field'); $fieldXml['code'] = $category; $fieldXml['type'] = $type; } } } } return $xml; } public function getFieldType($serviceCode, $fieldCode) { if (!\App\Config\Services_Basic::getInstance()->checkService($serviceCode)) { throw new \Exception(sprintf('Invalid service code - %s.', $serviceCode)); } if (!in_array($fieldCode, $this->allowedFields)) { throw new \Exception(sprintf('The field `%s` is not correct.', $fieldCode)); } $fields = $this->get($serviceCode); if ($fields[$fieldCode]) { return $fields[$fieldCode]; } else { return 'invisible'; } } } 