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_awards.php
<?php

/*
	
	This class handles all tasks regarding awards

*/

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


class Awardpage extends Page
{
	/**
	*
	*	@var array Category data
	*/
	private $_data = array('id'=>'', 'x2'=>'', 'title'=>'', 'description'=>'', 'categoryId'=>'', 'productId'=>'', 'link'=>'', 'date'=>'');
	
	/**
	*
	*	@var int Since this is an extention we need to hardcode the pageId
	*/
	private $_pageId = 2;
	
	/**
	*
	* Constructor
	*/
	public function __construct()
	{
		// Storage Session
		$this->dataSession = new DataSession('data_to_store',$this->_data);	
		
		// Name of the databasetable
		$this->tableName = 'page_awards_items';
	
		// database handler
		$this->dbh = new DatabaseHandler();
		$this->dbh->setTableName($this->tableName);
		$this->dbh->setOrder('itemOrder','DESC');

        // 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
		$getData = array('id'=>'',  'x2'=>'', 'title'=>'', 'description'=>'', 'link'=>'', 'categoryId'=>'', 'productId'=>'', 'timest'=>'', 'thumb'=>'');
		$this->dbh->setData($getData);
		
		$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 if((defined('URL_SUB') && URL_SUB == 'x2') || (defined('URL_SUB2') && URL_SUB2 == 'x2'))
        {
            $this->dbh->addRule('x2',1);
            $this->dbh->setActiveOnly(TRUE, $this->_pageId);
            $this->dbh->setScope(0,$this->maxItems);
            $data = $this->dbh->select();
        }
		else
		{ 
			// grab all categories
            $this->dbh->addRule('x2',1,'AND NOT');
            $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($key == 'date')
				{
					//timest to date conversion
                    $date = $data[$key] = date('m/d/Y',$data['timest']);
					$_SESSION['data_to_store'][$key] = $date;
				}
				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))
			{
				// date becomes timest - check if ! empty data, else get timest by time().
				if( ! empty($this->_data['date']))
				{
                    $temp = explode('/',$this->_data['date']);
                    $month = $temp[0];
                    $day = $temp[1];
					$year = $temp[2];
					if( ! empty($day) && ! empty($month) && ! empty($year))
					{
						$this->_data['timest'] = mktime(12,0,0, $month, $day, $year);
					}
				}
				else
				{
					$this->_data['timest'] = time();
				}
				unset($this->_data['date']);

                $this->_data['productId'] = $_POST['sub'.$this->_data['categoryId']];

				if($id == NULL)
				{
					// No id is set => Insert
					
					// order - get highest order from the table and add 1
					$orderData = array('itemOrder'=>'');
					$orderDbh = new DatabaseHandler();
					$orderDbh->setTableName($this->tableName);
					$orderDbh->setData($orderData);
					$orderDbh->setActiveOnly(TRUE, $this->_pageId);
					$orderDbh->setOrder('itemOrder','DESC');
					$orderDbh->setScope(0,1);
					if($result = $orderDbh->select())
					{	
						$highestOrder = $result['itemOrder'];
						
						$this->_data['itemOrder'] = $highestOrder + 1;
					}
					else
					{
						$this->_data['itemOrder'] = 1;
					}
					
					
					// insert data
					$this->dbh->setData($this->_data);
					
					if($insertId = $this->dbh->insert())
					{	
						$this->dbh->setTableName('kms_active');
						$this->dbh->setData(array('itemId'=>$insertId,'pageId'=>$this->_pageId,'active'=>1,'lastUpdate'=>time()));
						if($this->dbh->insert())
						{
							$this->dataSession->clear();
							return $insertId;
						}
					}
					else
					{
						$this->errors[] = $this->lang['general_something_went_wrong'];
                    }
				}
				else
				{
					// 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'];
		$link = $required[] = $this->_data['link'];
		
		// check for empty fields
		foreach($required as $value)
		{
			if(empty($value))
			{
				$errors[] = $this->lang['form_complete_all_fields'];
			}	
		}

        $checkboxes = array('x2');
        foreach($checkboxes as $key)
        {
            if( isset($_POST[$key]))
            {
                $this->_data[$key] = 1;
            }
            else
            {
                $this->_data[$key] = 0;
            }
        }

        //check category
        $category = $_POST['categoryId'];
        if($category != 0)
        {
            $sub = $_POST['sub'.$category];
            if($sub == 0)
            {
                $errors[] = $this->lang['awards_choose_category_product'];
            }
		}
        else{
            $errors[] = $this->lang['awards_choose_category'];
        }


		// is the link correctly displayed ?
		if(substr($link,0,4) != 'http')
		{
			$this->_data['link'] = 'http://'.$link;
		}

		// check unique values (only insert)
		$titleDbh = new DatabaseHandler();
		$titleDbh->setTableName($this->tableName);
		$titleDbh->setData(array('title'=>''));	
		$titleDbh->addRule('title', $title);	
		if( $id != NULL)
		{
			$titleDbh->addRule('id', $id, ' AND NOT ');	
		}
		if($titleDbh->select())
		{
			$errors[] = $this->lang['form_choose_unique_title'];
		}
	
		// check if valid
		
		if(isset($errors))
		{	
			$this->errors = $errors;
			return FALSE;
		}
		else
		{

			return TRUE;
		}
	}

    public function createCategories()
    {
        // grab all categories
        $fields = array('id','title');
        $data = array_fill_keys($fields,'');

        // tableNames
        $tables = array('','pcaccessories','pccases','cpucoolers','','','mobileaccessories','powersupply','systemcooling','tabletpc');

        // database handler
        $catDb = new DatabaseHandler();
        $catDb->setData($data);
        $catDb->setTableName('page_categorieen_items');
        $catDb->setOrder('title', 'ASC');
        $categories = $catDb->select();

        $catList = '';
        $subOutput = '';

        $catList .= '<option value="0">Select Category:</option>';

        foreach($categories as $category)
        {
            if($category['id'] != 4 && $category['id'] != 5)
            {
                $catList .= '<option value="'.$category['id'].'"';
                if($_SESSION['data_to_store']['categoryId'] == $category['id']) $catList .= ' selected';
                $catList .= '>'.$category['title'].'</option>';
                $prodTableName = $tables[$category['id']];

                // select products
                $prodDb = new DatabaseHandler();
                $prodDb->setData($data);
                $prodDb->setTableName('page_'.$prodTableName.'_items');
                $prodDb->setOrder('title', 'ASC');
                $products = $prodDb->select();
                if( ! is_array($products[key($products)])) $products = array($products);

                $subOutput .= '<label id="label' . $category['id'] . '" class="label" for="sub' . $category['id'] . '" style="display:none">Product: '. $category['title'] .'</label>';
                $subOutput .= '<select id="sub' . $category['id'] . '" name="sub' . $category['id'] . '" ';

                $_SESSION['data_to_store']['categoryId'] == $category['id'] ?  $subOutput .= 'style="display:block">' : $subOutput .= 'style="display:none">';
                $subOutput .= '<option value="0">Choose product</option>';
                foreach($products as $product)
                {
                    $subOutput .= '<option value="'.$product['id'].'"';
                    if($_SESSION['data_to_store']['categoryId'] == $category['id'] && $_SESSION['data_to_store']['productId'] == $product['id']) $subOutput .= ' selected';
                    $subOutput .= '>'.$product['title'].'</option>';

                }
                $subOutput .= '</select>';
                if($_SESSION['data_to_store']['categoryId'] == $category['id']) $subOutput .= '<script type="text/javascript">activeCategory = '. $category['id'] .'</script>';
            }
        }

        return array($catList, $subOutput);
    }
}