<?php
 namespace App\Config; class AgencyCostCenters extends XmlDynamicRecordsAbstract { protected $fileName=CONFIG_FILE_AGENCYCOSTCENTERS; protected function getAll_rawData_extractDataFromFile_list(\SimpleXMLElement $xml) { $return=[]; foreach($xml->costCenter as $cc) { $return [ (String)$cc['code'] ] = [ 'name'=>(String)$cc, 'disabled'=>(Int)$cc['disabled'] ]; } return $return; } protected function getAll_rawData_extractDataFromFile_default(\SimpleXMLElement $xml) { return (String)$xml['default']; } protected function getAll_rawData_extractDataFromFile_mandatory(\SimpleXMLElement $xml) { return false; } protected function itemCheck($code, $itemDetails) { if (!is_string($itemDetails['name'])) { throw new \Exception('Agency cost center name must be set as a string.'); } if (!$code) { $this->addErrorMessage('The agency cost center code was not defined.'); } if (!ctype_alnum($code)) { $this->addErrorMessage('The agency cost center code must contain only alphanumeric chars.'); } if (!$itemDetails['name']) { $this->addErrorMessage('The `agency cost center name` was not defined.'); } if (mb_strlen($code)>20) { $this->addErrorMessage('The `agency cost center` code length must be smaller than 20 chars.'); } if (mb_strlen($itemDetails['name'])>50) { $this->addErrorMessage('The `agency cost center` length must be smaller than 50 chars.'); } if (htmlClean($itemDetails['name']) != $itemDetails['name']){ $this->addErrorMessage('The `agency cost center name` cannot contain html special characters.'); } foreach($this->itemsList as $currCode=>$currDetails) { if ($currCode!=$code and mb_strtoupper($itemDetails['name'])==mb_strtoupper($currDetails['name'])) { $this->addErrorMessage("This agency cost center `{$currDetails['name']}` already exists."); } } } protected function save_check() { if ($this->defaultItem and $this->itemsList[$this->defaultItem]['disabled']) { $this->addErrorMessage("You cannot disable the default cost center."); } } protected function save_write_do_getXml() { $xml=simplexml_load_string('<?xml version="1.0" encoding="UTF-8"?><agencyCostCenters/>'); foreach($this->itemsList as $ccCode=>$ccDetails) { $servObj = $xml->addChild('costCenter', $ccDetails['name']); $servObj['code'] = $ccCode; if ($ccDetails['disabled']) { $servObj['disabled'] = 1; } } $xml['default']=$this->defaultItem; return $xml; } public function getListActive() { $ccs = []; foreach($this->getAll() as $ccCode=>$ccDet) { if (!$ccDet['disabled']) { $ccs[$ccCode] = $ccDet['name']; } } return $ccs; } public function getName($code) { return $this->get($code)['name']; } public function isActive($code) { $details = $this->get($code); return !$details['disabled']; } }