<?php
    
class attr_file_direct extends attr {
    function getDBType() {
    	return "varchar(255)";
    }

    function render_input() {
    	
		$html = '<input class="input" type="file" name="' . $this->attr_name . '">';    	
		$html .= '<input class="input" type="hidden" name="' . $this->attr_name . '" value="1">';    	
		if ( $this->getCurVal() ) {
			$html .= '<br><label><input class="checkbox" type="checkbox" name="' . $this->attr_name . '_del" value="1"> Удалить</label>';    	
		}
		return $html;
    }
    
    function lPost() {
    	$file = $_FILES[$this->attr_name];
    	$fname = md5(microtime(true));
    	
    	if ($_POST[$this->attr_name . '_del']) {
	    	$this->obj->set( $this->attr_name, '' );
    	}

    	if (!$file['error']) {

            $ext = pathinfo($file['name'], PATHINFO_EXTENSION);

            $fpath = substr($fname,0,2);
            @mkdir( _FILES_DIR_ . $fpath );

            $fpath2 = substr($fname,2,2);
            @mkdir( _FILES_DIR_ . $fpath . '/' . $fpath2);

            $fname = $fpath .'/' . $fpath2 . '/'. $file['name'];

	    	if ( move_uploaded_file( $file['tmp_name'], _FILES_DIR_.$fname ) ) {
		    	$this->obj->set( $this->attr_name, $fname );
	    	}
    	}
    }

    function render_text( $crop = false ) {
        if ($crop) {
		    $val = '<a href="'._STATIC_ROOT_.'files/'.$this->getCurVal().'">Скачать</a>';	
        } else {
            $val = $this->getCurVal();   
        }
    	return $val;
    }


}

?>