PHP code example of vis / articles_l5

1. Go to this page and download the library: Download vis/articles_l5 library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

vis / articles_l5 example snippets


    use Vis\Articles\Models\AbstractArticle;
    
    class PackageArticle extends AbstractArticle
    {    
        protected $table = 'package_articles';
    
        protected $viewFolder = 'package_articles';
    
        protected $sortOrder = "created_at:desc";
    
        protected $perPage = 25;
        protected $perPageSettingName = 'kol_statei-v-kataloge-novostei';
        
        protected $relationsInCatalog = [];
        protected $relationsInArticle = [];
    
        public function getUrl()
        {
            return route("package_article", [$this->getSlug(), $this->id]);
        }
    }

    protected $viewFolder = 'package_articles';

    protected $sortOrder = "created_at:desc";

    protected $perPage = 25;

    protected $perPageSettingName = 'kol_statei-v-kataloge-novostei';

    protected $relationsInCatalog = [];

    protected $relationsInArticle = [];

    public function getViewFolder(): string;

    public function getSortOrder(): string;

    public function getPerPage(): int;

    public function getRelationsInCatalog(): array;

    public function getRelationsInArticle(): array;

    public function scopeIsMain($query);

    public function scopeFilterCustomOrder($query, string $order);

    use Vis\Articles\Controllers\AbstractSimpleArticleController;
    
    class PackageArticlesController extends AbstractSimpleArticleController
    {
        protected $model = "PackageArticle";
    }

     protected $model = "PackageArticle";

    'articles_catalog' => array(
        'action' => 'PackageArticlesController@showCatalog',
        'node_definition' => 'node',
        'check' => function() {
            return true;
        },
        'title' => 'Каталог статей',
    ),

    Route::get('/test-articles-catalog/{slug}-{id}', [
        'as' => 'package_article',
        'uses' => 'PackageArticlesController@showArticle'
    ]);

@extends('layouts.default')

@section('main')

    <section class="article-section">
        <div class="article-text">
            <h1>
                {{$page->t('title')}}
            </h1>
        </div>
    </section>

@stop

@extends('layouts.default')

@section('main')

    <section class="associations-page">
        <div class="container">

            <div class="associations-list">
                @foreach($articles as $key => $article)
                    <div class="associations-item">
                        <div class="text">
                            <div class="title">
                                <a href="{{$article->getUrl()}}">
                                    {{$article->t('title')}}
                                </a>
                            </div>
                        </div>
                    </div>
                @endforeach
            </div>
        </div>
    </section>
@stop

use Vis\Articles\Models\AbstractFilterableArticle;

class PackageFilterableArticle extends AbstractFilterableArticle
{
    protected $table = 'articles_filterable';

    protected $viewFolder = 'package_filterable_articles';

    protected $sortOrder = "created_at:desc";

    protected $perPage = 25;
    protected $perPageSettingName = 'kol_statei-v-kataloge-novostei';

    protected $sortOptions = [
        ['name' => 'title', 'description' => 'По названию','value' => 'title:asc'],
        ['name' => 'new_first', 'description' => 'от новых к старым','value' => 'created_at:desc'],
        ['name' => 'old_first', 'description' => 'от старых к новым','value' => 'created_at:asc'],
    ];

    protected $countOptions = [
        ['name' => '15','description' => 'По 15', 'value' => 15 ],
        ['name' => 'all','description' => 'Все', 'value' => 99999999 ],
    ];
    
    protected $dateField = 'created_at';

    protected $relationsInCatalog = [];

    protected $relationsInArticle = [];

    public function getUrl()
    {
       return route("package_filterable_article", [$this->filterModel->getSlug(),$this->getSlug(), $this->id]);
    }

    public function filterModel()
    {
        return $this->belongsTo('Tree');
    }

    protected $sortOptions = [
        ['name' => 'title', 'description' => 'По названию','value' => 'title:asc'],
        ['name' => 'new_first', 'description' => 'от новых к старым','value' => 'created_at:desc'],
        ['name' => 'old_first', 'description' => 'от старых к новым','value' => 'created_at:asc'],
    ];

    protected $countOptions = [
        ['name' => '15','description' => 'По 15', 'value' => 15 ],
        ['name' => 'all','description' => 'Все', 'value' => 99999999 ],
    ];

    protected $dateField = 'created_at';

    public function getSortOptions(): array;

    public function getCountOptions(): array;

    public function getDateField(): string;

    public function scopeFilterRelation($query, $relationName, $relationSelected);

    public function scopeFilterDateDay($query, int $day = 0);

    public function scopeFilterDateMonth($query, int $month = 0);

    public function scopeFilterDateYear($query, int $year = 0);

    public function scopeFilterDateStrict($query, int $year = 0, int $month = 0, int $day = 0)

    public function scopeFilterDateRange($query, Carbon $dateFrom, Carbon $dateTo);

use Vis\Articles\Controllers\AbstractFilterableArticleController;

class PackageFilteredArticlesController extends AbstractFilterableArticleController
{
    protected $model = "PackageFilterableArticle";
    
