Added support for uploading two map images for each cemetery

git-svn-id: https://rosehill.googlecode.com/svn/branches/php@19 100bd78a-fc82-11de-b5bc-ffd2847a4b57
This commit is contained in:
inghamn 2010-01-04 20:23:16 +00:00
parent b949fd89be
commit 9545bd18d7
10 changed files with 104 additions and 15 deletions

View File

@ -6,7 +6,7 @@
*/ */
?> ?>
<h1>Add Cemetery</h1> <h1>Add Cemetery</h1>
<form method="post" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>"> <form method="post" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" enctype="multipart/form-data">
<fieldset><legend>Cemetery Info</legend> <fieldset><legend>Cemetery Info</legend>
<table> <table>
@ -25,4 +25,14 @@
Cancel Cancel
</button> </button>
</fieldset> </fieldset>
<fieldset><legend>Maps</legend>
<div>
<label for="map">Map</lable>
<input type="file" name="map" id="map" />
</div>
<div>
<label for="thumbnail">Thumbnail</lable>
<input type="file" name="thumbnail" id="thumbnail" />
</div>
</fieldset>
</form> </form>

View File

@ -7,7 +7,7 @@
*/ */
?> ?>
<h1>Update Cemetery</h1> <h1>Update Cemetery</h1>
<form method="post" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>"> <form method="post" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" enctype="multipart/form-data">
<fieldset><legend>Cemetery Info</legend> <fieldset><legend>Cemetery Info</legend>
<input name="cemetery_id" type="hidden" value="<?php echo $this->cemetery->getId(); ?>" /> <input name="cemetery_id" type="hidden" value="<?php echo $this->cemetery->getId(); ?>" />
<table> <table>
@ -30,4 +30,26 @@
Cancel Cancel
</button> </button>
</fieldset> </fieldset>
<fieldset><legend>Maps</legend>
<div>
<label for="map">Map</lable>
<input type="file" name="map" id="map" />
</div>
<?php
$map = $this->cemetery->getMap();
if ($map) {
echo "<div><img src=\"$map\" /></div>";
}
?>
<div>
<label for="thumbnail">Thumbnail</lable>
<input type="file" name="thumbnail" id="thumbnail" />
</div>
<?php
$map = $this->cemetery->getMap('thumbnail');
if ($map) {
echo "<div><img src=\"$map\" /></div>";
}
?>
</fieldset>
</form> </form>

View File

@ -7,7 +7,7 @@
*/ */
?> ?>
<h1>Add Section</h1> <h1>Add Section</h1>
<form method="post" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>"> <form method="post" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" enctype="multipart/form-data">
<fieldset><legend>Section Info</legend> <fieldset><legend>Section Info</legend>
<input type="hidden" name="cemetery_id" value="<?php echo $this->cemetery->getId(); ?>" /> <input type="hidden" name="cemetery_id" value="<?php echo $this->cemetery->getId(); ?>" />
<table> <table>
@ -28,4 +28,15 @@
Cancel Cancel
</button> </button>
</fieldset> </fieldset>
<fieldset><legend>Maps</legend>
<div>
<label for="highlight_map">Highlight Map</lable>
<input type="file" name="highlight_map" id="highlight_map" />
</div>
<div>
<label for="zoom_map">Zoomed Map</lable>
<input type="file" name="zoom_map" id="zoom_map" />
</div>
</fieldset>
</form> </form>

View File

@ -173,6 +173,11 @@ class Cemetery
return new URL(BASE_URL.'/cemeteries/viewCemetery.php?cemetery_id='.$this->id); return new URL(BASE_URL.'/cemeteries/viewCemetery.php?cemetery_id='.$this->id);
} }
private function getMapDirectory()
{
return 'images/cemeteries/'.$this->id;
}
/** /**
* Returns the URL to the map image for this cemetery * Returns the URL to the map image for this cemetery
* *
@ -183,9 +188,26 @@ class Cemetery
*/ */
public function getMap($type="full") public function getMap($type="full")
{ {
$mapDirectory = "images/cemeteries/{$this->id}"; $imageDir = "images/cemeteries/{$this->id}";
$filename = $type=='full' ? 'map.jpg' : 'map_thumb.jpg'; $filename = $type=='full' ? 'map' : 'map_thumb';
return BASE_URL."/$mapDirectory/$filename"; $glob = glob(APPLICATION_HOME."/html/$imageDir/$filename.*");
if (count($glob)) {
$filename = basename($glob[0]);
return BASE_URL."/$imageDir/$filename";
}
}
/**
* @param array|string $file Either an entry from $_FILES or a path to a file
*/
public function saveMap($file,$type)
{
$imageDir = $this->getMapDirectory();
$name = $type=='full' ? 'map' : 'map_thumb';
$directory = APPLICATION_HOME."/html/$imageDir";
Map::saveFile($directory,$file,$name);
} }
} }

View File

