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/PvdBoogaard/indoorski.nl/backup/oude-site/cms/api/class.tablecreator.php
<?php
/**
 * This file contains the iwp_tablecreator and iwp_tablecreator_field classes
 *
 * @version $Id$
 * 
 *
 * @package IWP
 * @subpackage IWP_API
 */

/**
 * Table Creator Class
 * This class is used to build a 'create table' SQL syntax. It's primiarly used for the modules.
 * This class keeps an array of children classes that use 'iwp_tablecreator_field'.
 * This class does not actually run any queries, but rather builds a query that can be run to create a table
 *
 * @package IWP
 * @subpackage IWP_API
 */
class iwp_tablecreator {
	/**
	 * An array of fields for this table. Each element in this array should use the fieldName as the key and the value must be an instance of iwp_tablecreator_field
	 * @var array
	 */
	public $fields = array();

	/**
	 * An array of additional indexes for this table. This is used when you want indexes that extend over more than one field.
	 * @var array
	 */
	public $indexes = array();

	/**
	 * This is the type of database table to create. More often than not it should be MyISAM or InnoDB
	 * @var string
	 */
	public $tableType = 'MyISAM';

	/**
	 * This is the collation for the table -- It should be 'utf8_general_ci' except for rare circumstances
	 * @var string
	 */
	public $collation = 'utf8_general_ci';

	/**
	 * This is the character set the table will use. More often than not it should remain as 'utf8'
	 * @var string
	 */
	public $charset = 'utf8';

	/**
	 * This is the full name of the database table that should be used.
	 * @var string
	 */
	public $name = '';

	/**
	 * The class constructor, which also sets the name of the database table if it is passed through as the first arguement
	 *
	 * @param string $name The name of the database table.
	 *
	 * @return void Doesn't return anything
	 */
	public function __construct($name=null){
		if(!is_null($name)){
			$this->name = $name;
		}
	}

	/**
	 * This function clears all the values of the current class and reverts it back to its original state.
	 *
	 * @return void Doesn't return anything
	 */
	public function NewTable(){
		$this->ClearFields();
		$this->SetDefaults();
	}

	/**
	 * This function sets the default values for all class member variables except for the fields array
	 *
	 * @return void Doesn't return anything
	 */
	public function SetDefaults(){
		$this->tableType = 'MyISAM';
		$this->collation = 'utf8_general_ci';
		$this->charset = 'utf8';
		$this->name = '';
	}

	/**
	 * This function clears the fields array, removing all fields
	 *
	 * @return void Doesn't return anything
	 */
	public function ClearFields(){
		$this->fields = array();
	}

	/**
	 * This function retrieves an instance of a particular field based on its fieldname
	 *
	 * @param string $name The name of the field to retrieve the object for
	 *
	 * @return boolean|iwp_tablecreator_field Returns either the instantiated class for the request field, or false if the object/field doesn't exist
	 */
	public function GetField($name){
		if(isset($this->fields[$name]) && is_object($this->fields[$name])){
			return $this->fields[$name];
		}
		return false;
	}

	/**
	 * Adds a new field to the table. It instantiates a new instance of iwp_tablecreator_field and adds it to the fields member variable array. The new object is then returned so chaining can be used.
	 *
	 * @param string $name The name of the new field to create
	 *
	 * @return iwp_tablecreator_field A new instance of iwp_tablecreator_field using the name passed in
	 */
	public function AddField($name){
		$this->fields[$name] = new iwp_tablecreator_field($name);
		return $this->fields[$name];
	}

	/**
	 * Adds a new manual index to the table. This relies on the input fields being valid fields.
	 *
	 * @param string $name The name of the index to use
	 * @param array $fields An array of fields to use for the index
	 *
	 * @return iwp_tablecreator It returns the current instance of the class so chaining can be used with this and other functions.
	 */
	public function AddIndex($name, $fields){
		$this->indexes[] = ' INDEX `'. $name .'` (`'. implode('`, `', (array)$fields) . '`) ';
		return $this;
	}



