{% import _self as self %}<?php
use Sulu\Component\Localization\Localization;
use Sulu\Component\Webspace\Manager\WebspaceCollection;
use Sulu\Component\Webspace\Portal;
use Sulu\Component\Webspace\PortalInformation;
use Sulu\Component\Webspace\Environment;
use Sulu\Component\Webspace\Security;
use Sulu\Component\Webspace\Segment;
use Sulu\Component\Webspace\Url;
use Sulu\Component\Webspace\CustomUrl;
use Sulu\Component\Webspace\Webspace;
use Sulu\Component\Webspace\Navigation;
use Sulu\Component\Webspace\NavigationContext;

/**
 * {{ cache_class }}
 *
 * DO NOT EDIT
 * This file is autogenerated by the Sulu Webspace component
 */
class {{ cache_class }} extends {{ base_class }}
{
    public function __construct()
    {
        $webspaceRefs = array();
        $portalRefs = array();
        $localizationRefs = array();
        $segmentRefs = array();
        $portalInformationRefs = array();

{% for webspace in collection.webspaces %}
        // new webspace
        $webspace = new Webspace();
        $webspace->setKey('{{ webspace.key }}');
        $webspace->setName('{{ webspace.name }}');
{% if webspace.security != null %}
        $security = new Security();
        $security->setSystem('{{ webspace.security.system }}');
        $security->setPermissionCheck({{ webspace.security.permissionCheck ? 'true' : 'false' }});
        $webspace->setSecurity($security);
{% endif %}
{% for localization in webspace.localizations %}
        // add localization to webspace
        $localization0 = new Localization();
        $localization0->setLanguage('{{ localization.language }}');
        $localization0->setCountry('{{ localization.country }}');
        $localization0->setShadow('{{ (localization.shadow) }}');
        $localization0->setDefault({{ localization.default ? 'true' : 'false' }});
        $localization0->setXDefault('{{ localization.xDefault }}');
{% for childLocalization in localization.children %}
{{ self.create_localizations(childLocalization, 1, 0, webspace) }}
{% endfor %}
        $localizationRefs['{{ webspace.key }}_{{ localization.localization }}'] = $localization0;
        $webspace->addLocalization($localization0);
{% endfor %}
{% for segment in webspace.segments %}
        // add segment to webspace
        $segment = new Segment();
        $segment->setKey('{{ segment.key }}');
        $segment->setMetadata({{ self.renderArray(segment.metadata)}});
        $segment->setDefault({{ segment.default ? 'true' : 'false' }});
        $webspace->addSegment($segment);
        $segmentRefs['{{ webspace.key }}_{{ segment.key }}'] = $segment;

{% endfor %}
        // add theme
{% if webspace.theme != null %}
        $webspace->setTheme('{{ webspace.theme }}');
{% endif %}


{% for type,template in webspace.templates %}        $webspace->addTemplate('{{ type|raw }}', '{{ template }}');
{% endfor %}
{% for type,template in webspace.defaultTemplates %}        $webspace->addDefaultTemplate('{{ type }}', '{{ template }}');
{% endfor %}
{% for template in webspace.excludedTemplates %}        $webspace->addExcludedTemplate('{{ template }}');
{% endfor %}

        // add navigation
        $navigation = new Navigation();
{% for context in webspace.navigation.contexts %}        $navigation->addContext(new NavigationContext('{{ context.key }}', {{ self.renderArray(context.metadata) }}));
{% endfor %}        $webspace->setNavigation($navigation);
        $webspace->setResourceLocatorStrategy('{{ webspace.resourceLocator.strategy }}');

{% for portal in webspace.portals %}

        // new portal
        $portal = new Portal();
        $portal->setName('{{ portal.name }}');
        $portal->setKey('{{ portal.key }}');
        $portal->setWebspace($webspace);
{% for localization in portal.localizations %}

        // add localization
        $localization = new Localization();
        $localization->setLanguage('{{ localization.language }}');
        $localization->setCountry('{{ localization.country }}');
        $localization->setDefault('{{ localization.default }}');
        $localization->setXDefault('{{ localization.xDefault }}');
        $portal->addLocalization($localization);
{% endfor %}

{% for environment in portal.environments %}

        // add environment
        $environment = new Environment();
        $environment->setType('{{ environment.type }}');
{% for url in environment.urls %}

        // add environment url
        $url = new Url();
        $url->setUrl('{{ url.url }}');
        $url->setLanguage('{{ url.language }}');
        $url->setCountry('{{ url.country }}');
        $url->setSegment('{{ url.segment }}');
        $url->setRedirect('{{ url.redirect }}');
        $url->setMain({{ url.main?'true':'false' }});
        $environment->addUrl($url);
{% endfor %}
{% for customUrl in environment.customUrls %}
        $environment->addCustomUrl(new CustomUrl('{{ customUrl.url }}'));
{% endfor %}

        $portal->addEnvironment($environment);
{% endfor %}
        $portalRefs['{{ portal.key }}'] = $portal;
        $webspace->addPortal($portal);

{% endfor %}
        $webspaceRefs['{{ webspace.key }}'] = $webspace;


{% endfor %}

{% for environmentKey, environment in collection.portalInformations %}
{% for portalInformation in environment %}
        $portalInformationRefs['{{ environmentKey }}']['{{ portalInformation.url }}'] = new PortalInformation(
            {{ portalInformation.type }},
            $webspaceRefs['{{ portalInformation.webspace }}'],
{% if portalInformation.portal %}
            $portalRefs['{{ portalInformation.portal }}'],
{% else %}
            null,
{% endif %}
{% if portalInformation.localization %}
            $localizationRefs['{{ portalInformation.webspace }}_{{ portalInformation.localization.localization }}'],
{% else %}
            null,
{% endif %}
            '{{ portalInformation.url }}',
{% if portalInformation.segment %}
            $segmentRefs['{{ portalInformation.webspace }}_{{ portalInformation.segment }}'],
{% else %}
            null,
{% endif %}
{% if portalInformation.redirect %}
            '{{ portalInformation.redirect }}',
{% else %}
            null,
{% endif %}
{% if portalInformation.main %}
            true,
{% else %}
            false,
{% endif %}
{% if portalInformation.urlExpression %}
            '{{ portalInformation.urlExpression }}',
{% else %}
            null,
{% endif %}
            {{ portalInformation.priority }}
        );

{% endfor %}
{% endfor %}

        $this->setWebspaces($webspaceRefs);
        $this->setPortals($portalRefs);
        $this->setPortalInformations($portalInformationRefs);
    }
}

{% macro renderArray(array) %}{% import _self as self %}
    array(
    {% for key, value in array %}
        {% if value is iterable %}
            '{{ key }}' => {{ self.renderArray(value) }},
        {% else %}
            '{{ key }}' => '{{ value }}',
        {% endif %}
    {% endfor %}
    )
{% endmacro %}

{% macro create_localizations(localization, count, parent, webspace) %}{% import _self as self %}
    // parent id {{ parent }}
    $localization{{ count }} = new Localization();
    $localization{{ count }}->setLanguage('{{ localization.language }}');
    $localization{{ count }}->setCountry('{{ localization.country }}');
    $localization{{ count }}->setShadow('{{ (localization.shadow) }}');
    $localization{{ count }}->setParent($localization{{ parent }});
    $localization{{ count }}->setDefault('{{ localization.default }}');
    $localization{{ count }}->setXDefault('{{ localization.xDefault }}');

    $localizationRefs['{{ webspace.key }}_{{ localization.localization }}'] = $localization{{ count }};

    {% for childLocalization in localization.children %}
        {{ self.create_localizations(childLocalization, count + 1, count) }}
    {% endfor %}
    $localization{{ count - 1 }}->addChild($localization{{ count }});
{% endmacro %}
