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/NVonken/mijneigenlied.com/wwwroot/Core/Core Parts/base.part.php
<?php

class BaseClass
{

    private $_definedProperties;
    private $_className;

    public static $_dbObject;
    public static $_otherFields;

    function __construct()
    {
        //get the child class name
        $childClass = get_called_class();

        //setup the base class
        $this->_definedProperties = array();
        $this->_className = $childClass;

        //get the defined props of the child class
        $this->_definedProperties = BaseClass::_getProperties($this->_className);
    }

    protected function Insert()
    {
        //get values of the properties
        $values = $this->_getValues($this);

        //build query base
        $insertQuery = "INSERT INTO `" . $this->_className . "` (" . join(",", $this->_definedProperties) . ") VALUES (";
        //add values
        foreach ($values as $value)
        {
            //make special case for NULL
            if ($value == null)
                $insertQuery .= "NULL,";
            else
                $insertQuery .= "'" . BaseClass::_db()->Escape($value) . "',";
        }
        $insertQuery = substr($insertQuery, 0, strlen($insertQuery) - 1);
        $insertQuery .= ")";

        //execute query
        BaseClass::_db()->query($insertQuery);

        //return insert id if any
        return BaseClass::_db()->insertId;
    }

    protected function InsertOnDuplicateKeyUpdate()
    {
        //get values of the properties
        $values = $this->_getValues($this);

        //using $this->_className directly in a call to static class is not allowed.
        $className = $this->_className;

        //build query base
        $insertQuery = "INSERT INTO `" . $this->_className . "` (" . join(",", $this->_definedProperties) . ") VALUES (";
        //add values
        foreach ($values as $value)
        {
            //make special case for NULL
            if ($value == null)
                $insertQuery .= "NULL,";
            else
                $insertQuery .= "'" . BaseClass::_db()->Escape($value) . "',";
        }
        $insertQuery = substr($insertQuery, 0, strlen($insertQuery) - 1);
        $insertQuery .= ") ON DUPLICATE KEY UPDATE ";

        foreach ($values as $prop => $value)
        {
            if (!in_array($prop, $className::$_primaryKey)) {
                $insertQuery .= "`" . $prop . "` = ";

                if ($value == null)
                    $insertQuery .= "NULL,";
                else
                    $insertQuery .= "'" . BaseClass::_db()->Escape($value) . "',";
            }
        }
        $insertQuery = substr($insertQuery, 0, strlen($insertQuery) - 1);

        //execute query
        BaseClass::_db()->query($insertQuery);

        //return insert id if any
        return BaseClass::_db()->insertId;
    }

    protected function Update()
    {
        //get values of the properties
        $values = $this->_getValues($this);

        //using $this->_className directly in a call to static class is not allowed.
        $className = $this->_className;

        //build query base
        $updateQuery = "UPDATE `" . $this->_className . "` SET ";
        //add values
        foreach ($values as $name => $value)
        {
            if (!in_array($name, $className::$_primaryKey)) {
                if ($value == null)
                    $updateQuery .= "`" . $name . "` = NULL,";
                else
                    $updateQuery .= "`" . $name . "` = '" . BaseClass::_db()->Escape($value) . "',";
            }
        }
        $updateQuery = substr($updateQuery, 0, strlen($updateQuery) - 1);
        if (count($className::$_primaryKey) > 0) {
            $updateQuery .= " WHERE ";

            //create where statement
            foreach ($className::$_primaryKey as $key)
            {
                $updateQuery .= "`" . $key . "` = '" . BaseClass::_db()->Escape($this->$key) . "' AND ";
            }

            $updateQuery = substr($updateQuery, 0, strlen($updateQuery) - 4);
        }
        //execute query
        BaseClass::_db()->query($updateQuery);
    }

    //select by single id
    protected static function Select($id)
    {
        $className = get_called_class();
        if (is_array($id)) {
            $query = "SELECT * FROM `" . $className . "` WHERE ";
            $i = 0;
            foreach ($className::$_primaryKey as $key)
            {
                $query .= " `" . $key . "` = '" . BaseClass::_db()->escape($id[$i]) . "' AND";
                $i++;
            }
            $query = substr($query, 0, strlen($query) - 3);
            return reset(BaseClass::SelectObjects($query));
        }
        else
        {
            return reset(BaseClass::SelectObjects("SELECT * FROM `" . $className . "` WHERE `" . reset($className::$_primaryKey) . "` = '" . BaseClass::_db()->escape($id) . "'", $className));
        }
    }

    //select all
    protected static function SelectAll($limit = null)
    {
        $className = get_called_class();
        return BaseClass::SelectObjects("SELECT * FROM `" . $className . "`" . ($limit != null ? " LIMIT 0, " . intval($limit) : ""), $className);

    }

    /**
     * Perform a query to the database and map this to objects
     * @static
     * @param string $sql SQL statement to perform
     * @param string $class name of the class to map to(defaults to the calling class)
     * @return array of objects
     */
    protected static function SelectObjects($sql, $class = null)
    {
        if ($class == null)
            $class = get_called_class();

        $query = BaseClass::_db()->query($sql);
        $resultObjects = array();
        while ($fetch = BaseClass::_db()->fetch($query))
        {
            $resultObjects[] = BaseClass::_mapResults($fetch, $class);
        }
        return $resultObjects;
    }

    //delete the child object from the db
    protected function Delete()
    {
        $deleteQuery = "DELETE FROM `" . $this->_className . "` WHERE ";

        //using $this->_className directly in a call to static class is not allowed.
        $className = $this->_className;

        foreach ($className::$_primaryKey as $key)
        {
            $deleteQuery .= "`" . $key . "` = '" . $this->$key . "' AND ";
        }
        $deleteQuery = substr($deleteQuery, 0, strlen($deleteQuery) - 4);
        BaseClass::_db()->query($deleteQuery);
    }

    /**
     * Get the singleton DB class;
     * @static
     * @return db
     */
    protected static function _db()
    {
        if (BaseClass::$_dbObject == null) {
            BaseClass::$_dbObject = new db();
        }
        return BaseClass::$_dbObject;
    }

    //map the results of a data row to a object
    private static function _mapResults($dataRow, $class)
    {
        //create new object of type
        $object = new $class();
        //get properties of this object
        $properties = BaseClass::_getProperties($class);
        //loop the properties and fill them
        foreach ($dataRow as $key => $data)
        {
            if (!in_array($key, $properties)) {
                $object->_otherFields[$key] = stripslashes($data);
            }
            else
            {
                $object->$key = stripslashes($dataRow[$key]);
            }
        }

        return $object;
    }

    //get property values from a class
    private function _getValues($object)
    {
        $returnValues = array();
        foreach ($this->_definedProperties as $property)
        {
            $returnValues[$property] = stripslashes($object->$property);
        }
        return $returnValues;
    }

    //get the defined and suited properties of a class
    private static function _getProperties($className)
    {
        $properties = get_class_vars($className);
        $vars = array();
        foreach ($properties as $name => $value)
        {
            //Skip properties starting with _
            //these are ignored by the base class extend methods
            if ($name[0] != "_")
                $vars[] = $name;

        }
        return $vars;
    }

}

?>