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

require_once('../classes/api.php');

/**
 * Class UpdateFlightProject
 * Update flight project linkage
 */
class UpdateFlightProject extends APIPage
{
   public function __construct()
   {
      // we want db connection
      parent::__construct(true);
   }

   public function render($regex_matches)
   {

      // Get flight id
      $flight_id = $regex_matches[1];

      // Gets "post" variables from data that is in the format of a query string
      parse_str(file_get_contents("php://input"), $post_vars);
      $needed_keys = array('old_project_code', 'new_project_code');


      if($this->has_keys($post_vars, $needed_keys) == false) {
         // Error, Not enough keys
         $this->error('Incorrect number of parameters sent to the api.');
      } else {
         // Validation success

         $parameter_array = array($flight_id, $post_vars['old_project_code'], $post_vars['new_project_code']);

         // Send to database
         $database_update_result = $this->db->update_flight_project($parameter_array);
         if($database_update_result) {
            // Success
            $this->output['success'] = true;
         } else {
            // Database Error
            $this->error('Failed to update the database.');
         }
      }
   }
}

/**
 * Class InsertFlightProject
 * Add new flight project link
 */
class InsertFlightProject extends APIPage
{
   public function __construct()
   {
      // we want db connection
      parent::__construct(true);
   }

   public function render($regex_matches)
   {

      // Get flight id
      $flight_id    = $regex_matches[1];
      $project_code = $regex_matches[2];

      $database_insert_result = $this->db->insert_flight_project($flight_id, $project_code);
      if($database_insert_result) {
         // Success
         $this->output['success'] = true;
      } else {
         // Database Error
         $this->error('Failed to update the database.');
      }
   }
}
