$pageTitle="Visualizations :: Timelines :: Manage Timelines";
include($_SERVER['DOCUMENT_ROOT'] . "/template/header.php");
echo "
Creating Timeline Person
\n";
echo "Return to Editing Your Timeline
\n";
$errorArray=array();
/*************************************************************
Start by figuring out who is logged in
*************************************************************/
if(user_logged_in() && !$doingLogout){
$username=$_COOKIE['sclcr'];
}
else{
// the user isn't logged in
?>
Not Logged In
You must be logged in before you can create timeline items.
include($_SERVER['DOCUMENT_ROOT'] . "/template/footer.php");
exit();
}
//END user check
//validate person's name
function validate_person_name($n){
if( strlen($n)>=2 && strlen($n)<=255 && str_word_count($n)>=1 ){
return true;
}
else{
return false;
}
}
//validate people descriptions
function validate_description($d){
if( strlen($d)>=5 && strlen($d)<=2000 && str_word_count($d)>=2 ){
return true;
}
else{
return false;
}
}
//validate a date
function generate_datetime($m, $d, $y){
if($d==0) $d=1;
if($m==0) $m=1;
if( !is_numeric($d) || !is_numeric($m) || !is_numeric($y) || $d<1 || $d>31 || $m<1 || $m>12 || $y<1000 || $y>9999 ) return false;
if( !checkdate($m, $d, $y) ) return false;
if( $d<10 ){ $d="0" . $d; } else { $d="$d"; }
if( $m<10 ){ $m="0" . $m; } else { $m="$m"; }
//YYYY-MM-DD HH:MM:SS
return "$y-$m-$d 00:00:00";
}
//take a year, month, and day and format a date for DISPLAY purposes
function format_display_date($y, $m, $d){
//if the month and day are both 1 then we ONLY show the year
if($m==1 && $d==1) return "$y";
//if the month is other than 1 and the day is one we dispay the month as well
if($m>1 && $d==1) return date("M", mktime(0, 0, 0, $m, 1, 2005)) . " " .$y;
//if the day is other than 1 we show the full date
if($d>1) return date("M", mktime(0, 0, 0, $m, 1, 2005)) . " $d, $y";
return false;
}
//let's see if they submitted any values and if so, we validate and process them
if( isset($_POST['submitPerson']) ){
//they submitted data so let's check it
if( isset($_POST['name']) && isset($_POST['description']) ){
//they did include a name and description
$name=trim(strip_tags($_POST['name']));
$description=$_POST['description'];
//let's validate the description and name
if(validate_person_name($name)){
if(validate_description($description)){
//now we check to see if this person is alive or dead (dead is a span)
if( isset($_POST['span']) && $_POST['span']=="span" ){
$span=true;
}
else{
$span=false;
}
/*****************************************************
COMPUTE START DATE
*****************************************************/
if( isset($_POST['day']) ){ $day=$_POST['day']; } else { $day=1; }
if( isset($_POST['month']) ){ $month=$_POST['month']; } else { $month=1; }
if( isset($_POST['year']) ){ $year=$_POST['year']; } else { $year=false; }
$startDateTime=generate_datetime($month, $day, $year);
if($startDateTime){
//the datetime is valid
//echo "VALID: $startDateTime
\n";
}
else{
//invalid date
$errorArray[]="invalidStartDate";
}
/***************************************************/
/*****************************************************
COMPUTE END DATE
*****************************************************/
if($span){
if( isset($_POST['dayEnd']) ){ $dayEnd=$_POST['dayEnd']; } else { $dayEnd=1; }
if( isset($_POST['monthEnd']) ){ $monthEnd=$_POST['monthEnd']; } else { $monthEnd=1; }
if( isset($_POST['yearEnd']) ){ $yearEnd=$_POST['yearEnd']; } else { $yearEnd=false; }
$endDateTime=generate_datetime($monthEnd, $dayEnd, $yearEnd);
if($endDateTime){
//the datetime is valid
//now we make sure the end date is AFTER the start date
if(strtotime($endDateTime) > strtotime($startDateTime)){
//the end date comes later
//echo "VALID: $endDateTime
\n";
}
else{
//they put the days in out of order
$endDateTime=false;
$errorArray[]="datesOutOfOrder";
}
}
else{
//invalid date
$errorArray[]="invalidEndDate";
}
}
else{
$endDateTime="0000-00-00 00:00:00";
}
/***************************************************/
//check to make sure we are ready to add to database
if( ($startDateTime) && ( (!$span) || ($span && $endDateTime) ) ){
//everything is valid and ready to roll
//format and clean the variables
if($span){ $span=1; } else { $span=0; }
$name=sanitize($name);
$description=sanitize($description);
$sqlInsertString="INSERT INTO TimelinePeople (span, start, end, name, description, contributor, submitted) VALUES ($span, '$startDateTime', '$endDateTime', '$name', '$description', '$username', NOW())";
$result=database_query($sqlInsertString);
if($result){
echo "\n";
echo "- Your new person was successfully added to the database. Thanks!
\n";
if( isset($_SESSION['targetTimeline']) ){
echo "- Continue editing your timeline
\n";
}
echo "
\n";
$name="";
$description="";
$span=0;
$month="";
$day="";
$year="";
$monthEnd="";
$dayEnd="";
$yearEnd="";
}
else{
$errorArray[]="databaseInsertFail";
}
}
else{
//something was wrong so we refuse to insert the new object into the database
$errorArray[]="spanDateTimeFormatFail";
}
}
else{
//description invalid
$errorArray[]="invalidDescription";
}
}
else{
//name invalid
$errorArray[]="invalidName";
}
}
else{
//they did not submit these required values so fail
$errorArray[]="missingNameOrDescription";
}
}
else{
//they didn't submit anything
//$errorArray[]="noData";
}
//if there were errors we need to display messages
if(count($errorArray)>=1){
echo "\n";
echo "
\n";
foreach($errorArray as $e){
if($e=="noData"){
echo "- No data was submitted. Please fill out the form and press the submit button.
\n";
}
elseif($e=="missingNameOrDescription"){
echo "- You are missing either the name or description. Both are required.
\n";
}
elseif($e=="invalidName"){
echo "- Your name is not valid. It must be between 2 and 255 characters long.
";
}
elseif($e=="invalidDescription"){
echo "- Your description is not valid. It must be between 5 and 1024 characters long and consist of at least three words.
";
}
elseif($e=="spanDateTimeFormatFail"){
echo "- If the person has died, check the passed away button and be sure to include both a birth and death date.
";
}
elseif($e=="databaseInsertFail"){
echo "- We experienced a technical error while attempting to add your person to the database. We apologize. Please try again in a few moments.
";
}
elseif($e=="invalidEndDate"){
echo "- You did not provide a valid end date. You must provide at least the year. Make sure the date is accurate and that you selected a valid day of the month.
";
}
elseif($e=="invalidStartDate"){
echo "- You did not provide a valid ";
if($span) echo "birth ";
echo "date. You must provide at least the year. Make sure the date is accurate and that you selected a valid day of the month.
";
}
elseif($e=="datesOutOfOrder"){
echo "- Your ending date is not after your birth date. The ending date must be after the birth date.
";
}
else{
echo "- An error occured. We apologize. Please try again in a few moments.
\n";
}
}
echo "
\n
\n";
}
?>
Similar People in the Database
include($_SERVER['DOCUMENT_ROOT'] . "/template/footer.php"); ?>