Created
November 30, 2017 20:39
-
-
Save scheibome/16b63d9192a9442a18ec36a4cf1c6e17 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Gets an element of an array by key | |
* | |
* <code> | |
* | |
* $array = array( | |
* 'cat' => 'miao', | |
* 'dog' => 'wuff', | |
* 'bird' => 'tweet' | |
* ); | |
* | |
* echo a::get($array, 'cat'); | |
* // output: 'miao' | |
* | |
* echo a::get($array, 'elephant', 'shut up'); | |
* // output: 'shut up' | |
* | |
* $catAndDog = a::get(array('cat', 'dog')); | |
* // result: array( | |
* // 'cat' => 'miao', | |
* // 'dog' => 'wuff' | |
* // ); | |
* | |
* </code> | |
* | |
* @param array $array The source array | |
* @param mixed $key The key to look for | |
* @param mixed $default Optional default value, which should be returned if no element has been found | |
* @return mixed | |
*/ | |
function get($array, $key, $default = null) { | |
// get an array of keys | |
if(is_array($key)) { | |
$result = array(); | |
foreach($key as $k) $result[$k] = get($array, $k); | |
return $result; | |
// get a single | |
} else if(isset($array[$key])) { | |
return $array[$key]; | |
// return the entire array if the key is null | |
} else if(is_null($key)) { | |
return $array; | |
// get the default value if nothing else worked out | |
} else { | |
return $default; | |
} | |
} | |
/** | |
* Shortcut for call_user_func_array with a better handling of arguments | |
* | |
* @param mixed $function | |
* @param mixed $arguments | |
* @return mixed | |
*/ | |
function call($function, $arguments = array()) { | |
if(!is_callable($function)) return false; | |
if(!is_array($arguments)) $arguments = array($arguments); | |
return call_user_func_array($function, $arguments); | |
} | |
/** | |
* Checks for invalid data | |
* | |
* @param array $data | |
* @param array $rules | |
* @param array $messages | |
* @return array | |
*/ | |
function invalid($data, $rules, $messages = []) { | |
$errors = []; | |
foreach ($rules as $field => $validations) { | |
$validationIndex = -1; | |
// See: http://php.net/manual/en/types.comparisons.php | |
// only false for: null, undefined variable, '', [] | |
$filled = isset($data[$field]) && $data[$field] !== '' && $data[$field] !== []; | |
$message = get($messages, $field, $field); | |
// True if there is an error message for each validation method. | |
$messageArray = is_array($message); | |
foreach ($validations as $method => $options) { | |
if (is_numeric($method)) { | |
$method = $options; | |
} | |
$validationIndex++; | |
if ($method === 'required') { | |
if ($filled) { | |
// Field is required and filled. | |
continue; | |
} | |
} else if ($filled) { | |
if (!is_array($options)) { | |
$options = [$options]; | |
} | |
array_unshift($options, get($data, $field)); | |
if (call(['v', $method], $options)) { | |
// Field is filled and passes validation method. | |
continue; | |
} | |
} else { | |
// If a field is not required and not filled, no validation should be done. | |
continue; | |
} | |
// If no continue was called we have a failed validation. | |
if ($messageArray) { | |
$errors[$field][] = get($message, $validationIndex, $field); | |
} else { | |
$errors[$field] = $message; | |
} | |
} | |
} | |
return $errors; | |
} | |
function validateForm($data) { | |
$response = array(); | |
// array of rules for form validation | |
$rules = array( | |
'name' => array('required'), | |
'address' => array('required'), | |
'zip' => array('required'), | |
'city' => array('required'), | |
'email' => array('required'), | |
'checkbox1' => array('required') | |
); | |
// array of messages to return if some of the data is not valid | |
$messages = array( | |
'name' => 'Please enter your name.', | |
'address' => 'Please enter your address.', | |
'zip' => 'Please enter your zip code.', | |
'city' => 'Please enter your city.', | |
'email' => 'Please enter a correct e-mail address.', | |
'checkbox1' => 'Please confirm the order terms.' | |
); | |
// evaluate data and rules using the invalid() helper | |
if ($invalid = invalid($data, $rules, $messages)) { | |
$response['errors'] = $invalid; | |
} else { | |
$response['success'] = true; | |
} | |
return $response; | |
} | |
function getCartItems($cart) { | |
$mailContent = '<tr style="background-color: #e3e4e8;"><td style="padding: 10px; border-left: 1px solid #999999;">Articlenumber</td><td style="padding: 10px; border-left: 1px solid #999999;">Price for one</td><td style="padding: 10px; border-left: 1px solid #999999;">Article</td><td style="padding: 10px; border-left: 1px solid #999999;">Properties</td><td style="padding: 10px; border-left: 1px solid #999999;">Amount</td><td style="padding: 10px; border-left: 1px solid #999999;">Price</td></tr>'; | |
foreach ($cart as $cartitems) { | |
$mailContent .= '<tr style="border-top: 1px solid #999999;">'; | |
$numItems = count($cartitems); | |
$i = 0; | |
$articledetail = ''; | |
foreach ($cartitems as $key => $value) { | |
// calc total of product | |
if ($key === 'price') { | |
$singlePrice = $value; | |
} | |
if ($key === 'qty') { | |
$qty = $value; | |
} | |
if ($key == 'name') { | |
$articledetail .= '<p>' . $value . '</p>'; | |
} | |
if ($key == 'properties') { | |
$property = explode(';', $value); | |
$value = ''; | |
foreach ($property as $v) { | |
$value .= $v . '<br />'; | |
} | |
} | |
if ($key == 'data') { | |
$articledetail .= $value; | |
$value = $articledetail; | |
} | |
// last element, now calc total of product | |
if (++$i === $numItems) { | |
$sum = $qty * str_replace(',', '.', $singlePrice); | |
$mailContent .= '<td style="padding: 10px; border-left: 1px solid #999999;">' . ($key === 'price' ? '$ ' : '') . $value . '</td><td style="padding: 10px; border-left: 1px solid #999999;">$ ' . str_replace('.', ',', $sum) . '</td>'; | |
} else { | |
if ($key !== 'image' && $key !== 'name' && $key !== 'propertieshash') { | |
$mailContent .= '<td style="padding: 10px; border-left: 1px solid #999999;">' . ($key === 'price' ? '$ ' : '') . $value . '</td>'; | |
} | |
} | |
} | |
$mailContent .= '</tr>'; | |
} | |
return $mailContent; | |
} | |
function sendOrderMail($data) { | |
$data = array_reverse($data, true); | |
$response = array(); | |
$labels = array( | |
'name' => 'Name: ', | |
'address' => 'Address: ', | |
'zip' => 'Zip: ', | |
'city' => 'City: ', | |
'email' => 'Email: ', | |
'total' => 'Total: ', | |
'shipping' => 'Shipping: ', | |
'full' => 'Full: ' | |
); | |
// Build Mailcontent | |
$mailAdress = '<table>'; | |
$mailContentSum = ''; | |
foreach ($data as $key => $value) { | |
if ($key === 'cart') { | |
// Cart | |
$mailContentCart = getCartItems($value); | |
} elseif ($key === 'total' || $key === 'shipping' || $key === 'full') { | |
// Costs | |
$mailContentSum .= '<tr style="background-color: #e3e4e8;"><td colspan="5" style="padding: 10px;">' . $labels[$key] . '</td><td style="padding: 10px;">$ '. $value . '</td></tr>'; | |
} else { | |
// Address | |
if($key !== 'checkbox1') { | |
$mailAdress .= '<tr><td>' . $labels[$key] . '</td><td>'. urldecode($value) . '</td></tr>'; | |
} | |
} | |
if ($key === 'email') { | |
$sendermail = urldecode($value); | |
} | |
} | |
$mailAdress .= '</table>'; | |
$sendText = '<table style="border-collapse: collapse; border-spacing: 0; border: 1px solid #999999">'; | |
$sendText .= $mailContentCart; | |
$sendText .= $mailContentSum; | |
$sendText .= '</table>'; | |
$sendText .= '<h4>The order goes to:</h4>'; | |
$sendText .= $mailAdress; | |
$response['success'] = '<h2>Demo-Order: Thank you for your order.</h2><p>Your data was sent by e-mail. I will get back soon as possible.</p>' . $sendText; | |
return $response; | |
} | |
$request_body = file_get_contents('php://input'); | |
if($request_body) { | |
$data = json_decode($request_body, true); | |
// call validateForm() with $data as parameter | |
$response = validateForm($data); | |
// if form validation succeeds, we try to add the data to the structure field | |
if(isset($response['success'])) { | |
$response = sendOrderMail($data); | |
} | |
echo json_encode($response, ARRAY_FILTER_USE_KEY); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment