<commit_before><?php
namespace SPHERE\Application\Contact\Address;

use SPHERE\Application\Contact\Address\Service\Data;
use SPHERE\Application\Contact\Address\Service\Entity\TblAddress;
use SPHERE\Application\Contact\Address\Service\Entity\TblCity;
use SPHERE\Application\Contact\Address\Service\Entity\TblState;
use SPHERE\Application\Contact\Address\Service\Entity\TblToCompany;
use SPHERE\Application\Contact\Address\Service\Entity\TblToPerson;
use SPHERE\Application\Contact\Address\Service\Entity\TblType;
use SPHERE\Application\Contact\Address\Service\Entity\ViewAddressToCompany;
use SPHERE\Application\Contact\Address\Service\Entity\ViewAddressToPerson;
use SPHERE\Application\Contact\Address\Service\Setup;
use SPHERE\Application\Corporation\Company\Service\Entity\TblCompany;
use SPHERE\Application\People\Person\Service\Entity\TblPerson;
use SPHERE\Common\Frontend\Ajax\Template\Notify;
use SPHERE\Common\Frontend\Form\IFormInterface;
use SPHERE\Common\Frontend\Icon\Repository\Ban;
use SPHERE\Common\Frontend\Icon\Repository\Exclamation;
use SPHERE\Common\Frontend\Message\Repository\Danger;
use SPHERE\Common\Frontend\Message\Repository\Success;
use SPHERE\Common\Window\Redirect;
use SPHERE\System\Database\Binding\AbstractService;

/**
 * Class Service
 *
 * @package SPHERE\Application\Contact\Address
 */
class Service extends AbstractService
{

    /**
     * @return false|ViewAddressToPerson[]
     */
    public function viewAddressToPersonAll()
    {

        return (new Data($this->getBinding()))->viewAddressToPersonAll();
    }

    /**
     * @return false|ViewAddressToCompany[]
     */
    public function viewAddressToCompanyAll()
    {

        return (new Data($this->getBinding()))->viewAddressToCompanyAll();
    }

    /**
     * @param bool $doSimulation
     * @param bool $withData
     *
     * @return string
     */
    public function setupService($doSimulation, $withData)
    {

        $Protocol = (new Setup($this->getStructure()))->setupDatabaseSchema($doSimulation);
        if (!$doSimulation && $withData) {
            (new Data($this->getBinding()))->setupDatabaseContent();
        }
        return $Protocol;
    }

    /**
     * @param integer $Id
     *
     * @return bool|TblCity
     */
    public function getCityById($Id)
    {

        return (new Data($this->getBinding()))->getCityById($Id);
    }

    /**
     * @param integer $Id
     *
     * @return bool|TblAddress
     */
    public function getAddressById($Id)
    {

        return (new Data($this->getBinding()))->getAddressById($Id);
    }

    /**
     * @return bool|TblCity[]
     */
    public function getCityAll()
    {

        return (new Data($this->getBinding()))->getCityAll();
    }

    /**
     * @return bool|TblState[]
     */
    public function getStateAll()
    {

        return (new Data($this->getBinding()))->getStateAll();
    }

    /**
     * @param string $Name
     *
     * @return bool|TblState
     */
    public function getStateByName($Name)
    {

        return (new Data($this->getBinding()))->getStateByName($Name);
    }

    /**
     * @return bool|TblAddress[]
     */
    public function getAddressAll()
    {

        return (new Data($this->getBinding()))->getAddressAll();
    }

    /**
     * @return bool|TblType[]
     */
    public function getTypeAll()
    {

        return (new Data($this->getBinding()))->getTypeAll();
    }

