{extends file="admin-layout.tpl"}

{block name="no-return-functions"}
    {$admin_current_location = 'configuration'}
{/block}

{block name="page-title"}{intl l='Thelia System Variables'}{/block}

{block name="check-resource"}admin.configuration.variable{/block}
{block name="check-access"}view{/block}

{block name="main-content"}
<div class="variables">

    <div id="wrapper" class="container">

        <ul class="breadcrumb">
            <li><a href="{url path='/admin/home'}">{intl l="Home"}</a></li>
            <li><a href="{url path='/admin/configuration'}">{intl l="Configuration"}</a></li>
            <li><a href="{url path='/admin/configuration/variables'}">{intl l="System variables"}</a></li>
        </ul>

        {hook name="variables.top" location="variables_top" }

        <div class="row">
            <div class="col-md-12">
                <form action="{url path='/admin/configuration/variables/update-values'}" method="post">
	                <div class="general-block-decorator">
                    <div class="table-responsive">
	                    <table class="table table-striped table-condensed table-left-aligned">
	                        <caption class="clearfix">
	                            {intl l='Thelia system variables'}
                                {include file='renderer/buttons.html' btn_group=false buttons=[
                                    [
                                        'type' => 'create',
                                        'class' => 'action-btn',
                                        'title' => {intl l='Add a new variable'},
                                        'href' => '#creation_dialog',
                                        'auth' => ['resource' => 'admin.configuration.variable'],
                                        'data' => [
                                            'toggle' => 'modal'
                                        ]
                                    ],
                                    [
                                        'type' => 'save',
                                        'class' => 'action-btn',
                                        'title' => {intl l='Save chages'},
                                        'auth' => ['resource' => 'admin.configuration.variable'],
                                        'with_text' => true,
                                        'tag' => 'button',
                                        'attr' => [
                                            'type' => 'submit'
                                        ]
                                    ]
                                ]}

	                        </caption>
                            <colgroup>
                                <col />
                                <col />
                                <col width="250" />
                                <col />
                            </colgroup>

                          <thead>
  	                        <tr>
  	                           <th>
                                 {admin_sortable_header
                                    current_order=$order
                                    order='title'
                                    reverse_order='title_reverse'
                                    path={url path='/admin/configuration/variables'}
                                    label={intl l='Purpose'}
                                 }
  	                           </th>

                                 <th>
                                 {admin_sortable_header
                                    current_order=$order
                                    order='name'
                                    reverse_order='name_reverse'
                                    path={url path='/admin/configuration/variables'}
                                    label={intl l='Name'}
                                 }
                                 </th>

                                 <th>
                                 {admin_sortable_header
                                    current_order=$order
                                    order='value'
                                    reverse_order='value_reverse'
                                    path={url path='/admin/configuration/variables'}
                                    label={intl l='Value'}
                                 }
                                 </th>

                                 {hook name="variables.table-header" location="variables_table_header" }

                                 <th>{intl l='Action'}</th>
  	                        </tr>
                          </thead>
                          <tbody>
  	                        {loop name="config" type="config" hidden="0" secured="*" backend_context="1" lang="$lang_id" order="$order"}
  	                            <tr>

  	                               <td>{$TITLE}</td>

                                     <td>
                                     {if ! $SECURED}
                                         {loop type="auth" name="can_change" role="ADMIN" resource="admin.configuration.variable" access="UPDATE"}
                                             <a title="{intl l='Change this variable'}" href="{url path='/admin/configuration/variables/update' variable_id="$ID"}">{$NAME}</a>
                                         {/loop}
                                         {elseloop rel="can_change"}
                                             {$NAME}
                                         {/elseloop}
                                     {else}
                                         {$NAME}
                                     {/if}
                                     </td>

  	                                <td>
  	                                    {if $SECURED}
  	                                        {$VALUE}
  	                                    {else}
                                            {if preg_match('/^(active-)([a-z]{3,5})(-template)$/',$NAME, $typeTemplate) }
                                                <select id="cancelable_edit_{$ID}" name="variable[{$ID}]" data-id="{$ID}" class="js-edit form-control">
                                                    {loop type="template" name="list_template" template-type=$typeTemplate[2]}
                                                        <option {if $VALUE == $NAME}selected{/if} value="{$NAME}">{$NAME}</option>
                                                    {/loop}
                                                </select>
                                            {else}
                                                <input id="cancelable_edit_{$ID}" class="js-edit form-control" data-id="{$ID}" type="text" name="variable[{$ID}]" value="{$VALUE}" />
  	                                        {/if}
                                        {/if}
  	                                </td>

  	                                {hook name="variables.table-row" location="variables_table_row" config_id={$ID} }

  	                                <td class="actions">
  	                                    {if ! $SECURED}
                                            {include file='renderer/buttons.html' buttons=[
                                                [
                                                    'type' => 'remove',
                                                    'id' => "cancel_edit_btn_$ID",
                                                    'class' => 'cancel-edit',
                                                    'title' => {intl l='Cancel changes and revert to original value'},
                                                    'href' => "#",
                                                    'auth' => ['resource' => 'admin.configuration.variable'],
                                                    'data' => [
                                                        'id' => $ID
                                                    ]
                                                ],
                                                [
                                                    'type' => 'edit',
                                                    'class' => 'config-change',
                                                    'title' => {intl l='Change this variable'},
                                                    'href' => {url path='/admin/configuration/variables/update' variable_id=$ID},
                                                    'auth' => ['resource' => 'admin.configuration.variable']
                                                ],
                                                [
                                                    'type' => 'delete',
                                                    'class' => 'config-delete',
                                                    'title' => {intl l='Delete this variable'},
                                                    'href' => '#delete_variable_dialog',
                                                    'data' => [
                                                        'id' => $ID,
                                                        'toggle' => 'modal'
                                                    ],
                                                    'auth' => ['resource' => 'admin.configuration.variable']
                                                ]
                                            ]}
  		                                {else}
  		                                    <span title="{intl l='This variable could not be changed.'}" class="glyphicon glyphicon-ban-circle"></span>
  	                                    {/if}
  	                                </td>
  	                            </tr>
  	                        {/loop}
                          </tbody>
	                    </table>
                    </div>
	                </div>
                </form>
            </div>
        </div>

        {hook name="variables.bottom" location="variables_bottom" }

    </div>
