The Framework Code

twigs/framework/view/table.twig

File List

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

{% set x = context.web.addCSP('script-src', "'unsafe-inline'") %}

{% set allownew = view is not defined and object is not defined %}
{% set inline = view is not defined %}

{% set parsley = allownew %}
{% set editable = inline %}

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

{% import '@util/formmacro.twig' as f %}
{% import '@util/modalmacro.twig' as m %}

{% block setup %}
    {%if inline %} {# only want this if we are doing inline edits  #}
        function mkinline(type, name, msg, id, value)
        {
            return '{{f.inline(f.wrap('type'), f.wrap('name'), f.wrap('msg'), f.wrap('id'), f.wrap('value'))}}';
        }

        function paramset(params)
        {
            params.id = params.pk;
            params.bean = '{{bean.name}}';
            params.op = 'bean';
            return params;
        }

        function tabparamset(params)
        {
            params.id = params.pk;
            params.bean = '{{bean.name}}';
            params.op = 'table';
            return params;
        }
    {% endif %}
{% endblock setup %}

{% block onload %}
    {%if inline %}
        $('.ppedit').editable({
            ajaxOptions: { type: '{{constant('Config\\Config::PUTORPATCH')}}' },
            params: paramset,
            onblur: 'cancel',
            url: framework.editcall
        });
        $('.tabppedit').editable({
            ajaxOptions: { type: '{{constant('Config\\Config::PUTORPATCH')}}' },
            params: tabparamset,
            onblur: 'cancel',
            url: framework.editcall
        });
    {% endif %}
    {% if allownew %}
        $('#nfform').on('submit', false).parsley();
        $('#addb').on('click', function(e){
            e.preventDefault();
            const pn = $('#nfname').val().toLowerCase();
            let sp = null;
            let error = false;
            $('.fname').each(function(e){
                const t = $(this).text();
                if (t == pn)
                {
                    error = true;
                    return false;
                }
                if (t > pn)
                {
                    sp = $(this);
                    return false;
                }
            })
            if (error)
            {
                bootbox.alert('That field already exists');
                return;
            }
        })
        $('#nfield').on('show.bs.modal', function(e){
            $('#nfform input,select').val('');
        })
    {% else %}
        $('#searchb').on('click', function(e){
            e.preventDefault();
            $.get(base + '/ajax/tablesearch/{{bean.name}}'+'/'+$('#fname').val()+'/'+$('#fop').val(), {
                value:  $('#fval').val(),
            }).done(function(data){
                var bdy = $('#sres article');
                if (data.length == 0)
                {
                    bdy.html('<div class="alert alert-warning">No results</div>');
                }
                else
                {
                    htm = ''
                    for (let bn of data)
                    {
                        htm += '<table class="table table-striped table-hover border mt-2"><tbody>';
                        for (var fld in bn)
                        {
                            htm += '<tr><td width="10em">' + fld + '</td><td>' + bn[fld] + '</td></tr>';
                        }
                    }
                    bdy.html(htm + '</tbody></table>');
                }
                $('#tsearch').modal('hide');
            }).fail(function(jx){
                bootbox.alert('<h3>Search failed</h3>'+jx.responseText);
            });
        });
    {% endif %}
{% endblock onload %}

{% block header %}
    <section class="col-md-12 mt-5">
        <h1>Bean &ldquo;{{bean.name}}&rdquo;</h1>
    </section>
{% endblock header %}

{% set foptions = [] %}
{% block main %}
    <section class="row">
        {% include '@util/message.twig' %}
        <article class="ml-auto col-md-10 mr-auto">
            <table class="table table-responsive table-striped">
                <tbody>
                    {% for name,type in bean.fields %}
                        {% set foptions = foptions|merge([{value: name}]) %}
                        <tr>
                            <td class="fname">{% if view is defined or name == 'id' %}{{name}}{% else %}{{f.inline('text', 'name', 'Enter new field name', name, name, 'tabppedit')}}{% endif %}</td>
                            <td>{% if view is defined or name == 'id' %}{{type}}{% else %}{{f.inline('text', 'type', 'Enter new field type', name, type, 'tabppedit')}}{% endif %}</td>
                            {% if object is defined %}
                                {% set x = attribute(object, name) %}
                                {% if name ends with '_id' %}
                                    {% set xn = name|split('_') %}
                                    <td><a href="{{base}}/admin/{{ view is defined ? 'view' : 'edit'}}/table/{{xn[0]}}/{{x}}">{{xn[0]}} {{x}}</a></td>
                                {% else %}
                                    <td>{% if view is defined  or name == 'id' %}{{x}}{% else %}{{f.inline('text', name, 'Enter '~name, object.getID, x)}}{% endif %}</td>
                                {% endif %}
                            {% endif %}
                        </tr>
                    {% endfor %}
                </tbody>
            </table>
            {% if allownew %}
                <p><button class="btn btn-primary" data-toggle="modal" data-target="#nfield" type="button">Add Field</button></p>
            {% else %}
                <p><button class="btn btn-primary" data-toggle="modal" data-target="#tsearch" type="button">Search</button></p>
            {% endif %}
        </article>
    </section>
    {% if allownew %}
        {{m.open({is: 'nfield', title: 'New Field'})}}
        <div class="modal-body">
            <form action="#" method="POST" id="nfform">
                {{f.text({label: 'Name', id: 'nfname', ph: 'Field Name', required: TRUE, parsley: {pattern: '[A-Za-z][a-zA-Z0-9]*'} })}}
                {{f.text({label: 'Sample Value', id: 'nsamp', ph: 'Sample field value'})}}
            </form>
        </div>
        {{m.close({action: 'Add', id: 'addb'})}}
    {% else %}
        {{m.open({id: 'tsearch', title: 'Search'})}}
        <div class="modal-body">
            <form action="#" method="POST" id="sform">
                {{f.select({label: 'Field', id: 'fname', ph: 'Field Name', required: TRUE, options: foptions })}}
                {{f.select({label: 'Operator', id: 'fop', ph: 'Comparison', required: TRUE, options: [
                   {value: 1, text: '=' },
                   {value: 2, text:  '!=' },
                   {value: 3, text:  'like' },
                   {value: 4, text:  'contains' },
                   {value: 5, text:  '>' },
                   {value: 6, text:  '>=' },
                   {value: 7, text:  '<' },
                   {value: 8, text:  '<=' },
                   {value: 9, text:  'regexp' },
                   {value: 10, text:  'is NULL' },
                   {value: 11, text:  'is not NULL' },
                ]})}}
                {{f.text({label: 'Value', id: 'fval', ph: 'Field Value'})}}
            </form>
        </div>
        {{m.close({action: 'Search', id: 'searchb'})}}
    {% endif %}
    <section id="sres" class="row">
        <article class="ml-auto col-md-10 mr-auto">
        </article>
    </section>
{% endblock main %}

{% block pagefooter %}
{% endblock pagefooter %}