<?php
	/**
	 * This file contains the QFileAssetDialog class.
	 *
	 * @package Controls
	 */

	class QFileAssetDialog extends QDialog {
		public $lblMessage;
		public $flcFileAsset;
		public $lblError;
		public $btnUpload;
		public $btnCancel;
		public $objSpinner;
		protected $strFileUploadCallback;

		public function __construct($objParentObject, $strFileUploadCallback, $strControlId = null) {
			// Call parent constructor and define FileUploadCallback
			parent::__construct($objParentObject, $strControlId);
			$this->strFileUploadCallback = $strFileUploadCallback;

			// Setup the Dialog Box, itself
			$this->strTemplate = __DOCROOT__ . __PHP_ASSETS__ . '/QFileAssetDialog.tpl.php';
			//$this->blnDisplay = false;
			//$this->blnMatteClickable = false;
            $this->AutoOpen = false;

			$this->strTitle = QApplication::Translate("Upload a File");

			// Controls for Upload FileAsset Dialog Box
			$this->lblMessage = new QLabel($this);
			$this->lblMessage->HtmlEntities = false;

			$this->lblError = new QLabel($this);
			$this->lblError->HtmlEntities = false;

			$this->flcFileAsset = new QFileControl($this);
			$this->btnUpload = new QButton($this);
			$this->btnCancel = new QButton($this);
			$this->objSpinner = new QWaitIcon($this);

			// Events on the Dialog Box Controls
			$this->flcFileAsset->AddAction(new QEnterKeyEvent(), new QTerminateAction());

			$this->btnUpload->AddAction(new QClickEvent(), new QToggleEnableAction($this->btnUpload));
			$this->btnUpload->AddAction(new QClickEvent(), new QToggleEnableAction($this->btnCancel));
			$this->btnUpload->AddAction(new QClickEvent(), new QToggleDisplayAction($this->objSpinner));
			$this->btnUpload->AddAction(new QClickEvent(), new QServerControlAction($this, 'btnUpload_Click'));

			$this->btnCancel->AddAction(new QClickEvent(), new QHideDialog($this));
		}

		public function btnUpload_Click($strFormId, $strControlId, $strParameter) {
			$this->btnUpload->Enabled = true;
			$this->btnCancel->Enabled = true;
			$this->objSpinner->Display = false;

			$strFileControlCallback = $this->strFileUploadCallback;
			
			if (isset($this->objParentControl)) {
				$parent = $this->objParentControl;
			} else {
				$parent = $this->objForm;
			}
			$parent->$strFileControlCallback($strFormId, $strControlId, $strParameter);
		}
		
		public function ShowError($strErrorMessage) {
			$this->lblError->Text = $strErrorMessage;
			$this->flcFileAsset->Focus();
			$this->Blink();
		}
	}