File: D:/HostingSpaces/SBogers64/klimroosbudel.nl/wwwroot/kms/lib/files/file_storage.class.php
<?php
/**
* file_storage.class.php
* Created by Komma Mediadesign.
* Author: mike
* Date: 4/16/13
*/
class File_Storage
{
/*
* Name of the table we're working in
*/
private $_tableName;
/*
* Session name of files to store
*/
private $_fileSessionName;
/*
* Name of the form we are working in
*/
private $_formName;
/*
* Database_Handler object
*/
private $_Dbh;
public function __construct()
{
$this->_Dbh = new DatabaseHandler();
}
/*
* Set
*/
public function set($prop, $val)
{
if (! empty($prop)) {
$this->{$prop} = $val;
}
}
/*
* Get
*/
public function get($key = null, $value = null)
{
// Set table name we will be working in
$this->_Dbh->setTableName($this->_tableName);
$this->_Dbh->resetData();
$this->_Dbh->clearRule();
$this->_Dbh->setOrder('session_key', 'ASC');
if ($key != null && $value != null) {
$this->_Dbh->addRule($key, $value);
}
if ($data = $this->_Dbh->select()) {
if ($key == null || $key == 'itemId') {
if (! is_array($data[key($data)])) {
$data = [$data];
}
}
return $data;
}
return false;
}
public function store($itemId)
{
// Set table name we will be working in
$this->_Dbh->setTableName($this->_tableName);
// get images in the session
$images = Session::get($this->_fileSessionName);
if (is_array($images)) {
if (! is_array($images[key($images)])) {
$images = [$images];
}
foreach ($images as $key => $image) {
// check if image isset in db
if (! $this->get('short_code', $image['short_code'])) {
$image['timestamp'] = time();
$image['itemId'] = $itemId;
$image['session_key'] = $key;
$this->_Dbh->setData($image);
$imageId = $this->_Dbh->insert();
} else {
if (! empty($image['short_code'])) {
// else get image id from database
/*$this->_Dbh->setData(array('id'=>''));
$this->_Dbh->addRule('short_code',$image['short_code']);
$result = $this->_Dbh->select();
$imageId = $result['id'];*/
$image['session_key'] = $key;
$this->_Dbh->setData(['caption'=>$image['caption'], 'session_key'=>$image['session_key']]);
$this->_Dbh->addRule('short_code', $image['short_code']);
$this->_Dbh->update();
$this->_Dbh->clearRule();
}
}
// update the thumb from key to id
if (isset($_POST['thumb'])) {
$thumbSession = [$this->_formName.'_data', 'thumb'];
if ($key == Session::get($thumbSession)) {
if (isset($_POST['thumb'])) {
$Db = new DatabaseHandler();
$tn = str_replace('_images', '_items', $this->_tableName);
$Db->setTableName($tn);
$Db->addRule('id', $itemId);
$Db->setData(['thumb'=>$key]);
$Db->update();
}
}
}
}
}
}
public function cleanDatabase($itemId)
{
// Get all shortCodes from session
$sessionShortCodes = [];
$sessionImages = Session::get($this->_fileSessionName);
if (! is_array($sessionImages[key($sessionImages)])) {
$sessionImages = [$sessionImages];
}
foreach ($sessionImages as $sessionImage) {
$sessionShortCodes[] = $sessionImage['short_code'];
}
// Get all images from database
if (! empty($itemId) && $dbImages = $this->get('itemId', $itemId)) {
if (is_array($dbImages)) {
foreach ($dbImages as $dbImage) {
// If shortCode not in session anymore, remove from database
if (! in_array($dbImage['short_code'], $sessionShortCodes)) {
$DbRemover = new DatabaseHandler();
$DbRemover->setTableName($this->_tableName);
$DbRemover->clearRule();
$DbRemover->addRule('id', $dbImage['id']);
$DbRemover->delete();
//todo unset images
$FileHandler = new File_Handler();
$name = str_replace('_data', '', $this->_fileSessionName);
$FileHandler->set('_name', $name);
$FileHandler->unlink(DOCUMENT_UPLOADS_ROOT.$dbImage['filename']);
$FileHandler->unlink(DOCUMENT_UPLOADS_ROOT.$dbImage['thumb']);
$FileHandler->unlink(DOCUMENT_UPLOADS_ROOT.$dbImage['kms_thumb']);
}
}
}
}
}
}