<?php
/**
 * User: tec
 * Date: 05/09/14
 * Time: 10:28
 */


/**
 * Class ArsfDatabase
 *
 * This class deals with interacting with the arsf database.
 */
class ArsfDatabase
{
   /**
    * Database Hostname / IP Address
    *
    * @var string
    */
   private $host;
   /**
    * Database Port
    *
    * @var int
    */
   private $port;
   /**
    * Database User
    *
    * @var string
    */
   private $user;
   /**
    * Database User Password
    *
    * @var string
    */
   private $password;
   /**
    * Database name
    *
    * @var string
    */
   private $dbname;
   /**
    * Use persistent connections when connecting to the database
    *
    * @var bool
    */
   private $persistent;
   /**
    * Database connection object
    *
    * @var
    */
   private $connection;


   /**
    * Database constructor
    *
    * @param string      $dbname     Database name
    * @param string      $host       Database Host
    * @param int         $port       Database Port
    * @param string      $user       Database User
    * @param string|null $password   Database Password
    * @param bool        $persistent Persistent Connections
    */
   public function __construct($dbname = 'arsf_status', $host = 'pgsql', $port = 5432, $user = 'rsguser',
                               $password = 'd0G8er?t', $persistent = true)
   {
      $this->host       = $host;
      $this->port       = $port;
      $this->user       = $user;
      $this->password   = $password;
      $this->persistent = $persistent;
      $this->dbname     = $dbname;
   }

   /**
    * Closes the database connection if its still open.
    */
   public function  __destruct()
   {
      if($this->connection != null) {
         $this->close();
      }
   }

   /**
    * Connects to the database.
    */
   public function connect()
   {
      $connection_string = sprintf('host=%s port=%d dbname=%s', $this->host, $this->port, $this->dbname);

      if($this->user != null) {
         $connection_string .= " user=" . $this->user;
      }

      if($this->password != null) {
         $connection_string .= " password=" . $this->password;
      }

      if($this->persistent) {
         $this->connection = pg_pconnect($connection_string)
         or die('Could not connect: ' . htmlspecialchars(pg_last_error()));
      } else {
         $this->connection = pg_connect($connection_string)
         or die('Could not connect: ' . htmlspecialchars(pg_last_error()));
      }
   }

   /**
    * Closes database connection.
    */
   public function close()
   {
      pg_close($this->connection);
      $this->connection = null;
   }

   /**
    * Performs an SQL query
    *
    * @param string $sql SQL to be executed.
    *
    * @return resource Result resource or FALSE
    */
   private function query($sql)
   {
      $query_result = pg_query($this->connection, $sql);

      if($query_result == false) {
         die('An error occurred whilst running the query: ' . htmlspecialchars($sql) . '\nPostgres says: ' . htmlspecialchars(pg_last_error()));
      }

      return $query_result;
   }

   /**
    * Performs an SQL query
    *
    * @param string $sql       SQL to be executed.
    * @param array  $arguments arguments to be substituted into the SQL.
    *
    * @return resource Result resource or FALSE
    */
   private function query_args($sql, $arguments)
   {
      $query_result = pg_query_params($this->connection, $sql, $arguments);

      if($query_result == false) {
         die('An error occurred whilst running the query: ' . htmlspecialchars($sql) . '\nPostgres says: ' . htmlspecialchars(pg_last_error()));
      }

      return $query_result;
   }

   /**
    * Gets all the PIs from the database
    *
    * @return array An array of [id, pi_name, address, email]
    */
   public function getAllPIs()
   {
      $query_result = $this->query('SELECT * FROM pi ORDER BY pi_name');

      $pi_array = array();

      while($row = pg_fetch_assoc($query_result)) {
         $pi_array[] = $row;
      }

      return $pi_array;
   }

