diff --git a/week-04/.gitignore b/week-04/.gitignore new file mode 100644 index 0000000..237406b --- /dev/null +++ b/week-04/.gitignore @@ -0,0 +1 @@ +reg \ No newline at end of file diff --git a/week-04/Register.php b/week-04/Register.php new file mode 100644 index 0000000..78798fa --- /dev/null +++ b/week-04/Register.php @@ -0,0 +1,341 @@ + + * @git WEBD-310-45 + * @license GNU General Public License version 2 or later; see LICENSE.txt + * + * @week4 + * Create a document with a form that registers bowlers for a bowling + * tournament. Use a single text fi le that saves information for each + * bowler on a separate line. Include the bowler’s name, age, and aver- + * age, separated by commas. Ensure that the Projects directory has read + * and write permissions for everyone. + * + */ + +// our little global values +$message = ''; +$error = array(); +$valid = true; +$registered = true; +// form keys +$form = array('name' => 'string', 'age' => 'int', 'average' => 'int'); +// form values +$entry = array(); + +// function to validate user input (basic) +// for more advance checkout https://github.com/joomla-framework/filter/blob/2.0-dev/src/InputFilter.php +function validatePostUserInput($key, $type = 'int') +{ + // bring our global values + global $message; + global $entry; + global $form; + // we have a local per/field validation switch + $valid = false; + // we clear the message each time + $message = ''; + // first we check if the key exist + if (!isset($_POST[$key])) + { + $valid = false; + } + // check if the string that its Register :) + elseif ($type === 'form') + { + $valid = $_POST[$key] === 'Register'; + } + // check if the string (only name) + elseif ($type === 'string') + { + $value = preg_replace("/[^A-Za-z\. ]/", '', $_POST[$key]); + $valid = true; + // check name + if ($value !== $_POST[$key]) + { + $message = "
Please only use alphabetical characters for the $key field!"; + $valid = false; + } + elseif (strlen(trim($value)) == 0) + { + $message = "
The $key field is required and can not be empty!"; + $valid = false; + } + // remove all white space + $value = trim($value); + } + // check that this is an int + elseif ($type === 'int' && is_numeric($_POST[$key]) && is_int($_POST[$key] + 0)) + { + $value = (int) $_POST[$key]; + $valid = true; + // check the age to be realistic + if ($key === 'age' && ($value > 120 || $value < 10)) + { + if ($value > 120) + { + $message = "
Only below 120 years can register to this tournament."; + } + else + { + $message = "
Only 10 years and above can register to this tournament."; + } + $valid = false; + } + // check the bowling average to be reasonable + // https://www.quora.com/What-is-a-good-bowling-average-for-an-amateur-Bowling-on-a-lane-not-bowling-in-cricket + // Lets set the range wide to allow beginners and professionals + if ($key === 'average' && ($value > 300 || $value < 60)) + { + $message = "
Please enter a reasonable average!"; + $valid = false; + } + } + else + { + $message = "
Only integer values allowed as $key!"; + $valid = false; + } + // we set the value if valid + if ($valid && isset($form[$key])) + { + $entry[$key] = $_POST[$key]; + } + + return $valid; +} + +// get a valid value +function getValue($key, $default = '') +{ + global $entry; + global $registered; + // check if the value was set + if ($registered && isset($entry[$key])) + { + return $entry[$key]; + } + + return $default; +} + +// to retain valid values +function setFormValue($key) +{ + global $entry; + global $registered; + // check if the value was set + if ($registered && isset($entry[$key])) + { + return " value=\"$entry[$key]\""; + } + + return ''; +} + +// to display an error message +function getError($key) +{ + global $error; + global $registered; + // check if the value was set + if ($registered && isset($error[$key])) + { + return "
$error[$key]"; + } + + return ''; +} + +// function to save the entry to a file +function saveToFile() +{ + // for errors + global $error; + // get the entries + global $entry; + // the path to our project registration directory + $path = getcwd() . '/reg'; + // check if the path exist + if (!file_exists($path)) + { + // We create the path so everyone has access to this project registration directory + if (!mkdir($path, 0777)) + { + $error[] = 'We could not create the project folder, check your folder permissions.'; + + return false; + } + } + // the file where we store the registrations + $file_path = $path . "/bowlers.txt"; + // check the file does not exist + if (!file_exists($file_path)) + { + // creat and place the values as the first line + if (file_put_contents($file_path, implode(",", $entry) . "\n")) + { + return true; + } + $error[] = 'We could not create/write to the registration file, check your file permissions.'; + + return false; + } + // or just append the values + elseif (file_put_contents($file_path, implode(",", $entry) . "\n", FILE_APPEND)) + { + return true; + } + $error[] = 'We could not write to the registration file, check your file permissions.'; + + return false; +} + +// we get the data from the post if there is any +if (!validatePostUserInput('Submit', 'form')) +{ + // registration was not yet made, or a serious issue. So we try again. + $registered = false; +} +else +{ + foreach ($form as $field => $validate) + { + if (!validatePostUserInput($field, $validate)) + { + // show an error as we do not have a hours + $error[$field] = "Invalid value detected!$message"; + $valid = false; + } + } +} + +?> + + + Registers Bowlers + + + + + +

was Registered Successfully to the + Bowling Tournament.

+
+
With these details: + " . ucfirst($key) . ": $value"; + }); ?> + +
+
+

+ +

+ +

There was an server error

+ 0) : ?> +
+ +
+ +

The registration could not be captured, please try again later. Should this + issue continue + then inform the Bowling Tournament organizers.

+

+ +

+ + +

Register for Bowling Tournament

+ 0) : ?> +

Submission failed, please try again

+ +
+
Name of Bowler:   + /> + +
+
+
Age of Bowler:   + />  + + +
+
+
Bowling Average:   + />  + + +
+
+
+    +
+
+ + + + \ No newline at end of file