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/spire.komma-mediadesign.nl/wwwroot/kms/client/models/m_categories.php
<?php

/*
	
	This class handles all tasks regarding categorie-items

*/

require_once DOCUMENT_ROOT.'app/models/m_page.php';


class Categorypage extends Page
{
	/**
	*
	*	@var array Category data
	*/
	private $_data = array('id'=>'', 'title'=>'', 'description'=>'', 'timest'=>'');
	
	/**
	*
	*	@var int Since this is an extention we need to hardcode the pageId
	*/
	private $_pageId = 3;
	
	/**
	*
	* Constructor
	*/
	public function __construct()
	{
		// Storage Session
		$this->dataSession = new DataSession('data_to_store',$this->_data);	
		
		// Name of the databasetable
		$this->tableName = 'page_categorieen_items';
	
		// database handler
		$this->dbh = new DatabaseHandler();
		$this->dbh->setTableName($this->tableName);
		$this->dbh->setOrder('title', 'ASC');

        // lang
        global $template;
        $this->lang = $template->lang;

	}
	
	/**
	* This functions graps data from the database ands returns this in an array
	* If an array of ids is passed trough only those rows from that list of ids will be returned, otherwise all rows wil be returned
	*
	* @access public
	* @param array(optional)
	* @return array
	*/
	public function get($ids = NULL)
	{
		// tell the database handler what data to get switch date with timest
		$this->dbh->setData($this->_data);
		
		$data = array();
		if( ! empty($ids))
		{
			// grap categories from specified ids
			$data = array();
			foreach($ids as $id)
			{
				$this->dbh->addRule('id',$id);
				$data[] = $this->dbh->select();
			}
		}
		else
		{ 
			// grap all categories
			$this->dbh->setActiveOnly(TRUE, $this->_pageId);
			$this->dbh->setScope(0,$this->maxItems);
			$data = $this->dbh->select();
		}
		
		return $data;
	}
	
	/**
	* Puts data into the session data_to_store
	*
	* @access public
	* @param int
	* @return null
	*/
	public function setStorageData($id)
	{
		if( ! empty($id) && is_numeric($id))
		{
			$tempdata = $this->get(array($id));
			$data = $tempdata[0];
			
			foreach(array_keys($_SESSION['data_to_store']) as $key)
			{
				if(isset($data[$key]))
				{
					$_SESSION['data_to_store'][$key] = $data[$key];
				}
			}
		}
	}
	
	/**
	* Save data in the datasession
	* Validate and store a item in the database
	* Id an id is set, the storage needs to be updated, else a new row is inserted
	*
	* @access public
	* @param array
	* @return boolean
	*/
	public function store($id = NULL)
	{
		// data obtained by post
		$this->_data = $this->dataSession->savePost();
							
		if( ! empty($this->_data))
		{
			if($this->validate($id))
			{	
				if($id == NULL)
				{
					// insert data
					$this->_data['timest'] = time();
					$this->dbh->setData($this->_data);
					
					if($insertId = $this->dbh->insert())
					{	
						$activeDb = new DatabaseHandler();
						$activeDb->setTableName('kms_active');
						$activeDb->setData(array('itemId'=>$insertId,'pageId'=>$this->_pageId,'active'=>1,'lastUpdate'=>time()));
						if($activeDb->insert())
						{
							$this->dataSession->clear();
							return $insertId;
						}
					}
					else
					{
						$this->errors[] = 'Er is helaas iets misgegaan.';	
					}
				}
				else
				{
					var_dump($this->dbh);
					// Id is set => Update
					$this->dbh->setData($this->_data);
					$this->dbh->addRule('id',$id);
					if($this->dbh->update())
					{
						$this->dataSession->clear();
						return TRUE;
					}
				}
			}
		}
		return FALSE;
	}
	
	/**
	* Validates item
	* Save errors if these appear
	* 
	* @access private
	* @param
	* @return boolean
	*/
	private function validate($id=NULL)
	{
		$required = array();
		$title = $required[] = $this->_data['title'];
		$description = $required[] = $this->_data['description'];
		
		// check for empty fields
		foreach($required as $value)
		{
			if(empty($value))
			{
				$errors[] = 'U dient de verplichte velden (*) in te vullen';
			}	
		}
		
		
		// check unique values (only insert)
		$titleDbh = new DatabaseHandler();
		$titleDbh->setTableName($this->tableName);
		$titleDbh->setOrder('title', 'ASC');
		$titleDbh->setData(array('title'=>''));	
		$titleDbh->addRule('title', $title);	
		if( $id != NULL)
		{
			$titleDbh->addRule('id', $id, ' AND NOT ');	
		}
		if($titleDbh->select())
		{
			$errors[] = 'Deze titel is al eerder gekozen, kies een unieke naam.';
		}
	
		// check if valid
		
		if(isset($errors))
		{	
			$this->errors = $errors;
			return FALSE;
		}
		else
		{

			return TRUE;
		}
	}
}