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/SBogers68/resortouddorpduin.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 = array();

    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 = array($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 = array($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 = array($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 = array();
        $sessionVideos = Session::get($this->_form_name . '_videos');
        if( ! is_array($sessionVideos[key($sessionVideos)]) ) $sessionVideos = array($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();
                    }
                }
            }
        }
    }
}