HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers96/smilefotografie.nl/app/Komma/Albums/AlbumRepository.php
<?php


namespace Komma\Albums;


class AlbumRepository
{
    // What page are the albums on
    private $albumPage = [];

    /**
     * Set all info about the page the album belongs to
     *
     * @param $page
     */
    public function setPage($page)
    {
        $data = [];
        $codeName = $page->code_name;

        // Set tables
        $data['table'] = snake_case($codeName) . '_albums';
        $data['tableTranslation'] = snake_case($codeName) . '_album_translations';
        $data['foreignKey'] = snake_case($codeName) . '_album_id';

        // Set models
        $namespace = 'Komma\Kms\\' . ucfirst($codeName) . 'Albums\Models\\';
        $data['model'] = $namespace . ucfirst($codeName) . 'Album';
        $data['modelTranslation'] = $namespace . ucfirst($codeName) . 'AlbumTranslation';

        // Create \StdClass with data
        $this->albumPage = $data;
    }

    /**
     * @param $codeName
     * @param null $filter
     * @return Array
     */
    public function albumsByPage($filter = null)
    {
        // Query all albums
        $albums = \DB::table($this->albumPage['table'])

            // Join translations
            ->leftJoin(
                $this->albumPage['tableTranslation'],
                $this->albumPage['tableTranslation'] .'.' . $this->albumPage['foreignKey'], '=', $this->albumPage['table'] . '.id')

            // Join thumbnail from images
            ->leftJoin('images', function($join){
                $join->on($this->albumPage['table'] . '.id', '=', 'images.imageble_id')
                    ->where('images.imageble_type', '=', $this->albumPage['model'])
                    ->where('images.attribute_key', '=', 'album_thumbnail');
            })

            // Join routes
            ->leftJoin('routes', function($join){
                $join->on($this->albumPage['tableTranslation'] . '.id', '=', 'routes.routable_id')
                    ->where('routes.routable_type', '=', $this->albumPage['modelTranslation']);
            })

            // Exclude root
            ->where($this->albumPage['table'] . '.lft', '!=', 1)

            ->orderBy('lft','asc')

            // Select only what we need
            ->select([
                $this->albumPage['table'] . '.id',

                $this->albumPage['tableTranslation'] . '.name',
                $this->albumPage['tableTranslation'] . '.description',

                'images.small_image_url',

                'routes.route',
            ]);

        // If filters passed trough, filter the query
        if( ! empty($filter) ) $albums = $albums->where('album_type','=',$filter);

        // Return array
        return $albums->get();
    }

    /**
     * Select album by id
     *
     * @param $albumId
     * @return mixed
     */
    public function albumById($albumId, $hasFilter = false)
    {
        // Default select
        $select = [
            $this->albumPage['table'] . '.id',
            $this->albumPage['table'] . '.negative',

            $this->albumPage['tableTranslation'] . '.name',
            $this->albumPage['tableTranslation'] . '.description',
        ];

        // Check if this album has a filter
        if($hasFilter) $select[] = $this->albumPage['table'] . '.album_type';

        return \DB::table($this->albumPage['table'])

            // Join translations
            ->leftJoin(
                $this->albumPage['tableTranslation'],
                $this->albumPage['tableTranslation'] .'.' . $this->albumPage['foreignKey'], '=', $this->albumPage['table'] . '.id')

            // Select only one album
            ->where($this->albumPage['table'] . '.id','=', $albumId)

            // Select only what we need
            ->select($select)

            ->first();
    }

    /**
     * @param $albumId
     */
    public function imagesFromAlbum($albumId)
    {
        return \DB::table('images')

            // Get only images from current page
            ->where('images.imageble_type', '=', $this->albumPage['model'])

            // Get only images from the album
            ->where('images.imageble_id', '=', $albumId)

            // Get only the images-attribute
            ->where('images.attribute_key', '=', 'images')

            ->orderBy('sort_order','asc')

            ->get();
    }
}