   /**
    * Gets all sensor names from the database
    *
    * @return array An array of sensor names
    */
   public function getAllSensors()
   {
      $query_result = $this->query('SELECT sensor_name FROM sensors ORDER BY sensor_name');

      $sensor_array = array();

      while($row = pg_fetch_assoc($query_result)) {
         $sensor_array[] = $row['sensor_name'];
      }

      return $sensor_array;
   }

   /**
    * Gets all sensors that should be displayed on the status page
    *
    * @return array An array of sensor names
    */
   public function getAllDisplayedSensors()
   {
      $query_result = $this->query('SELECT * FROM sensors WHERE display_on_page = TRUE ORDER BY sensor_name');

      $sensor_array = array();

      while($row = pg_fetch_assoc($query_result)) {
         $sensor_array[] = $row;
      }

      return $sensor_array;
   }

   /**
    * Gets the progress values from the database
    *
    * @return array An array of [progress_name, description]
    */
   public function getAllStatuses()
   {
      $query_result = $this->query('SELECT progress_name, description FROM progress ORDER BY value');

      $sensor_array = array();

      while($row = pg_fetch_assoc($query_result)) {
         $sensor_array[] = $row;
      }

      return $sensor_array;
   }

   /**
    * Gets all project codes from the database
    *
    * @return array A list of project codes.
    */
   public function getAllProjects()
   {
      $query_result = $this->query('SELECT project_code FROM projects ORDER BY project_code');

      $project_code_array = array();

      while($row = pg_fetch_assoc($query_result)) {
         $project_code_array[] = $row['project_code'];
      }

      return $project_code_array;
   }

   /**
    * Gets the available priorities from the database.
    *
    * @return array An array of [priority_name, value, description]
    */
   public function getAllPriorities()
   {
      $query_result = $this->query('SELECT * FROM priorities ORDER BY value DESC');

      $priority_array = array();

      while($row = pg_fetch_assoc($query_result)) {
         $priority_array[] = $row;
      }

      return $priority_array;
   }

   /**
    * Gets all the progress values from the database.
    *
    * @return array An array of [progress_name, value, description]
    */
   public function getAllProgress()
   {
      $query_result = $this->query('SELECT * FROM progress ORDER BY value DESC');

      $progress_array = array();

      while($row = pg_fetch_assoc($query_result)) {
         $progress_array[] = $row;
      }

      return $progress_array;
   }

   /**
    * Gets the results from the status page view
    *
    * @return array An array of many fields which can vary. ;)
    */
   public function getStatusPage()
   {
      $query_result = $this->query('SELECT * FROM status_page');

      $sensor_array = array();

      while($row = pg_fetch_assoc($query_result)) {
         $sensor_array[] = $row;
      }

      return $sensor_array;
   }

   /**
    * Gets the results from the status order page view
    *
    * @return array An array of many fields which can vary. ;)
    */
   public function getStatusOrderPage()
   {
      $query_result = $this->query('SELECT * FROM status_order_page');

      $sensor_array = array();

      while($row = pg_fetch_assoc($query_result)) {
         $sensor_array[] = $row;
      }

      return $sensor_array;
   }

   /**
    * Gets the results from the status page view
    *
    * @param array $variables Variables to filter view
    *
    * @return array An array of many fields which can vary. ;)
    */
   public function getStatusPageSorted($variables)
   {
      $sensor_array = array();
      //$query_result = null;

      $start_date   = sprintf("%s-%s-%s", $variables['start_year'], $variables['start_month'], $variables['start_day']);
      $end_date     = sprintf("%s-%s-%s", $variables['end_year'], $variables['end_month'], $variables['end_day']);
      $project_code = '%' . $variables['project'] . '%';

      $argument_list = array($start_date, $end_date);

      $sql1 = 'SELECT * FROM status_page WHERE flight_date BETWEEN $1 AND $2';

      $counter = 3;
      if($variables['pi'] != 'all') {
         $argument_list[] = $variables['pi'];
         $sql1 .= sprintf(" AND (pi1 = $%s OR pi2 = $%s)", $counter, $counter);

         $counter++;
      }
      if($variables['project'] != 'all') {
         $argument_list[] = $project_code;
         $sql1 .= sprintf(" AND project_code LIKE $%s", $counter);

         $counter++;
      }
      if($variables['exclude_proj'] != 'none') {
         $argument_list[] = $variables['exclude_proj'];
         $sql1 .= sprintf(" AND project_code NOT LIKE $%s", $counter);

         //$counter++;
      }


      $query_result = $this->query_args($sql1, $argument_list);


      while($row = pg_fetch_assoc($query_result)) {
         $sensor_array[] = $row;
      }

      return $sensor_array;
   }

