Files
HotLunches/photo_gallery_images.update.php
2026-10-19 15:22:52 -07:00

229 lines
8.7 KiB
PHP

<?php
require_once("include/globals.php");
if (SessionActive() != 1 || ! UserLoggedIn()) {
header("Location: /");
exit();
}
// Ensure that only valid user types have access to this module
$AllowedUserTypes = array('SuperUser','System Administrator','Communications Administrator');
if (checkPermission($AllowedUserTypes) != 1) {
return;
}
//
// Process all actions to list and edit records
//
$CurrentTable = "photo_gallery_images";
$DeleteField = "photo";
$dSeparator = "";
$FormFile = "photo_gallery_images.form.php";
$TitleText = "Gallery Image";
$OrderBy = "sequence,photo";
$Sequence = 'Y';
if (array_key_exists('naction',$_GET)) $naction = $_GET['naction'];
if (array_key_exists('naction',$_POST)) $naction = $_POST['naction'];
if (array_key_exists('gallery_num',$_GET)) $gallery_num = intval($_GET['gallery_num']);
if (array_key_exists('gallery_num',$_POST)) $gallery_num = intval($_POST['gallery_num']);
if (array_key_exists('record_num',$_GET)) $record_num = intval($_GET['record_num']);
if (array_key_exists('record_num',$_POST)) $record_num = intval($_POST['record_num']);
$school_num = $school_record['record_num'];
switch (strtolower($naction)) {
case "update":
extract($_POST);
$table = new Table("$CurrentTable");
$query = $table->BuildUpdate();
db_query($query);
$form_message = getLanguageText('RECORD-UPDATED-MSG');
$naction = "list";
header("Location: /?action=$action&naction=$naction&gallery_num=$gallery_num&form_message=" . urlencode($form_message));
exit();
case "delete-confirmed":
$record_num = intval($_GET['record_num']);
// Get record
$query = "select * from $CurrentTable where record_num = $record_num";
$results = db_query($query);
$row = db_fetch_array($results);
$file_name = $row['photo'];
$thumbnail = $row['thumbnail'];
// Delete files if they exists
if (is_file("$gallerydir/$file_name")) {
unlink("$gallerydir/$file_name");
}
if (is_file("$gallerydir/$thumbnail")) {
unlink("$gallerydir/$thumbnail");
}
// Delete the current record
$query = "delete from $CurrentTable where record_num = $record_num";
db_query($query);
$form_message = getLanguageText('RECORD-DELETED-MSG');
$naction = "list";
header("Location: /?action=$action&naction=$naction&gallery_num=$gallery_num&form_message=" . urlencode($form_message));
exit();
case 'upload-images':
extract($_POST);
$errorMessages = array();
$successMessages = array();
$numUploaded = 0;
$table = new Table("photo_gallery_images");
// Process each image field
for ($i = 1; $i <= 10; $i++) {
$fname = "filename$i";
$temp_name = $_FILES[$fname]['tmp_name'];
$image_name = $_FILES[$fname]['name'];
$file_name = str_replace(' ','_',$_FILES[$fname]['name']);
if (strlen($file_name) == 0) continue;
// Check for valid document types
if (stripos(".jpg",$file_name) === false && stripos(".jpeg",$file_name) === false && stripos(".jpe",$file_name) === false && stripos(".gif",$file_name) === false) {
$errorMessages[] = getLanguageText('MSG-INVALID-IMAGE-TYPE');
continue;
}
// Determine the file extension
list($fname,$ext) = explode('.',$file_name);
// Assign the image name
$image_num = 1;
$last_image_num = db_getVal("SELECT max(image_num) FROM photo_gallery_images WHERE school_num = $school_num");
if ($last_image_num > 0) $image_num = $last_image_num + 1;
$photo = "gallery-" . sprintf("%05d",$image_num) . $ext;
$thumbnail = "gallery-" . sprintf("%05d",$image_num) . "-tn" . $ext;
$date_uploaded = date("Y-m-d",time());
$sequence = intval(db_getVal("SELECT max(sequence) FROM photo_gallery_images WHERE school_num = $school_num AND gallery_num = $gallery_num"));
$sequence++;
// Move file to documents folder
if (!move_uploaded_file($temp_name,"$gallerydir/$photo")) {
$errorMessages[] = getLanguageText('MSG-UPLOAD-ERRORS');
}
else {
$successMessages[] = getLanguageText('MSG-IMAGES-UPLOADED');
$numUploaded++;
// Setup image conversion if required
$cmd="/usr/bin/convert";
$target = "$gallerydir/temp";
// Check if image is too large
$image_size = GetImageSize("$gallerydir/$photo");
$image_width = $image_size[0];
$image_height = $image_size[1];
// Check which is largest dimension
if ($image_width >= $image_height) {
// Create the thumbnail
$height = intval($image_height * $maxGalleryThumbDimension / $image_width);
$command = "$cmd -size {$image_width}x$image_height $gallerydir/$photo -resize {$maxGalleryThumbDimension}x$height +profile \"*\" $target";
exec($command);
rename($target,"$gallerydir/$thumbnail");
if ($image_width > $maxGalleryImageDimension) {
// Resize the photo
$height = intval($image_height * $maxGalleryImageDimension / $image_width);
$command = "$cmd -size {$image_width}x$image_height $gallerydir/$photo -resize {$maxGalleryImageDimension}x$height +profile \"*\" $target";
exec($command);
unlink("$gallerydir/$photo");
rename($target,"$gallerydir/$photo");
}
}
else {
// Create the thumbnail
$width = intval($image_width * $maxGalleryThumbDimension / $image_height);
$command = "$cmd -size {$image_width}x{$image_height} $gallerydir/$photo -resize {$width}x{$maxGalleryThumbDimension} +profile \"*\" $target";
exec($command);
rename($target,"$gallerydir/$thumbnail");
if ($image_height > $maxGalleryImageDimension) {
// Resize the photo
$width = intval($image_width * $maxGalleryImageDimension / $image_height);
$command = "$cmd -size {$image_width}x{$image_height} $gallerydir/$photo -resize {$width}x{$maxGalleryImageDimension} +profile \"*\" $target";
exec($command);
unlink("$gallerydir/$photo");
rename($target,"$gallerydir/$photo");
}
}
// Save image data to table
$query = $table->BuildInsert();
db_query($query);
}
}
if (count($errorMessages) == 0) {
if ($numUploaded > 0) {
$form_message = "$numUploaded " . getLanguageText('MSG-IMAGES-UPLOADED');
}
else {
$error_message = getLanguageText('MSG-ZERO-UPLOADED');
}
}
else $error_message = getLanguageText('MSG-UPLOAD-ERRORS');
$naction = "list";
if (!isset($form_message)) $form_message = '';
header("Location: /?action=$action&naction=$naction&gallery_num=$gallery_num&form_message=" . urlencode($form_message) . "&error_message=" . urlencode($error_message));
exit();
case 'delete-image-confirmed':
$photo = $_POST['selected_image'];
unlink("$gallerydir/$photo");
$form_message = getLanguageText('MSG-IMAGE-DELETED');
$naction = "list";
header("Location: /?action=$action&naction=$naction&gallery_num=$gallery_num&form_message=" . urlencode($form_message));
exit();
case "save-sort":
// Parse the hidden fields to update the sort sequence in the database
if (array_key_exists('row_sequence',$_POST) && strlen($_POST['row_sequence']) > 0) {
// Update the sequence to avoid duplicate keys
db_query("UPDATE $CurrentTable SET sequence = sequence + 1000 WHERE school_num = $school_num");
$row_sequence = db_escape_string(trim($_POST['row_sequence'], ", "));
$item_sequence = explode(',',$row_sequence);
for ($i = 0; $i < count($item_sequence); $i++) {
$rnum = $item_sequence[$i];
$sequence = $i + 1;
$query = "update $CurrentTable set sequence = $sequence where record_num = $rnum";
db_query($query);
}
}
$form_message = getLanguageText('RECORDS-SORTED-MSG');
$naction = "list";
header("Location: /?action=$action&naction=$naction&gallery_num=$gallery_num&form_message=" . urlencode($form_message));
exit();
}
?>