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

	class GeneralAttachments extends Widget {
		protected $bReadOnly = false;
		protected $aAttachments = array();

		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 ) {
			$this->aAttachments[] = $file;
		}

		public function clearAttachments() {
			$this->aAttachments = array();
		}

		public function getAttachments() {
			return $this->aAttachments;
		}

		public function handleEvents() {
			if (isset( $this->aP["inajax"] )) {
				if (!isset( $this->aP["ajaxevent"] )) return parent::handleEvents();
				if (!isset( $this->aP["jaid"] )) return parent::handleEvents();
				return parent::handleEvents();
			}
		
			if (!isset( $_FILES )) return parent::handleEvents();
			if (empty( $_FILES )) return parent::handleEvents();
			$sf = new FNTHSessionFile();
			$prefix = $this->getAttachmentPrefix();
			// TODO itt van valami gebasz, csak meg nem tudom, hogy milyen esetben
			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->addAttachment( $sessionfile );
			}
			return parent::handleEvents();
		}

		public function saveAttachments() {

		}

		public function loadAttachments() {

		}

		public function deleteAttachments() {
			$x = $this->getId()."_deletes";
			$dels = (isset( $this->aP[$x] )) ? $this->aP[$x] : "";
			if (empty( $dels )) return;
			$adels = explode( ";" , $dels );
			if (empty( $adels )) return;
			foreach ($adels as $del)
				$this->deleteAttachment( $del );
		}

		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 );
			FNTHPage::addUniquePageFullInit(
				sprintf( "InitGeneralAttachments( '%s',%s );\n",
					$this->getId(),
					json_encode( $datas ) ) );
			return parent::render();
		}

	}

?>
