<?php
    
class attr_video extends attr {
    function getDBType() {
    	return "int(12)";
    }

    function render_input() {
    	
		$html = '<input class="input" type="file" name="' . $this->attr_name . '"><br>(макс.: '.ini_get('upload_max_filesize').')';    	
		$html .= '<input class="input" type="hidden" name="' . $this->attr_name . '" value="1">';    	
		if ( $this->getCurVal() ) {
			$html .= '<br><label><img src="img.php?id='.$this->obj->get($this->attr_name.'_preview').'" width="200"><br><input class="checkbox" type="checkbox" name="' . $this->attr_name . '_del" value="1"> Удалить</label>';    	
		}
		return $html;
    }
    
    function lPost() {
    	$tmpObj = new cobject( '_sys_files' );
    	$file = $_FILES[$this->attr_name];
    	$nn = md5(microtime(true));
    	
    	if ($_POST[$this->attr_name . '_del']) {
	    	$this->obj->set( $this->attr_name, 0 );
    	}

    	if (!$file['error']) {
            $arr = explode('/', $file['type'] );
            if ($arr[0] == 'video' ||  $arr[0] == 'application') {
    	    	if ( move_uploaded_file( $file['tmp_name'], _FILES_DIR_.$nn ) ) {
    		    	$tmpObj->set('filename', $nn );
    		    	$tmpObj->set('realname', $file['name'] );
    		    	$tmpObj->set('ext', $file['type'] );
    		    	$tmpObj->save();
    		    	$this->obj->set( $this->attr_name, $tmpObj->id );
                    symlink( _FILES_DIR_.$nn , _VIDEO_DIR_ . $tmpObj->id . '.flv');
                    if ( isset($this->obj->attrs[$this->attr_name.'_preview']) ) {
                        $str = "ffmpeg -i "._FILES_DIR_.$nn. " -an -ss 00:00:05 -an -r 1 -vframes 1 -y -f image2 -s ".$this->obj->attrs[$this->attr_name.'_preview']['templ']." " ._FILES_DIR_.$nn . "_pre.jpg";
                        system($str);
                        copy( _FILES_DIR_.$nn.'_pre.jpg' , _FILES_DIR_.$nn.'_pre' );        
                        unlink( _FILES_DIR_.$nn.'_pre.jpg' );
                        $tmpObj->byId(0);
        		    	$tmpObj->set('filename', $nn.'_pre' );
        		    	$tmpObj->set('realname', $file['name'].'.jpg' );
        		    	$tmpObj->set('ext', 'image/jpeg' );
        		    	$tmpObj->save();
                        $this->obj->set($this->attr_name.'_preview', $tmpObj->id );
                    }
                    if ( isset($this->obj->attrs[$this->attr_name.'_full_preview']) ) {
                        $str = "ffmpeg -i "._FILES_DIR_.$nn. " -an -ss 00:00:05 -an -r 1 -vframes 1 -y -f image2 -s ".$this->obj->attrs[$this->attr_name.'_full_preview']['templ']." " ._FILES_DIR_.$nn . "_pre.jpg";
                        system($str);
                        copy( _FILES_DIR_.$nn.'_pre.jpg' , _FILES_DIR_.$nn.'_full_preview' );        
                        unlink( _FILES_DIR_.$nn.'_pre.jpg' );
                        $tmpObj->byId(0);
        		    	$tmpObj->set('filename', $nn.'_full_preview' );
        		    	$tmpObj->set('realname', $file['name'].'.jpg' );
        		    	$tmpObj->set('ext', 'image/jpeg' );
        		    	$tmpObj->save();
                        $this->obj->set($this->attr_name.'_full_preview', $tmpObj->id );
                    }
    	    	}
            }
    	}
    }

    function render_text( $crop = false ) {
    	if ($crop) {
    		$val = '<a href="file.php?id='.$this->getCurVal().'"><img src="img.php?id='.$this->obj->get($this->attr_name.'_preview').'" width="100">Скачать</a>';	
    	} else {
            if (!$this->getCurVal()) return '';
            return $this->getCurVal();
    	}
    	return $val;
    }


}

?>