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/app/models/m_trashcan.php
<?php 

/*
	
	This Class handles all tasks regarding the trash can, such as insert into trashcan, empty trashcan and recover from trashcan

*/

require_once DOCUMENT_ROOT.'app/models/images/m_imageStorage.php';
require_once DOCUMENT_ROOT.'app/models/images/m_docStorage.php';

class Trashcan
{
	private $_pdo, $_template, $_pages, $_pageinfo;
	private $_lang;
	
	public function __construct()
	{
		global $pdo, $template, $pages;
		
		$this->_pdo = $pdo;

		$this->_template = $template;
		$this->_pages = $pages;

        $this->_lang = $template->lang;
    }
	
	/**
	*  
	*
	* @access public
	* @param array(optional)
	* @return array
	*/
	public function get($ids=NULL)
	{
		$data = array();
		
		if(isset($ids) && !empty($ids) && is_array($ids))
		{
			// get items in array
			// run through array
			foreach($ids as $activeId)
			{
				// build query
				$query = 'SELECT itemId, pageId, lastUpdate FROM kms_active WHERE id = ? ORDER BY lastUpdate DESC';
				
				if($st = $this->_pdo->prepare($query))
				{	
					$st->bindParam(1, $activeId);
					$st->execute();
					
					$result = $st->fetch(PDO::FETCH_OBJ);
					
					if($st->rowCount() > 0)
					{
						$this->_pageinfo = $this->_pages->get($result->pageId);
						
						$label = $this->_pageinfo['label'];
						$linkname = $this->_pageinfo['linkname'];
						$tableItems = 'page_'.$linkname.'_items';
						
						$itemQuery = 'SELECT title FROM '.$tableItems.' WHERE id = '.$result->itemId.' LIMIT 1';
						if($stItem = $this->_pdo->query($itemQuery))
						{
							$itemResult = $stItem->fetch(PDO::FETCH_OBJ);
							$data[] = array('activeId' => $activeId, 'itemId' => $result->itemId, 'pageId' => $result->pageId, 'pageLabel' => $this->_pageinfo['label'], 
											'title' => $itemResult->title, 'lastUpdate' => $result->lastUpdate); 
						}
					}
				}
			}
		}
		else{
			// get all items
				
			// build query
			$query = 'SELECT id, itemId, pageId, lastUpdate FROM kms_active WHERE active = 0 ORDER BY lastUpdate DESC';
			
			if($st = $this->_pdo->prepare($query))
			{	
				$st->execute();
				while($result = $st->fetch(PDO::FETCH_OBJ))
				{
					$this->_pageinfo = $this->_pages->get($result->pageId);
					$tableItems = 'page_'.$this->_pageinfo['linkname'].'_items';
					
					$itemQuery = 'SELECT title FROM '.$tableItems.' WHERE id = '.$result->itemId.' LIMIT 1';
					
					if($stItem = $this->_pdo->query($itemQuery))
					{
						$itemResult = $stItem->fetch(PDO::FETCH_OBJ);
						$data[] = array('activeId' => $result->id, 'itemId' => $result->itemId, 'pageId' => $result->pageId, 'pageLabel' => $this->_pageinfo['label'], 
										'title' => $itemResult->title, 'lastUpdate' => $result->lastUpdate); 
					}
				}			
			}
		
		}
		
		return $data;
	}	
	
	/**
	* Sets the pageinfo variable
	*
	* @access public 
	* @param id
	* @return null
	*/
	public function setPageInfo($id)
	{	
		if(is_numeric($id))
		{
			$this->_pageinfo = $this->_pages->get($id);
		}
		else if(is_array($id))
		{
			$this->_pageinfo = $id;
		}
	}
	
