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/SBogers10/deensekroon.komma-mediadesign.nl/wwwroot/App/Brands/BrandRepository.php
<?php


namespace App\Brands;


use App\Core\Repository;

class BrandRepository extends Repository
{
    /**
     * Return all brands that have products
     *
     * @return array
     */
    public function brands()
    {
        // Create query
        $query = 'SELECT
                    brands.id as id,
                    brands.title AS name,
                    brands.description AS description_nl,
                    brands.descriptionEn AS description_en,
                    brands.image AS image_code,
                    brands.logo AS logo_code,

                    routes.route_nl,
                    routes.route_en

                  FROM merken as brands

                  /* Join routes */
                  LEFT JOIN routes
                  ON routes.routeable_id = brands.id
                  AND routes.routeable_type = "brand"

                  /* Only when not in trashed */
                  INNER JOIN content_status AS stat
                  ON stat.itemId = brands.id
                  AND stat.linkname = "merken"

                  WHERE stat.active = 1
                  AND brands.published = 1

                  ORDER BY brands.title';

        // Get result
        if( ! $result = $this->result($query)) return [];

        // Push result in array
        $brands = [];
        while ($brand = $result->fetch_object())
        {
            // Replace brackets before and after code
            $brand->image_code = substr($brand->image_code,1,-1);
            $brand->logo_code = substr($brand->logo_code,1,-1);

            // Get logo and image
            $logo = $this->imageByCode($brand->logo_code);
            $image = $this->imageByCode($brand->image_code);

            // Add to brands
//            $brand->logo = $logo ?
//                $logo->path :
//                null;

            // Generate logo object if available
            if($logo)
            {
                $brand->logo = (object)[
                    'medium' => $logo->image_medium,
                    'small' => $logo->image_small,
                    'title' => $logo->image_title
                ];
            }
            else $brand->logo = null;

            // Generate image object if available
            if($image)
            {
                $brand->impression = (object)[
                    'medium' => $image->image_medium,
                    'small' => $image->image_small,
                    'title' => $image->image_title
                ];
            }
            else $brand->impression = null;

            $brands[] = $brand;
        }

        return $brands;
    }

    /**
     * Get brand by id
     *
     * @param $id
     * @return bool|object|\stdClass
     */
    public function brand($id)
    {
        $query = 'SELECT brands.id,
                         brands.title as name,
                         brands.description,
                         brands.logo AS logo_code,
                         brands.image AS image_code
                  FROM merken AS brands

                  RIGHT JOIN content_status AS stat ON stat.itemId = brands.id

                  WHERE stat.active = 1
                  AND stat.linkname = "merken"

                  AND brands.id = ?';

        if( ! $statement = $this->mysqli->prepare($query))
        {
            $this->error = $this->mysqli->error;
            return false;
        }

        $statement->bind_param('i',$id);

        // Execute en get result
        $statement->execute();
        $result = $statement->get_result();
        $statement->close();

        if( ! $result->num_rows) return false;

        $brand = $result->fetch_object();

        // Replace brackets before and after code
        $brand->image_code = substr($brand->image_code,1,-1);
        $brand->logo_code = substr($brand->logo_code,1,-1);

        // Get logo and image
        $logo = $this->imageByCode($brand->logo_code);
        $image = $this->imageByCode($brand->image_code);

        // Add to brands
//            $brand->logo = $logo ?
//                $logo->path :
//                null;

        // Generate logo object if available
        if($logo)
        {
            $brand->logo = (object)[
                'medium' => $logo->image_medium,
                'small' => $logo->image_small,
                'title' => $logo->image_title
            ];
        }
        else $brand->logo = null;

        // Generate image object if available
        if($image)
        {
            $brand->impression = (object)[
                'medium' => $image->image_medium,
                'small' => $image->image_small,
                'title' => $image->image_title
            ];
        }
        else $brand->impression = null;

        return $brand;
    }

    /**
     * @param $code
     * @return bool|object|\stdClass
     */
    private function logoByCode($code)
    {
        // Get logo
        $query = 'SELECT logo.path
                  FROM media_files AS logo
                  WHERE logo.shortcode = ?';

        if( ! $statement = $this->mysqli->prepare($query))
        {
            $this->error = $this->mysqli->error;
            return false;
        }

        $statement->bind_param('s',$code);
        $statement->execute();
        $result = $statement->get_result();
        $statement->close();
        if( ! $result->num_rows) return false;
        return $result->fetch_object();
    }

    /**
     * @param $code
     * @return bool|object|\stdClass
     */
    private function imageByCode($code)
    {
        // Get logo
        $query = 'SELECT image.filename_medium AS image_medium,
                         image.filename_small AS image_small,
                         image.filename_small AS image_title
                  FROM media_files AS image
                  WHERE image.shortcode = ?';

        if( ! $statement = $this->mysqli->prepare($query))
        {
            $this->error = $this->mysqli->error;
            return false;
        }

        $statement->bind_param('s',$code);
        $statement->execute();
        $result = $statement->get_result();
        $statement->close();
        if( ! $result->num_rows) return false;
        return $result->fetch_object();
    }

    /**
     * Return all brands that have products
     *
     * @return array
     */
    public function brandsWhichHaveProducts()
    {
        // Create query
        $query = 'SELECT
                    brands.id as id,
                    brands.title AS name,

                    routes.route_nl,
                    routes.route_en

                  FROM merken as brands

                  /* Check if there are products in the tree */
                  RIGHT JOIN product_products AS products
                  ON products.brandId = brands.id

                  /* Join routes */
                  LEFT JOIN routes
                  ON routes.routeable_id = brands.id
                  AND routes.routeable_type = "brand"

                  /* Only when not in trashed */
                  INNER JOIN content_status AS stat
                  ON stat.itemId = brands.id
                  AND stat.linkname = "merken"
                  WHERE stat.active = 1

                  GROUP BY brands.id,
                  brands.title,
                  routes.route_nl,
                  routes.route_en

                  ORDER BY brands.title';

        // Get result
        if( ! $result = $this->result($query)) return [];

        // Get rows
        return $this->rows($result);
    }
}