<?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($stream = false){
		$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.jpg',$picture);
		}
		if($stream) return file_get_contents($picture);
		return 'action.php?action=hackpoint_download_file&file='.base64_encode('sketch'.SLASH.$this->id.SLASH.'cover.jpg');
	}

	public function download(){
		require_once(__DIR__.SLASH.'ResourceType.class.php');
		$stream = "Fonctionnalité en cours d'implémentation, désolé :p";
		$folder = $this->directory();//todo
		
		$filename = $this->slug.'-'.time().'.zip';
		$filepath = sys_get_temp_dir().DIRECTORY_SEPARATOR.$filename;
		$zip = new ZipArchive;
		$res = $zip->open($filepath, ZipArchive::CREATE);
		if ($res === TRUE) {

			$mainReadme = $this->toFile();
			//ajout du md principal
			$zip->addFromString($mainReadme['name'], $mainReadme['content']);
			//ajout de l'image de couverture du sketch
			$zip->addFromString('cover.jpg', $this->picture(true));
			

			foreach($this->resources() as $resource){
				$type = ResourceType::types($resource->type);
				$file = $type['class']::toFile($resource);

				$zip->addFromString($file['name'], $file['content']);
			}
			$zip->close();
		}
		$stream = file_get_contents($filepath);
		unlink($filepath);
		return $stream;
	}


	//Export vers un fichier brut
	public function toFile(){
		$content = '# '.$this->label.PHP_EOL.PHP_EOL;
		$content .= '``'.$this->comment.'``'.PHP_EOL.PHP_EOL;
		$content .= '* Visibilité : '.($this->state?'Public':'Privé').PHP_EOL;
		$content .= '* Créé par '.$this->creator.' le '.strtolower(relative_time($this->created)).PHP_EOL;
		$content .= '* Modifié par '.$this->updater.' le '.strtolower(relative_time($this->updated)).PHP_EOL;
		return array(
			'name'=> 'README-SKETCH.md',
			'content' => html_entity_decode($content)
		);
	}

	public function resources(){
		require_once(__DIR__.SLASH.'Resource.class.php');
		return Resource::loadAll(array('sketch'=>$this->id));
	}

	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);
    }
}
?>