Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //var_dump($_POST);
- $errors = [];
- const prices = [
- "glass_type" => [
- "4mm-sh" => 95,
- "4mm-ow" => 145,
- "6mm-sh" => 135,
- "6mm-ow" => 205
- ],
- "print_type" => [
- "cmyk" => 40,
- "cmyk_w" => 60,
- "cmyk_2x" => 80
- ],
- "hole_size" => [
- "6-10" => 9,
- "11-30" => 18,
- "31-50" => 21
- ],
- "hole_socket" => 36,
- "glass_edge_grinding" => 12
- ];
- function calculateSum($glassX, $glassY, $glassType, $printType, $holeCount, $holeSize, $holeSocket, $glassEdgeGrinding) {
- $glassPrice = prices["glass_type"][$glassType];
- $printPrice = prices["print_type"][$printType];
- $glassX = max(100, $glassX);
- $glassY = max(100, $glassY);
- $sum_part1 = (($glassX * $glassY / 10000) * ($glassPrice + $printPrice));
- $holeSizePrice = 0;
- if ($holeSize < 11)
- $holeSizePrice = prices["hole_size"]["6-10"];
- if ($holeSize >= 11 && $holeSize <= 30)
- $holeSizePrice = prices["hole_size"]["11-30"];
- if ($holeSize > 31)
- $holeSizePrice = prices["hole_size"]["31-50"];
- $sum_part2 = $holeCount * $holeSizePrice;
- $sum_part3 = $holeSocket * prices["hole_socket"];
- $glassEdgeGrinding = $glassEdgeGrinding ? prices["glass_edge_grinding"] : 0;
- // $sum_part4 = 2 * ($glassX + $glassY / 100) * $glassEdgeGrinding;
- $sum_part4 =($glassX + $glassY / 50) * $glassEdgeGrinding;
- return round($sum_part1 + $sum_part2 + $sum_part3 + $sum_part4);
- }
- function setValue($val) {
- return empty($val) ? 0 : $val;
- }
- if ($_SERVER["REQUEST_METHOD"] === "POST") {
- $glassX = (float)$_POST["glass_x"]; //required
- $glassY = (float)$_POST["glass_y"]; //required
- $glassType = (string)$_POST["glass_type"]; //required
- $printType = (string)$_POST["print_type"]; //required
- $holeCount = (float)setValue($_POST["hole_count"]);
- $holeSize = (float)setValue($_POST["hole_size"]);
- $holeSocket = (float)setValue($_POST["hole_socket"]);
- $userPhone = (string)setValue($_POST["user_phone"]);
- $userEmail = (string)setValue($_POST["user_email"]); //required
- $glassEdgeGrinding = isset($_POST["glass_edge_grinding"]) ? true : false;
- if (!filter_var($userEmail, FILTER_VALIDATE_EMAIL)) { //sprawdzamy czy email ma zły wzór
- array_push($errors, "email");
- }
- if (empty($glassX)) {
- array_push($errors, "glass_x");
- }
- if (empty($glassX)) {
- array_push($errors, "glass_y");
- }
- if (empty($glassType)) {
- array_push($errors, "glass_type");
- }
- if (empty($printType)) {
- array_push($errors, "print_type");
- }
- if (count($errors) > 0) {
- $return["errors"] = $errors;
- } else {
- $sum = calculateSum($glassX, $glassY, $glassType, $printType, $holeCount, $holeSize, $holeSocket, $glassEdgeGrinding);
- $headers = "MIME-Version: 1.0" . "\r\n";
- $headers .= "Content-type: text/html; charset=UTF-8". "\r\n";
- $headers .= "From: ".$userEmail."\r\n";
- $headers .= "BCC:" .$userEmail;
- $message = "
- <html>
- <head>
- <meta charset=\"utf-8\">
- </head>
- <body>
- <div> telefon: $userPhone</div>
- <div> email: <a href=\"mailto:$userEmail\">$userEmail</a> </div>
- glassX: $glassX<br>
- glassY: $glassY<br>
- glassType: $glassType<br>
- print_type: $printType<br>
- holeCount: $holeCount<br>
- holeSize: $holeSize<br>
- holeSocket: $holeSocket<br>
- glassEdgeGrinding: $glassEdgeGrinding<br>
- userPhone: $userPhone<br>
- userEmail: $userEmail<br>
- suma: $sum zł
- </body>
- </html>";
- if (mail($emailToSend, "Wiadomość ze strony - " . date("d-m-Y"), $message, $headers)) {
- $return["status"] = "ok";
- } else {
- $return["status"] = "error";
- }
- }
- echo json_encode($return);
- }
Advertisement
Add Comment
Please, Sign In to add comment