Advertisement
Guest User

Untitled

a guest
May 24th, 2015
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.22 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Database functions. You need to modify each of these to interact with the database and return appropriate results.
  4.  */
  5.  
  6. /**
  7.  * Connect to database
  8.  * This function does not need to be edited - just update config.ini with your own
  9.  * database connection details.
  10.  * @param string $file Location of configuration data
  11.  * @return PDO database object
  12.  * @throws exception
  13.  */
  14. function connect($file = 'config.ini') {
  15.     // read database seetings from config file
  16.     if ( !$settings = parse_ini_file($file, TRUE) )
  17.         throw new exception('Unable to open ' . $file);
  18.  
  19.     // parse contents of config.ini
  20.     $dns = $settings['database']['driver'] . ':' .
  21.             'host=' . $settings['database']['host'] .
  22.             ((!empty($settings['database']['port'])) ? (';port=' . $settings['database']['port']) : '') .
  23.             ';dbname=' . $settings['database']['schema'];
  24.     $user= $settings['db_user']['username'];
  25.     $pw  = $settings['db_user']['password'];
  26.  
  27.     // create new database connection
  28.     try {
  29.         $dbh=new PDO($dns, $user, $pw);
  30.         $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  31.     } catch (PDOException $e) {
  32.         print "Error Connecting to Database: " . $e->getMessage() . "<br/>";
  33.         die();
  34.     }
  35.     return $dbh;
  36. }
  37.  
  38. /**
  39.  * Check login details
  40.  * @param string $name Login name
  41.  * @param string $pass Password
  42.  * @return boolean True is login details are correct
  43.  */
  44. function checkLogin($name,$pass) {
  45.     $db = connect();
  46.     try {
  47.         $hashed = md5($pass);
  48.         $hashed = $pass;
  49.         $stmt = $db->prepare('SELECT password FROM PeerPark.Member WHERE email=:username');
  50.         $stmt->bindValue(':username', $name, PDO::PARAM_STR);
  51.         $stmt->execute();
  52.         $result = $stmt->fetchColumn();
  53.         $stmt->closeCursor();
  54.         if($result) {
  55.             if($result == $pass) {
  56.                 return True;
  57.             }
  58.         }
  59.         return False;
  60.  
  61.     } catch (PDOException $e) {
  62.         print "Error checking login: " . $e->getMessage();
  63.         return False;
  64.     }
  65.     return False;
  66. }
  67.  
  68. /**
  69.  * Get details of the current user
  70.  * @param string $user login name user
  71.  * @return array Details of user - see index.php
  72.  */
  73. function getUserDetails($user) {
  74.     // STUDENT TODO:
  75.     // JOIN WITH BAY AND BILLING FOR NAME.
  76.     // WHICH NAME?
  77.     // MEMBERNO DOESNT WORK?
  78.  
  79.     $db = connect();
  80.     try {
  81.     $stmt = $db->prepare("SELECT memberNo AS memberNo, nickName AS name,
  82.        adrStreetNo || ' ' ||  adrStreet || ', ' || adrCity AS address,
  83.        email,
  84.        prefBillingNo, 'lol' as prefBillingName,
  85.        prefBay, 'bay' as prefBayName,
  86.        stat_nrOfBookings AS nbookings
  87.        FROM PeerPark.Member
  88.        WHERE email=:username");
  89.     $stmt->bindValue(':username', $user, PDO::PARAM_STR);
  90.     $stmt->execute();
  91.     } catch (PDOException $e) {
  92.         print "Error in getting user details " . $e->getMessage();
  93.         return False;
  94.     }
  95.    
  96.  
  97.  
  98.     $results = $stmt->fetch();
  99.     // Example user data - this should come from a query
  100.    
  101.     //$results['memberNo'] = 32;
  102.     /*$results['name'] = $stmt->fetchColumn();
  103.     $results['address'] = $stmt->fetchColumn();
  104.     $results['email'] = $stmt->fetchColumn();
  105.     $results['prefBillingNo'] = $stmt->fetchColumn();
  106.     $results['prefBillingName'] = $stmt->fetchColumn();
  107.     $results['prefBay'] = $stmt->fetchColumn();
  108.     $results['prefBayName'] = $stmt->fetchColumn();
  109.     $results['nbookings'] = $stmt->fetchColumn();*/
  110.  
  111.     $stmt->closeCursor();
  112.  
  113.     return $results;
  114. }
  115.  
  116. /**
  117.  * Get list of bays with silimar address
  118.  * @param string $address address to be look up
  119.  * @return array Various details of each bay - see baylist.php
  120.  */
  121. function searchBay($address) {
  122.      // TODO: logic to figure out if available or not.
  123.     // TODO: For loop logic for all matches.
  124.     $db = connect();
  125.     $stmt = $db->prepare("SELECT
  126.        bayId, site, address, avail_wk_start, avail_wk_end, avail_wend_start, avail_wend_end
  127.        FROM PeerPark.ParkBay WHERE address LIKE %:search%");
  128.     $stmt->bindValue(':search', $address, PDO::PARAM_STR);
  129.     $stmt->execute();
  130.  
  131. $results = array(
  132.         array('bayID'=>954673, 'site'=> 'Sydney Uni Camp1', 'address'=> 'Search Add1', 'avail'=>true),
  133.         array('bayID'=>344578, 'site'=> 'Sydney Uni Camp2', 'address'=> 'Search Add2', 'avail'=>false)
  134.     );
  135.     return $results;
  136. }
  137.  
  138. /**
  139.  * Retrieve information of all bays
  140.   * @return array Various details of each bay - see baylist.php
  141.  * @throws Exception
  142.  */
  143.  
  144. function getBays() {
  145.     // Example booking info - this should come from a query. Format is
  146.     // (bay ID, site, address, availability of the bay)
  147.     // TODO: LOGIC For availability.
  148.     $db = connect();
  149.     try{
  150.         $stmt = $db->prepare("SELECT
  151.            bayId, site, address, avail_wk_start, avail_wk_end, avail_wend_start, avail_wend_end
  152.            FROM PeerPark.ParkBay");
  153.         $stmt->execute();
  154.  
  155.         // Fill up the array row by row.
  156.         $result = array();
  157.         while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  158.             array_push($result, array('bayID'=>$row['bayID'], 'site'=>$row['site'], 'address'=>$row['address'], 'avail'=>true));
  159.         }
  160.         $stmt->closeCursor();
  161.         return $result;
  162.     } catch (PDOException $e) {
  163.         print "Error checking login: " . $e->getMessage();
  164.         return False;
  165.     }
  166. }
  167.  
  168.  
  169. /**
  170.  * Retrieve information on bays
  171.  * @param string $memberNo ID of the member
  172.  * @return array  details of the member preferred bay - see baylist.php
  173.  * @throws Exception
  174.  */
  175.  
  176. function getPrefBayInformation($memberNo) {
  177.     // Replace lines below with code to get the information about the owner preferred bay from the database
  178.     // Example bay info - this should come from a query. Format is
  179.     // (bay ID, Owner, Latitude, Longitude, Address,  width, height, length, pod, site, week start, week end, weekend start, weekend end)
  180.     // TODO: Do we have to get owner name or just id?
  181.     // TODO: Join with Member for PrefBay.
  182.     $db = connect();
  183.     $stmt = $db->prepare("SELECT
  184.        bayId, owner, gps_lat, gps_long, address, width, height, length, pod, site, avail_wk_start, avail_wk_end, avail_wend_start, avail_wend_end
  185.        FROM PeerPark.ParkBay
  186.        WHERE bayID=0");
  187.     $stmt->execute();
  188.     $results = array(
  189.         array('bayID'=>896541, 'address'=> 'Glebe Point Road', 'site'=> 'Library', 'avail'=>true)
  190.     );
  191.     return $results;
  192. }
  193.  
  194.  
  195. /**
  196.  * Retrieve information on bays
  197.  * @param string $BayID ID of the bay
  198.  * @return array Various details of the bay - see baydetail.php
  199.  * @throws Exception
  200.  */
  201.  
  202. function getBayInformation($BayID) {
  203.     // STUDENT TODO:
  204.     // Replace lines below with code to get the information about a specific bay from the database
  205.     // Example bay info - this should come from a query. Format is
  206.     // (bay ID, Owner, Latitude, Longitude, Address,  width, height, length, pod, site, week start, week end, weekend start, weekend end)
  207.     $db = connect();
  208.     try {
  209.         $stmt = $db->prepare('SELECT * FROM PeerPark.Booking WHERE bookingID=:book');
  210.         $stmt->bindValue(':member', $memberNo, PDO::PARAM_STR);
  211.         $stmt->execute();
  212.         $stmt->closeCursor();
  213.         return
  214.         array('bayID'=>954673,'site'=>'Glebe Public School 1', 'owner'=>'Toni Collette', 'address'=> '25 Glebe Point Road','description'=>'Bay located next to Glebe Public School','gps_lat'=>456,'gps_long'=>5689,'locatedAt'=>19,'mapURL'=>'https://www.google.com.au/maps/place/Glebe+Public+School/@-33.8896527,151.186376,16z/data=!4m6!1m3!3m2!1s0x6b12b1d4ab9ef1d9:0x1d017d69037a07c0!2sThe+University+of+Sydney!3m1!1s0x0000000000000000:0x365d99076bb74a6d',
  215.         'width'=> 45,'height'=>56,'length'=>56,'pod'=>45,'avail_wk_start'=>9,'avail_wk_end'=>17,'avail_wend_start'=>2,'avail_wend_end'=>3);
  216.    
  217.  
  218.     } catch (PDOException $e) {
  219.         print "Error checking login: " . $e->getMessage();
  220.         return False;
  221.     }
  222.     return False;
  223. }
  224.  
  225. /**
  226.  * Retrieve information on active bookings for a member
  227.  * @param string $memberNo ID of member
  228.  * @return array Various details of each booking - see bookings.php
  229.  * @throws Exception
  230.  */
  231.  
  232. function getOpenBookings($memberNo) {
  233.     // Replace lines below with code to get list of bookings from the database
  234.     // Example booking info - this should come from a query. Format is
  235.     // (booking ID,  bay ID, Car Name, Booking start date, booking start time, booking duration)
  236.    
  237.     // TODO: query. get the bookings for a time past NOW owned by :member. Get the data as above.
  238.     // TODO: fill the array.
  239.     $db = connect();
  240.     try {
  241.         $stmt = $db->prepare('SELECT * FROM PeerPark.Booking WHERE bookingID=:book');
  242.         $stmt->bindValue(':member', $memberNo, PDO::PARAM_STR);
  243.         $stmt->execute();
  244.         $stmt->closeCursor();
  245.         $results = array(
  246.         array('bookingID'=>1,'bayLocation'=>'CBD','car'=>'Jenny the Yaris','bookingDate'=>'05/03/15' ),
  247.         array('bookingID'=>2,'bayLocation'=>'Glebe','car'=>'Garry the Getz','bookingDate'=>'11/04/15')
  248.         );
  249.         return $results;
  250.     } catch (PDOException $e) {
  251.         print "Error checking login: " . $e->getMessage();
  252.         return False;
  253.     }
  254.     return False;
  255.  
  256. }
  257.  
  258. /**
  259.  * Make a new booking for a bay
  260.  * @param string $memberNo Member booking the bay
  261.  * @param string $car         Name of the car
  262.  * @param string $bayID       ID of the bay to book
  263.  * @param string $bookingDate the date of the booking
  264.  * @param string $bookingHour the time of the booking
  265.  * @param string $duration    the duration of the booking
  266.  * @return array Various details of current visit - see newbooking.php
  267.  * @throws Exception
  268.  */
  269. function makeBooking($memberNo,$car,$bayID,$bookingDate,$bookingHour,$duration) {
  270.     //TODO: Query - needs to insert and to write.
  271.     // TODO: Fill array.
  272.     $db = connect();
  273.     try {
  274.         $stmt = $db->prepare('SELECT * FROM PeerPark.Booking WHERE bookingID=:book');
  275.         $stmt->bindValue(':book', $bookingID, PDO::PARAM_STR);
  276.         $stmt->execute();
  277.         $stmt->closeCursor();
  278.         return array(
  279.         'status'=>'success',
  280.         'bookingID'=>2,
  281.         'bayID'=>2,
  282.         'car'=>'Garry the Getz',
  283.         'bookingDate'=>'25/07/14',
  284.         'bookingHour'=>10,
  285.         'duration'=>1,
  286.         'cost'=>1000
  287.          );
  288.  
  289.     } catch (PDOException $e) {
  290.         print "Error checking login: " . $e->getMessage();
  291.         return False;
  292.     }
  293.    
  294. }
  295.  
  296. /**
  297.  * Retrieve information on the booking
  298.  * @param string $bookingID ID of the bay
  299.  * @return array Various details of the booking - see bookingDetail.php
  300.  * @throws Exception
  301.  */
  302. function getBookingInfo($bookingID) {
  303.     // Replace lines below with code to get the detail about the booking.
  304.     // Example booking info - this should come from a query. Format is
  305.     // (bookingID, bay Location, booking Date, booking Hour, duration, car, member Name)
  306.     // TODO: The query
  307.     // TODO: Fill the array.
  308.     $db = connect();
  309.     try {
  310.         $stmt = $db->prepare('SELECT * FROM PeerPark.Booking WHERE bookingID=:book');
  311.         $stmt->bindValue(':book', $bookingID, PDO::PARAM_STR);
  312.         $stmt->execute();
  313.         $stmt->closeCursor();
  314.         return array('bookingID'=>1, 'bayLocation'=>'CBD', 'bookingDate'=> '10/05/2015','bookingHour'=>'10:01','duration'=>2,'car'=> 'Harry the Goat','memberName'=>'Uwe');
  315.  
  316.     } catch (PDOException $e) {
  317.         print "Error checking login: " . $e->getMessage();
  318.         return False;
  319.     }
  320. }
  321.  
  322. /**
  323.  * Get details of the cars of the member
  324.  * @param string $user ID of member
  325.  * @return Name of the cars owned by the member - see index.php
  326.  */
  327. function getCars($email) {
  328.     $db = connect();
  329.     try {
  330.         $stmt = $db->prepare("SELECT name FROM PeerPark.Car
  331.            NATURAL JOIN PeerPark.Member
  332.            WHERE email=:email");
  333.         $stmt->bindValue(':email', $email, PDO::PARAM_INT);
  334.         $stmt->execute();
  335.         // Fill up the array row by row.
  336.         $result = array();
  337.         while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  338.             array_push($result, array('car'=>$row['name']));
  339.         }
  340.         $stmt->closeCursor();
  341.  
  342.         return $result;
  343.  
  344.     } catch (PDOException $e) {
  345.         print "Error getting cars";
  346.         return False;
  347.     }
  348.     return false;
  349. }
  350.  
  351. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement