{*
/*************************************************************************************/
/*      This file is part of the RewriteUrl module for Thelia.                       */
/*                                                                                   */
/*      Copyright (c) OpenStudio                                                     */
/*      email : dev@thelia.net                                                       */
/*      web : http://www.thelia.net                                                  */
/*                                                                                   */
/*      For the full copyright and license information, please view the LICENSE.txt  */
/*      file that was distributed with this source code.                             */
/*************************************************************************************/
*}
<style>
    #module-RewriteUrl .js-target-url {
        background-color: #f9f9f9;
        font-weight: bold;
    }
</style>
<div id="module-RewriteUrl">
    <div class="row">
        <div class="col-md-12">
            <div class="table-responsive">
                <table class="table table-striped table-condensed">
                    <caption>
                        {intl l='Default url' d="rewriteurl.bo.default"}
                        <button type="button" class="btn btn-default pull-right js-popover" data-html="true" data-container="body" data-toggle="popover" data-placement="left"
                                data-content="{intl l='For questions or bug reporting, thank you to use %url.' url='<a href=\'https://github.com/thelia-modules/RewriteUrl\' target=\'_blank\'>GitHub</a>' d="rewriteurl.bo.default"}">
                            <i class="glyphicon glyphicon-info-sign"></i>
                        </button>
                    </caption>

                    {ifloop rel="rewriteUrl"}
                        <thead>
                        <tr>
                            <th>#</th>
                            <th>{intl l='View locale' d="rewriteurl.bo.default"}</th>
                            <th>{intl l='Url' d="rewriteurl.bo.default"}</th>
                            <th class="actions">{intl l='Actions' d="rewriteurl.bo.default"}</th>
                        </tr>
                        </thead>

                        <tbody>
                        {loop type="rewrite_url" name="rewriteUrl" view_id="{$ID}" view="{$viewName}" redirect="0"}
                            <tr data-id="{$ID_URL}">
                                <td>
                                    {$ID_URL}
                                </td>
                                <td>
                                    {$code = "_"|explode:$VIEW_LOCALE}
                                    <img src="{image file="assets/img/flags/{$code[0]}.png"}" alt="{$VIEW_LOCALE}" title="{$VIEW_LOCALE}"/>
                                </td>
                                <td>
                                    {$URL}
                                </td>
                                <td class="actions">
                                    <a class="btn btn-default btn-danger btn-xs js-action-delete" title="{intl l='Delete this redirect' d="rewriteurl.bo.default"}"  href="#rewrite_delete_dialog" data-url="{$URL}" data-id="{$ID_URL}" data-is_default="{$IS_DEFAULT}" data-toggle="modal"><i class="glyphicon glyphicon-trash"></i></a>
                                    <a class="btn btn-default btn-info btn-xs js-action-reassign" title="{intl l='Reassign this redirect' d="rewriteurl.bo.default"}"  data-url="{$URL}" data-id="{$ID_URL}" data-is_default="{$IS_DEFAULT}"><i class="glyphicon glyphicon-refresh"></i></a>
                                </td>
                            </tr>
                        {/loop}
                        </tbody>
                    {/ifloop}
                </table>
            </div>
            <div class="table-responsive">
                <table class="table table-striped table-condensed">
                    <caption>
                        {intl l='Url redirected' d="rewriteurl.bo.default"}
                    </caption>

                    {ifloop rel="rewriteUrl"}
                        <thead>
                        <tr>
                            <th>#</th>
                            <th>{intl l='View locale' d="rewriteurl.bo.default"}</th>
                            <th>{intl l='Url' d="rewriteurl.bo.default"}</th>
                            <th>{intl l='Redirected' d="rewriteurl.bo.default"}</th>
                            <th>{intl l='Type' d="rewriteurl.bo.default"}</th>
                            <th class="actions">{intl l='Actions' d="rewriteurl.bo.default"}</th>
                        </tr>
                        </thead>

                        <tbody>
                        {loop type="rewrite_url" name="rewriteUrl" view_id="{$ID}" view="{$viewName}" redirect="1"}
                            <tr data-id="{$ID_URL}">
                                <td>
                                    {$ID_URL}
                                </td>
                                <td>
                                    {$code = "_"|explode:$VIEW_LOCALE}
                                    <img src="{image file="assets/img/flags/{$code[0]}.png"}" alt="{$VIEW_LOCALE}" title="{$VIEW_LOCALE}"/>
                                </td>
                                <td>
                                    {$URL}
                                </td>
                                <td class="object-title">
                                    <span class="js-action-view-redirected">{$REDIRECTED}</span>
                                </td>
                                <td>
                                    <select class="js-select-redirect-type" data-idurl="{$ID_URL}" data-httpcode="{$HTTPCODE}">
                                        <option value="301" {if $HTTPCODE == 301}selected{/if}>{intl l="Permanent redirect" d="rewriteurl.bo.default"}</option>
                                        <option value="302" {if $HTTPCODE == 302}selected{/if}>{intl l="Temporary redirect" d="rewriteurl.bo.default"}</option>
                                    </select>
                                </td>
                                <td class="actions">
                                    <div class="btn-group">
                                        <a class="btn btn-default btn-primary btn-xs js-action-default" title="{intl l='Set this redirect to default' d="rewriteurl.bo.default"}"  href="#rewrite_default_dialog" data-url="{$URL}" data-id="{$ID_URL}" data-toggle="modal"><i class="glyphicon glyphicon-home"></i></a>
                                        <a class="btn btn-default btn-danger btn-xs js-action-delete" title="{intl l='Delete this redirect' d="rewriteurl.bo.default"}"  href="#rewrite_delete_dialog" data-url="{$URL}" data-id="{$ID_URL}" data-is_default="{$IS_DEFAULT}" data-toggle="modal"><i class="glyphicon glyphicon-trash"></i></a>
                                        <a class="btn btn-default btn-info btn-xs js-action-reassign" title="{intl l='Reassign this redirect' d="rewriteurl.bo.default"}"  data-url="{$URL}" data-id="{$ID_URL}" data-is_default="{$IS_DEFAULT}"><i class="glyphicon glyphicon-refresh"></i></a>
                                    </div>
                                </td>
                            </tr>
                        {/loop}
                        </tbody>
                    {/ifloop}
                    {elseloop rel="rewriteUrl"}
                        <tbody>
                            <tr>
                                <td colspan="5">
                                    <div class="alert alert-info">
                                        {intl l="No redirected url." d="rewriteurl.bo.default"}
                                    </div>
                                </td>
                            </tr>
                        </tbody>
                    {/elseloop}
                </table>
                <div class="title title-without-tabs clearfix">
                    {intl l="New url" d="rewriteurl.bo.default"}
                </div>
                {form name="rewriteurl.add.form"}
                    <form class="js-form-add" action="{url path='/admin/module/rewriteurl/add' }" method="post">

                        {form_hidden_fields form=$form}

                        {if $form_error}
                            <div class="alert alert-danger">{$form_error_message nofilter}</div>
                        {/if}

                        <div class="alert alert-danger js-alert-error hide"></div>

                        {form_field form=$form field='success_url'}
                            <input type="hidden" name="{$name}" value="{url path={navigate to="current"} current_tab="modules"}" />
                        {/form_field}

                        {form_field form=$form field="view"}
                            <input type="hidden" name="{$name}" value="{$viewName}"/>
                        {/form_field}

                        {form_field form=$form field="view-id"}
                            <input type="hidden" name="{$name}" value="{$ID}"/>
                        {/form_field}

                        <div class="row">
                            <div class="col-md-2">
                                <div class="form-group ">
                                {form_field form=$form field="locale"}
                                    <select class="form-control" name="{$name}" id="">
                                        {loop type="lang" name="lang_loop"}
                                            <option value="{$LOCALE}"{if $LOCALE == $data}selected{/if} >
                                                {$TITLE}
                                            </option>
                                        {/loop}
                                    </select>
                                {/form_field}
                                </div>
                            </div>
                            <div class="col-md-4">
                                <div class="form-group">
                                {form_field form=$form field="url"}
                                    <div class="input-group">
                                        <span class="input-group-addon">{$url_language|default:{config key="url_site"}}/</span>
                                        <input type="text" name="{$name}" value="{$data}" class="form-control js-input-url-add" placeholder="Url" required>
                                    </div>
                                {/form_field}
                                </div>
                            </div>
                            <div class="col-md-2">
                                <div class="form-group ">
                                {form_field form=$form field="default"}
                                    <select class="form-control" name="{$name}">
                                        <option value="0" {if !$data}selected{/if}>{intl l="Redirect to default" d="rewriteurl.bo.default"}</option>
                                        <option value="1" {if $data}selected{/if}>{intl l="Default" d="rewriteurl.bo.default"}</option>
                                    </select>
                                {/form_field}
                                </div>
                            </div>
                            <div class="col-md-2">
                                <div class="form-group ">
                                    {form_field form=$form field="httpcode"}
                                    <select class="form-control" name="{$name}">
                                        <option value="301" {if !$data}selected{/if}>{intl l="Permanent redirect" d="rewriteurl.bo.default"}</option>
                                        <option value="302" {if $data}selected{/if}>{intl l="Temporary redirect" d="rewriteurl.bo.default"}</option>
                                    </select>
                                    {/form_field}
                                </div>
                            </div>
                            <div class="col-md-2">
                                <button type="submit" class="btn btn-primary js-action-add">{intl l="Validate" d="rewriteurl.bo.default"}</button>
                            </div>
                        </div>
                    </form>
                {/form}
            </div>
        </div>

        {loop type="rewrite_url" name="rewriteUrl" view_id=$ID view=$viewName limit=1}
        <div class="col-md-12">
            <p class="title title-without-tabs">
                {intl l='Redirect all urls' d="rewriteurl.bo.default"}
            </p>
            <div class="alert alert-warning">
                <div class="row">
                    <div class="col-md-6">
                        {intl l="Redirect all urls on a (category, product, folder, content, brand)." d="rewriteurl.bo.default"}
                        <br />
                        {intl l="This action is irreversible after confirmation." d="rewriteurl.bo.default"}
                    </div>
                    <div class="col-md-6">
                        <a href="#" class="btn btn-danger js-action-redirect-all" data-id="{$ID_URL}">
                            {intl l='Redirect all urls' d="rewriteurl.bo.default"}
                        </a>
                    </div>
                </div>
            </div>
        </div>
        {/loop}
    </div>

    <div class="modal fade js-modal-search" tabindex="-1" role="dialog" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    <h3></h3>
                </div>
                <div class="modal-body align-center">
                    <span class="js-info-redirected">
                        {intl l="Redirect the url %html on :" d="rewriteurl.bo.default" html="<span class=\"js-target-url\"></span>"}
                    </span>
                    <br/><br/>
                    <form class="js-form-search">
                        <div class="form-group">
                            <div class="input-group">
                                <div class="input-group-addon">{intl l="Search" d="rewriteurl.bo.default"}</div>
                                <input name="search" class="js-input-search form-control" type="text" placeholder="{intl l="Title, Ref ..." d="rewriteurl.bo.default"}"/>
                            </div>
                        </div>
                        <div class="js-loader text-center hide">{intl l="Please wait ..." d="rewriteurl.bo.default"}</div>
                        <div class="alert alert-warning text-center hide js-alert-no-result">
                            {intl l="No results found for your search." d="rewriteurl.bo.default"}
                        </div>
                        <div class="form-group js-block-search-result hide">
                            <select class="form-control" size="10"></select>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    {form name="rewriteurl.reassign.form"}
                        <form class="form-reassign form-horizontal" action="{url path='/admin/module/rewriteurl/reassign'}" method="post">

                            {if $form_error}<div class="alert alert-danger">{$form_error_message}</div>{/if}

                            {form_hidden_fields form=$form}

                            {form_field form=$form field='success_url'}
                                <input type="hidden" name="{$name}" value="{url path={navigate to="current"} current_tab="modules"}" />
                            {/form_field}

                            {form_field form=$form field="rewrite-id"}
                                <input type="hidden" name="{$name}" value="" class="js-input-url-id" />
                            {/form_field}

                            {form_field form=$form field="select-reassign"}
                                <input type="hidden" name="{$name}" value="" class="js-input-reassign" />
                            {/form_field}

                            {form_field form=$form field="all"}
                                <input type="hidden" name="{$name}" value="" class="js-input-all" />
                            {/form_field}

                            <button type="button" class="btn btn-default" data-dismiss="modal">{intl l="Close" d="rewriteurl.bo.default"}</button>

                            <input type="submit" class="btn btn-primary"/>
                        </form>
                    {/form}
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->

    {* -- Delete RewriteUrl confirmation dialog ----------------------------------- *}

    {capture "rewrite_delete_dialog"}
        <input type="hidden" name="id_url" class="js-input-delete" value="" />
    {/capture}

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

    dialog_id       = "rewrite_delete_dialog"
    dialog_title    = {intl l="Delete Url" d="rewriteurl.bo.default"}
    dialog_message  = {intl l="Do you really want to delete the url %html ?" d="rewriteurl.bo.default" html="<span class=\"js-target-url\"></span>"}

    form_action         = {url path='/admin/module/rewriteurl/delete'}
    form_content        = {$smarty.capture.rewrite_delete_dialog nofilter}
    }


    {* -- Default RewriteUrl confirmation dialog ----------------------------------- *}

    {capture "rewrite_default_dialog"}
        <input type="hidden" name="id_url" class="js-input-default" value="" />
    {/capture}

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

    dialog_id       = "rewrite_default_dialog"
    dialog_title    = {intl l="Default Url" d="rewriteurl.bo.default"}
    dialog_message  = {intl l="Do you really want to set the url %html as default ?" d="rewriteurl.bo.default" html="<span class=\"js-target-url\"></span>"}

    form_action         = {url path='/admin/module/rewriteurl/setdefault'}
    form_content        = {$smarty.capture.rewrite_default_dialog nofilter}
    }

</div>