<?php
/**
 * Define a sketch.
 * @author Idleman
 * @category Plugin
 * @license copyright
 */
class Sketch extends Entity{
	public $id,$label,$state,$comment,$slug,$progress;
	protected $TABLE_NAME = 'hackpoint_sketch';
	public $fields =
	array(
		'id' => 'key',
		'label' => 'string',
		'state' => 'string',
		'comment' => 'longstring',
		'progress' => 'int',
		'slug' => 'string'
	);

	public $links = array(
	);

	public function picture(){
		$folder = $this->directory();
		if(!file_exists($folder)) mkdir($folder,0755,true);
		$picture = $folder.SLASH.'cover.jpg';
		if(!file_exists($picture)){
			copy(__DIR__.SLASH.'img'.SLASH.'default-sketch.png',$picture);
		}
		return 'action.php?action=hackpoint_download_file&file='.base64_encode('sketch'.SLASH.$this->id.SLASH.'cover.jpg');
	}

	public function directory(){
		return File::dir().'hackpoint'.SLASH.'sketch'.SLASH.$this->id;
	}

	public function save()
    {
        if ($this->id == 0) {
            $this->slug = slugify($this->label);
        }
        parent::save();
    }

    public static function removeById($id){
    	require_once(__DIR__.SLASH.'Resource.class.php');
    	$sketch = self::getById($id);
    	self::deleteById($sketch->id);
    	Resource::delete(array('sketch'=>$sketch->id));
    	if(file_exists($sketch->directory())) delete_folder_tree($sketch->directory(),true);
    }
}
?>