File: D:/HostingSpaces/SBogers64/klimroosbudel.nl/wwwroot/kms/lib/files/video_handler.class.php
<?php
/**
* video_handler.class.php
* Created by Komma Mediadesign.
* Author: mike
* Date: 5/6/13
*/
class Video_Handler
{
private $_form_name = '';
private $_errors = [];
public function __construct()
{
}
/*
* Return the asked key to the user
*/
public function get($key)
{
return $this->{$key};
}
public function set($prop, $value)
{
$this->{$prop} = $value;
}
/*
* Return the asked key to the user
*/
public function getFromDb($key, $value)
{
// Set table name we will be working in
$Dbh = new DatabaseHandler();
$Dbh->setTableName(TABLE_PREFIX.$this->_form_name.'_videos');
if ($key != null && $value != null) {
$Dbh->addRule($key, $value);
}
if ($data = $Dbh->select()) {
if ($key == null || $key == 'itemId') {
if (! is_array($data[key($data)])) {
$data = [$data];
}
}
return $data;
}
return false;
}
/*
* Add a youtube link to the session
*/
public function add()
{
// Get the users input
$input = $_POST['videos'];
// Is the link valid
if (! empty($input) && $input != 'Youtube link') {
$data['youtube_id'] = str_replace('http://youtu.be/', '', $input);
$data['short_code'] = '0'.time();
// Add data to session
($images = Session::get($this->_form_name.'_videos')) ? $count = count($images) : $count = 0;
$key = [$this->_form_name.'_videos', $count];
Session::set($key, $data);
} else {
$this->_errors[] = 'The field is empty.';
}
return false;
}
/*
* Store a youtube link in the database
*/
public function store($itemId)
{
$Dbh = new DatabaseHandler();
$Dbh->setTableName(TABLE_PREFIX.$this->_form_name.'_videos');
// get images in the session
$videos = Session::get($this->_form_name.'_videos');
if (is_array($videos)) {
if (! is_array($videos[key($videos)])) {
$videos = [$videos];
}
foreach ($videos as $video) {
// check if image isset in db
if (! $this->getFromDb('short_code', $video['short_code'])) {
$video['timestamp'] = time();
$video['itemId'] = $itemId;
echo 'insert!';
$Dbh->setData($video);
$Dbh->insert();
}
}
}
}
/*
* Fill video session with videos from the database.
*/
public function fillSession()
{
if (defined('URL_SUB2')) {
// set item id
$key = 'itemId';
$value = URL_SUB2;
if ($videos = $this->getFromDb($key, $value)) {
$sessionKey = $this->_form_name.'_videos';
if (! isset($_SESSION[$sessionKey])) {
Session::set($sessionKey, $videos);
}
}
}
}
/*
* Clean up videos which are not in the database, but still in the session.
*/
public function cleanSession()
{
Session::destroy($this->_form_name.'_videos');
}
/*
* If an image is removed from the session when a item is saved, remove it from the database.
*/
public function cleanDatabase($itemId)
{
// Get all shortCodes from session
$sessionShortCodes = [];
$sessionVideos = Session::get($this->_form_name.'_videos');
if ($sessionVideos == false) {
$sessionVideos = [];
}
if (! is_array($sessionVideos[key($sessionVideos)])) {
$sessionVideos = [$sessionVideos];
}
foreach ($sessionVideos as $sessionVideo) {
$sessionShortCodes[] = $sessionVideo['short_code'];
}
// Get all images from database
if ($dbVideos = $this->getFromDb('itemId', $itemId)) {
if (is_array($dbVideos)) {
foreach ($dbVideos as $dbVideo) {
// If shortCode not in session anymore, remove from database
if (! in_array($dbVideo['short_code'], $sessionShortCodes)) {
$DbRemover = new DatabaseHandler();
$DbRemover->setTableName(TABLE_PREFIX.$this->_form_name.'_videos');
$DbRemover->clearRule();
$DbRemover->addRule('id', $dbVideo['id']);
$DbRemover->delete();
}
}
}
}
}
}