    private function handleArticles()
    {
        $page = $this->node;
        $filters = $this->filter;

        $filters->addCount()
            ->addSort()
            ->addRelation('filterModel', $page)
            ->addDateRange()
            ->addDateDay()
            ->addDateMonth()
            ->addDateYear()
            ->addDateStrict()
            ->handle();

        $sortOrder  = $filters->getSort()->getSelected();
        $perPage    = $filters->getCount()->getSelected();
        $filter     = $filters->getRelation('filterModel')->getSelected() ?: $filters->getRelation('filterModel')->setSelectedToFirstOption()->getSelected();
        $dateRange  = $filters->getDateRange()->getSelected();
        $dateDay    = $filters->getDateDay()->getSelected();
        $dateMonth  = $filters->getDateMonth()->getSelected();
        $dateYear   = $filters->getDateYear()->getSelected();
        $dateStrict = $filters->getDateStrict()->getSelected();

        $articles = $this->model->active()->with('filterModel')
            ->filterRelation('filterModel', $filter)
            ->filterDateRange($dateRange['date-from'], $dateRange['date-to'])
            ->filterDateDay($dateDay)
            ->filterDateMonth($dateMonth)
            ->filterDateYear($dateYear)
            ->filterDateStrict($dateStrict['year'], $dateStrict['month'], $dateStrict['day'])
            ->filterCustomOrder($sortOrder)
            ->paginate($perPage);

        
        $articles->load($this->model->getRelationsInCatalog());

        return view("pages." . $this->model->getViewFolder() . ".catalog", compact('page', 'articles', 'filters'));
    }

    public function showCatalog()
    {
        return $this->handleArticles();
    }

    public function showSubCatalog($catalog)
    {
        if (!$this->node) {
            $this->node = $this->model->filterModel()->where('slug', $catalog)->active()->first();
            if (!$this->node) {
                abort(404);
            }
        }

        return $this->handleArticles();
    }

    public function showArticle($catalog, $slug, $id)
    {
        $page = $this->model->where('id', $id)->active()->with('filterModel')->first();

        if (!$page) {
            abort(404);
        }

        if ($page->filterModel->getSlug() != $catalog) {
            return redirect($page->getUrl(), 302);
        }

        if ($page->getSlug() != $slug) {
            return redirect($page->getUrl(), 302);
        }

        $page->load($this->model->getRelationsInArticle());

        return view("pages." . $this->model->getViewFolder() . ".article", compact('page'));
    } // end showSingle

}

    'filterable_catalog' => array(
        'action' => 'PackageFilteredArticlesController@showCatalog',
        'node_definition' => 'node',
        'check' => function() {
            return true;
        },
        'title' => 'фильтруемый каталог',
    ),

    'filterable_sub_catalog' => array(
        'action' => 'PackageFilteredArticlesController@showSubCatalog',
        'node_definition' => 'node',
        'check' => function() {
            return true;
        },
        'title' => 'фильтруемый подкаталог',
    ),

    Route::get('/test-filterable-articles-catalog-foreign/{catalog}', [
        'as' => 'test-filterable-articles-catalog-foreign',
        'uses' => 'PackageFilteredForeignArticlesController@showSubCatalog'
    ]);

handler => 'Vis\Builder\Helpers\SlugHandler' 

    Route::get('/test-filterable-articles-catalog/{catalog}/{slug}-{id}', [
        'as' => 'package_filterable_article',
        'uses' => 'PackageFilteredArticlesController@showArticle'
    ]);

<div class="filters-container text-right">
    <div class="filter-btn">{{__('Фильтры')}}</div>
    <div class="row-inline filters-row">
        <div class="col-inline-xs-6 middle filter">
            <select class="filter-url-select">
                <option value="{{$filters->getRelation('filterModel')->getNoFilterUrl()}}">{{__('Все')}}</option>
                    @foreach($filters->getRelation('filterModel')->getOptions() as $filterOption)
                        <option value="{{$filterOption->getUrl()}}" {{url()->current() == $filterOption->getUrl() ? 'selected' : ''}}>{{$filterOption->t('title')}}</option>
                    @endforeach
            </select>

        </div>
        <div class="col-inline-xs-6 middle text-right sorting">
            <form name="filter_form" method="GET">
                <div class="sorting-select">
                    <select name="sort" class='filter-select'>
                        @foreach($filters->getSort()->getOptions() as $sortOption)
                            <option value="{{$sortOption['name']}}" {{$sortOption['value'] == $filters->getSort()->getSelected() ?'selected' : ''}}>{{$sortOption['description']}}</option>
                        @endforeach
                    </select>
                </div>
                <div class="show-select">
                    <select name="count" class='filter-select'>
                        @foreach($filters->getCount()->getOptions() as $optionValue)
                            <option value="{{$optionValue['name']}}" {{$optionValue['value'] == $filters->getCount()->getSelected() ? 'selected' : ''}}>{{$optionValue['description']}}</option>
                        @endforeach
                    </select>
                </div>

            </form>
        </div>
    </div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<script>
    'use strict';

    var Articles =
        {
            init: function ()
            {
                Articles.initSelects();
            },

            initSelects: function()
            {
                $(".filter-select").change(function() {
                    $(this).parents("form[name='filter_form']").submit();
                });

                $(".filter-url-select").change(function() {
                    location.href = $(this).find("option:selected").val();
                });

            },
        };

    jQuery(document).ready(function() {
        Articles.init();
    });
</script>