
{% extends '@CadolesCore/base.html.twig' %}


{% block pagewrapper %}
    {% if access=="config" %}
        <div class="pagemenu">
            <a href="{{ path('cadoles_portal_config_blog_view', {id:entity.id})}}">{{ entity.name }}</a>
            <a href='{{ path('cadoles_portal_config_blog_update', {id:entity.id}) }}' title='Modifier'><i class='fa fa-cog fa-fw'></i></a>
            <a href='{{ path('cadoles_portal_config_blog_writer', {id:entity.id}) }}' title='Permission'><i class='fa fa-users fa-fw'></i></a>
            <a href='{{ path('cadoles_portal_config_blog_delete', { id: entity.id }) }}' data-method='delete' data-confirm='Êtes-vous sûr de vouloir supprimer ?' title='Supprimer'><i class='fa fa-trash fa-fw'></i></a>
        </div>
    {% else %}
        <div style="margin:10px 0px 10px 0px; text-align:left;height:34px;">
            {% if not idpage is empty %}
            <a href="{{path('cadoles_portal_user_page_view',{id:idpage,usage:'group',group:idgroup})}}" class="btn btn-success">Revenir sur le Groupe</a>
            {% endif %}
        </div>

        <ul class="nav navbar-default nav-pills">
            <li id="menublog-0" class="{% if entity.id is not defined %}active{%endif%}" style="cursor:pointer">
                <a href="{{ path("cadoles_portal_user_blog_view") }}">
                    Tout
                </a>
            </li>

            {% for blog in blogs %} 
                <li id="menublog-{{blog.id}}" data-open="{% if blog.canupdate %}true{%endif%}" class="{% if entity.id is defined and entity.id==blog.id%}active{%endif%}" style="cursor:pointer">
                    <a href="{{ path("cadoles_portal_user_blog_view",{'id':blog.id}) }}">
                        {% if not blog.groups is empty %}
                            <i class="fa fa-users fa-fw"></i>
                        {% elseif app.user==blog.user %}
                            <i class="fa fa-user fa-fw"></i>
                        {%endif%}
                        {{ blog.name }}
                        {% if blog.canadd %}
                            <i class="fa fa-pencil"></i>
                        {% else %}
                            <i class="fa fa-lock"></i>
                        {% endif %}
                    </a>
                </li>
            {% endfor %}
            
            {% if canupdate %}
                <li><a id="menusubmit" style="cursor:pointer" onclick="submitBlog()" title='Ajouter un blog'><i class='fa fa-plus fa-fw'></i></a></li>
            {% endif %}
            <li><a id="menuupdate" style="cursor:pointer;display:none" onclick="updateBlog()" title='Modifier un blog'><i class='fa fa-file fa-fw'></i></a></li>
            <li><a id="menushare" style="cursor:pointer;display:none" onclick="shareBlog()" title='Partager un blog'><i class='fa fa-share-alt fa-fw'></i></a></li>
            <li><a id="menuwriter" style="cursor:pointer;display:none" onclick="writerBlog()" title='Permission du blog'><i class='fa fa-users fa-fw'></i></a></li>
            <li><a id="menudelete" style="cursor:pointer;display:none" onclick="deleteBlog()" title='Supprimer un blog'><i class='fa fa-trash fa-fw'></i></a></li>

        </ul>
    {% endif %}
    
    <div class="blogpreview">
        <h1 class="blogtitle">
            {% if entity.id is not defined %}
                Blog
                {% set idblog=0 %}
            {% else %}
                {{ entity.name }}
                {% set idblog=entity.id %}
            {% endif %}
        </h1>

        {% if canadd %}
        <a class="btn btn-primary" href='{{ path('cadoles_portal_'~access~'_blogarticle_submit',{'idblog':idblog}) }}' style="width:100%; font-size:20px" title='Ajouter un article'><i class='fa fa-paper-plane fa-fw'></i> Ajouter un Article</a>
        {% endif %}

        <nav aria-label="Page navigation" style="text-align:center">
            <ul class="pagination">
                {% set maxpage=(countarticles/pagination)|round(0,"ceil") %}
                {%if maxpage>1 %}
                    {% for count in 1..maxpage%}
                        <li class="{%if count==page %}active{%endif%}">
                            {% if entity.id is not defined %}
                                <a href="{{ path("cadoles_portal_user_blog_view",{'page':count}) }}">{{ count }}</a>
                            {% else %}
                                <a href="{{ path("cadoles_portal_user_blog_view",{'id':entity.id,'page':count}) }}">{{ count }}</a>
                            {% endif %}
                        </li>
                    {% endfor %}
                {% endif %}
            </ul>
        </nav>

        <div class="grid clearfix">
            {% for blogarticle in blogarticles %}
                {% if loop.index==1 %}
                    <div class="grid-sizer grid-preview"></div>
                    <div class="grid-gutter-sizer grid-preview-gutter-sizer"></div>
                {% endif %}

                <div class="grid-item grid-preview">
                    <div class="grid-item-content">
                        <a href="{{ path('cadoles_portal_'~access~'_blogarticle_view',{'id':blogarticle.id})  }}">
                            {% if blogarticle.image is not empty %}
                            <div class="grid-item-logo">
                                <img class="grid-item-vignette" src="/{{alias}}/{{blogarticle.image|replace({"/blogarticle/":"/blogarticle/thumb-"})}}" width="100%">
                            </div>                                

                            {% endif %}
                            <div class="caption">
                                <div class="grid-item-title">
                                    <h2>{{blogarticle.name }}</h2>
                                    <small>publié par {{ blogarticle.user.username }} le {{ blogarticle.submit|date("d/m/Y à H:i") }}<br>dans le blog {{blogarticle.blog.name }}</small>
                                </div>
                            </div>
                        </a>
                    </div>
                </div>
            {% endfor %}
        </div>
    </div>
{% endblock %}

