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/verrassendveel.komma.pro/wwwroot/app/meta/MetaService.php
<?php

/**
 * Created by PhpStorm.
 * User: mike
 * Date: 13/10/16
 * Time: 09:19
 */
class MetaService
{
    private $mysqli;
    private $machineName = '';
    private $title = '';
    private $description = '';

    public function __construct()
    {
        global $mysqli;
        $this->mysqli = $mysqli;

        $this->init();
    }


    /**
     * Return title
     *
     * @return string
     */
    public function title($default = null)
    {
        if( empty($this->title)) return $default;

        return $this->title;
    }

    /**
     * Return description
     *
     * @return string
     */
    public function description()
    {
        return $this->description;
    }

    /**
     * Load information
     */
    private function init()
    {
        $this->setMachineName();
        $this->loadAndSetMetaData($this->getIdFromSubPage());
    }


    /**
     * Load and set meta data
     *
     * @param null $id
     * @return bool
     */
    private function loadAndSetMetaData($id = null)
    {
        if($id == null)
        {
            $metaDataTable = null;

            switch ($this->machineName)
            {
                case 'winkelaanbod':
                    $metaDataTable = 'winkelintro';
                    break;
                case 'themes':
                    $metaDataTable = 'themesintro';
                    break;
                case 'ervaringen':
                    $metaDataTable = 'ervaringenintro';
                    break;
                case 'welkom':
                    $metaDataTable = 'welkom';
                    break;
            }

            if($metaDataTable != null) $query = 'SELECT meta_title, meta_description FROM ' . $metaDataTable . ' LIMIT 1';
        }
        else{
            // Build  query
            $query = 'SELECT meta_title, meta_description FROM ' . $this->machineName;
            $query .= ' WHERE id = ' . $id;
            $query .= ' LIMIT 1';
        }

        $record = null;
        if(isset($query))
        {
            // Check for result
            if( ! $result = $this->mysqli->query($query)) return false;
            $record = $result->fetch_assoc();
        }

        // Set data
        $this->setCustomOrDefault($record,'title');
        $this->setCustomOrDefault($record,'description');

    }

    /**
     * Check for whether to use custom or default meta data
     *
     * @param $record
     * @param $type
     */
    private function setCustomOrDefault($record, $type)
    {
        // Check for custom
        if(! empty($record['meta_' . $type])) $this->{ $type } = fromDatabase($record['meta_' . $type]);
    }

    /**
     * Convert route to machine name
     */
    private function setMachineName()
    {
        // Get conversion from route to table name
        $machineNames = include $_SERVER['DOCUMENT_ROOT'] . '/config/machineNames.php';

        // Define route
        $route = isset($_GET['page']) ? $_GET['page'] : 'home';

        // Set machine name
        $this->machineName = $machineNames[$route];
    }

    /**
     * Convert sub page title to id
     */
    private function getIdFromSubPage()
    {
        // If not on sub page, return null
        if( ! isset( $_GET['sub'])) return null;
        if( ! $result = $this->mysqli->query('SELECT id, title FROM winkelaanbod')) return null;
        while($record = $result->fetch_assoc()){
            $title = fromDatabase($record['title']);
            $link = linkname($title);
            if($link == $_GET['sub']) return $record['id'];
        }
    }
}