	/**
	* This function inserts items and such into the trashcan
	*
	* @access public
	* @param array / int
	* @return null
	*/
	public function insert($ids)
	{
		// build query
		$query = 'UPDATE kms_active SET active = 0 WHERE itemId = ? AND pageId = '.$this->_pageinfo['id'].' LIMIT 1';
		
		// prepare statement
		if($st = $this->_pdo->prepare($query))
		{
			$st->bindParam(1,$id);
			
			if(is_array($ids))
			{
				$count = count($ids);
				
				foreach($ids as $itemId)
				{
					$id = intval($itemId);
					$st->execute();
				}
			}
			else if(is_numeric($ids))
			{
				$id = $ids;
				$st->execute();
				$count = 1;
			}
		}
		
		// correct the order of items
		$linkname = $this->_pageinfo['linkname'];
		$tableItems = 'page_'.$linkname.'_items';
		
		// build query to get items
		$query = 'SELECT i.id, a.active FROM '.$tableItems.' AS i, kms_active AS a 
					WHERE a.itemId = i.id
					AND a.pageId = '.$this->_pageinfo['id'].'
					ORDER BY itemOrder ASC';	
		$st = $this->_pdo->prepare($query);
		$newOrder = 1;
		if($st->rowCount() > 0)
		{
			while($result = $st->fetch(PDO::FETCH_OBJ))
			{
				if($result->active == 1)
				{
					$this->_pdo->query('UPDATE '.$tableItems.' SET itemOrder = '.$newOrder.' WHERE id = '.$result->id.' LIMIT 1');
					$newOrder++;
				}
				else
				{
					$this->_pdo->query('UPDATE '.$tableItems.' SET itemOrder = 0 WHERE id = '.$result->id.' LIMIT 1');
				}
			}		
		}
		if($count == 1)
		{
			$alert = $this->_lang['success_remove_item'];
			$this->_template->setAlert($alert);
		}
		else
		{
			$alert = str_replace('[num]',$count,$this->_lang['success_remove_items']);
			$this->_template->setAlert($alert);
		}
	}
	
	/**
	* Permanently delete the files in the given array
	*
	* @access public
	* @param array
	* @return null
	*/
	public function delete($ids)
	{
		$valid = FALSE;
		if( ! empty($ids))
		{	
			// build query to get page info
			$queryGet = 'SELECT itemId, pageId FROM kms_active WHERE id = ? LIMIT 1';
			
			//prepare statement
			if($stGet = $this->_pdo->prepare($queryGet))
			{
				$stGet->bindParam(1, $id);
				
				if(is_array($ids))
				{	
					$count = count($ids);
					foreach($ids as $activeId)
					{
						$id = intval($activeId);
						$stGet->execute();
						
						if($stGet->rowCount() > 0){
						
							$result = $stGet->fetch(PDO::FETCH_OBJ);
							$itemId = $result->itemId;
							$pageId = $result->pageId;

							$this->_pageinfo = $this->_pages->get($pageId);
							$linkname = $this->_pageinfo['linkname'];
							
							$tableItems = 'page_'.$linkname.'_items';
							
							// remove images from database and server
							$tableImages = 'page_'.$linkname.'_images';
							$imageStorage = new ImageStorage($result->pageId);
							$imageStorage->setTableName($tableImages);
							if($images = $imageStorage->get('itemId',$itemId))
							{
                                if(!is_array($images[key($images)]))$images=array($images);
								foreach($images as $image)
								{
									$imageStorage->remove($image);
								}
							}

                            // remove docs from database and server
                            $tableImages = 'page_'.$linkname.'_docs';
                            $docStorage = new DocStorage($result->pageId);
                            $docStorage->setTableName($tableImages);
                            if($docs = $docStorage->get('itemId',$itemId))
                            {
                                if(!is_array($docs[key($docs)]))$docs=array($docs);
                                foreach($docs as $doc)
                                {
                                    $docStorage->remove($doc);
                                }
                            }

                            // Remove Where to buy products
                            if($linkname == 'wheretobuy')
                            {
                                $wtbQuery = 'DELETE FROM page_wheretobuy_products WHERE itemId = ?';
                                if($wtbSt = $this->_pdo->prepare($wtbQuery))
                                {
                                    $wtbSt->bindParam(1,$result->itemId);
                                    $wtbSt->execute();
                                }
                             }

                            if( ! empty($result->itemId))
                            {
                                // build query to remove pageItem from database
                                $queryDel = 'DELETE FROM '.$tableItems.' WHERE id = ? LIMIT 1';

                                //prepare statement
                                if($stDel = $this->_pdo->prepare($queryDel))
                                {
                                    $stDel->bindParam(1,$result->itemId);
                                    $stDel->execute();

                                    // delete from active
                                    // build query to remove pageItem from database
                                    $queryDelA = 'DELETE FROM kms_active WHERE id = ? LIMIT 1';
                                    if($stDelA = $this->_pdo->prepare($queryDelA))
                                    {
                                        $stDelA->bindParam(1,$activeId);
                                        $stDelA->execute();

                                        $valid = TRUE;
                                    }
                                }
                            }
						}
					}
				}
			}
		}
		
		if($valid)
		{
			if($count == 1)
			{
				$alert = $this->_lang['success_perm_delete_item'];
				$this->_template->setAlert($alert);
			}
			else
			{
				$alert = str_replace('[num]',$count,$this->_lang['success_perm_delete_items']);
				$this->_template->setAlert($alert);
			}
		}
	}
	