	/**
	 * This function runs through all the fields and returns the final 'CREATE TABLE' SQL syntax for MySQL.
	 *
	 * @return string The final CREATE TABLE SQL that is ready to be sent to MySQL to create the new table
	 */
	public function GetCreateTable (){
		$tableFields = '';
		$indexList = $fulltextList = array();
		foreach($this->fields as $fieldName=>$fieldObj){
			$tableFields[] = $fieldObj->GetCreateField('name');
			if($fieldObj->hasIndex()) {
				$indexList[] = $fieldName;
			}
			if($fieldObj->hasFulltext()) {
				$fulltextList[] = $fieldName;
			}
		}

		//	create a list of primary key columns for this table to generate a PRIMARY() index statement
		$pkeyFields = array();
		foreach($this->fields as $fieldName=>$fieldObj){
			if($fieldObj->isPrimaryKey()){
				$pkeyFields[] = $fieldName;
			}
		}
		if (!empty($pkeyFields)) {
			$tableFields[] = ' PRIMARY KEY ( `'. join('`,`', $pkeyFields) .'` ) ';
		}

		if(!empty($indexList)){
			$tableFields[] = ' INDEX (`'.implode('`, `', $indexList) . '`) ';
		}

		if(!empty($fulltextList)){
			$tableFields[] = ' FULLTEXT (`'.implode('`, `', $fulltextList) . '`) ';
		}

		if(!empty($this->indexes)){
			foreach ($this->indexes as $index){
				$tableFields[] = $index;
			}
		}

		return "CREATE TABLE `" . $this->name . "` ( \n" . implode(",\n", $tableFields) ." \n) ENGINE=" . $this->tableType . ' CHARACTER SET ' . $this->charset . ' COLLATE '. $this->collation;
	}

}

/**
 * Table Creator Field Class
 * This class is used as a child for iwp_tablecreator. It stores the information regarding one field within the table being constructed by iwp_tablecreator
 * This class is not functional to use on its own.
 *
 * @package IWP
 * @subpackage IWP_API
 */

class iwp_tablecreator_field {
	/**
	 * The name of the field
	 * @var string
	 */
	protected $name = '';

	/**
	 * The type of the current field. This can be any valid MySQL field type. See: http://dev.mysql.com/doc/refman/5.0/en/data-types.html
	 * Most common values will be 'int', 'tinyint', 'varchar', 'text', 'datetime' and 'longtext'
	 * @var string
	 */
	protected $type = '';

	/**
	 * This is the length of the field if it is of a type that requires a length, such as 'int' or 'varchar'. Fields such as 'text' can ignore this variable.
	 * @var string|integer
	 */
	protected $length = '';

	/**
	 * This is the collation for the field -- It should be 'utf8_general_ci' except for rare circumstances
	 * @var string
	 */
	protected $collation = 'utf8_general_ci';

	/**
	 * Whether or not this field is unsigned or not. This can only be used for number field types.
	 * @var boolean True if the field is unsigned, false otherwise
	 */
	protected $isUnsigned = true;

	/**
	 * Whether or not this field can be NULL.
	 * @var boolean True if the field can be null, false if the field should be 'NOT NULL'
	 */
	protected $isNull = false;

	/**
	 * The default value for the field
	 * @var mixed
	 */
	protected $default  = '';

	/**
	 * If the current field type is enum, set the values of the enum here as an array
	 * @var array
	 */
	protected $enumValues = array();

	/**
	 * A flag for whether or not the current field should automatically increment for each new row.
	 * @var boolean True if the field should be marked as 'AUTO_INCREMENT', false otherwise
	 */
	protected $autoIncrement = false;

	/**
	 * A flag for whether or not the current field is the primary key for the table.
	 * Only one field can be the primary key, so which ever field is first marked as the primary key will be the only one output as the primary key
	 * @var boolean True if this field is the primary key for the table, false otherwise
	 */
	protected $isPrimaryKey = false;