   /**
    * Gets the results from the status page view
    *
    * @param array $variables Variables to filter view
    *
    * @return array An array of many fields which can vary. ;)
    */
   public function getStatusOrderPageSorted($variables)
   {
      $sensor_array = array();
      //$query_result = null;

      $start_date   = sprintf("%s-%s-%s", $variables['start_year'], $variables['start_month'], $variables['start_day']);
      $end_date     = sprintf("%s-%s-%s", $variables['end_year'], $variables['end_month'], $variables['end_day']);
      $project_code = '%' . $variables['project'] . '%';

      $argument_list = array($start_date, $end_date);

      $sql1 = 'SELECT * FROM status_order_page WHERE flight_date BETWEEN $1 AND $2';

      $counter = 3;
      if($variables['pi'] != 'all') {
         $argument_list[] = $variables['pi'];
         $sql1 .= sprintf(" AND (pi1 = $%s OR pi2 = $%s)", $counter, $counter);

         $counter++;
      }
      if($variables['project'] != 'all') {
         $argument_list[] = $project_code;
         $sql1 .= sprintf(" AND project_code LIKE $%s", $counter);

         $counter++;
      }
      if($variables['exclude_proj'] != 'none') {
         $argument_list[] = $variables['exclude_proj'];
         $sql1 .= sprintf(" AND project_code NOT LIKE $%s", $counter);

         //$counter++;
      }


      $query_result = $this->query_args($sql1, $argument_list);


      while($row = pg_fetch_assoc($query_result)) {
         $sensor_array[] = $row;
      }

      return $sensor_array;
   }


   /**
    * Gets data from the 3 tables for the order page
    *
    * @return array An array from 3 views.
    */
   public function getOrderPage()
   {
      $primary_sensors_query_result        = $this->query('SELECT * FROM not_started_primary_sensors');
      $secondary_sensors_query_result      = $this->query('SELECT * FROM not_started_secondary_sensors');
      $delivery_check_sensors_query_result = $this->query('SELECT * FROM del_check_sensors');

      $primary_sensors   = array();
      $secondary_sensors = array();
      $delivery_check    = array();

      while($row = pg_fetch_array($primary_sensors_query_result, null, PGSQL_BOTH)) {
         $primary_sensors[] = $row;
      }
      while($row = pg_fetch_array($secondary_sensors_query_result, null, PGSQL_BOTH)) {
         $secondary_sensors[] = $row;
      }
      while($row = pg_fetch_array($delivery_check_sensors_query_result, null, PGSQL_BOTH)) {
         $delivery_check[] = $row;
      }

      return array('primary'   => $primary_sensors,
                   'secondary' => $secondary_sensors,
                   'delivery'  => $delivery_check);
   }

   /**
    * Gets the range of years that covers all flights.
    *
    * @return array The first item is the lowest year, and the 2nd item is the max year.
    */
   public function getYearRange()
   {
      $query_result = $this->query("SELECT MIN(DATE_PART('year', flight_date))::INT4 AS year FROM flights UNION SELECT MAX(DATE_PART('year', flight_date))::INT4 AS year FROM flights");

      $year_array = array();

      $min_year = pg_fetch_assoc($query_result)['year'];
      $max_year = pg_fetch_assoc($query_result)['year'];

      for($year = $min_year; $year <= $max_year; $year++) {
         $year_array[] = $year;
      }

      return $year_array;
   }