	/**
	* This function recovers item from the trashcan
	*
	* @access public
	* @param array / int
	* @return null
	*/
	public function recover($ids)
	{
		$valid = FALSE;
		if( ! empty($ids))
		{	
			// build query to get page info
			$queryGet = 'SELECT itemId, pageId FROM kms_active WHERE id = ? LIMIT 1';
			
			//prepare statement
			if($stGet = $this->_pdo->prepare($queryGet))
			{
				$stGet->bindParam(1,$id);
				
				if(is_array($ids))
				{
					$count = count($ids);
					foreach($ids as $activeId)
					{
						$id = intval($activeId);
						$stGet->execute();
					
						$result = $stGet->fetch(PDO::FETCH_OBJ);
						$itemId = $result->itemId;
						
						$this->_pageinfo = $this->_pages->get($result->pageId);
						// build query to active item
						$query = 'UPDATE kms_active SET active = 1 WHERE id = ? LIMIT 1';
						
						// prepare statement
						if($stSet = $this->_pdo->prepare($query))
						{
							$stSet->bindParam(1,$activeId);
							$stSet->execute();
							
							// correct the order of items
							$linkname = $this->_pageinfo['linkname'];
							$tableItems = 'page_'.$linkname.'_items';
							
							// build query to get items
							$newOrder = 1;
							$query = 'SELECT i.itemOrder FROM '.$tableItems.' AS i, kms_active AS a 
										WHERE a.active = 1
										AND a.itemId = i.id
										AND a.pageId = '.$this->_pageinfo['id'].'
										ORDER BY itemOrder DESC LIMIT 1';	
							$st = $this->_pdo->prepare($query);
							
							if($st->rowCount() > 0)
							{
								$record = $st->fetch(PDO::FETCH_ASSOC);
								$order = $record['itemOrder'];
								$newOrder = $order+1;
							}
							
							if($this->_pdo->query('UPDATE '.$tableItems.' SET itemOrder = '.$newOrder.' WHERE id = '.$itemId.' LIMIT 1'))
							{
								$valid = TRUE;
							}
						}
					}
				}
			}
		}					
		if($valid)
		{
			if($count == 1)
			{
				$this->_template->setAlert($this->_lang['success_recover_item']);
			}
			else
			{
                $alert = str_replace('[num]',$count,$this->_lang['success_recover_items']);
				$this->_template->setAlert($alert);
			}
		}
	}
	
	/*
	
		CREATE PAGE OUTPUT
	
	*/
	
	/**
	* Returns a string containing all listitems on this page
	*
	* @access public
	* @param array
	* @return string
	*/
	public function createList($data, $selectable = TRUE)
	{
		$output = $this->_lang['no_items_found'];
		$ids = array();
		
		if($data != NULL)
		{			
			$i = 1;	
			$output = '';
			foreach($data as $info)
			{
				if(isset($info['activeId']))
				{
					 $ids[] = $info['activeId'];	
				}
				else 
				{
					 $ids[] = $info['itemId'];	
				}
				$removed = date('d / m / Y',$info['lastUpdate']);
			
				$output .= '<li';
				if($i % 2 != 0) $output .= ' class="alt"';
				$output .= '>';
					$output .= '<div class="col cb">';
						if($selectable) $output .= '<input type="checkbox" name="selected[]" value="'.$info['activeId'].'" />';
					$output .= '</div>';
					$output .= '<div class="col title">'.ucfirst($info['title']).'</div>';
					$output .= '<div class="col added">'.$removed.'</div>';
					$output .= '<div class="col">'.ucfirst($info['pageLabel']).'</div>';
				$output .= '</li>';
				$i++;
			}
		}

		return array($output, $ids);
	}
}