studioespresso/craft-exporter

View on GitHub
src/templates/sprig/element/step_1.twig

Summary

Maintainability
Test Coverage
{% if export is not defined %}
    {% set export = exportId ? craft.exports.id(exportId).one() : create('studioespresso\\exporter\\elements\\ExportElement') %}
{% endif %}
{% import "_includes/forms" as forms %}
{% import _self as template %}

<form sprig s-action="exporter/element/step1" s-method="post">
    {% if export.id %}
        {{ hiddenInput('elementId', export.id) }}
    {% endif %}

    {{ forms.textField({
        first: true,
        label: "Name"|t('exporter'),
        instructions: "What this export will be called in the CP."|t('exporter'),
        id: 'name',
        name: 'name',
        value: name ?? export.name,
        errors: (errors is defined and attribute(errors, 'name') is defined) ? attribute(errors, 'name'): false,
        required: true
    }) }}

    {% if export.id %}
        {% set settings = settings ?? export.getSettings() %}
        {% set elementType =  elementType ?? export.elementType %}
        {% set showSiteOptions = true %}
    {% else %}
        {% if elementType is not defined %}
            {% set elementType = "craft\\elements\\Entry" %}
        {% endif %}
        {% set showSiteOptions = false %}
    {% endif %}

    <div class="field" tabindex="-1">
        <div class="heading">
            <label id="elementType-label" for="elementType">{{ 'Element Type'|t('exporter') }}<span
                        class="visually-hidden">Required</span><span class="required" aria-hidden="true"></span></label>
        </div>
        <div id="elementType-instructions" class="instructions">
            <p>{{ 'Choose which type of element you want to export.'|t('exporter') }}</p>
        </div>
        <div class="input ltr">
            <div class="select">
                <select sprig name="elementType" id="elementType" aria-describedby="elementType-instructions"
                        aria-labelledby="elementType-label">
                    {% for t, element in elementTypeOptions %}
                        <option value="{{ t }}" {{ elementType == t ? 'selected' }}>{{ element }}</option>
                    {% endfor %}
                </select>
            </div>
        </div>
    </div>

    {% set group = group ?? settings.group ?? '' %}
    {% set subGroup = subGroup ?? settings.subGroup ?? '' %}

    {% if elementType is defined %}
        {% set elementOptions = craft.exporter.getElementTypeSettings(elementType) %}
        {% if elementOptions.group is defined %}
            {% set groupOptions = elementOptions.getGroup() %}
            <div class="flex-fields">
                <div class="field width-50" id="elementType-entry-subfields">
                    <div class="heading">
                        <label id="group-label" for="group">{{ groupOptions.label|t('exporter') }}<span
                                    class="visually-hidden">{{ 'Required'|t('exporter') }}</span>
                            <span class="required" aria-hidden="true"></span>
                        </label>
                    </div>
                    {% if groupOptions.items|length %}
                        {% if groupOptions.instructions is defined %}
                            <div id="group-instructions" class="instructions">
                                <p>{{ groupOptions.instructions|t('exporter') }}</p>
                            </div>
                        {% endif %}
                        <div class="input ltr {% if errors is defined and attribute(errors, 'group') is defined %}errors{% endif %}">
                            <div class="select">
                                <select sprig name="settings[group]" id="group" aria-describedby="group-instructions"
                                        aria-labelledby="group-label">
                                    <option value="" selected>---</option>
                                    {% for g in groupOptions.items %}
                                        <option value="{{ g.id }}" {{ group == g.id ? 'selected' }}>
                                            {% if groupOptions.nameProperty is defined %}
                                                {{ attribute(g, groupOptions.nameProperty) }}
                                            {% else %}
                                                {{ g.name }}
                                            {% endif %}
                                        </option>
                                    {% endfor %}
                                </select>
                            </div>
                        </div>
                        {{ template.renderErrors(errors ?? [], 'group') }}
                    {% else %}
                        <div class="error">{{ "No groups found to export"|t('exporter') }}</div>
                    {% endif %}
                </div>
                {% if elementOptions.subGroup is defined and (settings is defined and settings.group is defined) %}
                    {% set subGroupSettings = elementOptions.getSubGroup() %}
                    {% if subGroupSettings %}
                        <div class="field width-50" id="elementType-entry-subfields">
                            <div class="heading">
                                <label id="section-label" for="section">{{ subGroupSettings.label|t('exporter') }}<span
                                            class="visually-hidden">{{ 'Required'|t('exporter') }}</span><span
                                            class="required"
                                            aria-hidden="true"></span></label>
                            </div>
                            {% if subGroupSettings.instructions is defined %}
                                <div id="section-instructions" class="instructions">
                                    <p>Choose which entry type you want to export</p>
                                </div>
                            {% endif %}
                            <div class="input ltr">
                                <div class="select">
                                    <select sprig name="settings[subGroup]" id="section"
                                            aria-describedby="section-instructions"
                                            aria-labelledby="section-label">
                                        <option value="" selected>---</option>
                                        {% for s in craft.exporter.getSubGroupItems(subGroupSettings, group) %}
                                            <option value="{{ s.id }}" {{ subGroup == s.id ? 'selected' }}>{{ s.name }}</option>
                                        {% endfor %}
                                    </select>
                                </div>
                            </div>
                        </div>
                    {% endif %}
                {% endif %}
            </div>
        {% endif %}
    {% endif %}

    {% set isReady =  false %}
    {% if settings is defined and settings.group is defined and settings.subGroup is defined %}
        {% set isReady = true %}
    {% endif %}

    {% if isReady %}
        {% set showSiteOptions = not elementOptions.shouldBeLocalised ? elementOptions.shouldBeLocalised : true %}
    {% endif %}

    {% if showSiteOptions %}

        <div class="field">
            <div class="heading">
                <label id="site-label" for="elementType">Site<span
                            class="visually-hidden">Required</span><span class="required"
                                                                         aria-hidden="true"></span></label>
            </div>
            <div id="elementType-instructions" class="instructions"><p>Select one or our sites from which you want to
                    export
                    elements.</p>
            </div>
            <div class="input ltr">
                <ul>
                    {% for s in craft.app.sites.getAllSites() %}
                        <li>
                            <input type="checkbox" id="{{ s.id }}" class="checkbox" name="settings[sites][]"
                                   value="{{ s.id }}"
                                   {% if settings is defined and settings.sites is defined %}{% if s.id in settings.sites %}checked{% endif %}{% endif %} >
                            <label for="{{ s.id }}">
                                {{ s.name }}
                            </label>
                        </li>
                    {% endfor %}
                </ul>
            </div>
        </div>
    {% endif %}
    <hr>
    <div class="field">
        {{ hiddenInput('nextStep', '2') }}
        <button type="submit" class="btn submit">{{ "Next" }}</button>
    </div>

</form>

{% macro renderErrors(errors, attr) %}
    {% if attribute(errors, attr) is defined %}
        <ul id="name-errors" class="errors">
            {% for e in attribute(errors, attr) %}
                <li>
                    <span class="visually-hidden">Error:</span>
                    {{ e }}
                </li>
            {% endfor %}
        </ul>
    {% endif %}
{% endmacro %}