File: D:/HostingSpaces/SBogers10/komma-mediadesign.nl/wwwroot/beheer/app/models/images/m_imageStorage.php
<?php
/*
Handles all tasks regarding images stored in the database
*/
class ImageStorage
{
private $_pdo, $_template, $_pages;
private $_tableNames;
private $_pageinfo;
private $_data;
public function __construct($pageId)
{
global $pdo, $template, $pages;
$this->_pdo = $pdo;
$this->_template = $template;
$this->_pages = $pages;
$this->_pageinfo = $this->_pages->get($pageId);
$this->_tableNames['images'] = 'page_'.$this->_template->encodeDbName($this->_pageinfo['label']).'_images';
$this->_data = array();
}
/*
GET / SET
*/
/**
* Set tablename
*
* @access public
* @param string
* @return null
*/
public function setTableName($name)
{
if( ! empty($name) && is_string($name))
{
$this->_tableNames['images'] = $name;
}
}
/**
* Get images from the database
*
* @access public
* @param string, int
* @return array
*/
public function get($key=NULL, $val=NULL)
{
$data = array();
if( ! empty($key))
{
// get images by an identifier
switch($key)
{
case 'shortcode':
// by shortcode
if(is_string($val))
{
$query = 'SELECT id, shortcode, filename_original, filename_thumb, mo_thumb
FROM '.$this->_tableNames['images'].'
WHERE shortcode = ? LIMIT 1';
}
break;
case 'itemId':
if( ! empty($val) && is_numeric($val))
{
$query = 'SELECT id, shortcode, filename_original, filename_thumb, mo_thumb
FROM '.$this->_tableNames['images'].'
WHERE itemId = ? ORDER BY imageOrder DESC';
}
break;
}
if($st = $this->_pdo->prepare($query))
{
$st->bindParam(1, $val);
$st->execute();
if($st->rowCount() > 0)
{
switch($key)
{
case 'shortcode':
$result = $st->fetch(PDO::FETCH_OBJ);
$data = array('id' => $result->id, 'shortcode' => $result->shortcode, 'mothumb' => $result->mo_thumb, 'name' => $result->filename_original, 'thumb' => $result->filename_thumb);
break;
case 'itemId':
while($result = $st->fetch(PDO::FETCH_OBJ))
{
$data[] = array('id' => $result->id, 'shortcode' => $result->shortcode, 'mothumb' => $result->mo_thumb, 'name' => $result->filename_original, 'thumb' => $result->filename_thumb);
}
break;
}
return $data;
}
}
}
return FALSE;
}
/**
* Stores the images in the database
*
* @access private
* @param array
* @return null
*/
public function store($itemId, $imageSession)
{
if($imageSession->count() > 0)
{
// build queries
$insertQuery = 'INSERT INTO '.$this->_tableNames['images'].'(itemId, shortcode, filename_original, filename_thumb, mo_thumb, imageOrder ) VALUES (?, ?, ?, ?, ?, ?)';
$updateQuery = 'UPDATE '.$this->_tableNames['images'].' SET imageOrder = ? WHERE id = ? LIMIT 1';
if($stInsert = $this->_pdo->prepare($insertQuery))
{
if($stUpdate = $this->_pdo->prepare($updateQuery))
{
$stInsert->bindParam(1, $itemId);
$stInsert->bindParam(2, $shortcode);
$stInsert->bindParam(3, $filename_original);
$stInsert->bindParam(4, $filename_thumb);
$stInsert->bindParam(5, $mo_thumb);
$stInsert->bindParam(6, $imageOrder);
$stUpdate->bindParam(1, $imageOrder);
$stUpdate->bindParam(2, $id);
$imageOrder = $imageSession->count();
foreach($imageSession->get() as $data)
{
$shortcode = $data['shortcode'];
$filename_original = $data['name'];
$filename_thumb = $data['thumb'];
$mo_thumb = $data['mothumb'];
if($image = $this->get('shortcode',$shortcode))
{
$id = $image['id'];
$stUpdate->execute();
}
else
{
$stInsert->execute();
}
$imageOrder--;
}
return TRUE;
}
}
}
}
/**
* Removes images from the database
*
* @access public
* @param
* @return
*/
public function remove($data)
{
if( ! empty ($data))
{
if( ! empty($data['name']) && is_file(DOCUMENT_IMAGE_ROOT.'uploads/'.$data['name']) ) unlink(DOCUMENT_IMAGE_ROOT.'uploads/'.$data['name']);
if( ! empty($data['thumb']) && is_file(DOCUMENT_IMAGE_ROOT.'uploads/'.$data['thumb']) ) unlink(DOCUMENT_IMAGE_ROOT.'uploads/'.$data['thumb']);
if( ! empty($data['mothumb']) && is_file(DOCUMENT_IMAGE_ROOT.'uploads/'.$data['mothumb']) ) unlink(DOCUMENT_IMAGE_ROOT.'uploads/'.$data['mothumb']);
if( ! empty($data['id']))
{
if($this->_pdo->query('DELETE FROM '.$this->_tableNames['images'].' WHERE id = '.$data['id'].' LIMIT 1'))
{
return TRUE;
}
}
}
return FALSE;
}
}