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

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

/**
 * Class GetPiInfo
 * Get PI information
 */
class GetPiInfo 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];

      // Call db to get rows
      $this->output = $this->db->getPi($flight_id);
   }
}

/**
 * Class GetPiFromProject
 * Get PI information using a project code
 */
class GetPiFromProject extends APIPage
{
   public function __construct()
   {
      // we want db connection
      parent::__construct(true);
   }

   public function render($regex_matches)
   {

      // Get project_code
      $project_code = $regex_matches[1];

      // Call db to get rows
      $this->output = $this->db->getPIFromProject($project_code);
   }
}

/**
 * Class UpdatePi
 * Update PI info
 */
class UpdatePi extends APIPage
{
   public function __construct()
   {
      // we want db connection
      parent::__construct(true);
   }

   public function render($regex_matches)
   {

      // Get flight id
      $pi_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('pi_name', 'pi_email', 'pi_address');


      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($pi_id, $post_vars['pi_name'], $post_vars['pi_email'], $post_vars['pi_address']);

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