title = "weekly-news"
url = "/weekly-news"
layout = "default"
is_hidden = 0
==
<?php
function onStart(){

    $month = Input::get('month');
    $year = Input::get('year');

    $paginate = 15;

    if(isset($month)){
        $this['month'] = $month;
    }
    if(isset($year)){
        $this['year'] = $year;
    }

    if(isset($month) && isset($year)){
        $this['news'] = Admin\WeeklyNews\Models\WeeklyNews::where("month", $month)
            ->where("year", $year)->orderBy('year', 'DESC')
            ->orderBy('month', 'DESC')
            ->orderBy('week', 'DESC')->paginate($paginate);
    }else if(isset($month) || isset($year)){
        $this['news'] = Admin\WeeklyNews\Models\WeeklyNews::where("month", $month)
            ->orWhere("year", $year)
            ->orderBy('year', 'DESC')->orderBy('month', 'DESC')-
            >orderBy('week', 'DESC')->paginate($paginate);
    }else{
        $this['news'] = Admin\WeeklyNews\Models\WeeklyNews::orderBy('year', 'DESC')
            ->orderBy('month', 'DESC')->orderBy('week', 'DESC')
            ->paginate($paginate);
    }

}
?>
==
<section class="section" id="intro">
        <div class="auto_container">
            <div class="which_page">
                <a href="{{ 'index'|page}}" class="page_name">
                    {{'Главная'|_}}
                </a>
                <div class="slash">\</div>
                <div class="page_name">
                    {{'Еженедельный вестник'|_}}
                </div>
            </div>
            <div class="weekly_wrapper">
                <div class="title">
                    {{'Еженедельный вестник'|_}}
                </div>
                <div  class="week_form">
                    <div class="week_col">
                      <div class="select select_month">
                          <select name="month" placeholder="MM" onchange="onMonthSelect(this)">
                          <option value="">
                            {% if month is defined or month != null %}
                                {% if month == 1 %}
                                    {{'January'|_}}
                                {% elseif month ==2 %}
                                    {{'February'|_}}
                                {% elseif month ==3 %}
                                    {{'March'|_}}
                                {% elseif month ==4 %}
                                    {{'April'|_}}
                                {% elseif month ==5 %}
                                    {{'May'|_}}
                                {% elseif month ==6 %}
                                    {{'June'|_}}
                                {% elseif month ==7 %}
                                    {{'July'|_}}
                                {% elseif month ==8 %}
                                    {{'August'|_}}
                                {% elseif month ==9 %}
                                    {{'September'|_}}
                                {% elseif month ==10 %}
                                    {{'October'|_}}
                                {% elseif month ==11 %}
                                    {{'November'|_}}
                                {% elseif month ==12 %}
                                    {{'December'|_}}

                                {% endif %}

                            {% else %}
                                {{'Month'|_}}
                            {% endif %}
                          </option>
                          <option value="1">{{'January'|_}}</option>
                          <option value="2">{{'February'|_}}</option>
                          <option value="3">{{'March'|_}}</option>
                          <option value="4">{{'April'|_}}</option>
                          <option value="5">{{'May'|_}}</option>
                          <option value="6">{{'June'|_}}</option>
                          <option value="7">{{'July'|_}}</option>
                          <option value="8">{{'August'|_}}</option>
                          <option value="9">{{'September'|_}}</option>
                          <option value="10">{{'October'|_}}</option>
                          <option value="11">{{'November'|_}}</option>
                          <option value="12">{{'December'|_}}</option>
                        </select>
                      </div>

                      <div class="select">
                        <select name="year" onchange="onYearSelect(this)">
                          <option value="">
                            {% if year is defined or year != null %}
                                {{year}}
                            {% else %}
                                {{'Year'|_}}
                            {% endif %}

                          </option>
                          <option value="2021">2021</option>
                        </select>
                      </div>
                    </div>

                </div>

                <div class="magazines">

                    {% for key, item in news %}
                        <div class="magazine_item">
                            <div class="magazine_photo">
                                {% if activeLocale == "tk" %}
                                        <img src="{{item.image_tk.getPath()}}" alt="">
                                    {% elseif activeLocale == "ru" %}
                                        <img src="{{item.image_ru.getPath()}}" alt="">
                                    {% elseif activeLocale == "en" %}
                                        <img src="{{item.image_en.getPath()}}" alt="">
                                {% endif %}
                            </div>
                            <div class="magazine_date">
                                {{item.week}} / {{item.year}}
                            </div>
                            {% if activeLocale == "tk" %}
                                        <a href="{{item.pdf_tk.getPath()}}" class="magazine_btn" download> {{'Скачать'|_}} </a>
                                    {% elseif activeLocale == "ru" %}
                                        <a href="{{item.pdf_ru.getPath()}}" class="magazine_btn" download> {{'Скачать'|_}} </a>
                                    {% elseif activeLocale == "en" %}
                                        <a href="{{item.pdf_en.getPath()}}" class="magazine_btn" download> {{'Скачать'|_}} </a>
                                {% endif %}

                        </div>
                    {% endfor %}

                </div>
                {% if news.total > 15 %}
                    <div class="section_end">
                        <div class="page_num">
                            <a href="{{news.previousPageUrl}}">
                                <div class="direct_btn">
                                        <img src="{{ 'assets/images/left.svg'|theme }}" alt="">
                                </div>
                            </a>
                            <form action="" method="GET">
                                <input type="text" name="page" value="{{news.currentPage}}" class="input_page">
                            </form>
                            <a href="{{news.nextPageUrl}}">
                                <div class="direct_btn">
                                        <img src="{{ 'assets/images/right.svg'|theme }}" alt="">
                                </div>
                            </a>
                        </div>
                        <div class="page_list">
                            {{(news.total/news.perPage)|round(0,"ceil")}} {{'страниц'|_}}
                        </div>
                    </div>
                {% endif %}
            </div>
        </div>
    </section>

