HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/PvdBoogaard/indoorski.nl/backup/oude-site/cms/api/form/class.field.imagedir.php
<?php

/**
 * The file holding the object for fields of type imagedir
 *
 * @author Allan Shone <allan@interspire.com>
 *
 * @package IWP
 * @subpackage IWP_FormFields
**/

/**
 * The field class implementation for imagedir
 *
 * @author Allan Shone <allan@interspire.com>
 *
 * @package IWP
 * @subpackage IWP_FormFields
**/
class iwp_field_imagedir extends iwp_field {

	/**
	 * The type of class this is, in terms of fields for iwp
	 *
	 * @var String
	**/
	public $type = 'imagedir';
	protected $showLabel = false;
	/**
	 * A local file path for the folder containing the images
	 *
	 * @var string
	**/
	private $ImagesLocation;

	/**
	 * The default constructor will simply call the parent
	 *
	 * @param mixed
	 * @uses iwp_field::__construct()
	**/
	public function __construct($name=null){
		parent::__construct($name);

		$this->Prepend = "<div class='ImageBox'>";
		$this->Append = "</div>\n<input type='hidden' name='". iwp_htmlspecialchars($this->FieldName()) ."' id='". iwp_htmlspecialchars($this->FieldName()) ."' value='' />";
	}

	/**
	 * GetFieldOutput
	 * Returns the HTML for this field. It generates the relevant parts, assigns them to template variables and returns a parse template file.
	 *
	 * @return string Returns the field HTML
	**/
	public function GetFieldOutput($setOnly=false) {
		$field = $this->Prepend;

		if(!($list = $this->GetImageList())) {
			// Have an error message display?
			// May have a debugging setting to allow for the path to be printed
			$field .= GetLang("ImageDirOpenFailure") . htmlentities($this->ImagesLocation);
		} else {
			$count = 0;
			$jsExtra = ' var ImageID = new Array();';
			foreach($list as $img) {
				$count++;
				$field .= "\t<div id='img_" . $this->valid->FilterAlphaNumeric($img) . "' onclick='SelectIcon(\"img_" . $this->valid->FilterAlphaNumeric($img) . "\", \"" . $img . "\")' class='CategoryIcon' style='display: inline; cursor: default; margin: 3px;'><img id='icon_" . $this->valid->FilterAlphaNumeric($img) . "' src='" . IWP_BASE_URI . $this->ImagesLocation . '/' . $img . "' /></div>\n";

				$jsExtra .= "ImageID[" . $count . "] = '" . $this->valid->FilterAlphaNumeric($img) . "'; ";
			}

			$field .= "<script type='text/javascript'>\n<!--\n" .
					  "function SelectIcon(IconId, IconName) {\n\tfor(i = 1; i < numIcons; i++) {\n" .
					  "\t\tvar span = document.getElementById(\"img_\" + ImageID[i]);\n\t\tspan.className = 'CategoryIcon';\n" .
					  "\t}\n\tvar span = document.getElementById(IconId);\n" .
					  "\tspan.className = 'SelectedCategoryIcon';\n\tdocument.getElementById(\"". iwp_htmlspecialchars($this->FieldName()) ."\").value = IconName;\n" .
					  "}\nvar numIcons=" . $count . ";\n" . $jsExtra ."\n".
					  "//-->\n</script>\n";
		}

		$field .= $this->Append;
		//$helpTip = '';

		parent::GetFieldOutput();

		$this->template->Assign('inputField', $field);
		$this->template->Assign('FieldName', $this->FieldName);

		if(!$setOnly) {
			return $this->template->ParseTemplate('form.imagedir', true);
		}
		return '';
	}

	/**
	 * To return the value of the index in the passed through array
	 *
	 * @param Array
	 * @return mixed
	**/
	public function Validate($arrData){
		return $arrData[$this->FieldName];
	}

	/**
	 * This will return an associative array of the listing of images to be displayed
	 *
	 * @return Mixed False on failure and Array on success
	**/
	private function GetImageList() {
		if(!is_dir(IWP_BASE_PATH . $this->ImagesLocation)) {
			return false;
		}

		if(!($dir = opendir(IWP_BASE_PATH . $this->ImagesLocation))) {
			return false;
		}

		$entries = array();
		while(($entry = readdir($dir))) {
			if (is_dir(IWP_BASE_PATH . $this->ImagesLocation .'/'. $entry)) {
				//	skip directories
				continue;
			}
			$entries[] =  $entry;
		}
		return $entries;
	}

	/**
	 * Will set the location of the folder to use for the image list
	**/
	public function SetLocation($loc) {
		// expects a beginning slash
		if(substr($loc, 0, 1) != '/') {
			$loc = '/' . $loc;
		}

		$this->ImagesLocation = $loc;
	}
}