<?php
class Library_Virtual_Directory extends Library_Virtual_Abstract {
	/**
	 * Objekt s radkem tabulky
	 * @var Zend_Db_Table_Row_Abstract
	 */
	private $_thisRow;
	
	/**
	 * seznam podrazenych adresaru
	 * @var array
	 */
	protected $_directories = array();
	
	/**
	 * seznam podrizenych dokumentu
	 * @var array
	 */
	protected $_documents = array();
	
	public function __construct(Zend_Db_Table_Row_Abstract $row, Library_Virtual_Abstract $parent = null) {
		$this->_thisRow = $row;
		
		parent::__construct($row->directory_name, $parent);
	}
	
	/**
	 * nacte podrizene objekty adresare
	 * @param bool $recursive = true
	 * @return Library_Virtual_Directory
	 */
	public function loadChildrens($recursive = true) {
		//nacteni podrizenych adresaru
		$this->_directories = array();
		
		$dirList = $this->_thisRow->findDependentRowset("Application_Model_DocumentsDirectories", "parent");
		
		foreach ($dirList as $directory) {
			$this->_directories[] = new Library_Virtual_Directory($directory, $this);
		}
		
		//nacteni seznamu jmen a jeho zpracovani
		$nameList = $this->_thisRow->findDependentRowset("Application_Model_DocumentsDirectoriesHasManyDocuments", "directory");
		$names = array();
		
		foreach ($nameList as $name) {
			$names[$name->document_uuid] = $name->document_name;
		}
		
		//nacteni seznamu dokumentu
		$documentList = $this->_thisRow->findManyToManyRowset("Application_Model_Documents", "Application_Model_DocumentsDirectoriesHasManyDocuments", "directory", "document");
		$this->_documents = array();
		
		//zapsani seznamu dokumentu
		foreach ($documentList as $document) {
			$name = $names[$document->uuid];
			
			$this->_documents[] = new Library_Virtual_Document($document, $this, $name);
		}
		
		//kontrola jestli se ma nacitat rekurzivne
		if ($recursive) {
			foreach ($this->_directories as $directory) {
				$directory->loadChildrens(true);
			}
		}
		
		return $this;
	}
	
	/**
	 * vraci pole podrizenych adredaru
	 * @return array
	 */
	public function getDirs() {
		return $this->_directories;
	}
	
	/**
	 * vraci pole dokumentu v adredari
	 * @return array
	 */
	public function getDocuments() {
		return $this->_documents;
	}
	
	/**
	 * nahraje dokumenty a adresare na server
	 * @param resource $ftp zdroj FTP spojeni
	 * @param bool $recursive pokud je TRUE, nahraje se obsah rekurzivne
	 */
	public function sendToFtp($ftp, $recursive = true) {
		//nacteni a naparsovani vypisu dokumentu
		$rawList = ftp_rawlist($ftp, ".");
		$nList = ftp_nlist($ftp, ".");
		
		//priprava seznamu souboru a adredaru
		$dirList = array();
		$fileList = array();
		
		$maxI = count($rawList);
		
		for ($i = 0; $i < $maxI; $i++) {
			//kontorla jeslti je objekt adresarem nebo souborem
			if ($rawList[$i][0] == "d") {
				//objekt je adredarem
				$dirList[] = $nList[$i];
			} else {
				//objekt je souborem
				$fileList[$i] = $nList[$i];
			}
		}
		
		//zapsani dokumentu
		/* @var $document Library_Virtual_Document */
		foreach ($this->_documents as $document) {
			//odeslani dokumentu na server
			$uuid = $document->getUuid();
			$doucumentName = $document->getName();
			
			//pokud soubor existuje, smaze se
			if (in_array($doucumentName, $fileList)) {
				ftp_delete($ftp, $doucumentName);
			}
			
			ftp_put($ftp, $doucumentName, CONTENT_PATH . "/" . $uuid, FTP_BINARY);
		}
		
		//konotrla existence potrebnych adredaru
		/* @var $directory Library_Virtual_Directory */
		foreach ($this->_directories as $directory) {
			$dirName = $directory->getName();
			
			//konotrla jestli adredar existuje
			if (!in_array($dirName, $dirList)) {
				//adresar neexistuje, musi se vytvorit
				ftp_mkdir($ftp, $dirName . "/");
			}
			
			//vstup do adresare na FTP a ulozeni lokalniho adresare
			ftp_chdir($ftp, $dirName);
			
			if ($recursive) {
				$directory->sendToFtp($ftp);
			}
			
			//vystup z adresare
			ftp_chdir($ftp, "..");
		}
	}
}
?>