<script>


    let url_string = window.location.href;
    let url = new URL(url_string);
    let month = url.searchParams.get("month");
    let year = url.searchParams.get("year");
    console.log("month: " + month +   "\n" + "year: " + year);

    function onMonthSelect(month){
                    window.location.replace('{{"weekly-news"|page}}?month=' + month.value);

        if(year){
            window.location.replace('{{"weekly-news"|page}}?month=' + month.value + '&year=' + year);
        }else{
            window.location.replace('{{"weekly-news"|page}}?month=' + month.value);
        }

    }

    function onYearSelect(year){
                    window.location.replace('{{"weekly-news"|page}}?year=' + year.value);

        if(month){
            window.location.replace('{{"weekly-news"|page}}?month=' + month + '&year=' + year.value);
        }else{
            window.location.replace('{{"weekly-news"|page}}?year=' + year.value);
        }

    }

</script>

<script>
    // select box ====================

    let x, i, j, l, ll, selElmnt, a, b, c;
    x = document.getElementsByClassName("custom-select");
    l = x.length;
    for (i = 0; i < l; i++) {
        selElmnt = x[i].getElementsByTagName("select")[0];
        ll = selElmnt.length;
        a = document.createElement("DIV");
        a.setAttribute("class", "select-selected");
        a.innerHTML = selElmnt.options[selElmnt.selectedIndex].innerHTML;
        x[i].appendChild(a);
        b = document.createElement("DIV");
        b.setAttribute("class", "select-items select-hide");
        for (j = 1; j < ll; j++) {
            c = document.createElement("DIV");
            c.innerHTML = selElmnt.options[j].innerHTML;
            c.addEventListener("click", function (e) {
                let y, i, k, s, h, sl, yl;
                s = this.parentNode.parentNode.getElementsByTagName("select")[0];
                sl = s.length;
                h = this.parentNode.previousSibling;
                for (i = 0; i < sl; i++) {
                    if (s.options[i].innerHTML == this.innerHTML) {
                        s.selectedIndex = i;
                        h.innerHTML = this.innerHTML;

                        y = this.parentNode.getElementsByClassName("same-as-selected");
                        yl = y.length;
                        for (k = 0; k < yl; k++) {
                            y[k].removeAttribute("class");
                        }

                        this.setAttribute("class", "same-as-selected");
                        break;
                    }
                }
                h.click();
            });
            b.appendChild(c);
        }
        x[i].appendChild(b);
        a.addEventListener("click", function (e) {
            e.stopPropagation();
            closeAllSelect(this);
            this.nextSibling.classList.toggle("select-hide");
            this.classList.toggle("select-arrow-active");
        });
    }
    function closeAllSelect(elmnt) {
        let x, y, i, xl, yl, arrNo = [];
        x = document.getElementsByClassName("select-items");
        y = document.getElementsByClassName("select-selected");
        xl = x.length;
        yl = y.length;
        for (i = 0; i < yl; i++) {
            if (elmnt == y[i]) {
                arrNo.push(i)
            } else {
                y[i].classList.remove("select-arrow-active");
            }
        }
        for (i = 0; i < xl; i++) {
            if (arrNo.indexOf(i)) {
                x[i].classList.add("select-hide");
            }
        }
    }
    document.addEventListener("click", closeAllSelect);

// select box end ====================
</script>
