Files
pae.co.at/other/archive/version_01/functions/func_events.php
2017-03-02 21:57:21 -06:00

254 lines
7.3 KiB
PHP
Executable File

<?php
#################################################################################
# #
# written by benjamin ramey #
# bsr@rameydesign.net #
# #
# This file contains: #
# Functions that govern adding/editing/deleting, etc of event items #
# #
#################################################################################
########################################################## FUNCTION: events_add #
function events_add($data)
{
global $_CONFIG,$_DOC_ROOT;
# gather all the current events from xml file
$events = xml_parse_file($_CONFIG['events_file']);
$events = xml_force_array($events['events']['event']);
# the new id will be one more than the highest id so far
$new_id = 0;
foreach($events as $event)
{
if($event['id'] > $new_id)
{
$new_id = $event['id'];
}
}
$new_id++;
# if a picture has been uploaded, then take of that file
if(!empty($_FILES['picture']['name']))
{
$data['picture'] = events_save_picture($new_id);
}
# create the new event's xml array
$events[] = array( 'id' => $new_id,
'title' => $data['title'],
'date' => $data['date'],
'starttime' => $data['starttime'],
'endtime' => $data['endtime'],
'location' => $data['location'],
'description' => $data['description'],
'picture' => $data['picture']
);
# opening and closing tags for the new events file
$opening_tags = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<events>";
$closing_tags = "\n</events>";
# open the old file and truncate it to write the new one
$handle = fopen($_CONFIG['events_file'],'w');
fwrite($handle,$opening_tags);
# now, write an entry for each event, including the new one
foreach($events as $event)
{
$xml = events_create_xml($event);
fwrite($handle,$xml);
}
fwrite($handle,$closing_tags);
fclose($handle);
# make a message to the user to let him know the event
# was added successfully
if(!err_exist())
{
mesg_make("Event '{$data['title']}' successfully added.");
}
}
################################################# FUNCTION: events_save_picture #
function events_save_picture($event_id)
{
global $_CONFIG;
# we want to get the extension of this file to save it with the
# right extension again
$ext = explode('.',$_FILES['picture']['name']);
$ext = '.'.$ext[count($ext)-1];
# the name and location where I'm saving this file to
# note that the name of the file is the id number of the event
$save_file_to = $_CONFIG['events_pics_dir'].'/event_'.$event_id.$ext;
if(move_uploaded_file($_FILES['picture']['tmp_name'],$save_file_to))
{
# change the permissions on the pictures so that they can
# be displayed. Was always uploading to 600
chmod($save_file_to, 0644);
# return the name of the picture file
return('event_'.$event_id.$ext);
}
else
{
err_make('Could not upload the event picture. Event has still been added.','pub');
return(NULL);
}
}
####################################################### FUNCTION: events_delete #
function events_delete($data)
{
global $_CONFIG;
# gather all the current events from xml file
$events = xml_parse_file($_CONFIG['events_file']);
$events = xml_force_array($events['events']['event']);
$deleted_event = '';
# opening and closing tags for the new events file
$opening_tags = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<events>";
$closing_tags = "\n</events>";
# open the old file and truncate it to write the new one
$handle = fopen($_CONFIG['events_file'],'w');
fwrite($handle,$opening_tags);
# now, write an entry for each event, including the new one
foreach($events as $event)
{
# skip adding this event to the new file if it is
# the event we are looking to delete
if($event['id'] == $data['delete_id'])
{
# get title of this deleted event so show it in the
# message to the user
$deleted_event = $event['title'];
continue;
}
$xml = events_create_xml($event);
fwrite($handle,$xml);
}
fwrite($handle,$closing_tags);
fclose($handle);
# make a message to the user to let him know the event
# was added successfully
if(!err_exist())
{
mesg_make("Event '$deleted_event' successfully deleted.");
}
}
######################################################### FUNCTION: events_edit #
function events_edit($data)
{
global $_CONFIG;
# gather all the current events from xml file
$events = xml_parse_file($_CONFIG['events_file']);
$events = xml_force_array($events['events']['event']);
# opening and closing tags for the new events file
$opening_tags = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<events>";
$closing_tags = "\n</events>";
# open the old file and truncate it to write the new one
$handle = fopen($_CONFIG['events_file'],'w');
fwrite($handle,$opening_tags);
# now, write an entry for each event, including the new one
foreach($events as $event)
{
# pause for a moment when we come across the event
# that we are editing so that we can update it's data
if($event['id'] == $data['edit_id'])
{
# the picture needs to be specially done
if(!empty($_FILES['picture']['name']))
{
$data['picture'] = events_save_picture($data['edit_id']);
}
# step through all the form data and update
# the event array with the edited data
foreach($data as $key => $value)
{
# there are form elements that aren't part of the
# xml document, so only update the event array
# with the form elements that are also xml elements
if(array_key_exists($key,$event))
{
$event[$key] = $value;
}
}
}
$xml = events_create_xml($event);
fwrite($handle,$xml);
}
fwrite($handle,$closing_tags);
fclose($handle);
# make a message to the user to let him know the event
# was added successfully
if(!err_exist())
{
mesg_make("Event '{$data['title']}' successfully edited.");
}
}
########################################################## FUNCTION: events_get #
function events_get($event_id)
{
global $_CONFIG;
# gather all the current events from xml file
$events = xml_parse_file($_CONFIG['events_file']);
$events = xml_force_array($events['events']['event']);
# look for the event with the id we are looking for
# if we find it, return the event array
foreach($events as $event)
{
if($event['id'] == $event_id)
{
return($event);
}
}
}
################################################### FUNCTION: events_create_xml #
function events_create_xml($data)
{
# I think slashes are being automatically added by php to
# the stuff passed via a post request. This will make
# sure any text that the user entered are stripped before
# we add them to the xml document
foreach($data as $key => $value)
{
$data[$key] = stripslashes($value);
}
$xml = "
<event id='{$data['id']}'>
<title>{$data['title']}</title>
<date>{$data['date']}</date>
<starttime>{$data['starttime']}</starttime>
<endtime>{$data['endtime']}</endtime>
<location>{$data['location']}</location>
<description>{$data['description']}</description>
<picture>{$data['picture']}</picture>
</event>";
return($xml);
}