@ -16,9 +16,9 @@ class Map
/** /**
* @param string $directory Where to store the file * @param string $directory Where to store the file
* @param array|string $file Either an entry from $_FILES or a path to a file * @param array|string $file Either an entry from $_FILES or a path to a file
* @param int $id The unique ID for the object the map is for * @param string $newName The filename to use
*/ */
public static function saveFile($directory,$file,$id) public static function saveFile($directory,$file,$newName)
{ {
// Handle passing in either a $_FILES array or just a path to a file // Handle passing in either a $_FILES array or just a path to a file
$tempFile = is_array($file) ? $file['tmp_name'] : $file; $tempFile = is_array($file) ? $file['tmp_name'] : $file;
@ -36,17 +36,20 @@ class Map
throw new Exception('unknownFileType'); throw new Exception('unknownFileType');
} }
// Clean out any previous version of the file
foreach(glob("$directory/$newName.*") as $file) {
unlink($file);
}
// Move the file where it's supposed to go // Move the file where it's supposed to go
if (!is_dir($directory)) { if (!is_dir($directory)) {
mkdir($directory,0777,true); mkdir($directory,0777,true);
} }
$newFile = "$directory/$id.$extension"; $newFile = "$directory/$newName.$extension";
echo "Saving $newFile\n";
rename($tempFile,$newFile); rename($tempFile,$newFile);
chmod($newFile,0666); chmod($newFile,0666);
if (!is_file($newFile)) { if (!is_file($newFile)) {
echo "$newFile was not saved\n";
throw new Exception('media/uploadFailed'); throw new Exception('media/uploadFailed');
} }
} }

View File

@ -230,7 +230,6 @@ class Section
$directory = APPLICATION_HOME."/html/$imageDir/$type"; $directory = APPLICATION_HOME."/html/$imageDir/$type";
echo "Saving $type map to $directory\n";
Map::saveFile($directory,$file,$this->id); Map::saveFile($directory,$file,$this->id);
} }
} }

View File

@ -19,6 +19,14 @@ if (isset($_POST['cemetery'])) {
try { try {
$cemetery->save(); $cemetery->save();
if (isset($_FILES)) {
if (isset($_FILES['map']) && $_FILES['map']['tmp_name']) {
$cemetery->saveMap($_FILES['map'],'full');
}
if (isset($_FILES['thumbnail']) && $_FILES['thumbnail']['tmp_name']) {
$section->saveMap($_FILES['thumbnail'],'thumbnail');
}
}
header('Location: '.BASE_URL.'/cemeteries'); header('Location: '.BASE_URL.'/cemeteries');
exit(); exit();
} }

View File

@ -19,6 +19,14 @@ if (isset($_POST['cemetery'])) {
try { try {
$cemetery->save(); $cemetery->save();
if (isset($_FILES)) {
if (isset($_FILES['map']) && $_FILES['map']['tmp_name']) {
$cemetery->saveMap($_FILES['map'],'full');
}
if (isset($_FILES['thumbnail']) && $_FILES['thumbnail']['tmp_name']) {
$section->saveMap($_FILES['thumbnail'],'thumbnail');
}
}
header('Location: '.BASE_URL.'/cemeteries'); header('Location: '.BASE_URL.'/cemeteries');
exit(); exit();
} }

View File

@ -22,6 +22,14 @@ if (isset($_POST['section'])) {
try { try {
$section->save(); $section->save();
if (isset($_FILES)) {
if (isset($_FILES['highlight_map']) && $_FILES['highlight_map']['tmp_name']) {
$section->saveMap($_FILES['highlight_map'],'highlight');
}
if (isset($_FILES['zoom_map']) && $_FILES['zoom_map']['tmp_name']) {
$section->saveMap($_FILES['zoom_map'],'zoom');
}
}
header('Location: '.$cemetery->getURL()); header('Location: '.$cemetery->getURL());
exit(); exit();
} }

View File

@ -19,8 +19,6 @@ if (isset($_POST['section_id'])) {
try { try {
$section->save(); $section->save();
if (isset($_FILES)) { if (isset($_FILES)) {
echo "Found files\n";
print_r($_FILES);
if (isset($_FILES['highlight_map']) && $_FILES['highlight_map']['tmp_name']) { if (isset($_FILES['highlight_map']) && $_FILES['highlight_map']['tmp_name']) {
$section->saveMap($_FILES['highlight_map'],'highlight'); $section->saveMap($_FILES['highlight_map'],'highlight');
} }
@ -28,7 +26,7 @@ if (isset($_POST['section_id'])) {
$section->saveMap($_FILES['zoom_map'],'zoom'); $section->saveMap($_FILES['zoom_map'],'zoom');
} }
} }
#header('Location: '.$section->getCemetery()->getURL()); header('Location: '.$section->getCemetery()->getURL());
exit(); exit();
} }
catch (Exception $e) { catch (Exception $e) {