<?php
	require_once( $GLOBALS["site_root"] . "/includes/widgets/Widget.inc.php" );
	FNTH_class( "SessionFile" );

	class JobAttachments extends Widget {
		protected $bReadOnly = false;

		public function setReadOnly( $v ) { $this->bReadOnly = $v; }

		protected function generateLangDefs() {
			fo3_add_js_langdef( "Add_attachment" );
			fo3_add_js_langdef( "Del_attachment" );
			fo3_add_js_langdef( "Too_much_attachments" );
		}

		protected function getAttachmentPrefix( ) {
			return sprintf( "%s_file_" , $this->getId() );
		}

		protected function getCommentPrefix() {
			return sprintf( "%s_comment_" , $this->getId() );
		}

		public function addAttachment( $file ) {
			$id = $this->getId();
			if (!isset( $_SESSION[$id] )) $_SESSION[$id]=array();
			$_SESSION[$id][]=$file;
		}

		public function setAttachment( $fid , $file ) {
			fo3_log( sprintf( "setAttachment(%s,%s) called" , $fid , print_r( $file , true ) ));
			$id = $this->getId();
			if (!isset( $_SESSION[$id] )) $_SESSION[$id]=array();
			$_SESSION[$id][$fid]=$file;
		}

		public function unsetAttachment( $fid ) {
			fo3_log( sprintf( "unsetAttachment(%s) called" , $fid ));
			$id = $this->getId();
			if (!isset( $_SESSION[$id] )) $_SESSION[$id]=array();
			while (isset( $_SESSION[$id][$fid+1] )) {
				$_SESSION[$id][$fid]=$_SESSION[$id][$fid+1];
				$fid++;
			}
			unset( $_SESSION[$id][$fid] );
		}

		public function clearAttachments() {
			fo3_log( sprintf( "clearAttachments() called" ));
			$id = $this->getId();
			unset( $_SESSION[$id] );
		}

		public function getAttachments() {
			fo3_log( sprintf( "getAttachments() called" ));
			$id = $this->getId();
			if (!isset( $_SESSION[$id] )) $_SESSION[$id]=array();
			$retval = $_SESSION[$id];
			fo3_log( sprintf( "getAttachments() returned:\n%s" , print_r( $retval , true ) ));
			return $retval;
		}

		public function handleEvents() {
			if (isset( $this->aP["inajax"] )) {
				if (!isset( $this->aP["ajaxevent"] )) return parent::handleEvents();
				if (!isset( $this->aP["jaid"] )) return parent::handleEvents();
				if ($this->aP["jaid"]!=$this->getId()) return parent::handleEvents();
				if ($this->aP["ajaxevent"]=="delete_attachment") {
					$this->unsetAttachment( $this->aP["num"] );
					FNTH_echo_json_response( array( "return" => "ok" ) );
				}
			}
		
			if (!isset( $_FILES )) return parent::handleEvents();
			if (empty( $_FILES )) return parent::handleEvents();
			$sf = new FNTHSessionFile( "/temp" , UPLOAD_TIMEOUT );
			$prefix = $this->getAttachmentPrefix();
			// TODO itt van valami gebasz, csak meg nem tudom, hogy milyen esetben
			// valszeg akkor, ha az ido tul van (UPLOAD_TIMEOUT)
			foreach ($_FILES as $name => $file) {
				if (strpos( $name , $prefix )!==0) continue;	// nem ra vonatkozik
				if ($file["error"]) {
					$msg = sprintf( "%s <b>(%s)</b>: ",
						__("globalerror_Upload_error" ),
						htmlspecialchars( $file["name"] ) );
					switch ($file["error"]) {
					case UPLOAD_ERR_INI_SIZE:
						$msg.=__("globalerror_Upload_failed_php_max_filesize");
						break;
					case UPLOAD_ERR_FORM_SIZE:
						$msg.=__("globalerror_Upload_failed_form_size");
						break;
					case UPLOAD_ERR_PARTIAL:
						$msg.=__("globalerror_Upload_failed_partial_upload");
						break;
					case UPLOAD_ERR_NO_FILE:
						continue;
					}
					FrontendPage::nextPageWarningMessage( uniqid("") , $msg );
					continue;	// hibas file feltoltes
				}
				// sessionbe rogzitjuk
				fo3_log( sprintf( "file saving to session %s" , $name ) );
				$sessionfile = $sf->saveSessionFileFromFILES( $name );
				$num = str_replace( $prefix , "" , $name );
				$c = $this->getCommentPrefix().$num;
				if (isset( $this->aP[$c] ))
					$sessionfile["comment"]=$this->aP[$c];
				$this->setAttachment( $num , $sessionfile );
			}
			return parent::handleEvents();
		}

		public function refreshValueFromArray( $arr = null ) {
			if (empty( $_FILES )) return;
			$prefix = sprintf( "%s_file_" , $this->getId() );
			foreach ($_FILES as $name => $val) {
				if (strpos( $name , $prefix )!==0) continue;
			}
		}
		
		public function render() {
			if ($this->sRendered) return $this->sRendered;
			$this->generatelangDefs();
			$datas = array();
			$datas["files"] = $this->getAttachments();
			$datas["bReadOnly"]=$this->bReadOnly;
			$this->setParam( "bReadOnly" , $this->bReadOnly );
			// FNTH_da( $datas );
			FNTHPage::addUniquePageFullInit(
				sprintf( "InitJobAttachments( '%s',%s );\n",
					$this->getId(),
					json_encode( $datas ) ) );
			return parent::render();
		}

	}

?>
