Advertisement
Guest User

Untitled

a guest
May 26th, 2015
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.69 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. // FINISHED.
  45. function checkLogin($name,$pass) {
  46.     $db = connect();
  47.     try {
  48.         $hashed = md5($pass);
  49.         $hashed = $pass;
  50.         $stmt = $db->prepare('SELECT password FROM PeerPark.Member WHERE email=:username');
  51.         $stmt->bindValue(':username', $name, PDO::PARAM_STR);
  52.         $stmt->execute();
  53.         $result = $stmt->fetchColumn();
  54.         $stmt->closeCursor();
  55.         if($result) {
  56.             if($result == $pass) {
  57.                 return True;
  58.             }
  59.         }
  60.         return False;
  61.  
  62.     } catch (PDOException $e) {
  63.         print "Error checking login: " . $e->getMessage();
  64.         return False;
  65.     }
  66.     return False;
  67. }
  68.  
  69. /**
  70.  * Get details of the current user
  71.  * @param string $user login name user
  72.  * @return array Details of user - see index.php
  73.  */
  74. function getUserDetails($user) {
  75.     // HALF DONE
  76.     // TODO:
  77.     // JOIN WITH BAY AND BILLING FOR NAME.
  78.     // WHICH NAME?
  79.     // MEMBERNO DOESNT WORK?
  80.  
  81.     $db = connect();
  82.     try {
  83.     $stmt = $db->prepare("SELECT memberNo AS memberNo, nickName AS name,
  84.        adrStreetNo || ' ' ||  adrStreet || ', ' || adrCity AS address,
  85.        email,
  86.        prefBillingNo, 'lol' as prefBillingName,
  87.        prefBay, 'bay' as prefBayName,
  88.        stat_nrOfBookings AS nbookings
  89.        FROM PeerPark.Member
  90.        WHERE email=:username");
  91.     $stmt->bindValue(':username', $user, PDO::PARAM_STR);
  92.     $stmt->execute();
  93.     } catch (PDOException $e) {
  94.         print "Error in getting user details " . $e->getMessage();
  95.         return False;
  96.     }
  97.    
  98.  
  99.  
  100.     $results = $stmt->fetch();
  101.     // Example user data - this should come from a query
  102.    
  103.     //$results['memberNo'] = 32;
  104.     /*$results['name'] = $stmt->fetchColumn();
  105.     $results['address'] = $stmt->fetchColumn();
  106.     $results['email'] = $stmt->fetchColumn();
  107.     $results['prefBillingNo'] = $stmt->fetchColumn();
  108.     $results['prefBillingName'] = $stmt->fetchColumn();
  109.     $results['prefBay'] = $stmt->fetchColumn();
  110.     $results['prefBayName'] = $stmt->fetchColumn();
  111.     $results['nbookings'] = $stmt->fetchColumn();*/
  112.  
  113.     $stmt->closeCursor();
  114.  
  115.     return $results;
  116. }
  117.  
  118. /**
  119.  * Get list of bays with silimar address
  120.  * @param string $address address to be look up
  121.  * @return array Various details of each bay - see baylist.php
  122.  */
  123. function searchBay($address) {
  124.     // QUERY DONE
  125.     // TODO: logic to figure out if available or not.
  126.     $db = connect();
  127.     try {
  128.          $stmt = $db->prepare("SELECT
  129.            bayId, site, address, avail_wk_start, avail_wk_end, avail_wend_start, avail_wend_end
  130.            FROM PeerPark.ParkBay
  131.            WHERE address LIKE '%' || :address || '%'");
  132.         $stmt->bindValue(':address', $address, PDO::PARAM_STR);
  133.         $stmt->execute();
  134.  
  135.         // Fill up the array row by row.
  136.         $result = array();
  137.         while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  138.             array_push($result, array('bayID'=>$row['bayID'], 'site'=>$row['site'], 'address'=>$row['address'], 'avail'=>true));
  139.         }
  140.         $stmt->closeCursor();
  141.         return $result;
  142.     }
  143.     catch (PDOException $e) {
  144.         print "Error searching bays " . $e->getMessage();
  145.         return False;
  146.     }
  147.    
  148. }
  149.  
  150. /**
  151.  * Retrieve information of all bays
  152.   * @return array Various details of each bay - see baylist.php
  153.  * @throws Exception
  154.  */
  155.  
  156. function getBays() {
  157.     // Example booking info - this should come from a query. Format is
  158.     // (bay ID, site, address, availability of the bay)
  159.     // MOSTLY DONE
  160.     // TODO: LOGIC For availability.
  161.     $db = connect();
  162.     try{
  163.         $stmt = $db->prepare("SELECT
  164.            bayId, site, address, avail_wk_start, avail_wk_end, avail_wend_start, avail_wend_end
  165.            FROM PeerPark.ParkBay");
  166.         $stmt->execute();
  167.  
  168.         // Fill up the array row by row.
  169.         $result = array();
  170.         while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  171.             array_push($result, array('bayID'=>$row['bayID'], 'site'=>$row['site'], 'address'=>$row['address'], 'avail'=>true));
  172.         }
  173.         $stmt->closeCursor();
  174.         return $result;
  175.     } catch (PDOException $e) {
  176.         print "Error checking login: " . $e->getMessage();
  177.         return False;
  178.     }
  179. }
  180.  
  181.  
  182. /**
  183.  * Retrieve information on bays
  184.  * @param string $memberNo ID of the member
  185.  * @return array  details of the member preferred bay - see baylist.php
  186.  * @throws Exception
  187.  */
  188.  
  189. function getPrefBayInformation($memberNo) {
  190.     // Replace lines below with code to get the information about the owner preferred bay from the database
  191.     // Example bay info - this should come from a query. Format is
  192.     // (bay ID, Owner, Latitude, Longitude, Address,  width, height, length, pod, site, week start, week end, weekend start, weekend end)
  193.     // TODO: Nearly done.
  194.     // TODO: Do we have to get owner name or just id?
  195.     // TODO: Join with Member for PrefBay.
  196.     $db = connect();
  197.     try {
  198.         $stmt = $db->prepare("SELECT
  199.            bayID, address, pod, site, avail_wk_start, avail_wk_end, avail_wend_start, avail_wend_end
  200.            FROM PeerPark.ParkBay
  201.            JOIN PeerPark.Member ON PeerPark.ParkBay.bayID = PeerPark.Member.prefBay
  202.            WHERE PeerPark.Member.email=:member");
  203.         $stmt->bindValue(':member', $memberNo, PDO::PARAM_STR);
  204.         $stmt->execute();
  205.         $results = array();
  206.  
  207.         $results = $stmt->fetch(PDO::FETCH_ASSOC);
  208.         //$results = array('bayID'=>$row['bayID'], 'site'=>$row['site'], 'address'=>$row['address'], 'avail'=>true);
  209.        
  210.         return $results;
  211.     }
  212.     catch (PDOException $e) {
  213.         print "Error checking login: " . $e->getMessage();
  214.         return False;
  215.     }
  216.     return false;
  217. }
  218.  
  219.  
  220. /**
  221.  * Retrieve information on bays
  222.  * @param string $BayID ID of the bay
  223.  * @return array Various details of the bay - see baydetail.php
  224.  * @throws Exception
  225.  */
  226.  
  227. function getBayInformation($BayID) {
  228.     // TODO:
  229.     // Check right data is in query.
  230.     // Fill array
  231.     // NOT DONE.
  232.     // Replace lines below with code to get the information about a specific bay from the database
  233.     // Example bay info - this should come from a query. Format is
  234.     // (bay ID, Owner, Latitude, Longitude, Address,  width, height, length, pod, site, week start, week end, weekend start, weekend end)
  235.     $db = connect();
  236.     try {
  237.         $stmt = $db->prepare('SELECT * FROM PeerPark.Booking WHERE bookingID=:book');
  238.         $stmt->bindValue(':member', $memberNo, PDO::PARAM_STR);
  239.         $stmt->execute();
  240.         $stmt->closeCursor();
  241.         return
  242.         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',
  243.         'width'=> 45,'height'=>56,'length'=>56,'pod'=>45,'avail_wk_start'=>9,'avail_wk_end'=>17,'avail_wend_start'=>2,'avail_wend_end'=>3);
  244.    
  245.  
  246.     } catch (PDOException $e) {
  247.         print "Error checking login: " . $e->getMessage();
  248.         return False;
  249.     }
  250.     return False;
  251. }
  252.  
  253. /**
  254.  * Retrieve information on active bookings for a member
  255.  * @param string $memberNo ID of member
  256.  * @return array Various details of each booking - see bookings.php
  257.  * @throws Exception
  258.  */
  259.  
  260. function getOpenBookings($memberNo) {
  261.     // Replace lines below with code to get list of bookings from the database
  262.     // Example booking info - this should come from a query. Format is
  263.     // (booking ID,  bay ID, Car Name, Booking start date, booking start time, booking duration)
  264.    
  265.     // TODO: query. get the bookings for a time past NOW owned by :member. Get the data as above.
  266.     // TODO: fill the array.
  267.     // NOT DONE.
  268.     $db = connect();
  269.     try {
  270.         $stmt = $db->prepare('SELECT * FROM PeerPark.Booking WHERE bookingID=:book');
  271.         $stmt->bindValue(':member', $memberNo, PDO::PARAM_STR);
  272.         $stmt->execute();
  273.         $stmt->closeCursor();
  274.         $results = array(
  275.         array('bookingID'=>1,'bayLocation'=>'CBD','car'=>'Jenny the Yaris','bookingDate'=>'05/03/15' ),
  276.         array('bookingID'=>2,'bayLocation'=>'Glebe','car'=>'Garry the Getz','bookingDate'=>'11/04/15')
  277.         );
  278.         return $results;
  279.     } catch (PDOException $e) {
  280.         print "Error checking login: " . $e->getMessage();
  281.         return False;
  282.     }
  283.     return False;
  284.  
  285. }
  286.  
  287. /**
  288.  * Make a new booking for a bay
  289.  * @param string $memberNo Member booking the bay
  290.  * @param string $car         Name of the car
  291.  * @param string $bayID       ID of the bay to book
  292.  * @param string $bookingDate the date of the booking
  293.  * @param string $bookingHour the time of the booking
  294.  * @param string $duration    the duration of the booking
  295.  * @return array Various details of current visit - see newbooking.php
  296.  * @throws Exception
  297.  */
  298. function makeBooking($memberNo,$car,$bayID,$bookingDate,$bookingHour,$duration) {
  299.     // TODO: TODO: Cost
  300.     // NOT DONE
  301.     $db = connect();
  302.     try {
  303.  
  304.         $stmt = $db->prepare('INSERT INTO PeerPark.Booking VALUES ((SELECT MAX(bookingID) + 1 FROM PeerPark.Booking), :bayID, :bookingDate, :bookingHour, :duration,
  305.        (SELECT memberNo FROM PeerPark.Member WHERE email=:memberNo),
  306.        :car);');
  307.       $stmt->bindValue(':bayID',$bayID, PDO::PARAM_INT);
  308.       $stmt->bindValue(':bookingDate',$bookingDate, PDO::PARAM_STR);      
  309.       $stmt->bindValue(':bookingHour', $bookingHour, PDO::PARAM_INT);
  310.       $stmt->bindValue(':duration',$duration, PDO::PARAM_INT);
  311.       $stmt->bindValue(':memberNo',$memberNo, PDO::PARAM_STR);
  312.       $stmt->bindValue(':car',$car, PDO::PARAM_STR);
  313.         $stmt->execute();
  314.         $stmt = $db->prepare('SELECT MAX(bookingID) as id FROM PeerPark.Booking');
  315.         $stmt->execute();
  316.         $row = $stmt->fetch();
  317.  
  318.         $bookingID = $row['id'];
  319.         print($bookingID);
  320.         $stmt->closeCursor();
  321.         return array(
  322.         'status'=>'success',
  323.         'bookingID'=>$bookingID,
  324.         'bayID'=>$bayID,
  325.         'car'=>$car,
  326.         'bookingDate'=>$bookingDate,
  327.         'bookingHour'=>$bookingHour,
  328.         'duration'=>$duration,
  329.         'cost'=>1000
  330.          );
  331.  
  332.     } catch (PDOException $e) {
  333.         print "Error checking login: " . $e->getMessage();
  334.         return False;
  335.     }
  336.    
  337. }
  338.  
  339. /**
  340.  * Retrieve information on the booking
  341.  * @param string $bookingID ID of the bay
  342.  * @return array Various details of the booking - see bookingDetail.php
  343.  * @throws Exception
  344.  */
  345. function getBookingInfo($bookingID) {
  346.     // Replace lines below with code to get the detail about the booking.
  347.     // Example booking info - this should come from a query. Format is
  348.     // (bookingID, bay Location, booking Date, booking Hour, duration, car, member Name)
  349.     // NOT DONE.
  350.     // TODO: The query
  351.     // TODO: Fill the array.
  352.     $db = connect();
  353.     try {
  354.         $stmt = $db->prepare('SELECT * FROM PeerPark.Booking WHERE bookingID=:book');
  355.         $stmt->bindValue(':book', $bookingID, PDO::PARAM_STR);
  356.         $stmt->execute();
  357.         $stmt->closeCursor();
  358.         return array('bookingID'=>1, 'bayLocation'=>'CBD', 'bookingDate'=> '10/05/2015','bookingHour'=>'10:01','duration'=>2,'car'=> 'Harry the Goat','memberName'=>'Uwe');
  359.  
  360.     } catch (PDOException $e) {
  361.         print "Error checking login: " . $e->getMessage();
  362.         return False;
  363.     }
  364. }
  365.  
  366. /**
  367.  * Get details of the cars of the member
  368.  * @param string $user ID of member
  369.  * @return Name of the cars owned by the member - see index.php
  370.  */
  371. function getCars($email) {
  372.     // DONE.
  373.     $db = connect();
  374.     try {
  375.         $stmt = $db->prepare("SELECT name FROM PeerPark.Car
  376.            NATURAL JOIN PeerPark.Member
  377.            WHERE email=:email");
  378.         $stmt->bindValue(':email', $email, PDO::PARAM_INT);
  379.         $stmt->execute();
  380.         // Fill up the array row by row.
  381.         $result = array();
  382.         while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  383.             array_push($result, array('car'=>$row['name']));
  384.         }
  385.         $stmt->closeCursor();
  386.  
  387.         return $result;
  388.  
  389.     } catch (PDOException $e) {
  390.         print "Error getting cars";
  391.         return False;
  392.     }
  393.     return false;
  394. }
  395.  
  396. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement