PageRenderTime 45ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/functions.php

https://bitbucket.org/skurhade/nustechfork
PHP | 363 lines | 284 code | 51 blank | 28 comment | 43 complexity | e60b098863d406d54e0d50cf399adc34 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. $result = $db_link->prepare($query);
  138. $result->bind_result($average);
  139. $result->execute();
  140. if ($result->fetch())
  141. {
  142. return number_format(floatval($average), 2);
  143. }
  144. else
  145. {
  146. return "There was no average";
  147. }
  148. }
  149. function store_saving ($price, $special, $supermarket, $savings, $multiplier)
  150. {
  151. if (! isset($savings[$supermarket])) {
  152. $savings[$supermarket] = 0;
  153. }
  154. $savings[$supermarket] += ($multiplier * (floatval($price) - floatval($special)));
  155. return $savings;
  156. }
  157. function GetFormattedDBDate($the_date) {
  158. $formatted_date = '';
  159. if ($the_date && ($the_date != '0000-00-00')) {
  160. $date_parts = explode('-', $the_date);
  161. $formatted_date = ($date_parts[2] . '/' . $date_parts[1] . '/' . $date_parts[0]);
  162. }
  163. return $formatted_date;
  164. }
  165. function GetAislesWithNoProductTypes() {
  166. static $data = null;
  167. if ($data === null) {
  168. $data = array();
  169. $db_link = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
  170. $query = "SELECT aisle.id AS aisle_id
  171. FROM aisle
  172. LEFT JOIN product_types ON product_types.aisle_id=aisle.id
  173. WHERE product_types.id IS NULL";
  174. $result = $db_link->query($query);
  175. while ($row = $result->fetch_assoc()) {
  176. $data[intval($row['aisle_id'])] = true;
  177. }
  178. }
  179. return $data;
  180. }
  181. function GetDemographicsOfProductsForSale($filter_on_user_postcode = true) {
  182. static $data = null;
  183. global $user;
  184. if ($data === null) {
  185. $data = array();
  186. $data['products'] = array();
  187. $data['product_types'] = array();
  188. $data['aisles'] = array();
  189. $now_date_string = date('Y-m-d');
  190. $db_link = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
  191. $query = "SELECT products.id AS product_id,
  192. product_types.id AS product_type_id,
  193. aisle.id AS aisle_id
  194. FROM product_sales
  195. INNER JOIN products ON products.id=product_sales.product_id
  196. INNER JOIN product_types ON product_types.id=products.type
  197. INNER JOIN aisle ON aisle.id=product_types.aisle_id
  198. WHERE product_sales.start_date <= '$now_date_string'
  199. AND product_sales.end_date >= '$now_date_string'";
  200. if ($filter_on_user_postcode) {
  201. $gm_cookie = new GM_Cookie();
  202. $postcode = $gm_cookie->registered_postcode;
  203. if ($postcode && is_numeric($postcode)) {
  204. $state_code = substr($postcode, 0, 1);
  205. $state_code_minimum = ($state_code * 1000);
  206. $state_code_maximum = (($state_code * 1000) + 999);
  207. $query .= " AND product_sales.postcode >= $state_code_minimum AND product_sales.postcode <= $state_code_maximum";
  208. }
  209. }
  210. $result = $db_link->query($query);
  211. while ($row = $result->fetch_assoc()) {
  212. $product_id = intval($row['product_id']);
  213. $product_type_id = intval($row['product_type_id']);
  214. $aisle_id = intval($row['aisle_id']);
  215. if (! isset($data['products'][$product_id])) {
  216. $data['products'][$product_id] = true;
  217. }
  218. if (! isset($data['product_types'][$product_type_id])) {
  219. $data['product_types'][$product_type_id] = true;
  220. }
  221. if (! isset($data['aisles'][$aisle_id])) {
  222. $data['aisles'][$aisle_id] = true;
  223. }
  224. }
  225. }
  226. return $data;
  227. }
  228. class GM_Cookie {
  229. const NAME = 'gm_registered_data';
  230. var $the_original_cookie_string;
  231. var $has_visited_before;
  232. var $is_registered;
  233. var $registered_postcode;
  234. function __construct() {
  235. $this->the_original_cookie_string = '';
  236. $this->has_visited_before = false;
  237. $this->is_registered = false;
  238. $this->registered_postcode = '';
  239. $this->LoadFromCurrentCookie();
  240. }
  241. function LoadFromCurrentCookie() {
  242. global $user;
  243. $this->the_original_cookie_string = '';
  244. if (isset($_COOKIE)) {
  245. if (isset($_COOKIE[self::NAME])) {
  246. if ($_COOKIE[self::NAME]) {
  247. $the_cookie_string = $_COOKIE[self::NAME];
  248. $this->the_original_cookie_string = $the_cookie_string;
  249. $the_cookie_string_parts = explode('|', $the_cookie_string);
  250. if (count($the_cookie_string_parts) == 3) {
  251. $this->has_visited_before = ($the_cookie_string_parts[0] == '1');
  252. $this->is_registered = ($the_cookie_string_parts[1] == '1');
  253. $this->registered_postcode = $the_cookie_string_parts[2];
  254. }
  255. }
  256. }
  257. }
  258. // Now... there's something we might know other than the cookie.
  259. if ($user && $user->isAuthorized()) {
  260. $this->has_visited_before = true;
  261. $this->is_registered = true;
  262. $this->registered_postcode = ($user->getPostCode() ? $user->getPostCode() : '');
  263. }
  264. }
  265. function IsUserLoggedIn() {
  266. global $user;
  267. return ($user && $user->isAuthorized());
  268. }
  269. function WriteBackCookie() {
  270. // Maybe things have changed... we know the 'visited before' flag has, at least!
  271. $this->has_visited_before = true;
  272. $new_cookie_string = ($this->has_visited_before ? '1' : '0');
  273. $new_cookie_string .= '|' . ($this->is_registered ? '1' : '0');
  274. $new_cookie_string .= '|' . $this->registered_postcode;
  275. $_COOKIE[self::NAME] = $new_cookie_string;
  276. setcookie(self::NAME, $new_cookie_string, (time() + (60 * 60 * 24 * 365 * 10)));
  277. }
  278. }
  279. function RewriteSmartQuotes($the_string) {
  280. // Get the tricky encodings first...
  281. // from http://shiflett.org/blog/2005/oct/convert-smart-quotes-with-php
  282. // and http://digitalcolony.com/2007/07/replacing-the-extended-ascii-dash-in-c-and-sql/
  283. // http://www.fileformat.info/info/unicode/char/2795/index.htm: useful lookups
  284. $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");
  285. $other_smart_quote_replacements = array("'", "'", '"', '"', "-", "-", "...", "+");
  286. $the_string = str_replace($other_smart_quote_searches, $other_smart_quote_replacements, $the_string);
  287. // now convert a handful of other icky (read "smart-quote-type") characters...
  288. $smart_quote_character_code_replacements = array();
  289. $smart_quote_character_code_replacements[145] = "'";
  290. $smart_quote_character_code_replacements[213] = "'";
  291. $smart_quote_character_code_replacements[146] = "'";
  292. $smart_quote_character_code_replacements[147] = '"';
  293. $smart_quote_character_code_replacements[148] = '"';
  294. $smart_quote_character_code_replacements[150] = '-';
  295. $smart_quote_character_code_replacements[151] = '-';
  296. foreach ($smart_quote_character_code_replacements as $smart_quote_code => $smart_quote_replacement) {
  297. $the_string = str_replace(chr($smart_quote_code), $smart_quote_replacement, $the_string);
  298. }
  299. // Copied from http://forums.solmetra.com/viewtopic.php?f=1&t=1116
  300. // function fix_fancy_quotes($text) {
  301. $p = array("\xBB","\xAB","\xAA","\xD2","\x93","\x94","\x8D",
  302. "\xBA","\xD3","\x8E","\xD4","\x92","\x8F","\xD5",
  303. "\x90","\xD0","\xD1","\x97","\x84", "\x85");
  304. $r = array( ")" , "(" , '"' , '"' , '"' , '"' , '"' , '"' , '"' , '"' ,
  305. "'" , "'" , "'" , "'" , "'" , "-" , "-" , "-" , "-", "...");
  306. $the_string = str_replace($p,$r,$the_string);
  307. // }
  308. return $the_string;
  309. }
  310. ?>