Mary_Pieroszkiewicz

Untitled

Mar 30th, 2021
700
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.22 KB | None | 0 0
  1. <?php
  2. //var_dump($_POST);
  3. $emailToSend = "[email protected]";
  4.  
  5. $errors = [];
  6.  
  7. const prices = [
  8.     "glass_type" => [
  9.         "4mm-sh" => 95,
  10.         "4mm-ow" => 145,
  11.         "6mm-sh" => 135,
  12.         "6mm-ow" => 205
  13.     ],
  14.     "print_type" => [
  15.         "cmyk" => 40,
  16.         "cmyk_w" => 60,
  17.         "cmyk_2x" => 80
  18.     ],
  19.     "hole_size" => [
  20.         "6-10" => 9,
  21.         "11-30" => 18,
  22.         "31-50" => 21
  23.     ],
  24.     "hole_socket" => 36,
  25.     "glass_edge_grinding" => 12
  26. ];
  27.  
  28. function calculateSum($glassX, $glassY, $glassType, $printType, $holeCount, $holeSize, $holeSocket, $glassEdgeGrinding) {
  29.     $glassPrice = prices["glass_type"][$glassType];
  30.     $printPrice = prices["print_type"][$printType];
  31.  
  32.     $glassX = max(100, $glassX);
  33.     $glassY = max(100, $glassY);
  34.  
  35.     $sum_part1 = (($glassX * $glassY / 10000) * ($glassPrice + $printPrice));
  36.  
  37.     $holeSizePrice = 0;
  38.     if ($holeSize < 11)
  39.         $holeSizePrice = prices["hole_size"]["6-10"];
  40.     if ($holeSize >= 11 && $holeSize <= 30)
  41.         $holeSizePrice = prices["hole_size"]["11-30"];
  42.     if ($holeSize > 31)
  43.         $holeSizePrice = prices["hole_size"]["31-50"];
  44.     $sum_part2 = $holeCount * $holeSizePrice;
  45.  
  46.     $sum_part3 = $holeSocket * prices["hole_socket"];
  47.  
  48.     $glassEdgeGrinding = $glassEdgeGrinding ? prices["glass_edge_grinding"] : 0;
  49. //     $sum_part4 = 2 * ($glassX + $glassY / 100) * $glassEdgeGrinding;
  50.     $sum_part4 =($glassX + $glassY / 50) * $glassEdgeGrinding;
  51.  
  52.     return round($sum_part1 + $sum_part2 + $sum_part3 + $sum_part4);
  53. }
  54.  
  55. function setValue($val) {
  56.     return empty($val) ? 0 : $val;
  57. }
  58.  
  59. if ($_SERVER["REQUEST_METHOD"] === "POST") {
  60.     $glassX = (float)$_POST["glass_x"]; //required
  61.     $glassY = (float)$_POST["glass_y"]; //required
  62.     $glassType = (string)$_POST["glass_type"]; //required
  63.     $printType = (string)$_POST["print_type"]; //required
  64.     $holeCount = (float)setValue($_POST["hole_count"]);
  65.     $holeSize = (float)setValue($_POST["hole_size"]);
  66.     $holeSocket = (float)setValue($_POST["hole_socket"]);
  67.     $userPhone = (string)setValue($_POST["user_phone"]);
  68.     $userEmail = (string)setValue($_POST["user_email"]); //required
  69.     $glassEdgeGrinding = isset($_POST["glass_edge_grinding"]) ? true : false;
  70.  
  71.     if (!filter_var($userEmail, FILTER_VALIDATE_EMAIL)) { //sprawdzamy czy email ma zły wzór
  72.         array_push($errors, "email");
  73.     }
  74.  
  75.     if (empty($glassX)) {
  76.         array_push($errors, "glass_x");
  77.     }
  78.     if (empty($glassX)) {
  79.         array_push($errors, "glass_y");
  80.     }
  81.     if (empty($glassType)) {
  82.         array_push($errors, "glass_type");
  83.     }
  84.     if (empty($printType)) {
  85.         array_push($errors, "print_type");
  86.     }
  87.  
  88.     if (count($errors) > 0) {
  89.         $return["errors"] = $errors;
  90.     } else {
  91.  
  92.         $sum = calculateSum($glassX, $glassY, $glassType, $printType, $holeCount, $holeSize, $holeSocket, $glassEdgeGrinding);
  93.  
  94.         $headers  = "MIME-Version: 1.0" . "\r\n";
  95.         $headers .= "Content-type: text/html; charset=UTF-8". "\r\n";
  96.         $headers .= "From: ".$userEmail."\r\n";
  97.         $headers .= "BCC:" .$userEmail;
  98.         $message  = "
  99.            <html>
  100.                <head>
  101.                    <meta charset=\"utf-8\">
  102.                </head>
  103.                <body>
  104.                    <div> telefon: $userPhone</div>
  105.                    <div> email: <a href=\"mailto:$userEmail\">$userEmail</a> </div>
  106.                    
  107.                    glassX: $glassX<br>
  108.                    glassY: $glassY<br>
  109.                    glassType: $glassType<br>
  110.                    print_type: $printType<br>
  111.                    holeCount: $holeCount<br>
  112.                    holeSize: $holeSize<br>
  113.                    holeSocket: $holeSocket<br>
  114.                    glassEdgeGrinding: $glassEdgeGrinding<br>
  115.                    userPhone: $userPhone<br>
  116.                    userEmail: $userEmail<br>
  117.                    suma: $sum
  118.                </body>
  119.            </html>";
  120.  
  121.         if (mail($emailToSend, "Wiadomość ze strony - " . date("d-m-Y"), $message, $headers)) {
  122.             $return["status"] = "ok";
  123.         } else {
  124.             $return["status"] = "error";
  125.         }
  126.     }
  127.  
  128.     echo json_encode($return);
  129. }
Advertisement
Add Comment
Please, Sign In to add comment