<?php
if( $a == 'pagedir' || $a == 'productdir' ){
	$aFiles = $oFile->throwFilesInDir( ( isset( $_GET['iPage'] ) ? ( $_GET['iPage'] - 1 ) * 20 : 0 ), 20, $sPhrase );
	$oTpl->assign( 'aFiles', $aFiles );
	echo ( $oTpl->parse( 'files_dir.tpl' ) );
	exit;
} elseif( $a == 'list_links' ){
	header( 'Content-type: text/javascript' );
	header( 'pragma: no-cache' );
	header( 'expires: 0' );
	if( !empty( $iPage ) ){
		$sql = 'SELECT `sFileName` AS `title`, CONCAT( \'' . $config['dir_files'] . '\', `sFileName` ) AS `value` FROM ' . $oDB->throwTableName( DB_PAGES_FILES ) . ' WHERE `iPage` = ' . intval( $iPage );// . ' AND `iType` = 2';
	} elseif( !empty( $iProduct ) ){
		$sql = 'SELECT `sFileName` AS `title`, CONCAT( \'' . $config['dir_files'] . '\', `sFileName` ) AS `value` FROM ' . $oDB->throwTableName( DB_PRODUCTS_FILES ) . ' WHERE `iProduct` = ' . intval( $iProduct );// . ' AND `iType` = 2';
	} elseif( !empty( $iBox ) ){
		$sql = 'SELECT `sFileName` AS `title`, CONCAT( \'' . $config['dir_files'] . '\', `sFileName` ) AS `value` FROM ' . $oDB->throwTableName( DB_BOXES_FILES ) . ' WHERE `iBox` = ' . intval( $iBox );// . ' AND `iType` = 2';
	} else exit;
	$aFiles = $oDB->executeSQL( $sql, true, MYSQLI_ASSOC );
	function listSubpagesTimyMCE(&$aPagesList, $iPageParent, $iDepth ){
		global $oPage;
		if( is_array( $oPage->aPagesChildren[$iPageParent] ) ) foreach( $oPage->aPagesChildren[$iPageParent] as $iPage ){
			$aData = $oPage->aPages[$iPage];
			$aPagesList[] = Array( 'title' => str_repeat( '-', $iDepth ) . $aData['sName'], 'value' => $aData['sLinkName'] );
			if( isset( $oPage->aPagesChildren[$iPage] ) ){
				listSubpagesTimyMCE( $aPagesList, $iPage, $iDepth + 1 );
			}
		}
	}
	if( isset( $oPage->aPagesParentsTypes ) ){
		$aPagesList = Array( );
		foreach( $oPage->aPagesParentsTypes as $aPages ){
			foreach( $aPages as $iPage ){
				$aData = $oPage->aPages[$iPage];
				$aPagesList[] = Array( 'title' => $aData['sName'], 'value' => $aData['sLinkName'] );
				if( isset( $oPage->aPagesChildren[$iPage] ) ){
					listSubpagesTimyMCE( $aPagesList, $iPage, 1 );
				}
			} // end for
			
		}
	}
	echo ( json_encode( array_merge( $aFiles, $aPagesList ) ) );
	exit;
} elseif( $a == 'list_images' ){
	header( 'Content-type: text/javascript' );
	header( 'pragma: no-cache' );
	header( 'expires: 0' );
	if( !empty( $iPage ) ){
		$sql = 'SELECT `sFileName` AS `title`, CONCAT( \'' . $config['dir_files'] . '\', `sFileName` ) AS `value` FROM ' . $oDB->throwTableName( DB_PAGES_FILES ) . ' WHERE `iPage` = ' . intval( $iPage ) . ' AND `iPhoto` = 1';// . ' AND `iType` = 2';
	} elseif( !empty( $iProduct ) ){
		$sql = 'SELECT `sFileName` AS `title`, CONCAT( \'' . $config['dir_files'] . '\', `sFileName` ) AS `value` FROM ' . $oDB->throwTableName( DB_PRODUCTS_FILES ) . ' WHERE `iProduct` = ' . intval( $iProduct ) . ' AND `iPhoto` = 1';// . ' AND `iType` = 2';
	} elseif( !empty( $iBox ) ){
		$sql = 'SELECT `sFileName` AS `title`, CONCAT( \'' . $config['dir_files'] . '\', `sFileName` ) AS `value` FROM ' . $oDB->throwTableName( DB_BOXES_FILES ) . ' WHERE `iBox` = ' . intval( $iBox ) . ' AND `iPhoto` = 1';// . ' AND `iType` = 2';
	} else exit;
	$aImages = $oDB->executeSQL( $sql, true, MYSQLI_ASSOC );
	echo ( json_encode( $aImages ) );
	exit;
}
elseif( $a == 'multiupload' ){
	if( isset( $_FILES['multiupload'] ) ){
		$time = time( );
		require_once( 'libraries/pclzip.lib.php' );
		$oArchive = new PclZip( $_FILES['multiupload']['tmp_name'] );
		$oArchive->extract( PCLZIP_OPT_PATH, 'upload/multiupload-' . $time );
		$aFiles = glob( 'upload/multiupload-' . $time . '/*.*' );
		foreach( $aFiles as $sFile ){
			$sFileName = basename( $sFile );
			if( preg_match( '/^([0-9]+)(-([0-9]+))?\\.(jpg|jpeg|gif|png)$/i', $sFileName, $aMatches ) ){
				$sFileName = $oDB->checkIsFile( $sFileName, 'upload/' );
				rename( $sFile, 'upload/' . $sFileName );
				$oDB->save( DB_PRODUCTS_FILES, Array( 'iProduct' => $aMatches[1], 'iPhoto' => 1, 'sFileName' => $sFileName, 'iPosition' => !empty( $aMatches[3] ) ? $aMatches[3] : '0', 'iType' => 7, 'iSize' => 100, 'sDescription' => '' ) );
				$content .= '<p>' . $lang['file_added'] . ': ' . $sFileName . '</p>';
			}
		}
		deleteDir( 'upload/multiupload-' . $time );
	} else{
		$content .= $oTpl->parse( 'multiupload_form.tpl' );
	}
}
elseif( $a == 'edit' ){
	if( isset( $_GET['sFileName'] ) ){
		$sImgSource = $config['dir_files'] . $_GET['sFileName'];
		if( isset( $_POST['sOption'] ) ){
			$sImgExt = FileJobs::throwExtOfFile( $sImgSource );
			$aImgProperties = GetImageSize( $sImgSource );
			if( $aImgProperties[2] == 2 ){
				switch( $sImgExt ){
					case 'gif':
						$mImgCreate = ImageCreateFromGIF( $sImgSource );
					break;
					case 'jpeg':
					case 'jpg':
						$mImgCreate = ImageCreateFromJPEG( $sImgSource );
					break;
					case 'png':
						$mImgCreate = ImageCreateFromPNG( $sImgSource );
					break;
					default:
						die( 'Nieobsługiwany typ pliku!' );
					break;
				}
				if( in_array( $_POST['rotate'], Array( 90, 180, 270 ) ) ){
					//Obrót
					$rotate = imagerotate($mImgCreate, 360 - $_POST['rotate'], 0);
					imagedestroy( $mImgCreate );
				} else{
					$rotate = $mImgCreate;
				}
				$sImgDest = $rotate;
				if( !empty( $_POST['jrac_crop_width'] ) && !empty( $_POST['jrac_crop_height'] ) ){
					//Kadrowanie
					$sCreateFunction = 'imagecopyresampled';
					if( !function_exists( $sCreateFunction ) ) $sCreateFunction = 'imagecopyresized';
					$srcX = ImageSX( $rotate );
					$srcY = ImageSY( $rotate );
					if( in_array( $_POST['rotate'], Array( 90, 270 ) ) ){
						$scaleY = $_POST['jrac_image_width'];
						$scaleX = $_POST['jrac_image_height'];
					} else{
						$scaleX = $_POST['jrac_image_width'];
						$scaleY = $_POST['jrac_image_height'];
					}
					$cropX = $_POST['jrac_crop_x'];
					$cropY = $_POST['jrac_crop_y'];
					$cropW = $_POST['jrac_crop_width'];
					$cropH = $_POST['jrac_crop_height'];

					$mImgDest = imagecreatetruecolor( $cropW, $cropH );
					switch( $sImgExt ){
						case "png":
							// integer representation of the color black (rgb: 0,0,0)
							$background = imagecolorallocate( $mImgCreate, 0, 0, 0 );
							// removing the black from the placeholder
							imagecolortransparent( $mImgDest, $background );
							// turning off alpha blending (to ensure alpha channel information is preserved, rather than removed (blending with the rest of the image in the form of black))
							imagealphablending( $mImgDest, false );
							// turning on alpha channel information saving (to ensure the full range of transparency is preserved)
							imagesavealpha( $mImgDest, true );
						break;
						case "gif":
							// integer representation of the color black (rgb: 0,0,0)
							$background = imagecolorallocate( $mImgCreate, 0, 0, 0 );
							// removing the black from the placeholder
							imagecolortransparent( $mImgDest, $background );
						break;
					}
					$sCreateFunction( $mImgDest, $rotate, 0, 0, intval( $cropX * $srcX / $scaleX ), intval( $cropY * $srcY / $scaleY ), $cropW, $cropH, intval( $cropW * $srcX / $scaleX ), intval( $cropH * $srcY / $scaleY ) );
					imagedestroy( $rotate );
				} else{
					$mImgDest = $rotate;
				}
				//Zapis
				$sImgOutputName = FileJobs::checkIsFile( $_GET['sFileName'], $config['dir_files'] );
				$sImgOutput = $config['dir_files'] . $sImgOutputName;
				if( !is_file( $sImgOutput ) ){
					touch( $sImgOutput );
					chmod( $sImgOutput, 0666 );
				}
				switch( $sImgExt ){
					case 'gif':
						if( function_exists( "imagegif" ) ) $Image = ImageGIF( $mImgDest, $sImgOutput );
						else{
							$Image = ImagePNG( $mImgDest, $sImgOutput );
						}
						break;
					case 'jpeg':
					case 'jpg':
						$Image = ImageJPEG( $mImgDest, $sImgOutput );
						break;
					case 'png':
						$Image = ImagePNG( $mImgDest, $sImgOutput );
						break;
				}
				imagedestroy( $mImgDest );
				if( !empty( $_GET['iFile'] ) && !empty( $_GET['iType'] ) ){
					$sTblName = $oDB->throwTableName( Files::throwDbNames( $_GET['iType'] ) );
					$sIndex = Files::throwIndexes( $_GET['iType'] );
					if( !empty( $sTblName ) && !empty( $sIndex ) ){
						$oDB->executeSQL( 'INSERT INTO `' . $sTblName . '` ( `' . $sIndex . '`, `sFileName`, `sDescription`, `iPhoto`, `iPosition`, `iType`, `iSize`, `sTextSearch` ) SELECT `' . $sIndex . '`, \'' . $oDB->escape( $sImgOutputName ) . '\', `sDescription`, `iPhoto`, `iPosition`, `iType`, `iSize`, `sTextSearch` FROM `' . $sTblName . '` WHERE `iFile` = ' . intval( $_GET['iFile'] ) );
						$oDB->executeSQL( 'UPDATE `' . $sTblName . '` SET `iType` = 2 WHERE `iFile` = ' . intval( $_GET['iFile'] ) );
						$iIndex = $oDB->executeSQL( 'SELECT `' . $sIndex . '` FROM `' . $sTblName . '` WHERE `iFile` = ' . intval( $_GET['iFile'] ), true, DataBase::SINGLE_VALUE );
						switch( $_GET['iType'] ){
							case 1: header( 'Location: admin.php?p=p-form&iPage=' . $iIndex ); exit;
							case 2: header( 'Location: admin.php?p=products-form&iProduct=' . $iIndex ); exit;
							case 3: header( 'Location: admin.php?p=boxes-form&iBox=' . $iIndex ); exit;
						}
					}
				}
			}
		} else{
			$content .= $oTpl->parse( 'image_edit.tpl' );
		}
	}
}
?>