PageRenderTime 27ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/main_list_page.php

https://bitbucket.org/rkandpal/nustechgmgitfork
PHP | 246 lines | 220 code | 20 blank | 6 comment | 31 complexity | 82a0b83645ee43355ad5fdaac87ffb24 MD5 | raw file
  1. <?php
  2. require_once('functions.php');
  3. require_once('config.php');
  4. require_once('_inc.php');
  5. StartSession();
  6. $db_link = new MySQLi(DB_HOST, DB_USER, DB_PASS, DB_NAME);
  7. $current_list = (isset($_SESSION['list_id']) ? $_SESSION['list_id'] : get_last_id());
  8. if(isset($_GET['list_archive']) && $_GET['list_archive'] != '')
  9. {
  10. $list_archive = intval($_GET['list_archive']);
  11. }
  12. //echo $list_archive;
  13. if($list_archive):
  14. $query = "SELECT shopping_lists_products.shopping_list_product_sale_id, shopping_lists_products.shopping_list_product_count, shopping_lists_products.price, shopping_lists_products.special_price, products.name, supermarkets.name
  15. FROM products, product_sales, shopping_lists_products, supermarkets
  16. WHERE shopping_lists_products.shopping_list_id = $list_archive AND
  17. product_sales.id = shopping_lists_products.shopping_list_product_sale_id AND
  18. products.id = shopping_lists_products.shopping_list_product_id AND
  19. shopping_lists_products.supermarket_id = supermarkets.id";
  20. $list_details = "SELECT name, date FROM shopping_lists WHERE id = $list_archive;";
  21. else:
  22. $query = "SELECT shopping_lists_products.shopping_list_product_sale_id, shopping_lists_products.shopping_list_product_count,
  23. product_sales.price, product_sales.special_price,
  24. products.name,
  25. supermarkets.name
  26. FROM products, product_sales, shopping_lists_products, supermarkets
  27. WHERE shopping_list_id = $current_list AND
  28. product_sales.id = shopping_lists_products.shopping_list_product_sale_id AND
  29. products.id = shopping_lists_products.shopping_list_product_id AND
  30. supermarkets.id = product_sales.supermarket_id";
  31. $query .= (isset($_GET['supermarket']) && $_GET['supermarket'] != '')? " AND supermarkets.name = '".$_GET['supermarket']."'" : "";
  32. $query .= " ORDER BY supermarkets.name ASC";
  33. $list_details = "SELECT name, date FROM shopping_lists WHERE id = $current_list;";
  34. endif;
  35. $results = $db_link->prepare($query);
  36. $results->bind_result($id, $product_count, $price, $special, $product_name, $supermarket);
  37. $results->execute();
  38. $results->store_result();
  39. $list_details_result = $db_link->prepare($list_details);
  40. $list_details_result->bind_result($list_name, $list_date);
  41. $list_details_result->execute();
  42. $list_details_result->store_result();
  43. $supermarkets = $db_link->prepare("SELECT name FROM supermarkets");
  44. $supermarkets->bind_result($name);
  45. $supermarkets->execute();
  46. $supermarkets->store_result();
  47. $total_price = 0;
  48. $total_special = 0;
  49. $savings = array();
  50. ?>
  51. <?php require_once '_header.php'; ?>
  52. <div id="shopping-list-container">
  53. <div id="big_shopping_list" class="rounded-corners">
  54. <input type="button" id="big_shopping_list_back_button" value="Back" onclick="javascript: window.history.back(); return false;" />
  55. <div id="save-message"></div>
  56. <form action="javascript:saveShoppingList();" method="post" name="shoppinglist" id="shoppinglist-list-form">
  57. <fieldset>
  58. <legend class="form-title top-rounded-corners cart">SHOPPING LIST
  59. <?php
  60. if ($list_archive) {
  61. $list_details_result->fetch();
  62. echo ($list_name != 'temp' && $list_name != '')? ' - <b>'.$list_name.'</b>' : '';
  63. echo ' ';
  64. echo ($list_date != '0000-00-00')? '('.date("d/m/Y", strtotime($list_date)).')' : '';
  65. }
  66. ?>
  67. </legend>
  68. <table width="99%" cellpadding="0" cellspacing="0" align="center" class="rounded-corners">
  69. <thead>
  70. <tr>
  71. <th>Item</th>
  72. <th>RRP</th>
  73. <th>Special</th>
  74. <th>Available At</th>
  75. <th>List</th>
  76. <th>Savings</th>
  77. <th class="for-print">Bought</th>
  78. </tr>
  79. </thead>
  80. <tbody>
  81. <?php
  82. if($results->num_rows > 0):
  83. while($results->fetch()):
  84. //Add values for each supermarket
  85. $savings = store_saving($price, $special, $supermarket, $savings, $product_count);
  86. $amount_saved = number_format((($price - $special) * $product_count), 2);
  87. if (! $list_archive) {
  88. $dropdown_html = '<select name="product_count[' . $id . ']" id="product_count-' . $id . '" onchange="javascript:ChangeBigBasketProductCount(' . $id . ');">';
  89. for ($i = 1; ($i <= 20); $i++) {
  90. $dropdown_html .= '<option value="' . $i . '"';
  91. if ($i == $product_count) {
  92. $dropdown_html .= ' selected';
  93. }
  94. $dropdown_html .= '>' . $i . '</option>';
  95. }
  96. $dropdown_html .= '</select>';
  97. } else {
  98. $dropdown_html = $product_count;
  99. }
  100. ?>
  101. <tr>
  102. <td align="left"><?php echo $dropdown_html . 'x ' . $product_name; ?></td>
  103. <td align="center">$<?php echo $price; ?></td>
  104. <td align="center">$<?php echo $special; ?></td>
  105. <td align="center" width="120"><?php echo $supermarket ?></td>
  106. <td align="center"><button type="button" name="product[]" id="<?php echo $id; ?>" class="delete-product-button" onclick="javascript:removeFromBigBasket(<?php echo $id ?>);"><img src="images/checkbox-x.png" width="10" height="10" /></button></td>
  107. <td align="center">$<?php echo $amount_saved; ?></td>
  108. <td class="for-print" valign="middle" align="center"><img src="images/tickbox.jpg" /></td>
  109. </tr>
  110. <?php
  111. $total_price += ($product_count * $price);
  112. $total_special += ($product_count * $special);
  113. endwhile;
  114. else:
  115. ?>
  116. <tr><td colspan="6" align="center" style="padding:15px 0;">There are no items in your basket</td></tr>
  117. <?php
  118. endif;
  119. ?>
  120. </tbody>
  121. <tfoot>
  122. <?php if ($results->num_rows > 0): ?>
  123. <tr>
  124. <td align="center" class="border-top">Total</td>
  125. <td align="center" class="border-top">$<span id="total"><?php echo number_format($total_price, 2) ?></span></td>
  126. <td align="center" class="border-top">$<span id="special-total"><?php echo number_format($total_special, 2) ?></span></td>
  127. <td align="center" class="border-top">&nbsp;</td>
  128. <td align="center" class="border-top">&nbsp;</td>
  129. <td align="center" class="border-top">&nbsp;</td>
  130. </tr>
  131. <?php endif; ?>
  132. <tr>
  133. <td colspan="6" >
  134. <table width="100%">
  135. <?php foreach ($savings as $supermarket_name => $value): ?>
  136. <tr>
  137. <td class="no-border" align="right" width="50%"><img src="images/supermarket/<?php echo strtolower($supermarket_name); ?>.png" /></td>
  138. <td class="no-border" align="left">savings of <span class="green">&nbsp;$<?php echo number_format($value, 2) ?></span>
  139. </td>
  140. </tr>
  141. <?php endforeach; ?>
  142. </table>
  143. </td>
  144. </tr>
  145. <?php if(!$list_archive): ?>
  146. <tr>
  147. <td colspan="1" class="border-top select" align="left">
  148. <p>Select a supermarket <select id="select-supermarket">
  149. <option value="">----</option>
  150. <option value="">All</option>
  151. <?php
  152. while($supermarkets->fetch()):
  153. ?>
  154. <option value="<?php echo $name ?>" <?php echo (isset($_GET['supermarket']) && $name == $_GET['supermarket'])? "selected" : ""; ?>><?php echo $name ?></option>
  155. <?php endwhile; ?> </select></p>
  156. </td>
  157. <td colspan="5" class="border-top buttons">
  158. <p>
  159. <?php
  160. $suggested_list_name = '';
  161. if (! $list_archive) {
  162. if (($list_name != 'temp') && ($list_name != '')) {
  163. $suggested_list_name = $list_name;
  164. } else if (isset($_SESSION['list_last_saved_name']) && $_SESSION['list_last_saved_name']) {
  165. $suggested_list_name = $_SESSION['list_last_saved_name'];
  166. }
  167. }
  168. ?>
  169. <label for="shopping-list-name-input">Shopping List Name:</label><input type="text" id="shopping-list-name-input" name="shopping-list-name-input" value="<?php echo htmlentities($suggested_list_name); ?>" />
  170. <input type="hidden" id="user-id-input" name="user-id-input" value="<?php echo $user->getId();?>" />
  171. <input type="submit" id="save" name="save" value="SAVE" />
  172. <input type="button" id="print" name="print" value="PRINT" onclick="javascript: window.print(); return false;" />
  173. </p>
  174. </td>
  175. </tr>
  176. <?php else: ?>
  177. <tr>
  178. <td colspan="3" class="border-top select">&nbsp;
  179. </td>
  180. <td colspan="3" class="border-top buttons">
  181. <input type="button" id="delete" name="delete" value="DELETE" onClick="javascript:deleteShoppingList(<?php echo $list_archive; ?>); return false;" />
  182. <input type="button" id="find-sales-again" name="find-sales-again" value="FIND SALES AGAIN" onClick="javascript:findSalesAgain(<?php echo $list_archive; ?>); return false;" />
  183. </td>
  184. </tr>
  185. <?php endif; ?>
  186. </tfoot>
  187. </table>
  188. </fieldset>
  189. </form>
  190. <script>
  191. // Basically, if we are going to overwrite a shopping list with the same date, that isn't the
  192. // last one we saved, we should confirm the overwrite...
  193. shopping_lists_to_confirm_overwrite = [];
  194. <?php
  195. // The 'date' of the list is just the date that it was saved? So for now, let's just look at the last 7 days...
  196. $overwrite_lists_query = "SELECT name
  197. FROM shopping_lists
  198. WHERE `date`>=DATE_SUB(CURDATE(), INTERVAL 7 DAY)
  199. AND user_id=" . intval($user->getId()) . "
  200. ORDER BY creation_time DESC";
  201. $overwrite_lists_result = $db_link->query($overwrite_lists_query);
  202. $lists_to_confirm = array();
  203. // We don't confirm overwriting the last list...
  204. $has_skipped_last = false;
  205. while ($overwrite_lists_row = $overwrite_lists_result->fetch_assoc()) {
  206. if (! $has_skipped_last) {
  207. $has_skipped_last = true;
  208. continue;
  209. }
  210. $overwrite_list_name = $overwrite_lists_row['name'];
  211. $overwrite_list_name = strtolower($overwrite_list_name);
  212. $overwrite_list_name = trim($overwrite_list_name);
  213. $overwrite_list_name = str_ireplace("\\", "\\\\", $overwrite_list_name);
  214. $overwrite_list_name = str_ireplace("'", "\\'", $overwrite_list_name);
  215. $overwrite_list_name = "'" . $overwrite_list_name . "'";
  216. if (! in_array($overwrite_list_name, $lists_to_confirm)) {
  217. $lists_to_confirm[] = $overwrite_list_name;
  218. }
  219. }
  220. ?>
  221. shopping_lists_to_confirm_overwrite = [<?php echo implode(', ', $lists_to_confirm); ?>];
  222. </script>
  223. </div>
  224. </div>
  225. <?php require_once '_footer.php'; ?>