</div>


{* Adding a new variable *}


    {form name="thelia.admin.config.creation"}

        {* Capture the dialog body, to pass it to the generic dialog *}
        {capture "creation_dialog"}

			{form_hidden_fields}

			{form_field field='success_url'}
			{* on success, redirect to the edition page, _ID_ is replaced with the created variable ID, see controller  *}
			<input type="hidden" name="{$name}" value="{url path='/admin/configuration/variables/update' variable_id='_ID_'}" />
			{/form_field}

			{* We do not allow users to create hidden or secured variables from here *}

			{form_field field='hidden'}
			<input type="hidden" name="{$name}" value="0" />
			{/form_field}

			{form_field field='secured'}
			<input type="hidden" name="{$name}" value="0" />
			{/form_field}

		    {form_field field='name'}
		    <div class="form-group {if $error}has-error{/if}">
		        <label for="{$label_attr.for}" class="control-label">{$label} : </label>
		        <input type="text" id="{$label_attr.for}" required="required" name="{$name}" value="{$value}" title="{intl l='Variable name'}" placeholder="{intl l='Variable name'}" class="form-control">
		    </div>
		    {/form_field}

		    {form_field field='value'}
		    <div class="form-group {if $error}has-error{/if}">
		        <label for="{$label_attr.for}" class="control-label">{$label} : </label>
		        <input type="text" id="{$label_attr.for}" name="{$name}" value="{$value}" title="{intl l='Variable value'}" placeholder="{intl l='Variable value'}" class="form-control">
		    </div>
		    {/form_field}

		    {form_field field='title'}
		    <div class="form-group {if $error}has-error{/if}">
		        <label for="{$label_attr.for}" class="control-label">{$label} : </label>

		        {loop type="lang" name="default-lang" default_only="1"}

		            {* Switch edition to the current locale *}
		            <input type="hidden" name="edit_language_id" value="{$ID}" />

		            <div class="input-group">
		              <input type="text" id="{$label_attr.for}" required="required" name="{$name}" value="{$value}" title="{intl l='Variable purpose'}" placeholder="{intl l='Variable purpose'}" class="form-control">
		              <span class="input-group-addon"><img src="{image file="assets/img/flags/{$CODE}.png"}" alt="{$TITLE}" /></span>
		            </div>

		            <div class="help-block">{intl l='Enter here the category name in the default language (%title)' title={$TITLE}}</div>

		            {form_field field='locale'}
		              <input type="hidden" name="{$name}" value="{$LOCALE}" />
		            {/form_field}
		        {/loop}
		    </div>
		    {/form_field}


            {hook name="variable.create-form" location="variable_create_form" }
        {/capture}

	    {include
	        file = "includes/generic-create-dialog.html"

	        dialog_id    = "creation_dialog"
	        dialog_title = {intl l="Create a new variable"}
	        dialog_body  = {$smarty.capture.creation_dialog nofilter}

	        dialog_ok_label     = {intl l="Create this variable"}

	        form_action        = {url path='/admin/configuration/variables/create'}
	        form_enctype       = {form_enctype}
	        form_error_message = $form_error_message
	    }
    {/form}

    {* Delete category confirmation dialog *}

    {capture "delete_dialog"}
        <input type="hidden" name="variable_id" id="variable_delete_id" value="" />

        {hook name="variable.delete-form" location="variable_delete_form" }

    {/capture}

    {include
        file = "includes/generic-confirm-dialog.html"

        dialog_id       = "delete_variable_dialog"
        dialog_title    = {intl l="Delete a variable"}
        dialog_message  = {intl l="Do you really want to delete this variable ?"}

        form_action         = {token_url path='/admin/configuration/variables/delete'}
        form_content        = {$smarty.capture.delete_dialog nofilter}
    }
{/block}

{block name="javascript-initialization"}
<script>
    $(function() {

        // JS stuff for creation form
        {include
            file      = "includes/generic-js-dialog.html"
            dialog_id = "creation_dialog"
            form_name = "thelia.admin.config.creation"
        }

    	// Set proper variable ID in delete from
    	$('a.config-delete').click(function(ev) {
    		$('#variable_delete_id').val($(this).data('id'));
     	});

    	// Edition canceling management
        $('.cancel-edit').each(function() {
            var zis   = $(this);
            var field = $('#cancelable_edit_' + $(this).data('id'));

            zis.addClass('disabled');
            zis.data('original-value', field.val());

            zis.click(function(ev) {

                if (!zis.is('.disabled')) {
                    zis.addClass('disabled');
                    field.val(zis.data('original-value'));
                }

                ev.preventDefault();
            });
        })

        $('.js-edit').keyup(function() {
            $('#cancel_edit_btn_' + $(this).data('id')).removeClass('disabled');
        });

        $('select.js-edit').change(function() {
            $('#cancel_edit_btn_' + $(this).data('id')).removeClass('disabled');
        });

    });
</script>
{/block}

{block name="javascript-last-call"}
    {hook name="variables.js" location="variables-js" }
{/block}