	/**
	 * A flag for whether or not this field has an index or not. This does not count fulltext indexes.
	 * @var boolean True if this field should have an index, false otherwise
	 */
	protected $hasIndex = false;

	/**
	 * A flag for whether or not this field has a fulltext index or not.
	 * @var boolean True if the field has a fulltext index, false otherwise
	 */
	protected $hasFullText = false;

	/**
	 * A flag for whether or not this field should be marked as unique or not. This will mean no value placed in this field can be the same as another row.
	 * @var boolean True if this field should have the unique flag, false otherwise
	 */
	protected $isUnique = false;

	/**
	 * This should only be used for tiemstamp fields
	 * @var boolean Set to true if this field should update to the current timestamp everytime a row is modified
	 */
	protected $onUpdateTimeStamp = false;

	/**
	 * The class constructor, which also sets the name of the field in the table if it is passed through as the first argument
	 *
	 * @param string $name The name of the field
	 *
	 * @return void Doesn't return anything
	 */
	public function __construct($name=null){
		if(!is_null($name)){
			$this->name = $name;
		}
	}

	/**
	 * This returns the value of the protected member variable 'hasIndex'
	 *
	 * @return boolean The value of $this->hasIndex
	 *
	 * @see $hasIndex
	 */
	public function hasIndex(){
		return (bool)$this->hasIndex;
	}

	/**
	 * This returns the value of the protected member variable 'hasFullText'
	 *
	 * @return boolean The value of $this->hasFullText
	 *
	 * @see hasFullText
	 */
	public function hasFulltext(){
		return (bool)$this->hasFullText;
	}

	/**
	 * This returns the value of the protected member variable 'isPrimaryKey'
	 *
	 * @return boolean The value of $this->isPrimaryKey
	 *
	 * @see $isPrimaryKey
	 */
	public function isPrimaryKey(){
		return (bool)$this->isPrimaryKey;
	}

	/**
	 * This is the global setter for protected member variables. This checks the existing value first, and if it is boolean, only lets the new value be a boolean.
	 * If its the field type being set, it makes sure the value is converted to lowercase.
	 * For all other values it sets the value directly without any further filtering
	 *
	 * @param string $name The name of the variable to set
	 * @param string $value The value to set the variable to
	 *
	 * @return iwp_tablecreator_field It returns the current instance of the class so chaining can be used with this and other functions.
	 */
	public function Set($name, $value){

		if(isset($this->$name)){
			if(is_bool($this->$name) && is_bool($value)){
				$this->$name = (bool)$value;
			}elseif (!is_bool($this->$name)){
				if($name == 'type'){
					$value = iwp_strtolower($value);
				}
				$this->$name = $value;
			}else{
			}
		}

		return $this;
	}

	/**
	 * This is a shortcut/helper function to set the current field to a char with the most common settings
	 *
	 * @param string|integer $length The length of the field. This defaults to 255. This should only be used if the value is to be lower than this default value.
	 *
	 * @return iwp_tablecreator_field It returns the current instance of the class so chaining can be used with this and other functions.
	 */
	public function SetAsChar ($length = null) {
		$this->type = 'char';
		$this->length = '255';
		if ($length !== null) {
			$this->length = $length;
		}
		return $this;
	}

	/**
	 * This is a shortcut/helper function to set the current field to a varchar with the most common settings
	 *
	 * @param string|integer $length The length of the field. This defaults to 255. This should only be used if the value is to be lower than this default value.
	 *
	 * @return iwp_tablecreator_field It returns the current instance of the class so chaining can be used with this and other functions.
	 */
	public function SetAsVarChar($length=null) {
		$this->type = 'varchar';
		$this->length = '255';
		if(!is_null($length)){
			$this->length = $length;
		}
		return $this;
	}

	public function SetAsText() {
		$this->type = 'text';
		$this->length = 0;
		return $this;
	}

	public function SetAsTinyText()
	{
		$this->type   = 'tinytext';
		$this->length = 0;

		return $this;
	}