   /**
    * Gets flight info from the flight table.
    *
    * @param int $flight_id Flight ID
    *
    * @return array A result resource.
    */
   public function getFlight($flight_id)
   {
      $query_result = $this->query_args('SELECT * FROM flights WHERE id = $1', array($flight_id));

      return pg_fetch_assoc($query_result);
   }

   /**
    * Gets flight info for progress page from the flight table.
    *
    * @param int $flight_id Flight ID
    *
    * @return array A result resource.
    */
   public function getProgressPageFlight($flight_id)
   {
      $query_result = $this->query_args('SELECT f.area, f.flight_date, f.sortie, f.primary_ticket, f.secondary_tickets, f.kml_file_location, pf.project_code1, pf.project_code2 FROM flights f JOIN project_flights_single pf ON (f.id = pf.flight_id) WHERE id = $1', array($flight_id));
      $sql2         = 'SELECT p.summary, pi.pi_name FROM projects p JOIN pi ON (p.pi_id = pi.id) WHERE project_code = $1';

      $flight_data = pg_fetch_assoc($query_result);

      $project_code_1_details    = pg_fetch_assoc($this->query_args($sql2, array($flight_data['project_code1'])));
      $flight_data['projects']   = array();
      $flight_data['projects'][] = array('project_code' => $flight_data['project_code1'], 'details' => $project_code_1_details);

      unset($flight_data['project_code1']);
      if($flight_data['project_code2'] != null) {
         $project_code_2_details    = pg_fetch_assoc($this->query_args($sql2, array($flight_data['project_code2'])));
         $flight_data['projects'][] = array('project_code' => $flight_data['project_code2'], 'details' => $project_code_2_details);
         unset($flight_data['project_code2']);
      }

      return $flight_data;

   }

   /**
    * Get Details for the progress page
    *
    * @param int $sensor_id Sensor ID
    *
    * @return array A result resource.
    */
   public function getProgressPageSensorDetails($sensor_id)
   {
      $sensor_details           = pg_fetch_assoc($this->query_args('SELECT fs.sensor_name, fs.number_of_flightlines, p.description FROM flight_sensors fs JOIN priorities p ON (fs.priority = p.priority_name) WHERE id = $1', array($sensor_id)));
      $sensor_details['status'] = array();

      $query_result = $this->query_args('SELECT submit_time, progress, blocked_reason, p.value, p.description FROM sensor_status ss JOIN progress p ON (ss.progress = p.progress_name) WHERE flight_sensor_id = $1 ORDER BY submit_time ASC;', array($sensor_id));

      while($row = pg_fetch_assoc($query_result)) {
         $sensor_details['status'][] = $row;
      }

      return $sensor_details;
   }


   /**
    * Gets a flight sensor from the database
    *
    * @param int $sensor_id Flight Sensor ID
    *
    * @return array A result resource
    */
   public function getFlightSensor($sensor_id)
   {
      $query_result = $this->query_args('SELECT fs.*, ss.progress FROM flight_sensors fs LEFT JOIN sensor_status ss ON (fs.id = ss.flight_sensor_id) WHERE fs.id = $1 ORDER BY ss.submit_time DESC LIMIT 1', array($sensor_id));

      return pg_fetch_assoc($query_result);
   }

   /**
    * Gets projects from database
    *
    * @param int $flight_id Flight ID
    *
    * @return array An array of data ;)
    */
   public function getProject($flight_id)
   {
      $query_result = $this->query_args('SELECT * FROM project_flights pf LEFT JOIN projects p ON (pf.project_code = p.project_code) WHERE pf.flight_id = $1', array($flight_id));

      $project_array = array();

      while($row = pg_fetch_assoc($query_result)) {
         $project_array[] = $row;
      }

      return $project_array;
   }

