PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/getProducts.php

https://bitbucket.org/rkandpal/nustechgmgitfork
PHP | 344 lines | 282 code | 43 blank | 19 comment | 33 complexity | f31ec09762fa1496f637c681be585aa8 MD5 | raw file
  1. <?php
  2. require_once(dirname(__FILE__).'/../functions.php');
  3. require_once(dirname(__FILE__).'/../config.php');
  4. StartSession();
  5. $arrSuperMarketDisplayDetail = array('2'=>array('pricelabel'=>'Norm Price'),'1'=>array('pricelabel'=>'Norm Price'),'3'=>array('pricelabel'=>'Norm Price'));
  6. /* print("<pre>");
  7. print_r($arrBrandDisplayDetail);
  8. exit; */
  9. $db_link = new MySQLi(DB_HOST, DB_USER, DB_PASS, DB_NAME)
  10. or die("There was an error connecting to the database: ".$db_link->error);
  11. $product_type_id = $_POST['productType'];
  12. $on_sale_only = (isset($_POST['onSaleOnly']) && $_POST['onSaleOnly']);
  13. $strFilterWith = $_POST['filterWith'];
  14. $strFilterOrder = $_POST['filterOrder'];
  15. $intAsileId = $_POST['asile'];
  16. $now_date_string = date('Y-m-d');
  17. if($product_type_id)
  18. {
  19. $query = "SELECT products.id, products.highlighted, product_sales.id, products.measure, products.uom, products.image,
  20. product_sales.price, product_sales.special_price, product_sales.end_date,
  21. brands.name, products.name, products.technical_name,
  22. product_types.name,
  23. supermarkets.name,supermarkets.id,
  24. aisle.id AS aisle_id, aisle_name
  25. FROM products, product_sales, brands, product_types, supermarkets, aisle
  26. WHERE products.type = $product_type_id AND
  27. products.brand = brands.id AND
  28. product_sales.product_id = products.id AND
  29. products.type = product_types.id AND
  30. product_types.aisle_id = aisle.id AND
  31. product_sales.supermarket_id = supermarkets.id";
  32. }
  33. if($intAsileId)
  34. {
  35. $query = "SELECT products.id, products.highlighted, product_sales.id, products.measure, products.uom, products.image,
  36. product_sales.price, product_sales.special_price, product_sales.end_date,
  37. brands.name, products.name, products.technical_name,
  38. product_types.name,
  39. supermarkets.name,supermarkets.id,
  40. aisle.id AS aisle_id, aisle_name
  41. FROM products, product_sales, brands, product_types, supermarkets, aisle
  42. WHERE aisle.id = $intAsileId AND
  43. products.brand = brands.id AND
  44. product_sales.product_id = products.id AND
  45. products.type = product_types.id AND
  46. product_types.aisle_id = aisle.id AND
  47. product_sales.supermarket_id = supermarkets.id";
  48. }
  49. if ($on_sale_only) {
  50. $query .= " AND product_sales.start_date<='$now_date_string' AND product_sales.end_date>='$now_date_string'";
  51. }
  52. if (true /*$filter_on_user_postcode*/) {
  53. $gm_cookie = new GM_Cookie();
  54. if($user->uid)
  55. {
  56. $postcode = $user->get_meta('post_code');
  57. } else {
  58. $postcode = $gm_cookie->registered_postcode;
  59. }
  60. if ($postcode && is_numeric($postcode)) {
  61. $state_code = substr($postcode, 0, 1);
  62. $state_code_minimum = ($state_code * 1000);
  63. $state_code_maximum = (($state_code * 1000) + 999);
  64. $query .= " AND product_sales.postcode >= $state_code_minimum AND product_sales.postcode <= $state_code_maximum";
  65. } else {
  66. $query .= " GROUP BY products.id, supermarkets.name";
  67. }
  68. // error_log($query);
  69. }
  70. if($strFilterWith)
  71. {
  72. switch($strFilterWith)
  73. {
  74. case"price":
  75. $query .= " ORDER BY product_sales.price";
  76. break;
  77. case"unit_price": $query .= " ORDER BY product_sales.unit_price";
  78. break;
  79. case"popular": $query .= " ORDER BY products.product_sale_cnt";
  80. break;
  81. case"a-z":
  82. $query .= " ORDER BY products.name";
  83. break;
  84. }
  85. }
  86. if($strFilterOrder)
  87. {
  88. switch($strFilterOrder)
  89. {
  90. case"low_to_high":
  91. $query .= " ASC";
  92. break;
  93. case"high_to_low":
  94. $query .= " DESC";
  95. break;
  96. }
  97. }
  98. //echo "--".$query;
  99. $results = $db_link->prepare($query);
  100. $results->bind_result($id, $highlighted, $sale_id, $measure, $uom, $image, $price, $special, $sale_end_date, $brand, $name, $technical_name, $product_type, $supermarket, $supermarket_id, $aisle_id, $aisle_name);
  101. $results->execute();
  102. $results->store_result();
  103. $row_cnt = $results->num_rows;
  104. $odd_even_highlight = "odd";
  105. $odd_even = "odd";
  106. $output = array();
  107. //echo $row_cnt;
  108. if(empty($row_cnt)):
  109. $output['result'] = false;
  110. $output['html'] = '<p>Sorry we cant find any products on special in this Aisle.</p>';
  111. $output['header'] = '<p></p>';
  112. $output['ishighlightHtml'] = false;
  113. else :
  114. $output['result'] = true;
  115. $output['highlighthtml'] = '';
  116. $output['html'] = '';
  117. $first_row = true;
  118. $strHighlightedClass = "";
  119. while($results->fetch())
  120. {
  121. $strHighlightedClass = "";
  122. $product_image = ($image != '')?$image:"default_product.jpg";
  123. if ($first_row) {
  124. $output['html'] .= '<div class="hidden" id="product-type">'.ucwords($aisle_name).' &gt; '.ucwords($product_type).'</div>';
  125. $output['highlighthtml'] .= '<div class="hidden" id="product-type">'.ucwords($aisle_name).' &gt; '.ucwords($product_type).'</div>';
  126. $first_row = false;
  127. }
  128. //get image size
  129. $image_size = array();
  130. $image_width = "";
  131. $image_height = "";
  132. if(file_exists(dirname(__FILE__).'/../images/'.$product_image))
  133. {
  134. $image_size = getimagesize(dirname(__FILE__).'/../images/'.$product_image);
  135. $image_width = $image_size[0];
  136. $image_height = $image_size[1];
  137. }
  138. if($highlighted)
  139. {
  140. $output['ishighlightHtml'] = true;
  141. $strHighlightedClass = "highlighted";
  142. $output['highlighthtml'] .= '<div class="product rounded-corners'.$strHighlightedClass.'" style="width:96%;border: 5px solid red;margin-bottom:5px;">';
  143. $output['highlighthtml'] .= '<table cellpadding="0" cellspacing="0" width="55%" style="margin-left:23%;">';
  144. $output['highlighthtml'] .= ' <tr>';
  145. $output['highlighthtml'] .= ' <td class="image"><img class="product-thumb rounded-corners" src="images/'.$product_image.'" style="vertical-align:top"';
  146. $output['highlighthtml'] .= ($image_size[0] > $image_size[1])? 'width="130"' : 'height=""';
  147. $output['highlighthtml'] .= ' /></td>';
  148. $output['highlighthtml'] .= ' <td class="product-data">';
  149. $output['highlighthtml'] .= ' <table cellpadding="0" cellspacing="0" width="100%">';
  150. $output['highlighthtml'] .= ' <tr><td class="product-name">'.ucwords($brand . ' ' . ($name ? $name : $technical_name)).'</td>';
  151. $output['highlighthtml'] .= ' <tr><td class="weight">'.round($measure,2) . ' ' . $uom .'</td></tr>';
  152. $output['highlighthtml'] .= fnPrintProductPrice($supermarket_id,$price);
  153. if($price && round($measure,2))
  154. {
  155. $output['highlighthtml'] .= fnCalculateUnitPrice($price,round($measure,2),$uom);
  156. }
  157. //$output['html'] .= ' <tr><td class="price">RRP: $' . $price . '</td></tr>';
  158. if($special)
  159. {
  160. $output['highlighthtml'] .= ' <tr><td class="special">Special: $'.$special.'</td></tr>';
  161. $output['highlighthtml'] .= fnCalculateSavingsOnThisItem($price,$special);
  162. }
  163. /*$formatted_sale_date = GetFormattedDBDate($sale_end_date);
  164. if ($formatted_sale_date) {
  165. $output['html'] .= ' <tr><td class="sale-date">On sale until ' . $formatted_sale_date . '</td></tr>';
  166. }*/
  167. $output['highlighthtml'] .= '</table>';
  168. $output['highlighthtml'] .= ' </td>';
  169. $output['highlighthtml'] .= ' </tr>';
  170. $output['highlighthtml'] .= ' <tr>';
  171. $output['highlighthtml'] .= ' <td class="supermarket"><img src="images/supermarket/'.strtolower(str_replace(' ', '_',$supermarket)).'.png" width="80"/></td>';
  172. $output['highlighthtml'] .= ' <td class="forms"><div class="product-form">';
  173. $output['highlighthtml'] .= ' <form class="view-product" name="view-product" id="view-'.$id.'" method="get" action="product.php">';
  174. $output['highlighthtml'] .= ' <input type="submit" class="rounded-corners" name="view" id="view-'.$id.'" value="VIEW" />';
  175. $output['highlighthtml'] .= ' <input type="hidden" name="pid" value="'.$id.'" />';
  176. $output['highlighthtml'] .= ' </form>';
  177. $output['highlighthtml'] .= ' <form class="add-to-basket" name="add-to-basket" onsubmit="javascript:addToBasket('.$sale_id.'); return false;">';
  178. $output['highlighthtml'] .= ' <input type="submit" class="rounded-corners" id="add" name="add" value="ADD" />';
  179. $output['highlighthtml'] .= ' <input type="hidden" name="product_id" id="product_id" value="'.$sale_id.'" />';
  180. $output['highlighthtml'] .= ' </form>';
  181. $output['highlighthtml'] .= ' </div>';
  182. $output['highlighthtml'] .= ' </td>';
  183. $output['highlighthtml'] .= ' </tr>';
  184. $output['highlighthtml'] .= '</table>';
  185. $output['highlighthtml'] .= '</div>';
  186. $_SESSION['url'] = $_POST['url'];
  187. $hash_url = '';
  188. if($product_type_id)
  189. {
  190. $hash_url = ('#' . $aisle_id."-".$product_type_id."-"."1");
  191. //$hash_url .= '/' . preg_replace('/[^a-z0-9]/i', '-', $aisle_name);
  192. $hash_url .= '/' . preg_replace('/[^a-z0-9]/i', '-', $aisle_name);
  193. }
  194. $output['parent_hash_url'] = $hash_url;
  195. $odd_even_highlight = ($odd_even_highlight == "odd")? "even" : "odd";
  196. }
  197. else
  198. {
  199. $output['html'] .= '<div class="product rounded-corners '.$odd_even.' '.$strHighlightedClass.'">';
  200. $output['html'] .= '<table cellpadding="0" cellspacing="0" width="100%">';
  201. $output['html'] .= ' <tr>';
  202. $output['html'] .= ' <td class="image"><img class="product-thumb rounded-corners" src="images/'.$product_image.'" style="vertical-align:top"';
  203. $output['html'] .= ($image_size[0] > $image_size[1])? 'width="130"' : 'height=""';
  204. $output['html'] .= ' /></td>';
  205. $output['html'] .= ' <td class="product-data">';
  206. $output['html'] .= ' <table cellpadding="0" cellspacing="0" width="100%">';
  207. $output['html'] .= ' <tr><td class="product-name">'.ucwords($brand . ' ' . ($name ? $name : $technical_name)).'</td>';
  208. $output['html'] .= ' <tr><td class="weight">'.round($measure,2) . ' ' . $uom .'</td></tr>';
  209. $output['html'] .= fnPrintProductPrice($supermarket_id,$price);
  210. if($price && round($measure,2))
  211. {
  212. $output['html'] .= fnCalculateUnitPrice($price,round($measure,2),$uom);
  213. }
  214. //$output['html'] .= ' <tr><td class="price">RRP: $' . $price . '</td></tr>';
  215. if($special)
  216. {
  217. $output['html'] .= ' <tr><td class="special">Special: $'.$special.'</td></tr>';
  218. $output['html'] .= fnCalculateSavingsOnThisItem($price,$special);
  219. }
  220. /*$formatted_sale_date = GetFormattedDBDate($sale_end_date);
  221. if ($formatted_sale_date) {
  222. $output['html'] .= ' <tr><td class="sale-date">On sale until ' . $formatted_sale_date . '</td></tr>';
  223. }*/
  224. $output['html'] .= '</table>';
  225. $output['html'] .= ' </td>';
  226. $output['html'] .= ' </tr>';
  227. $output['html'] .= ' <tr>';
  228. $output['html'] .= ' <td class="supermarket"><img src="images/supermarket/'.strtolower(str_replace(' ', '_',$supermarket)).'.png" width="80"/></td>';
  229. $output['html'] .= ' <td class="forms"><div class="product-form">';
  230. $output['html'] .= ' <form class="view-product" name="view-product" id="view-'.$id.'" method="get" action="product.php">';
  231. $output['html'] .= ' <input type="submit" class="rounded-corners" name="view" id="view-'.$id.'" value="VIEW" />';
  232. $output['html'] .= ' <input type="hidden" name="pid" value="'.$id.'" />';
  233. $output['html'] .= ' </form>';
  234. $output['html'] .= ' <form class="add-to-basket" name="add-to-basket" onsubmit="javascript:addToBasket('.$sale_id.'); return false;">';
  235. $output['html'] .= ' <input type="submit" class="rounded-corners" id="add" name="add" value="ADD" />';
  236. $output['html'] .= ' <input type="hidden" name="product_id" id="product_id" value="'.$sale_id.'" />';
  237. $output['html'] .= ' </form>';
  238. $output['html'] .= ' </div>';
  239. $output['html'] .= ' </td>';
  240. $output['html'] .= ' </tr>';
  241. $output['html'] .= '</table>';
  242. $output['html'] .= '</div>';
  243. $_SESSION['url'] = $_POST['url'];
  244. $hash_url = '';
  245. if($product_type_id)
  246. {
  247. $hash_url = ('#' .$aisle_id."-".$product_type_id."-"."1");
  248. //$hash_url .= '/' . preg_replace('/[^a-z0-9]/i', '-', $aisle_name);
  249. $hash_url .= '/' . preg_replace('/[^a-z0-9]/i', '-', $aisle_name);
  250. }
  251. $output['parent_hash_url'] = $hash_url;
  252. $odd_even = ($odd_even == "odd")? "even" : "odd";
  253. }
  254. }
  255. endif;
  256. echo json_encode($output);
  257. function fnCalculateUnitPrice($intActualPrice = "", $intTotalQty = "", $strMeasuringUnit = "")
  258. {
  259. $strUnitPriceHtml = "";
  260. $intUnitPriceAmount = "";
  261. if($intActualPrice && $intTotalQty)
  262. {
  263. $intUnitPriceAmount = round(($intActualPrice / $intTotalQty),2);
  264. }
  265. $strUnitPriceHtml = '<tr><td>Unit Price: $' . $intUnitPriceAmount . '/'.$strMeasuringUnit.'</td></tr>';
  266. return $strUnitPriceHtml;
  267. }
  268. function fnCalculateSavingsOnThisItem($intNormalPrice = "", $intSpecialPrice = "")
  269. {
  270. $intSavingAmount = "";
  271. $intSavingAmountHtml = "";
  272. if($intSpecialPrice)
  273. {
  274. $intSavingAmount = ($intNormalPrice - $intSpecialPrice);
  275. }
  276. $intSavingAmountHtml = '<tr><td style="color:green;">Savings: $' . $intSavingAmount . '</td></tr>';
  277. return $intSavingAmountHtml;
  278. }
  279. function fnPrintProductPrice($intSuperMarketIdIs = "", $intAmnt = "")
  280. {
  281. global $arrSuperMarketDisplayDetail;
  282. $strProductPriceHtml = "";
  283. if($intSuperMarketIdIs)
  284. {
  285. $arrSuperMarkets = array_keys($arrSuperMarketDisplayDetail);
  286. if(in_array($intSuperMarketIdIs,$arrSuperMarkets))
  287. {
  288. $strProductPriceHtml = '<tr><td class="price">'.$arrSuperMarketDisplayDetail[$intSuperMarketIdIs]['pricelabel'].': $' . $intAmnt . '</td></tr>';
  289. }
  290. else
  291. {
  292. $strProductPriceHtml = '<tr><td class="price">RRP: $' . $intAmnt . '</td></tr>';
  293. }
  294. }
  295. else
  296. {
  297. $strProductPriceHtml = '<tr><td class="price">RRP: $' . $intAmnt . '</td></tr>';
  298. }
  299. return $strProductPriceHtml;
  300. }
  301. ?>