	public function SetAsEnum($values, $default = null)
	{
		$values = (array) $values;

		$this->type       = 'enum';
		$this->enumValues = $values;

		if (!is_null($default)) {
			$this->default = $default;
		}

		return $this;
	}

	public function SetAsDateTime () {
		$this->type = 'datetime';
		$this->length = 0;
		return $this;
	}

	/**
	 * This is a shortcut/helper function to set the current field to a 'tinyint' with the most common settings
	 *
	 * @param boolean $hasIndex Whether or not this field should have an index. True if it should, false if it shouldn't
	 *
	 * @return iwp_tablecreator_field It returns the current instance of the class so chaining can be used with this and other functions.
	 */
	public function SetAsTinyInt($hasIndex=false){
		$this->type = 'tinyint';
		$this->isUnsigned = true;
		$this->length = '1';
		$this->default = 0;
		if($hasIndex){
			$this->hasIndex = true;
		}
		return $this;
	}

	/**
	 * This is a shortcut/helper function to set the current field to a 'int' with the most common settings
	 *
	 * @param boolean $hasIndex Whether or not this field should have an index. True if it should, false if it shouldn't
	 *
	 * @return iwp_tablecreator_field It returns the current instance of the class so chaining can be used with this and other functions.
	 */
	public function SetAsInt($hasIndex=false){
		$this->type = 'int';
		$this->isUnsigned = true;
		$this->length = '11';
		$this->default = 0;
		if($hasIndex){
			$this->hasIndex = true;
		}
		return $this;
	}

	/**
	 * This is a shortcut/helper function to set the current field as the primary key for the table with the most common settings
	 *
	 * @param boolean $reconfigure Optional. Default true. If true, will reconfigure other parameters of the field to match a typical autoincrementing integer id field. Set as false to create your own custom primary key fields.
	 * @return iwp_tablecreator_field It returns the current instance of the class so chaining can be used with this and other functions.
	 */
	public function SetAsPrimaryKey($reconfigure = true){
		$this->isPrimaryKey = true;

		if ($reconfigure) {
			$this->SetAsInt();
			$this->default = null;
			$this->autoIncrement = true;
		}

		return $this;
	}

	/**
	 * This is the universal 'getter' function to retrieve the value of any member variable
	 *
	 * @param string $name The name of the variable to retrieve the value from
	 *
	 * @return mixed The value of the request member variable
	 */
	public function Get($name){
		if(isset($this->$name)){
			return $this->$name;
		}
		return '';
	}

	/**
	 * This sets the default value for this field.
	 *
	 * @param $value
	 * @return iwp_tablecreator_field It returns the current instance of the class so chaining can be used with this and other functions.
	 */
	public function SetDefault ($value) {
		$this->default = $value;
		return $this;
	}

	/**
	 * This gets the default value for this field for use in the create table query.
	 *
	 * @return string Either blank for no default value or the SQL for the default value
	 *
	 * @see $default
	 */
	public function GetDefault(){
		if ($this->default === null) {
			return ' DEFAULT NULL ';
		}

		if (is_int($this->default) || is_float($this->default)) {
			return ' DEFAULT ' . $this->default . ' ';
		}

		if (is_bool($this->default)) {
			return ' DEFAULT ' . ($this->default ? '1' : '0') . ' ';
		}

		if (is_string($this->default)) {

			if (strlen($this->default) == 0) {
				return '';
			}

			if(iwp_strtoupper($this->default) == 'CURRENT_TIMESTAMP') {
				return ' DEFAULT CURRENT_TIMESTAMP ';
			}

			return ' DEFAULT \''. $this->default .'\' ';
		}

		return '';
	}

	/**
	 * Sets the member variable flag 'onUpdateTimeStamp' to true
	 *
	 * @return void Doesn't return anything
	 *
	 * @see $onUpdateTimeStamp
	 */
	public function SetOnUpdateTimeStamp(){
		$this->onUpdateTimeStamp = true;
	}