   /**
    * Gets PIs and their respective project code from the database.
    *
    * @param int $flight_id Flight ID
    *
    * @return array An array of [project_code, pi.*]
    */
   public function getPI($flight_id)
   {
      $query_result = $this->query_args('SELECT p.project_code, pi.* FROM project_flights pf LEFT JOIN projects p ON (pf.project_code = p.project_code) LEFT JOIN pi ON (p.pi_id = pi.id) WHERE pf.flight_id = $1', array($flight_id));

      $pi_array = array();

      while($row = pg_fetch_assoc($query_result)) {
         $pi_array[] = $row;
      }

      return $pi_array;
   }

   /**
    * Gets PIs and their respective project code from the database.
    *
    * @param int $project_code Project Code
    *
    * @return array An array of [pi.*]
    */
   public function getPIFromProject($project_code)
   {
      $query_result = $this->query_args('SELECT pi.* FROM projects p JOIN pi ON (p.pi_id = pi.id) WHERE p.project_code = $1', array($project_code));

      return pg_fetch_assoc($query_result);
   }

   /**
    * Gets statues for a specific flight sensor
    *
    * @param int $flight_sensor_id Flight Sensor ID
    *
    * @return array An array of status details
    */
   public function getSensorStatuses($flight_sensor_id)
   {
      $query_result = $this->query_args('SELECT id, username, submit_time, progress, notes FROM sensor_status WHERE flight_sensor_id = $1', array($flight_sensor_id));

      $status_array = array();

      while($row = pg_fetch_assoc($query_result)) {
         $status_array[] = $row;
      }

      return $status_array;
   }


   /**
    * Adds a new entry for a flight sensor and adds a status of not started.
    *
    * @param int    $flight_id   Flight ID
    * @param string $sensor_name Sensor Name
    * @param string $priority    Sensor Priority
    * @param int    $lines       Number of flightlines
    * @param string $user        Username
    *
    * @return boolean True if sucessfull, false otherwise.
    */
   public function insert_flight_sensor($flight_id, $sensor_name, $priority, $lines, $user)
   {
      $sql_query1 = 'INSERT INTO flight_sensors(flight_id, sensor_name, priority, number_of_flightlines) VALUES ($1, $2, $3, $4);';
      $sql_query2 = "INSERT INTO sensor_status(flight_sensor_id, username, progress) VALUES ($1, $2, 'not_started');";

      $this->query_args($sql_query1, array($flight_id, $sensor_name, $priority, $lines));
      $insert_result = $this->query('SELECT lastval();');
      $insert_id     = pg_fetch_row($insert_result)[0];

      $this->query_args($sql_query2, array($insert_id, $user));
      $insert_result = $this->query('SELECT lastval();');
      $insert_id     = pg_fetch_row($insert_result)[0];

      return $insert_id > 0;
   }

   /**
    * Adds an entry into sensor_status for a new status update.
    *
    * @param array $variable_array Array of variables to be substituted into SQL
    *
    * @return resource
    */
   public function insert_sensor_status($variable_array)
   {
      /* 1                 2               3        4
       * flight_sensor_id, status_progress username blocked_reason
       */

      $sql = 'INSERT INTO sensor_status(flight_sensor_id, progress, username, blocked_reason) VALUES ($1, $2, $3, $4);';

      $result = $this->query_args($sql, $variable_array);

      return $result;
   }

