<?php
/*
    =========================================
        Ajax
    =========================================
*/

class Ajax
{
  private static $is_ajax = false;
  private static $meta_data = [];

  public static function check()
  {
    self::$is_ajax = isset($_GET['xhr']) &&
    $_GET['xhr'] == true;

    if (self::$is_ajax)
      self::$meta_data = [];
  }

  public static function add_custom_data($data_array)
  {
    foreach ($data_array as $key => $value)
      self::$meta_data[$key] = [$value];
  }

  private static function run_ajax()
  {
    self::$meta_data['content'] = ob_get_clean();
    echo json_encode( self::$meta_data );
  }

  public static function run ()
  {
    if (self::$is_ajax)
    {
      self::run_ajax();
    }
    else
    {
      $content = ob_get_clean();

      require(TPL . 'main.php');
    }
  }

}