	/**
	 * If the flag member variable onUpdateTimeStamp is set to true, this will return some SQL that tells MySQL to update this field with the latest timestamp everytime a row is modified
	 *
	 * @return string Either blank or the SQL ' on update CURRENT_TIMESTAMP '
	 */
	public function GetOnUpdateTimestamp(){
		if($this->onUpdateTimeStamp && $this->type == 'timestamp'){
			return ' on update CURRENT_TIMESTAMP ';
		}
		return '';
	}

	/**
	 * This gets the autoincrement value for this field for use in the create table query.
	 *
	 * @return string Either blank for no autoincrement or the SQL for the autoincrement
	 *
	 * @see $autoIncrement
	 */
	public function GetAutoIncrement(){
		if($this->autoIncrement){
			return ' AUTO_INCREMENT ';
		}
		return '';
	}

	/**
	 * This gets the Unsigned value for this field for use in the create table query.
	 *
	 * @return string Either blank for no Unsigned or the SQL for the Unsigned flag
	 *
	 * @see $isUnsigned
	 */
	public function GetUnsigned(){
		if(in_array($this->type, array('int', 'tinyint', 'bigint', 'smallint', 'mediumint', 'float'))){
			if($this->isUnsigned){
				return ' UNSIGNED ';
			}
		}
		return '';
	}

	/**
	 * This sets whether or not the field allows NULL values.
	 *
	 * @param boolean $isNull If set to false NOT NULL will be used in the create table query, indicating NULL values are not allowed.
	 * @return iwp_tablecreator_field It returns the current instance of the class so chaining can be used with this and other functions.
	 */
	public function SetIsNull ($isNull = true) {
		$this->isNull = $isNull;
		return $this;
	}

	/**
	 * This gets the NULL value for this field for use in the create table query.
	 *
	 * @return string 'NULL' or 'NOT NULL' based on this fields isNull value
	 *
	 * @see $isNull
	 */
	public function GetIsNull(){
		if(!$this->isNull){
			return ' NOT NULL ';
		}
		return '';
	}


	/**
	 * This returns the full SQL for this field to be used in a CREATE TABLE SQL query
	 *
	 * @return string The SQL ready to be used to add this field to the table in a CREATE TABLE query
	 */
	public function GetCreateField(){
		$return = ' `' . $this->name . '` ';
		if(in_array($this->type, array('int', 'tinyint', 'bigint', 'smallint', 'mediumint', 'float'))){
			if($this->length > 0){
				return $return . iwp_strtoupper($this->type) . '(' . $this->length . ')' . $this->GetUnsigned() . $this->GetDefault() . $this->GetIsNull() . $this->GetAutoIncrement();
			}else{
				return $return . iwp_strtoupper($this->type) . $this->GetUnsigned() . $this->GetDefault() . $this->GetIsNull();
			}

		}elseif (in_array($this->type, array('char', 'varchar'))){
			if($this->length > 0){
				return $return . iwp_strtoupper($this->type) . '(' . $this->length . ')' . $this->GetDefault() . $this->GetIsNull();
			}else{
				return $return . iwp_strtoupper($this->type) . '(255)' . $this->GetDefault() . $this->GetIsNull();
			}

		}elseif(in_array($this->type, array('tinytext', 'text', 'blob', 'mediumtext', 'mediumblob', 'longtext', 'longblob'))){
			return $return . iwp_strtoupper($this->type) . $this->GetDefault() . $this->GetIsNull();

		}elseif (in_array($this->type, array('datetime', 'date', 'time'))){
			return $return . iwp_strtoupper($this->type) . $this->GetDefault() . $this->GetIsNull();

		}elseif ($this->type ==  'timestamp'){
			return $return . iwp_strtoupper($this->type) . $this->GetDefault() . $this->GetOnUpdateTimestamp() .  $this->GetIsNull();

		}elseif ($this->type == 'enum'){
			return $return . iwp_strtoupper($this->type) . '("' . implode('", "', $this->enumValues) . '")' . $this->GetDefault() . $this->GetIsNull();

		}else{
			return $return . iwp_strtoupper($this->type) . $this->GetDefault() . $this->GetIsNull();
		}

		return '';
	}

}