   /**
    * Adds entries into flight
    *
    * @param array $flight_data  Array of variables to be substituted into SQL
    * @param array $project_data Array of variables to be substituted into SQL
    * @param array $sensor_data  Array of variables to be substituted into SQL
    *
    * @return resource
    */
   public function insert_flight($flight_data, $project_data, $sensor_data)
   {
      /* 1           2      3    4        5     6                7
       * flight_date sortie area priority notes data_recieved_on data_location
       *
       * 1
       * project_code
       *
       * 1           2                     3
       * sensor_name number_of_flightlines priority
       */

      $continue  = true;
      $flight_id = null;

      $sql1 = 'START TRANSACTION';
      $sql2 = 'INSERT INTO flights(flight_date, sortie, area, priority, notes, data_recieved_on, data_location)  VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING id;';
      $sql3 = 'INSERT INTO project_flights(flight_id, project_code) VALUES ($1, $2);';
      $sql4 = 'INSERT INTO flight_sensors(flight_id, sensor_name, number_of_flightlines, priority) VALUES ($1, $2, $3, $4);';
      $sql5 = 'COMMIT;';
      $sql6 = 'ROLLBACK;';

      $this->query($sql1);

      $result = $this->query_args($sql2, $flight_data);
      $continue &= ($result != false);

      if($continue) {
         $flight_id = pg_fetch_assoc($result)['id'];

         foreach($project_data as $project) {
            $sql_args = array($flight_id, $project);
            $result   = $this->query_args($sql3, $sql_args);
            $continue &= ($result != false);
         }
      }

      if($continue) {
         foreach($sensor_data as $sensor) {
            array_unshift($sensor, $flight_id);
            $result = $this->query_args($sql4, $sensor);
            $continue &= ($result != false);
         }
      }

      if($continue) {
         $this->query($sql5);
      } else {
         $this->query($sql6);
      }

      return $continue;
   }

   /**
    * Adds a PI to the database
    *
    * @param string $pi_name    PI Name
    * @param string $pi_email   PI Email
    * @param string $pi_address PI Address
    *
    * @return integer of the PI's ID
    */
   public function insert_pi($pi_name, $pi_email, $pi_address)
   {
      $sql_query = 'INSERT INTO pi(pi_name, email, address) VALUES ($1, $2, $3);';

      $this->query_args($sql_query, array($pi_name, $pi_email, $pi_address));
      $insert_result = $this->query('SELECT lastval();');
      $insert_id     = pg_fetch_row($insert_result)[0];

      return $insert_id;
   }

   /**
    * Adds a Project to the database
    *
    * @param array $project_args Project details
    *
    * @return true if insert was successful false otherwise.
    */
   public function insert_project($project_args)
   {
      $sql_query = 'INSERT INTO projects(project_code, pi_id, summary, eufar_code, objective) VALUES ($1, $2, $3, $4, $5);';

      $result = $this->query_args($sql_query, $project_args);

      return $result != false; // Return true instead of a resource
   }

   /**
    * Adds a Project to a flight in the database
    *
    * @param int    $flight_id    Flight ID
    * @param string $project_code Project Code
    *
    * @return bool if insert was successful returns true, false otherwise.
    */
   public function insert_flight_project($flight_id, $project_code)
   {
      $sql_query = 'INSERT INTO project_flights(flight_id, project_code) VALUES ($1, $2);';

      $result = $this->query_args($sql_query, array($flight_id, $project_code));

      return $result != false; // Return true instead of a resource
   }


   /**
    * Updates the flight details
    *
    * @param array $variable_array Array of flight details
    *
    * @return resource Returns FALSE if not exectuted.
    */
   public function update_flight($variable_array)
   {
      /* 'flight_id','flight_year','flight_day','flight_letter','flight_area',
      'flight_primary_ticket','flight_secondary_ticket','flight_kml','flight_notes','flight_datalocation');*/

      $sql = 'UPDATE flights SET flight_date=$2, primary_ticket=$3, kml_file_location=$4, data_location=$5, sortie=$6, notes=$7, secondary_tickets=$8, area=$9, priority=$10 WHERE id=$1;';

      $result = $this->query_args($sql, $variable_array);

      return $result;
   }

   /**
    * Updates the flight project linkage
    *
    * @param array $variable_array Array of project details
    *
    * @return resource Returns FALSE if not exectuted.
    */
   public function update_flight_project($variable_array)
   {
      /* 1         2                3
       * flight_id old_project_code new_project_code
       */

      $sql = 'UPDATE project_flights SET project_code=$3 WHERE project_code = $2 AND flight_id = $1;';

      $result = $this->query_args($sql, $variable_array);

      return $result;
   }