    /**
     * @param IFormInterface $Form
     * @param TblPerson $tblPerson
     * @param                $Street
     * @param                $City
     * @param                $Type
     * @param TblAddress|null $tblAddress
     *
     * @return bool
     */
    public function checkFormAddressToPerson(
        IFormInterface &$Form,
        TblPerson $tblPerson,
        $Street,
        $City,
        $Type,
        TblAddress $tblAddress = null
    ) {

        $Error = false;
        if (isset($Street['Name']) && empty($Street['Name'])) {
            $Form->setError('Street[Name]', 'Bitte geben Sie eine Strasse an');
            $Error = true;
        } else {
            $Form->setSuccess('Street[Name]');
        }
        if (isset($Street['Number']) && empty($Street['Number'])) {
            $Form->setError('Street[Number]', 'Bitte geben Sie eine Hausnummer an');
            $Error = true;
        } else {
            $Form->setSuccess('Street[Number]');
        }

        if (isset($City['Code']) && empty($City['Code'])) {
            $Form->setError('City[Code]', 'Bitte geben Sie eine Postleitzahl ein');
            $Error = true;
        } else {
            $Form->setSuccess('City[Code]');
        }
        if (isset($City['Name']) && empty($City['Name'])) {
            $Form->setError('City[Name]', 'Bitte geben Sie einen Namen ein');
            $Error = true;
        } else {
            $Form->setSuccess('City[Name]');
        }
        if (!($tblType = $this->getTypeById($Type['Type']))) {
            $Form->setError('Type[Type]', 'Bitte geben Sie einen Typ ein');
            $Error = true;
        } else {
            // control there is no other MainAddress
            if ($tblType->getName() == 'Hauptadresse') {
                $tblAddressMain = Address::useService()->getAddressByPerson($tblPerson);
                if ($tblAddressMain && $tblAddress->getId() != $tblAddressMain->getId()) {
                    $Form->setError('Type[Type]', '"'.$tblPerson->getFullName()
                        .'" besitzt bereits eine Hauptadresse');
                    $Error = true;
                }
            }
        }
        return $Error;
    }

