src/Controller/DefaultController.php line 393

Open in your IDE?
  1. <?php namespace App\Controller;
  2. use App\{Entity\OrderDetail, SpecialManager};
  3. use DateTime;
  4. use Doctrine\Persistence\ManagerRegistry;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\{Request, Response};
  7. use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
  8. use Symfony\Component\Mailer\MailerInterface;
  9. use Symfony\Component\Mime\Address;
  10. use Symfony\Component\Mime\Email;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. /**
  13. * Class DefaultController
  14. * @package Almeda\Controller
  15. */
  16. class DefaultController extends AbstractController {
  17. #[Route('/', name: 'app_default_index')]
  18. public function indexAction(): Response {
  19. return $this->render( 'index.html.twig' );
  20. }
  21. #[Route('/history', name: 'app_default_history')]
  22. public function historyAction(): Response {
  23. return $this->render( 'history.html.twig' );
  24. }
  25. #[Route('/chocolate', name: 'app_default_chocolate')]
  26. public function chocolateAction( ManagerRegistry $doctrine ): Response {
  27. return $this->render( 'chocolate.html.twig', [
  28. 'chocArray' => $this->getPageData( 1, $doctrine ),
  29. ] );
  30. }
  31. #[Route('/nuts', name: 'app_default_nuts')]
  32. public function nutsAction( ManagerRegistry $doctrine ): Response {
  33. return $this->render( 'nuts.html.twig', [
  34. 'nutsArray' => $this->getPageData( 2, $doctrine ),
  35. ] );
  36. }
  37. public function getPageData( $category, ManagerRegistry $doctrine ): array {
  38. $conn = $doctrine->getConnection();
  39. $sql = "SELECT * FROM almeda.vPageSectionInfo WHERE CategoryID = :catID ";
  40. if( $category === 1 ) {
  41. $sql .= 'OR ProductID = 9 ';
  42. }
  43. $sql .= 'ORDER BY PageTitle, PageSequence, SizeSequence, ProductCost;';
  44. $data = $conn->prepare( $sql );
  45. $data->bindValue( 'catID', $category );
  46. return $data->executeQuery()->fetchAllAssociative();
  47. }
  48. #[Route('/order', name: 'app_default_order')]
  49. public function orderAction(ManagerRegistry $doctrine): Response {
  50. $jscript = $this->shipToFormAction($doctrine);
  51. $chocArray = $this->getPageData( 1, $doctrine );
  52. $nutsArray = $this->getPageData( 2, $doctrine );
  53. return $this->render( 'order.html.twig', [ 'js' => $jscript, 'chocArray' => $chocArray, 'nutsArray' => $nutsArray ] );
  54. }
  55. #[Route('/orderSubmit', name: 'app_default_ordersubmit')]
  56. public function orderSubmitAction( Request $request, MailerInterface $mailer, ManagerRegistry $doctrine): Response {
  57. $errorMessage = '';
  58. $postData = $request->request->all();
  59. $bill_name = $postData['bill_name'];
  60. $bill_street = $postData['bill_street'];
  61. $bill_street_2 = $postData['bill_street_2'];
  62. $bill_city = $postData['bill_city'];
  63. $bill_state = $postData['bill_state'];
  64. $bill_zip = $postData['bill_zip'];
  65. $day_phone = $postData['day_phone'];
  66. $eve_phone = $postData['eve_phone'];
  67. $email = $postData['email'];
  68. $recipNums = $postData['recip_nums'];
  69. $recipArr = explode( ',', $recipNums );
  70. $recipCount = count( $recipArr );
  71. $total = 0;
  72. $ship_name_rows = [];
  73. $ship_street_rows = [];
  74. $ship_street_2_rows = [];
  75. $ship_city_rows = [];
  76. $ship_state_rows = [];
  77. $ship_zip_rows = [];
  78. $bus_add_rows = [];
  79. $gift_wrap_rows = [];
  80. $greeting_rows = [];
  81. $greetings_to_rows = [];
  82. $ship_now_rows = [];
  83. $ship_later_rows = [];
  84. $ship_month_rows = [];
  85. $ship_year_rows = [];
  86. $check_mo_encl_rows = [];
  87. $orderArr2 = [];
  88. $quantity_rows = [];
  89. $size_rows = [];
  90. $product_rows = [];
  91. for ( $i = 0; $i < $recipCount; $i ++ ) {
  92. $whichRecip = $recipArr[$i];
  93. $ship_name_rows[] = $postData["ship_name$whichRecip"];
  94. $ship_street_rows[] = $postData["ship_street$whichRecip"];
  95. $ship_street_2_rows[] = $postData["ship_street_2$whichRecip"];
  96. $ship_city_rows[] = $postData["ship_city$whichRecip"];
  97. $ship_state_rows[] = $postData["ship_state$whichRecip"];
  98. $ship_zip_rows[] = $postData["ship_zip$whichRecip"];
  99. $gift_wrap_rows[] = isset( $postData["gift_wrap$whichRecip"] ) ? 'yes' : 'no';
  100. $greeting_rows[] = isset( $postData["greeting$whichRecip"] ) ? 'yes' : 'no';
  101. $greetings_to_rows[] = $postData["greetings_to$whichRecip"];
  102. $bus_add_rows[] = isset( $postData["bus_add$whichRecip"] ) ? 'yes' : 'no';
  103. $ship_now_rows[] = isset( $postData["ship_now$whichRecip"] ) ? 'yes' : 'no';
  104. $ship_later_rows[] = isset( $postData["ship_later$whichRecip"] ) ? 'yes' : 'no';
  105. $ship_month_rows[] = $postData["ship_month$whichRecip"];
  106. $ship_year_rows[] = $postData["ship_year$whichRecip"];
  107. $check_mo_encl_rows[] = isset( $postData["check_mo_encl$whichRecip"] ) ? 'yes' : 'no';
  108. $orderNums[] = $postData["order_nums$whichRecip"];
  109. $orderArr = explode( ',', $orderNums[$i] );
  110. foreach ( $orderArr as $jValue ) {
  111. $quantity_rows[$i][] = $postData["{$whichRecip}quantity$jValue"];
  112. $product_rows[$i][] = $postData["{$whichRecip}product$jValue"];
  113. $size_rows[$i][] = $postData["{$whichRecip}size$jValue"];
  114. $orderArr2[$i][] = $jValue;
  115. }
  116. }
  117. $notes = $postData['notes'];
  118. $ip = getenv( 'REMOTE_ADDR' );
  119. if ( false !== stripos( $notes . $bill_name . $bill_street . $bill_street_2 . $email . $bill_city . $day_phone . $eve_phone, 'http' ) ) {
  120. $errorMessage .= 'We do not allow weblinks in any of the fields! ';
  121. return $this->render( 'orderError.html.twig', compact( 'errorMessage' ) );
  122. }
  123. if ( ! ( str_contains($email, '@') && str_contains($email, '.')) ) {
  124. $errorMessage .= <<<"HTML"
  125. <h2>Use Back - Enter valid e-mail</h2>
  126. <h2>Order was NOT submitted</h2>\n
  127. HTML;
  128. return $this->render( 'orderError.html.twig', compact( 'errorMessage' ) );
  129. }
  130. /*
  131. if ( empty( $bill_name ) ||
  132. empty( $email ) ||
  133. empty( $bill_street ) ||
  134. empty( $bill_city ) ||
  135. empty( $bill_state ) ||
  136. empty( $bill_zip ) ||
  137. ( empty( $day_phone ) && empty( $eve_phone ) )
  138. ) {
  139. $errorMessage .= "<h2>Use Back - fill in billing information fields</h2>\nUse back! !";
  140. return $this->render( 'orderError.html.twig', compact( 'errorMessage' ) );
  141. }
  142. */
  143. $name = $bill_name;
  144. $date_now = new DateTime();
  145. $todayis = date( 'l, F j, Y, g:i a' );
  146. $message = <<<"HTML"
  147. <html lang="en"><body> $todayis [EST] <br /><br />
  148. <b>Billing Information:</b><br />
  149. Name: $bill_name <br />
  150. Street: $bill_street <br />
  151. Street 2: $bill_street_2 <br />
  152. City: $bill_city <br />
  153. State: $bill_state <br />
  154. Zipcode: $bill_zip <br /><br />
  155. <b>Other Information:</b><br />
  156. Daytime Phone: $day_phone<br />
  157. Evening Phone: $eve_phone<br />
  158. Email: $email <br /><br />
  159. <b>Shipping Information:</b><br />
  160. HTML;
  161. $grand_total = 0;
  162. $row_count = count( $ship_name_rows );
  163. $i = $j = 0;
  164. do {
  165. $j++;
  166. $message .= <<<"HTML"
  167. <b>Record $j</b><br />
  168. Name: $ship_name_rows[$i] <br />
  169. Street: $ship_street_rows[$i] <br />
  170. Street 2: $ship_street_2_rows[$i] <br />
  171. City: $ship_city_rows[$i] <br />
  172. State: $ship_state_rows[$i] <br />
  173. Zipcode: $ship_zip_rows[$i] <br />
  174. Business Address: $bus_add_rows[$i] <br />
  175. HTML;
  176. if ( $gift_wrap_rows[$i] === 'yes' && 1 === 2 ) {
  177. $message .= "Gift Wrap: $gift_wrap_rows[$i]<br />";
  178. }
  179. if ( $greeting_rows[$i] === 'yes' ) {
  180. $message .= "Greeting: $greetings_to_rows[$i]<br />";
  181. }
  182. if ( $ship_now_rows[$i] === 'yes' ) {
  183. $message .= "Ship Now: $ship_now_rows[$i]";
  184. } else if ( $ship_later_rows[$i] === 'yes' ) {
  185. $message .= "Ship Later: $ship_later_rows[$i] ($ship_month_rows[$i] / $ship_year_rows[$i])";
  186. } else {
  187. $message .= "Check/MO Enclosed: $check_mo_encl_rows[$i]";
  188. }
  189. $order_row_count = count( $orderArr2[$i] );
  190. $message .= <<<"HTML"
  191. <br />
  192. <table><thead>
  193. <tr><th colspan='4'>Order Details</th></tr><tr>
  194. <th>Qty</th>
  195. <th>Product</th>
  196. <th>Size</th>
  197. <th>Total</th>
  198. </tr></thead><tbody>
  199. HTML;
  200. $conn = $doctrine->getConnection();
  201. for ( $j = 0; $j < $order_row_count; $j ++ ) {
  202. $subtotal = $quantity_rows[$i][$j] * $size_rows[$i][$j];
  203. $total += $subtotal;
  204. $product = $product_rows[$i][$j];
  205. $size = $size_rows[$i][$j];
  206. $sql = 'SELECT * FROM almeda.product WHERE id = :id';
  207. $query = $conn->prepare( $sql );
  208. $query->bindValue( 'id', $product );
  209. $data = $query->executeQuery()->fetchAllAssociative();
  210. $product = htmlentities( $data[0]['title'], ENT_QUOTES );
  211. $group = htmlentities( $data[0]['size_id'], ENT_QUOTES );
  212. $sql = 'SELECT * FROM almeda.size WHERE product_id = :pid AND cost = :cost';
  213. $query = $conn->prepare( $sql );
  214. $query->execute( [ 'pid' => $group, 'cost' => $size ] );
  215. $data2 = $query->executeQuery()->fetchAllAssociative();
  216. $size = $data2[0]['size'] . " (\$$size)";
  217. $subtotfmt = number_format( $subtotal, 2 );
  218. $message .= <<<HTML
  219. <tr>
  220. <td style='text-align:right;'>{$quantity_rows[$i][$j]}</td>
  221. <td style='text-align:left;'>$product</td>
  222. <td style='text-align:right;'>$size</td>
  223. <td style='text-align:right;'>$$subtotfmt</td>
  224. </tr>
  225. HTML;
  226. }
  227. $message .= "</tbody><tfoot><tr><th style='text-align:right;' colspan='4'>Sub-total (before shipping & applicable discounts): \$" . number_format( $total, 2 ) .
  228. '</th></tr></tfoot></table> <br /><br />';
  229. $grand_total += $total;
  230. $total = 0;
  231. $i ++;
  232. } while ( $i < $row_count );
  233. $message .= '<br /><b>Additional Notes:</b><br />' . $notes . '<br />Grand total for order (before shipping & applicable discounts): $' .
  234. number_format( $grand_total, 2 ) . "<br /><input type='hidden' value='" . $ip . "' /></body></html>";
  235. try {
  236. $newOrder = (new OrderDetail())
  237. ->setIp($ip)
  238. ->setUserName($name)
  239. ->setEmail($email)
  240. ->setDate($date_now)
  241. ->setRecipNum($recipCount)
  242. ->setOrderTotal($grand_total)
  243. ->setMessage($message)
  244. ;
  245. $em = $doctrine->getManager();
  246. $em->persist($newOrder);
  247. $em->flush();
  248. $emailObj = (new Email())
  249. ->subject('New Order!')
  250. ->to( new Address( $email, $bill_name ) )
  251. ->cc( new Address( 'info@al-meda.com', 'Al-Meda Chocolates, Inc.' ) )
  252. ->from( new Address( $email, $bill_name ) )
  253. ->replyTo( new Address( $email, $bill_name ) )
  254. ->cc( new Address( 'almeda@rtecexpress.net', 'Al-Meda Chocolates, Inc.' ) )
  255. // ->bcc( new Address( 'admin@manzwebdesigns.com', 'Al-Meda Webmaster' ) )
  256. ->html( $message );
  257. $mailer->send( $emailObj );
  258. } catch ( TransportExceptionInterface $exception ) {
  259. $encoded_message = preg_replace( '/^$^$/', '', rawurlencode( $message ) );
  260. $message = <<<HTML
  261. <div class="alert alert-warning" style="font-weight: bold; font-size: 18px;">
  262. I am sorry, but we couldn't automatically email your order.<br/>Please either call us at
  263. <a href='tel:419-446-2676'>(419) 446-2676</a> or, if you wish to email us directly,
  264. <a href="mailto:info@al-meda.com?subject=New Order!&body=$encoded_message">please click here</a>
  265. to send it using your email client with your order in the email body to send.<br/>Please accept our apology.<br/><br/>Thanks.
  266. </div>
  267. <h3>Your order:</h3>
  268. $message
  269. HTML;
  270. return $this->render( 'orderError.html.twig', array( 'message' => $message ) );
  271. }
  272. return $this->render( 'orderSuccess.html.twig',
  273. array(
  274. 'message' => <<<"HTML"
  275. Thank you, $bill_name, your order was successfully sent!&nbsp; Please check your email ($email) for confirmation,
  276. we will process your order as soon as possible!<br />Please click <a href='/order'>here</a> to return to the order page
  277. or <a href='/'>here</a> to return to the home page.<br /><br /><b>NOTE: If you don't recieve a confirmation email, please
  278. check your spam mailbox and email the <a href='mailto:admin@manzwebdesigns.com' target='_blank'>webmaster</a>
  279. if it isn't there!</b><br><br>Your order:<br>$message
  280. HTML
  281. ) );
  282. }
  283. #[Route('/shipToForm', name: 'app_default_shiptoform')]
  284. public function shipToFormAction(ManagerRegistry $doctrine): string {
  285. $conn = $doctrine->getConnection();
  286. $sql = <<<SQL
  287. SELECT p.id
  288. ,p.title
  289. ,p.description
  290. ,c.title AS category
  291. ,p.size_id AS grp
  292. FROM almeda.product p
  293. JOIN almeda.category c
  294. ON c.id = p.category_id
  295. WHERE p.enabled = 1
  296. ORDER BY p.size_id, p.title
  297. SQL;
  298. $query = $conn->query( $sql );
  299. $data = $query->fetchAll();
  300. $groupArray = array();
  301. foreach ( $data as $product ) {
  302. $groupArray[ $product['id'] ] = array( 'group_id' => $product['grp'] );
  303. }
  304. $sql = 'SELECT DISTINCT product_id FROM almeda.size ORDER BY product_id;';
  305. $query = $conn->query( $sql );
  306. $sizeIndex = $query->fetchAll();
  307. $sql = 'SELECT * FROM almeda.size ORDER BY product_id, id;';
  308. $query = $conn->query( $sql );
  309. $sizeData = $query->fetchAll();
  310. $sizeArray = array();
  311. foreach ( $sizeIndex as $index ) {
  312. $sizeArray[] = array(
  313. 'prod_id' => $index['product_id'],
  314. 'options' => $this->getSizeOptions( $index['product_id'], $sizeData ),
  315. );
  316. }
  317. $productOptions = '';
  318. foreach ( $data as $value ) {
  319. $id = $value['id'];
  320. $description = htmlentities( $value['description'], ENT_QUOTES );
  321. $category = htmlentities( $value['category'], ENT_QUOTES );
  322. $title = htmlentities( $value['title'], ENT_QUOTES );
  323. $productOptions .= sprintf("<option value=\"%s\" title=\"%s (%s)\">%s</option>", $id, $description, $category, $title);
  324. }
  325. return $this->renderView( 'shipToForm.js.twig', [
  326. 'groupArray' => json_encode( $groupArray, JSON_THROW_ON_ERROR ),
  327. 'sizeArray' => json_encode( $sizeArray, JSON_THROW_ON_ERROR ),
  328. 'productOptions' => $productOptions,
  329. ] );
  330. }
  331. protected function getSizeOptions( $tmpNum, $tmpData ): string {
  332. $tmpRetVal = '';
  333. foreach ( $tmpData as $value ) {
  334. $id = $value['product_id'];
  335. if ( $id === $tmpNum ) {
  336. $cost = htmlentities( $value['cost'], ENT_QUOTES );
  337. $size = htmlentities( $value['size'], ENT_QUOTES );
  338. $tmpRetVal .= sprintf( '<option value="%s" title="%s">%s ($%s)</option>', $cost, $cost, $size, $cost );
  339. }
  340. }
  341. return $tmpRetVal;
  342. }
  343. #[Route('/specials', name: 'app_default_specials')]
  344. public function specialsAction(): Response {
  345. $specialMgr = new SpecialManager();
  346. $specials = null; //$specialMgr->getCurrentSpecials();
  347. return $this->render( 'specials.html.twig', [ 'specials' => $specials ] );
  348. }
  349. #[Route('/stores', name: 'app_default_stores')]
  350. public function storesAction(): Response {
  351. return $this->render( 'stores.html.twig' );
  352. }
  353. #[Route('/viewOrders', name: 'app_default_vieworders')]
  354. public function viewOrdersAction(ManagerRegistry $doctrine): Response {
  355. $em = $doctrine->getManager();
  356. $orders = $em->getRepository(OrderDetail::class)
  357. ->findBy( [], ['date' => 'DESC'], 100);
  358. return $this->render( 'viewOrders.html.twig', compact('orders') );
  359. }
  360. }