   /**
    * Updates the project details
    *
    * @param array $variable_array Array of project details
    *
    * @return resource Returns FALSE if not exectuted.
    */
   public function update_project($variable_array)
   {
      /* 1            2               3                 4                 5
       * project_code project_summary project_objective project_eufarcode project_priority
       */

      $sql = 'UPDATE projects SET summary=$2, objective=$3, eufar_code=$4 WHERE project_code = $1;';

      $result = $this->query_args($sql, $variable_array);

      return $result;
   }

   /**
    * Updates the pi details
    *
    * @param array $variable_array Array of pi details
    *
    * @return resource Returns FALSE if not exectuted.
    */
   public function update_pi($variable_array)
   {
      /* 1      2       3        4
       * pi_id, pi_name pi_email pi_address
       */

      $sql = 'UPDATE pi SET pi_name=$2, email=$3, address=$4 WHERE id=$1;';

      $result = $this->query_args($sql, $variable_array);

      return $result;
   }

   /**
    * Updates the flight sensor details
    *
    * @param array $variable_array Array of flight sensor details
    *
    * @return resource Returns FALSE if not exectuted.
    */
   public function update_flight_sensor($variable_array)
   {
      /* 1                2               3                      4                  5
       * flight_sensor_id sensor_priority sensor_priority_offset sensor_flightlines sensor_del_check_time
       */

      $sql = 'UPDATE flight_sensors SET priority=$2, priority_offset=$3, number_of_flightlines=$4, delivery_check_time=$5 WHERE id=$1;';

      $result = $this->query_args($sql, $variable_array);

      return $result;
   }

   /**
    * Updates a status
    *
    * @param $variable_array Array of status details
    *
    * @return resource Returns FALSE if not exectuted.
    */
   public function update_sensor_status($variable_array)
   {
      /* 1   2         3         4      5
       * id, username, datetime, notes, progress
       */

      $sql = 'UPDATE sensor_status SET username=$2, submit_time=$3, notes=$4, progress=$5 WHERE id=$1;';

      $result = $this->query_args($sql, $variable_array);

      return $result;
   }


   /**
    * Deletes a flight sensor
    *
    * @param int $flight_sensor_id Flight Sensor ID
    *
    * @return resource
    */
   public function delete_flight_sensor($flight_sensor_id)
   {
      $sql1 = 'DELETE FROM sensor_status WHERE flight_sensor_id = $1;';
      $sql2 = 'DELETE FROM flight_sensors WHERE id = $1;';

      $this->query_args($sql1, array($flight_sensor_id));
      $result = $this->query_args($sql2, array($flight_sensor_id));

      return $result;
   }

   /**
    * Deletes a flight
    *
    * @param int $flight_id Flight ID
    *
    * @return resource
    */
   public function delete_flight($flight_id)
   {
      $sql = 'DELETE FROM flights WHERE id = $1;';

      $result = $this->query_args($sql, array($flight_id));

      return $result;
   }

   /**
    * Deletes a project
    *
    * @param int    $flight_id    Flight ID
    * @param string $project_code Project Code
    *
    * @return boolean true if delete is sucessful
    */
   public function delete_project($flight_id, $project_code)
   {
      $sql = 'DELETE FROM project_flights WHERE flight_id = $1 AND project_code = $2;';

      $result = $this->query_args($sql, array($flight_id, $project_code));

      return $result != false;
   }

   /**
    * Deletes a status
    *
    * @param int $status_id Status ID
    *
    * @return resource
    */
   public function delete_sensor_status($status_id)
   {
      $sql = 'DELETE FROM sensor_status WHERE id = $1;';

      $result = $this->query_args($sql, array($status_id));

      return $result;
   }

}

