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/Routes/RouteRepository.php
<?php


namespace App\Routes;


include_once($_SERVER['DOCUMENT_ROOT'] . '/App/Core/Repository.php');
include_once($_SERVER['DOCUMENT_ROOT'] . '/lib/Carbon/Carbon.php');


use App\Core\Repository;
use Carbon\Carbon;


class RoutesRepository extends Repository
{
    /**
     * Find a route by path
     *
     * @param $route
     * @return bool|object|\stdClass
     */
    public function find($path)
    {
        // Create query
        $query = 'SELECT *
                  FROM routes
                  WHERE route_nl = ?
                  OR route_en = ?';

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

        // Bind params
        $statement->bind_param('ss',$path,$path);
        $statement->execute();
        $result = $statement->get_result();

        return $result->fetch_object();
    }

    /**
     * @param $id
     * @return bool|object|\stdClass
     */
    public function findByRouteable($id,$type)
    {
        // Create query
        $query = 'SELECT *
                  FROM routes
                  WHERE routeable_id = ?
                  AND routeable_type = ?';

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

        // Bind params
        $statement->bind_param('is',$id,$type);
        $statement->execute();
        $result = $statement->get_result();

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

        return $result->fetch_object();
    }


    /**
     * @param \StdClass $route
     * @param int $selfId
     * @param string $selfType
     * @return bool|object|\stdClass
     */
    public function unique($route,$selfId=null,$selfType=null)
    {
        // Create query
        $query = 'SELECT id
                  FROM routes
                  WHERE (
                    route_nl = ?
                    OR route_nl = ?
                    OR route_en = ?
                    OR route_en = ?

                    )';

        // For update checks
        if( $selfType != null) $query .= ' AND NOT (routeable_id = ? AND routeable_type = ?)';
        // Prepare statement
        if( ! $statement = $this->mysqli->prepare($query))
        {
            $this->error = $this->mysqli->error;
            return false;
        }

        if( $selfType != null)
        {
            $statement->bind_param('ssssis',$route->route_nl,$route->route_en,$route->route_en,$route->route_nl,$selfId,$selfType);
        }
        else
        {
            $statement->bind_param('ssss',$route->route_nl,$route->route_en,$route->route_en,$route->route_nl);
        }

        $statement->execute();
        $result = $statement->get_result();
        $statement->close();

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

        return false;
    }

    /**
     * Add route to the database
     *
     * @param $route
     * @param $routeable_id
     * @param $routeable_type
     * @return bool|mixed
     */
    public function store($route, $routeable_id, $routeable_type)
    {
        // Create query
        $query = 'INSERT INTO routes(
                    route_nl,
                    route_en,
                    routeable_id,
                    routeable_type,
                    created_at,
                    updated_at)
                  VALUES(?,?,?,?,?,?)';

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

        // Bind params
        $createdAt = $updatedAt = Carbon::now();
        $statement->bind_param('ssisss',
            $route->route_nl,
            $route->route_en,
            $routeable_id,
            $routeable_type,
            $createdAt,
            $updatedAt);

        // Execute and close
        $statement->execute();
        $statement->close();

        return $this->mysqli->insert_id;
    }

    /**
     * Update route in the database
     *
     * @param $route
     * @param $routeable_id
     * @param $routeable_type
     * @return bool|mixed
     */
    public function update($route, $routeable_id, $routeable_type)
    {
        // Create query
        $query = 'UPDATE routes SET
                    route_nl = ?,
                    route_en = ?,
                    updated_at = ?
                   WHERE routeable_id = ?
                   AND routeable_type = ?';

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

        // Bind params
        $updatedAt = Carbon::now();
        $statement->bind_param('sssis',
            $route->route_nl,
            $route->route_en,
            $updatedAt,
            $routeable_id,
            $routeable_type);

        // Execute and close
        $statement->execute();
        $statement->close();

        return true;
    }

    /**
     * Get all children by route
     *
     * @param $route
     * @return bool|object|\stdClass
     */
    public function childrenByRoute($route)
    {
        // Make sure the route is not empty
        if(empty($route->route_nl)) return false;

        // Create query
        $query = 'SELECT *
                  FROM routes
                  WHERE route_nl LIKE ?
                  AND route_en LIKE ?';

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

        $route_nl = $route->route_nl . '/%';
        $route_en = $route->route_en . '/%';
        // Bind params
        $statement->bind_param('ss', $route_nl, $route_en);
        $statement->execute();
        $result = $statement->get_result();

        $rows = [];
        while($row = $result->fetch_object())
        {
            $rows[] = $row;
        }
        return $rows;
    }
}