The Framework Code

twigs/framework/admin/config.twig

File List

{% set ajax = TRUE %}
{% set fwutils = TRUE %}
{% set parsley = TRUE %}

{% extends '@content/page.twig' %}

{% import '@content/iconmacro.twig' as i %}
{% import '@util/formmacro.twig' as f %}

{% set patch = constant('Config\\Config::PUTORPATCH') %}
{% set config = constant('Config\\Framework::CONFIG') %}

{% block setup %}
    function setlocal(e)
    {
        e.preventDefault();
        const t = $(this);
        const id = t.parent().parent().parent().parent().data('id');
        $.ajax('{{base}}/ajax/bean/{{config}}/'+id+'/local/', {
            method: '{{patch}}', // because we are updating an item
            data: {
                value: t.is(':checked') ? 1 : 0
            }
        }).fail(function(jx){
            bootbox.alert('<h3>Update failed</h3>'+jx.responseText);
        });
    }

    function delbean(e)
    {
        framework.dodelbean(e, this, '{{config}}');
    }
{% endblock setup %}

{% block onload %}
    $('#inval').hide();
    $('#cfr').on('submit', false);
    $('.delb').on('click', framework.delbean)
    $('.lval').on('change', setlocal)
    $('.cval').on('blur', function(e){
        e.preventDefault();
        const t = $(this);
        const id = t.parent().parent().data('id');
        $.ajax('{{base}}/ajax/bean/{{config}}/'+id+'/value/', {
            method: '{{patch}}', // because we are updating an item
            data: { value: t.val() }
        }).fail(function(jx){
            bootbox.alert('<h3>Update failed</h3>'+jx.responseText);
        });
// clear out integrity and crossorigin - they might be diferent
        $.ajax('{{base}}/ajax/bean/{{config}}/'+id+'/integrity/', {
            method: '{{patch}}', // because we are updating an item
            data: { value: '' }
        }).fail(function(jx){
            bootbox.alert('<h3>Update failed</h3>'+jx.responseText);
        });
        $.ajax('{{base}}/ajax/bean/{{config}}/'+id+'/crossorigin/', {
            method: '{{patch}}', // because we are updating an item
            data: { value: '' }
        }).fail(function(jx){
            bootbox.alert('<h3>Update failed</h3>'+jx.responseText);
        });
    });
    $('.nname').on('blur', function(e){
        e.preventDefault()
        const t = $(this)
        const name = t.val()
        if (name != '')
        {
            if (!name.match(/^[a-z][a-z0-9]*$/i))
            {
                bootbox.alert('Invalid name format')
                t.val('')
            }
            else if ($('input[name="'+name+'"]').length != 0)
            {
                bootbox.alert('That item already exists');
            }
            else
            {
                $('#inval').show();
                $('#Xnewval').focus();
            }
        }
    })
    $('.nval').on('blur', function(e){
        e.preventDefault();
        const t = $(this);
        const name = $('.nname').val();
        const value = t.val();
        const local = $('#Xnewlocal').is(':checked') ? 1 : 0;
        $.ajax('{{base}}/ajax/bean/{{config}}/', {
            method: 'POST', // because we are creating a new item
            data: {
                name: name,
                value: value,
                local: 0
            }
        }).done(function(data){
            $('.nname').parent().parent().prev().prev().after('<div class="row mb-2" data-id="'+data+'">'+
                '<label class="col-md-1 col-form-label" for="X'+name+'">'+name+'</label>'+
                    '<div class="col-md-9">'+
                            '<input type="text" id="X'+name+'" name="'+name+'" value="'+value+'" class="form-control cval"/>'+
                    '</div>'+
                    '<div class="col-md-2">'+
                        '<div class="form-check form-check-inline">'+
                            '<label class="form-check-label"><input class="lval nlval" type="checkbox" value="1" name="L'+name+'"/> Local</label>'+
                        '</div>{{i.delete('delb ndelb')}}</div></div>'
            );
            $('.ndelb').on('click', framework.framework.delbean).removeClass('ndelb')
            $('.nlval').on('change', setlocal).removeClass('nlval')
            $('.nname').val('');
            t.val('');
            $('#inval').hide();
        }).fail(function(jx){
            bootbox.alert('<h3>Failed to add new configuration item</h3>'+jx.responseText);
        });
    });
{% endblock onload %}

{% if not page is defined %}
    {% set page = 1 %}
    {% set pagesize = 10 %}
{% endif %}

{% set pages = siteinfo.pagecount(config, pagesize) %}

{% block header %}
    <section class="col-md-12 mt-5">
        <h1>Configuration Values</h1>
    </section>
{% endblock header %}

{% block main %}
    <section class="row">
        <article class="col-md-12">
            <form id="cfr" action="#" method="post">
                {% include '@util/paginate.twig' with { page : page, pagesize: pagesize, pages: pages} %}
                {% for p in siteinfo.siteconfig(page, pagesize) %}
                    <div class="row mb-2" data-id="{{p.getID}}">
                        <label class="col-md-1 col-form-label" for="X{{p.getID}}">{{p.name}}</label>
                        <div class="col-md-9">
                            <input type="text" id="X{{p.getID}}" name="{{p.name}}" value="{{p.value}}" class="form-control cval"/>
                        </div>
                        <div class="col-md-2">
                            <div class="form-check form-check-inline">
                                <label class="form-check-label"><input class="lval" type="checkbox" value="1" name="L{{p.name}}"{%if p.local %} checked="checked"{% endif %}/> Local</label>
                            </div>
                            <a href="{{base}}/admin/edit/{{constant('Config\\Framework::CONFIG')}}/{{p.getID}}/">{{i.edit('editb')}}</a>
                            {% if not p.fixed %}
                                {{i.delete('delb')}}
                            {% endif %}
                        </div>
                    </div>
                {% endfor %}
{# todo: fix item names in this bit to avoid potential future clashes of names added by others #}
                {% include '@util/paginate.twig' with { page : page, pagesize: pagesize, pages: pages} %}
                <div class="jumbotron">
                    {{f.text({label: 'New Config Item', name: 'newconf', class: 'nname', id: 'Xnewconf', ph: 'Name for new config item' })}}
                    <div id="inval">
                        {{f.text({label: 'New Value', name: 'newval', class: 'nval', id: 'Xnewval', ph: 'Value for new config item' })}}
                        {{f.select({label : 'Type', name: 'type',
                        options: [
                            { value: '',  text: '&mdash; Pick a Type &mdash;' },
                            { value: 'boolean', text: 'boolean' },
                            { value: 'css', text: 'css' },
                            { value: 'integer', text: 'integer' },
                            { value: 'js', text: 'js' },
                            { value: 'string', text: 'string' },
                        ]
                    })}}
                    </div>
                </div>
            </form>
        </div>
    </section>
{% endblock main %}

{% block pagefooter %}
{# I don't want a footer #}
{% endblock pagefooter %}