PageRenderTime 56ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/functions.php

https://bitbucket.org/rkandpal/nustechgmgit
PHP | 396 lines | 289 code | 60 blank | 47 comment | 43 complexity | 8518addcbb9dc3ca631ed3c36fbb3522 MD5 | raw file
  1. <?php
  2. /*
  3. /* This is the functions file, do as much of the computing in this file
  4. /* file as possible so the content will be neater and therefore easier
  5. /* to change.
  6. */
  7. require_once('_inc.php');
  8. require_once('config.php');
  9. ?>
  10. <?php
  11. /* iniciate the page */
  12. function page_ini(){
  13. if(!isset($_GET['page'])):
  14. $_SESSION['home'] = true;
  15. //echo('Home is set to true');
  16. else:
  17. $_SESSION['page'] = $_GET['page'];
  18. $_SESSION['home'] = false;
  19. //echo('Page has been set to '.$_GET['page']);
  20. endif;
  21. }
  22. /* include the search module when ever get_search() is called */
  23. function get_search(){
  24. include('includes/product-search.php');
  25. }
  26. /* include the logo module when ever get_logo() is called */
  27. function get_logo(){
  28. include('includes/logo.php');
  29. }
  30. /* include the banner when ever get_banner() is called */
  31. function get_banner(){
  32. include('includes/banner.php');
  33. }
  34. /* include the browse module when ever get_browse() is called */
  35. function get_browse(){
  36. include('includes/browse.php');
  37. }
  38. /* include the header of the page*/
  39. function get_header(){
  40. include('user/_header.php');
  41. }
  42. function get_content(){
  43. include('includes/'.$_SESSION['page'].'.php');
  44. }
  45. function get_page_title(){
  46. $title = ($_SESSION['home'] == true)? '': str_replace('-', ' ', $_SESSION['page']);
  47. $title = ' | ' . ucwords($title);
  48. return $title;
  49. }
  50. function get_navigation()
  51. {
  52. ?>
  53. <ul class="toolbar-nav">
  54. <?php if (isset($_SESSION['login']) && $_SESSION['login'] == true):?>
  55. <li class="inline first">Hello <?php print_user_detail('name') ?></li>
  56. <?php endif; ?>
  57. <?php if (isset($_SESSION['login']) && $_SESSION['login'] == true): ?>
  58. <li class="inline"><a href="<?php echo SITE_URL ?>?action=logout">Logout</a>
  59. <?php else: ?>
  60. <li class="inline first"><a href="?page=login">Login</a>
  61. <?php endif; ?>
  62. </li>
  63. <li class="inline">
  64. <a href="?page=browse">Browse</a>
  65. </li>
  66. <li class="inline">
  67. <a href="?page=search">Search</a>
  68. </li>
  69. <?php if (isset($_SESSION['login']) && $_SESSION['login'] == true): ?>
  70. <li class="inline"><a href="?page=user-home">My Account</a>
  71. <?php endif; ?>
  72. </ul><!-- .toolbar-nav -->
  73. <?php
  74. }
  75. function LoadCurrentUserDetails() {
  76. static $current_details = null;
  77. if ($current_details === null) {
  78. $current_details = array('name' => '', 'email' => '', 'password' => '');
  79. if (isset($_SESSION['login']) && $_SESSION['login']) {
  80. if (isset($_SESSION['user_id']) && $_SESSION['user_id']) {
  81. $db_link = new MySQLi(DB_HOST, DB_USER, DB_PASS, DB_NAME);
  82. $query = "SELECT * FROM users WHERE id=" . intval($_SESSION['user_id']);
  83. $result = $db_link->query($query);
  84. if ($result->num_rows) {
  85. $row = $result->fetch_assoc();
  86. $current_details['name'] = $row['display_name'];
  87. $current_details['email'] = $row['email'];
  88. }
  89. }
  90. }
  91. }
  92. return $current_details;
  93. }
  94. /*
  95. /* get_user_detail($detail) returns the value of the field on the user and
  96. /* the detail ask for.
  97. */
  98. function get_user_detail($detail){
  99. $user = LoadCurrentUserDetails();
  100. return $user[$detail];
  101. }
  102. function print_user_detail($detail){
  103. $user = LoadCurrentUserDetails();
  104. echo $user[$detail];
  105. }
  106. /* get_shoppinglist_lists gets all the shopping list the user has made */
  107. function get_shoppinglist_list(){
  108. include('includes/shoppinglist_list.php');
  109. //return "Getting Shopping list list";
  110. }
  111. function get_mini_shopping_list(){
  112. include('includes/mini_shopping_list.php');
  113. }
  114. function do_action($action){
  115. switch($action){
  116. case 'logout':
  117. $_SESSION['login'] = false;
  118. break;
  119. }
  120. }
  121. function get_item_details($id){
  122. //connect to database, get details and return them in a object array[]
  123. }
  124. function get_last_id(){
  125. $db_link = new MySQLi(DB_HOST, DB_USER, DB_PASS, DB_NAME) or die('Unable to connect to database: '.$db_link->error);
  126. $make_new_list = $db_link->query("INSERT INTO shopping_lists (name, creation_time) VALUES ('temp', NOW())");
  127. return $db_link->insert_id;
  128. }
  129. function StartSession() {
  130. if (! isset($_SESSION)) {
  131. session_start();
  132. }
  133. }
  134. function get_average_savings(){
  135. $db_link = new MySQLi(DB_HOST, DB_USER, DB_PASS, DB_NAME);
  136. //$query = "select avg(total_rrp - total_special) AS average from shopping_lists;";
  137. $strShoppingListQuery = "select COUNT(*) as total_rows from shopping_lists";
  138. $strShoppingListQueryExe = mysql_query($strShoppingListQuery);
  139. $arrResultSet = mysql_fetch_array($strShoppingListQueryExe);
  140. $intTotalLists = $arrResultSet['total_rows'];
  141. $query = "select SUM(total_rrp) as total_price, SUM(total_special) as total_special_price from shopping_lists";
  142. $queryExe = mysql_query($query);
  143. $arrPriceesultSet = mysql_fetch_array($queryExe);
  144. /* echo "--".$arrPriceesultSet['total_price'];
  145. echo "--".$arrPriceesultSet['total_special_price']; */
  146. $floatSaving = (($arrPriceesultSet['total_price'])-($arrPriceesultSet['total_special_price']));
  147. $floatAvgSaving = number_format(($floatSaving / $intTotalLists),2);
  148. if($floatAvgSaving)
  149. {
  150. return $floatAvgSaving;
  151. }
  152. else
  153. {
  154. return "There was no average";
  155. }
  156. /* $result = $db_link->prepare($query);
  157. $result->bind_result($total_price, $total_special_price);
  158. $result->execute();
  159. //echo "---".$total_price;
  160. //echo "---".$total_special_price;
  161. if ($result->fetch())
  162. {
  163. //return number_format(floatval($average), 2);
  164. }
  165. else
  166. {
  167. return "There was no average";
  168. } */
  169. }
  170. function store_saving ($price, $special, $supermarket, $savings, $multiplier)
  171. {
  172. if (! isset($savings[$supermarket])) {
  173. $savings[$supermarket] = 0;
  174. }
  175. $savings[$supermarket] += ($multiplier * (floatval($price) - floatval($special)));
  176. return $savings;
  177. }
  178. function GetFormattedDBDate($the_date) {
  179. $formatted_date = '';
  180. if ($the_date && ($the_date != '0000-00-00')) {
  181. $date_parts = explode('-', $the_date);
  182. $formatted_date = ($date_parts[2] . '/' . $date_parts[1] . '/' . $date_parts[0]);
  183. }
  184. return $formatted_date;
  185. }
  186. function GetAislesWithNoProductTypes() {
  187. static $data = null;
  188. if ($data === null) {
  189. $data = array();
  190. $db_link = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
  191. $query = "SELECT aisle.id AS aisle_id
  192. FROM aisle
  193. LEFT JOIN product_types ON product_types.aisle_id=aisle.id
  194. WHERE product_types.id IS NULL";
  195. $result = $db_link->query($query);
  196. while ($row = $result->fetch_assoc()) {
  197. $data[intval($row['aisle_id'])] = true;
  198. }
  199. }
  200. return $data;
  201. }
  202. function GetDemographicsOfProductsForSale($filter_on_user_postcode = true) {
  203. static $data = null;
  204. global $user;
  205. if ($data === null) {
  206. $data = array();
  207. $data['products'] = array();
  208. $data['product_types'] = array();
  209. $data['aisles'] = array();
  210. $now_date_string = date('Y-m-d');
  211. $db_link = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
  212. $query = "SELECT products.id AS product_id,
  213. product_types.id AS product_type_id,
  214. aisle.id AS aisle_id
  215. FROM product_sales
  216. INNER JOIN products ON products.id=product_sales.product_id
  217. INNER JOIN product_types ON product_types.id=products.type
  218. INNER JOIN aisle ON aisle.id=product_types.aisle_id
  219. WHERE product_sales.start_date <= '$now_date_string'
  220. AND product_sales.end_date >= '$now_date_string'";
  221. if ($filter_on_user_postcode) {
  222. $gm_cookie = new GM_Cookie();
  223. $postcode = $gm_cookie->registered_postcode;
  224. if ($postcode && is_numeric($postcode)) {
  225. $state_code = substr($postcode, 0, 1);
  226. $state_code_minimum = ($state_code * 1000);
  227. $state_code_maximum = (($state_code * 1000) + 999);
  228. $query .= " AND product_sales.postcode >= $state_code_minimum AND product_sales.postcode <= $state_code_maximum";
  229. }
  230. }
  231. $result = $db_link->query($query);
  232. while ($row = $result->fetch_assoc()) {
  233. $product_id = intval($row['product_id']);
  234. $product_type_id = intval($row['product_type_id']);
  235. $aisle_id = intval($row['aisle_id']);
  236. if (! isset($data['products'][$product_id])) {
  237. $data['products'][$product_id] = true;
  238. }
  239. if (! isset($data['product_types'][$product_type_id])) {
  240. $data['product_types'][$product_type_id] = true;
  241. }
  242. if (! isset($data['aisles'][$aisle_id])) {
  243. $data['aisles'][$aisle_id] = true;
  244. }
  245. }
  246. }
  247. return $data;
  248. }
  249. class GM_Cookie {
  250. const NAME = 'gm_registered_data';
  251. var $the_original_cookie_string;
  252. var $has_visited_before;
  253. var $is_registered;
  254. var $registered_postcode;
  255. function __construct() {
  256. $this->the_original_cookie_string = '';
  257. $this->has_visited_before = false;
  258. $this->is_registered = false;
  259. $this->registered_postcode = '';
  260. $this->LoadFromCurrentCookie();
  261. }
  262. function LoadFromCurrentCookie() {
  263. global $user;
  264. $this->the_original_cookie_string = '';
  265. if (isset($_COOKIE)) {
  266. if (isset($_COOKIE[self::NAME])) {
  267. if ($_COOKIE[self::NAME]) {
  268. $the_cookie_string = $_COOKIE[self::NAME];
  269. $this->the_original_cookie_string = $the_cookie_string;
  270. $the_cookie_string_parts = explode('|', $the_cookie_string);
  271. if (count($the_cookie_string_parts) == 3) {
  272. $this->has_visited_before = ($the_cookie_string_parts[0] == '1');
  273. $this->is_registered = ($the_cookie_string_parts[1] == '1');
  274. $this->registered_postcode = $the_cookie_string_parts[2];
  275. }
  276. }
  277. }
  278. }
  279. // Now... there's something we might know other than the cookie.
  280. if ($user && $user->isAuthorized()) {
  281. $this->has_visited_before = true;
  282. $this->is_registered = true;
  283. $this->registered_postcode = ($user->getPostCode() ? $user->getPostCode() : '');
  284. }
  285. }
  286. function IsUserLoggedIn() {
  287. global $user;
  288. return ($user && $user->isAuthorized());
  289. }
  290. function WriteBackCookie() {
  291. // Maybe things have changed... we know the 'visited before' flag has, at least!
  292. $this->has_visited_before = true;
  293. $new_cookie_string = ($this->has_visited_before ? '1' : '0');
  294. $new_cookie_string .= '|' . ($this->is_registered ? '1' : '0');
  295. $new_cookie_string .= '|' . $this->registered_postcode;
  296. $_COOKIE[self::NAME] = $new_cookie_string;
  297. setcookie(self::NAME, $new_cookie_string, (time() + (60 * 60 * 24 * 365 * 10)));
  298. }
  299. }
  300. function RewriteSmartQuotes($the_string) {
  301. // Get the tricky encodings first...
  302. // from http://shiflett.org/blog/2005/oct/convert-smart-quotes-with-php
  303. // and http://digitalcolony.com/2007/07/replacing-the-extended-ascii-dash-in-c-and-sql/
  304. // http://www.fileformat.info/info/unicode/char/2795/index.htm: useful lookups
  305. $other_smart_quote_searches = array("\xe2\x80\x98", "\xe2\x80\x99", "\xe2\x80\x9c", "\xe2\x80\x9d", "\xe2\x80\x93", "\xe2\x80\x94", "\xe2\x80\xa6", "\xe2\x9e\x95");
  306. $other_smart_quote_replacements = array("'", "'", '"', '"', "-", "-", "...", "+");
  307. $the_string = str_replace($other_smart_quote_searches, $other_smart_quote_replacements, $the_string);
  308. // now convert a handful of other icky (read "smart-quote-type") characters...
  309. $smart_quote_character_code_replacements = array();
  310. $smart_quote_character_code_replacements[145] = "'";
  311. $smart_quote_character_code_replacements[213] = "'";
  312. $smart_quote_character_code_replacements[146] = "'";
  313. $smart_quote_character_code_replacements[147] = '"';
  314. $smart_quote_character_code_replacements[148] = '"';
  315. $smart_quote_character_code_replacements[150] = '-';
  316. $smart_quote_character_code_replacements[151] = '-';
  317. foreach ($smart_quote_character_code_replacements as $smart_quote_code => $smart_quote_replacement) {
  318. $the_string = str_replace(chr($smart_quote_code), $smart_quote_replacement, $the_string);
  319. }
  320. // Copied from http://forums.solmetra.com/viewtopic.php?f=1&t=1116
  321. // function fix_fancy_quotes($text) {
  322. $p = array("\xBB","\xAB","\xAA","\xD2","\x93","\x94","\x8D",
  323. "\xBA","\xD3","\x8E","\xD4","\x92","\x8F","\xD5",
  324. "\x90","\xD0","\xD1","\x97","\x84", "\x85");
  325. $r = array( ")" , "(" , '"' , '"' , '"' , '"' , '"' , '"' , '"' , '"' ,
  326. "'" , "'" , "'" , "'" , "'" , "-" , "-" , "-" , "-", "...");
  327. $the_string = str_replace($p,$r,$the_string);
  328. // }
  329. return $the_string;
  330. }
  331. ?>