    /**
     * @param IFormInterface $Form
     * @param TblPerson $tblPerson
     * @param array $Street
     * @param array $City
     * @param integer $State
     * @param array $Type
     * @param string $County
     * @param string $Nation
     * @param $Group
     *
     * @return IFormInterface|string|TblToPerson
     */
    public function createAddressToPerson(
        IFormInterface $Form,
        TblPerson $tblPerson,
        $Street,<commit_msg>fix: SSW-309 Validierung 2. Hauptadresse<commit_after><?php
namespace SPHERE\Application\Contact\Address;

use SPHERE\Application\Contact\Address\Service\Data;
use SPHERE\Application\Contact\Address\Service\Entity\TblAddress;
use SPHERE\Application\Contact\Address\Service\Entity\TblCity;
use SPHERE\Application\Contact\Address\Service\Entity\TblState;
use SPHERE\Application\Contact\Address\Service\Entity\TblToCompany;
use SPHERE\Application\Contact\Address\Service\Entity\TblToPerson;
use SPHERE\Application\Contact\Address\Service\Entity\TblType;
use SPHERE\Application\Contact\Address\Service\Entity\ViewAddressToCompany;
use SPHERE\Application\Contact\Address\Service\Entity\ViewAddressToPerson;
use SPHERE\Application\Contact\Address\Service\Setup;
use SPHERE\Application\Corporation\Company\Service\Entity\TblCompany;
use SPHERE\Application\People\Person\Service\Entity\TblPerson;
use SPHERE\Common\Frontend\Ajax\Template\Notify;
use SPHERE\Common\Frontend\Form\IFormInterface;
use SPHERE\Common\Frontend\Icon\Repository\Ban;
use SPHERE\Common\Frontend\Icon\Repository\Exclamation;
use SPHERE\Common\Frontend\Message\Repository\Danger;
use SPHERE\Common\Frontend\Message\Repository\Success;
use SPHERE\Common\Window\Redirect;
use SPHERE\System\Database\Binding\AbstractService;

/**
 * Class Service
 *
 * @package SPHERE\Application\Contact\Address
 */
class Service extends AbstractService
{

    /**
     * @return false|ViewAddressToPerson[]
     */
    public function viewAddressToPersonAll()
    {

        return (new Data($this->getBinding()))->viewAddressToPersonAll();
    }

    /**
     * @return false|ViewAddressToCompany[]
     */
    public function viewAddressToCompanyAll()
    {

        return (new Data($this->getBinding()))->viewAddressToCompanyAll();
    }

    /**
     * @param bool $doSimulation
     * @param bool $withData
     *
     * @return string
     */
    public function setupService($doSimulation, $withData)
    {

        $Protocol = (new Setup($this->getStructure()))->setupDatabaseSchema($doSimulation);
        if (!$doSimulation && $withData) {
            (new Data($this->getBinding()))->setupDatabaseContent();
        }
        return $Protocol;
    }

    /**
     * @param integer $Id
     *
     * @return bool|TblCity
     */
    public function getCityById($Id)
    {

        return (new Data($this->getBinding()))->getCityById($Id);
    }

    /**
     * @param integer $Id
     *
     * @return bool|TblAddress
     */
    public function getAddressById($Id)
    {

        return (new Data($this->getBinding()))->getAddressById($Id);
    }

    /**
     * @return bool|TblCity[]
     */
    public function getCityAll()
    {

        return (new Data($this->getBinding()))->getCityAll();
    }

    /**
     * @return bool|TblState[]
     */
    public function getStateAll()
    {

        return (new Data($this->getBinding()))->getStateAll();
    }

    /**
     * @param string $Name
     *
     * @return bool|TblState
     */
    public function getStateByName($Name)
    {

        return (new Data($this->getBinding()))->getStateByName($Name);
    }

    /**
     * @return bool|TblAddress[]
     */
    public function getAddressAll()
    {

        return (new Data($this->getBinding()))->getAddressAll();
    }

    /**
     * @return bool|TblType[]
     */
    public function getTypeAll()
    {

        return (new Data($this->getBinding()))->getTypeAll();
    }

    /**
     * @param IFormInterface $Form
     * @param TblPerson $tblPerson
     * @param                $Street
     * @param                $City
     * @param                $Type
     * @param TblAddress|null $tblAddress
     *
     * @return bool
     */
    public function checkFormAddressToPerson(
        IFormInterface &$Form,
        TblPerson $tblPerson,
        $Street,
        $City,
        $Type,
        TblAddress $tblAddress = null
    ) {

        $Error = false;
        if (isset($Street['Name']) && empty($Street['Name'])) {
            $Form->setError('Street[Name]', 'Bitte geben Sie eine Strasse an');
            $Error = true;
        } else {
            $Form->setSuccess('Street[Name]');
        }
        if (isset($Street['Number']) && empty($Street['Number'])) {
            $Form->setError('Street[Number]', 'Bitte geben Sie eine Hausnummer an');
            $Error = true;
        } else {
            $Form->setSuccess('Street[Number]');
        }

        if (isset($City['Code']) && empty($City['Code'])) {
            $Form->setError('City[Code]', 'Bitte geben Sie eine Postleitzahl ein');
            $Error = true;
        } else {
            $Form->setSuccess('City[Code]');
        }
        if (isset($City['Name']) && empty($City['Name'])) {
            $Form->setError('City[Name]', 'Bitte geben Sie einen Namen ein');
            $Error = true;
        } else {
            $Form->setSuccess('City[Name]');
        }
        if (!($tblType = $this->getTypeById($Type['Type']))) {
            $Form->setError('Type[Type]', 'Bitte geben Sie einen Typ ein');
            $Error = true;
        } else {
            // control there is no other MainAddress
            if ($tblType->getName() == 'Hauptadresse') {
                $tblAddressMain = Address::useService()->getAddressByPerson($tblPerson);
                if ($tblAddressMain && (($tblAddress && $tblAddress->getId() != $tblAddressMain->getId()) || !$tblAddress)) {
                    $Form->setError('Type[Type]', '"'.$tblPerson->getFullName()
                        .'" besitzt bereits eine Hauptadresse');
                    $Error = true;
                }
            }
        }
        return $Error;
    }

    /**
     * @param IFormInterface $Form
     * @param TblPerson $tblPerson
     * @param array $Street
     * @param array $City
     * @param integer $State
     * @param array $Type
     * @param string $County
     * @param string $Nation
     * @param $Group
     *
     * @return IFormInterface|string|TblToPerson
     */
    public function createAddressToPerson(
        IFormInterface $Form,
        TblPerson $tblPerson,
        $Street,
{'fraction_non_alphanumeric': 0.14395929087327644, 'fraction_numerical': 0.00032829940906106366, 'mean_word_length': 2.4083916083916086, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 79, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3938907', 'n_tokens_mistral': 3665, 'n_tokens_neox': 3564, 'n_words': 637}