{% block localjavascript %}
    var idblog;

    $('document').ready(function(){
        {% if entity.id is defined %}
            {% for blog in blogs %} 
                {% if blog.id==entity.id %}
                    showBlog({{ blog.id }}, {{ blog.canupdate }});
                {% endif %}
            {% endfor %}
        {% else %}
            showBlog(0, {{ canupdate }});
        {% endif %}  
        
        // Création des grilles d'items
        var optiongrid={columnWidth: '.grid-sizer', itemSelector: '.grid-item', gutter: '.grid-gutter-sizer'};
        var grid = $('.grid').masonry(optiongrid);       
    });

    function submitBlog() {
        var url="{{ path('cadoles_portal_'~access~'_blog_submit') }}";
        $(location).attr('href',url);
    }  

    function updateBlog() {
        var url="{{ path('cadoles_portal_'~access~'_blog_update',{id:'xx'}) }}";
        url=url.replace('xx',idblog);

        $(location).attr('href',url);
    }    

    function shareBlog() {
        var url="{{ path('cadoles_portal_'~access~'_blog_share',{id:'xx'}) }}";
        url=url.replace('xx',idblog);

        $(location).attr('href',url);
    } 

    function writerBlog() {
        var url="{{ path('cadoles_portal_'~access~'_blog_writer',{id:'xx'}) }}";
        url=url.replace('xx',idblog);

        $(location).attr('href',url);
    } 

    function deleteBlog() {
        if (confirm('Êtes-vous sûr de vouloir supprimer ?')) {
            var url="{{ path('cadoles_portal_'~access~'_blog_delete',{id:'xx'}) }}";
            url=url.replace('xx',idblog);

            $(location).attr('href',url);
        }
    } 

    // Affichages des blogs
    function showBlog(id,canupdate) {
        idblog=id;

        // Rendre actif le page en cours dans le menu
        $(".navbar-top-links li").removeClass("active");
        $("#menublog-"+id).addClass("active");

        // Cacher les actions possibles sur la page
        $("#menuupdate").hide();
        $("#menushare").hide();
        $("#menuwriter").hide();
        $("#menudelete").hide();

        if($("#menublog-"+id).data("open")) {
            console.log($("#menublog-"+id).data("open"));
            $("#menuupdate").show();
            $("#menushare").show();
            $("#menuwriter").show();
            $("#menudelete").show();
        }
    }

    
{% endblock %}
